hw.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302
  1. /*
  2. * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2007 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007 Matthew W. S. Bell <mentor@madwifi.org>
  5. * Copyright (c) 2007 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
  6. * Copyright (c) 2007 Pavel Roskin <proski@gnu.org>
  7. * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  8. *
  9. * Permission to use, copy, modify, and distribute this software for any
  10. * purpose with or without fee is hereby granted, provided that the above
  11. * copyright notice and this permission notice appear in all copies.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  14. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  16. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20. *
  21. */
  22. /*
  23. * HW related functions for Atheros Wireless LAN devices.
  24. */
  25. #include <linux/pci.h>
  26. #include <linux/delay.h>
  27. #include "reg.h"
  28. #include "base.h"
  29. #include "debug.h"
  30. /*Rate tables*/
  31. static const struct ath5k_rate_table ath5k_rt_11a = AR5K_RATES_11A;
  32. static const struct ath5k_rate_table ath5k_rt_11b = AR5K_RATES_11B;
  33. static const struct ath5k_rate_table ath5k_rt_11g = AR5K_RATES_11G;
  34. static const struct ath5k_rate_table ath5k_rt_turbo = AR5K_RATES_TURBO;
  35. static const struct ath5k_rate_table ath5k_rt_xr = AR5K_RATES_XR;
  36. /*Prototypes*/
  37. static int ath5k_hw_nic_reset(struct ath5k_hw *, u32);
  38. static int ath5k_hw_nic_wakeup(struct ath5k_hw *, int, bool);
  39. static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
  40. unsigned int, unsigned int, enum ath5k_pkt_type, unsigned int,
  41. unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
  42. unsigned int, unsigned int);
  43. static int ath5k_hw_setup_xr_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
  44. unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
  45. unsigned int);
  46. static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *, struct ath5k_desc *);
  47. static int ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
  48. unsigned int, unsigned int, enum ath5k_pkt_type, unsigned int,
  49. unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
  50. unsigned int, unsigned int);
  51. static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *, struct ath5k_desc *);
  52. static int ath5k_hw_proc_new_rx_status(struct ath5k_hw *, struct ath5k_desc *);
  53. static int ath5k_hw_proc_old_rx_status(struct ath5k_hw *, struct ath5k_desc *);
  54. static int ath5k_hw_get_capabilities(struct ath5k_hw *);
  55. static int ath5k_eeprom_init(struct ath5k_hw *);
  56. static int ath5k_eeprom_read_mac(struct ath5k_hw *, u8 *);
  57. static int ath5k_hw_enable_pspoll(struct ath5k_hw *, u8 *, u16);
  58. static int ath5k_hw_disable_pspoll(struct ath5k_hw *);
  59. /*
  60. * Enable to overwrite the country code (use "00" for debug)
  61. */
  62. #if 0
  63. #define COUNTRYCODE "00"
  64. #endif
  65. /*******************\
  66. General Functions
  67. \*******************/
  68. /*
  69. * Functions used internaly
  70. */
  71. static inline unsigned int ath5k_hw_htoclock(unsigned int usec, bool turbo)
  72. {
  73. return turbo == true ? (usec * 80) : (usec * 40);
  74. }
  75. static inline unsigned int ath5k_hw_clocktoh(unsigned int clock, bool turbo)
  76. {
  77. return turbo == true ? (clock / 80) : (clock / 40);
  78. }
  79. /*
  80. * Check if a register write has been completed
  81. */
  82. int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
  83. bool is_set)
  84. {
  85. int i;
  86. u32 data;
  87. for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
  88. data = ath5k_hw_reg_read(ah, reg);
  89. if ((is_set == true) && (data & flag))
  90. break;
  91. else if ((data & flag) == val)
  92. break;
  93. udelay(15);
  94. }
  95. return (i <= 0) ? -EAGAIN : 0;
  96. }
  97. /***************************************\
  98. Attach/Detach Functions
  99. \***************************************/
  100. /*
  101. * Check if the device is supported and initialize the needed structs
  102. */
  103. struct ath5k_hw *ath5k_hw_attach(struct ath5k_softc *sc, u8 mac_version)
  104. {
  105. struct ath5k_hw *ah;
  106. u8 mac[ETH_ALEN];
  107. int ret;
  108. u32 srev;
  109. /*If we passed the test malloc a ath5k_hw struct*/
  110. ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
  111. if (ah == NULL) {
  112. ret = -ENOMEM;
  113. ATH5K_ERR(sc, "out of memory\n");
  114. goto err;
  115. }
  116. ah->ah_sc = sc;
  117. ah->ah_iobase = sc->iobase;
  118. /*
  119. * HW information
  120. */
  121. ah->ah_op_mode = IEEE80211_IF_TYPE_STA;
  122. ah->ah_radar.r_enabled = AR5K_TUNE_RADAR_ALERT;
  123. ah->ah_turbo = false;
  124. ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
  125. ah->ah_imr = 0;
  126. ah->ah_atim_window = 0;
  127. ah->ah_aifs = AR5K_TUNE_AIFS;
  128. ah->ah_cw_min = AR5K_TUNE_CWMIN;
  129. ah->ah_limit_tx_retries = AR5K_INIT_TX_RETRY;
  130. ah->ah_software_retry = false;
  131. ah->ah_ant_diversity = AR5K_TUNE_ANT_DIVERSITY;
  132. /*
  133. * Set the mac revision based on the pci id
  134. */
  135. ah->ah_version = mac_version;
  136. /*Fill the ath5k_hw struct with the needed functions*/
  137. if (ah->ah_version == AR5K_AR5212)
  138. ah->ah_magic = AR5K_EEPROM_MAGIC_5212;
  139. else if (ah->ah_version == AR5K_AR5211)
  140. ah->ah_magic = AR5K_EEPROM_MAGIC_5211;
  141. if (ah->ah_version == AR5K_AR5212) {
  142. ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
  143. ah->ah_setup_xtx_desc = ath5k_hw_setup_xr_tx_desc;
  144. ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
  145. } else {
  146. ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
  147. ah->ah_setup_xtx_desc = ath5k_hw_setup_xr_tx_desc;
  148. ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
  149. }
  150. if (ah->ah_version == AR5K_AR5212)
  151. ah->ah_proc_rx_desc = ath5k_hw_proc_new_rx_status;
  152. else if (ah->ah_version <= AR5K_AR5211)
  153. ah->ah_proc_rx_desc = ath5k_hw_proc_old_rx_status;
  154. /* Bring device out of sleep and reset it's units */
  155. ret = ath5k_hw_nic_wakeup(ah, AR5K_INIT_MODE, true);
  156. if (ret)
  157. goto err_free;
  158. /* Get MAC, PHY and RADIO revisions */
  159. srev = ath5k_hw_reg_read(ah, AR5K_SREV);
  160. ah->ah_mac_srev = srev;
  161. ah->ah_mac_version = AR5K_REG_MS(srev, AR5K_SREV_VER);
  162. ah->ah_mac_revision = AR5K_REG_MS(srev, AR5K_SREV_REV);
  163. ah->ah_phy_revision = ath5k_hw_reg_read(ah, AR5K_PHY_CHIP_ID) &
  164. 0xffffffff;
  165. ah->ah_radio_5ghz_revision = ath5k_hw_radio_revision(ah,
  166. CHANNEL_5GHZ);
  167. if (ah->ah_version == AR5K_AR5210)
  168. ah->ah_radio_2ghz_revision = 0;
  169. else
  170. ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
  171. CHANNEL_2GHZ);
  172. /* Return on unsuported chips (unsupported eeprom etc) */
  173. if(srev >= AR5K_SREV_VER_AR5416){
  174. ATH5K_ERR(sc, "Device not yet supported.\n");
  175. ret = -ENODEV;
  176. goto err_free;
  177. }
  178. /* Identify single chip solutions */
  179. if((srev <= AR5K_SREV_VER_AR5414) &&
  180. (srev >= AR5K_SREV_VER_AR2413)) {
  181. ah->ah_single_chip = true;
  182. } else {
  183. ah->ah_single_chip = false;
  184. }
  185. /* Single chip radio */
  186. if (ah->ah_radio_2ghz_revision == ah->ah_radio_5ghz_revision)
  187. ah->ah_radio_2ghz_revision = 0;
  188. /* Identify the radio chip*/
  189. if (ah->ah_version == AR5K_AR5210) {
  190. ah->ah_radio = AR5K_RF5110;
  191. } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112) {
  192. ah->ah_radio = AR5K_RF5111;
  193. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5111;
  194. } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC0) {
  195. ah->ah_radio = AR5K_RF5112;
  196. if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
  197. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112;
  198. } else {
  199. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
  200. }
  201. } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC1) {
  202. ah->ah_radio = AR5K_RF2413;
  203. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
  204. } else {
  205. ah->ah_radio = AR5K_RF5413;
  206. if (ah->ah_mac_srev <= AR5K_SREV_VER_AR5424 &&
  207. ah->ah_mac_srev >= AR5K_SREV_VER_AR2424)
  208. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5424;
  209. else if (ah->ah_mac_srev >= AR5K_SREV_VER_AR2425)
  210. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112;
  211. else
  212. ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
  213. }
  214. ah->ah_phy = AR5K_PHY(0);
  215. /*
  216. * Get card capabilities, values, ...
  217. */
  218. ret = ath5k_eeprom_init(ah);
  219. if (ret) {
  220. ATH5K_ERR(sc, "unable to init EEPROM\n");
  221. goto err_free;
  222. }
  223. /* Get misc capabilities */
  224. ret = ath5k_hw_get_capabilities(ah);
  225. if (ret) {
  226. ATH5K_ERR(sc, "unable to get device capabilities: 0x%04x\n",
  227. sc->pdev->device);
  228. goto err_free;
  229. }
  230. /* Get MAC address */
  231. ret = ath5k_eeprom_read_mac(ah, mac);
  232. if (ret) {
  233. ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
  234. sc->pdev->device);
  235. goto err_free;
  236. }
  237. ath5k_hw_set_lladdr(ah, mac);
  238. /* Set BSSID to bcast address: ff:ff:ff:ff:ff:ff for now */
  239. memset(ah->ah_bssid, 0xff, ETH_ALEN);
  240. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  241. ath5k_hw_set_opmode(ah);
  242. ath5k_hw_set_rfgain_opt(ah);
  243. return ah;
  244. err_free:
  245. kfree(ah);
  246. err:
  247. return ERR_PTR(ret);
  248. }
  249. /*
  250. * Bring up MAC + PHY Chips
  251. */
  252. static int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
  253. {
  254. u32 turbo, mode, clock;
  255. int ret;
  256. turbo = 0;
  257. mode = 0;
  258. clock = 0;
  259. ATH5K_TRACE(ah->ah_sc);
  260. /* Wakeup the device */
  261. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  262. if (ret) {
  263. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  264. return ret;
  265. }
  266. if (ah->ah_version != AR5K_AR5210) {
  267. /*
  268. * Get channel mode flags
  269. */
  270. if (ah->ah_radio >= AR5K_RF5112) {
  271. mode = AR5K_PHY_MODE_RAD_RF5112;
  272. clock = AR5K_PHY_PLL_RF5112;
  273. } else {
  274. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  275. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  276. }
  277. if (flags & CHANNEL_2GHZ) {
  278. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  279. clock |= AR5K_PHY_PLL_44MHZ;
  280. if (flags & CHANNEL_CCK) {
  281. mode |= AR5K_PHY_MODE_MOD_CCK;
  282. } else if (flags & CHANNEL_OFDM) {
  283. /* XXX Dynamic OFDM/CCK is not supported by the
  284. * AR5211 so we set MOD_OFDM for plain g (no
  285. * CCK headers) operation. We need to test
  286. * this, 5211 might support ofdm-only g after
  287. * all, there are also initial register values
  288. * in the code for g mode (see initvals.c). */
  289. if (ah->ah_version == AR5K_AR5211)
  290. mode |= AR5K_PHY_MODE_MOD_OFDM;
  291. else
  292. mode |= AR5K_PHY_MODE_MOD_DYN;
  293. } else {
  294. ATH5K_ERR(ah->ah_sc,
  295. "invalid radio modulation mode\n");
  296. return -EINVAL;
  297. }
  298. } else if (flags & CHANNEL_5GHZ) {
  299. mode |= AR5K_PHY_MODE_FREQ_5GHZ;
  300. clock |= AR5K_PHY_PLL_40MHZ;
  301. if (flags & CHANNEL_OFDM)
  302. mode |= AR5K_PHY_MODE_MOD_OFDM;
  303. else {
  304. ATH5K_ERR(ah->ah_sc,
  305. "invalid radio modulation mode\n");
  306. return -EINVAL;
  307. }
  308. } else {
  309. ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
  310. return -EINVAL;
  311. }
  312. if (flags & CHANNEL_TURBO)
  313. turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
  314. } else { /* Reset the device */
  315. /* ...enable Atheros turbo mode if requested */
  316. if (flags & CHANNEL_TURBO)
  317. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  318. AR5K_PHY_TURBO);
  319. }
  320. /* ...reset chipset and PCI device */
  321. if (ah->ah_single_chip == false && ath5k_hw_nic_reset(ah,
  322. AR5K_RESET_CTL_CHIP | AR5K_RESET_CTL_PCI)) {
  323. ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip + PCI\n");
  324. return -EIO;
  325. }
  326. if (ah->ah_version == AR5K_AR5210)
  327. udelay(2300);
  328. /* ...wakeup again!*/
  329. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  330. if (ret) {
  331. ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
  332. return ret;
  333. }
  334. /* ...final warm reset */
  335. if (ath5k_hw_nic_reset(ah, 0)) {
  336. ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
  337. return -EIO;
  338. }
  339. if (ah->ah_version != AR5K_AR5210) {
  340. /* ...set the PHY operating mode */
  341. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  342. udelay(300);
  343. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  344. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  345. }
  346. return 0;
  347. }
  348. /*
  349. * Get the rate table for a specific operation mode
  350. */
  351. const struct ath5k_rate_table *ath5k_hw_get_rate_table(struct ath5k_hw *ah,
  352. unsigned int mode)
  353. {
  354. ATH5K_TRACE(ah->ah_sc);
  355. if (!test_bit(mode, ah->ah_capabilities.cap_mode))
  356. return NULL;
  357. /* Get rate tables */
  358. switch (mode) {
  359. case AR5K_MODE_11A:
  360. return &ath5k_rt_11a;
  361. case AR5K_MODE_11A_TURBO:
  362. return &ath5k_rt_turbo;
  363. case AR5K_MODE_11B:
  364. return &ath5k_rt_11b;
  365. case AR5K_MODE_11G:
  366. return &ath5k_rt_11g;
  367. case AR5K_MODE_11G_TURBO:
  368. return &ath5k_rt_xr;
  369. }
  370. return NULL;
  371. }
  372. /*
  373. * Free the ath5k_hw struct
  374. */
  375. void ath5k_hw_detach(struct ath5k_hw *ah)
  376. {
  377. ATH5K_TRACE(ah->ah_sc);
  378. if (ah->ah_rf_banks != NULL)
  379. kfree(ah->ah_rf_banks);
  380. /* assume interrupts are down */
  381. kfree(ah);
  382. }
  383. /****************************\
  384. Reset function and helpers
  385. \****************************/
  386. /**
  387. * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
  388. *
  389. * @ah: the &struct ath5k_hw
  390. * @channel: the currently set channel upon reset
  391. *
  392. * Write the OFDM timings for the AR5212 upon reset. This is a helper for
  393. * ath5k_hw_reset(). This seems to tune the PLL a specified frequency
  394. * depending on the bandwidth of the channel.
  395. *
  396. */
  397. static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
  398. struct ieee80211_channel *channel)
  399. {
  400. /* Get exponent and mantissa and set it */
  401. u32 coef_scaled, coef_exp, coef_man,
  402. ds_coef_exp, ds_coef_man, clock;
  403. if (!(ah->ah_version == AR5K_AR5212) ||
  404. !(channel->hw_value & CHANNEL_OFDM))
  405. BUG();
  406. /* Seems there are two PLLs, one for baseband sampling and one
  407. * for tuning. Tuning basebands are 40 MHz or 80MHz when in
  408. * turbo. */
  409. clock = channel->hw_value & CHANNEL_TURBO ? 80 : 40;
  410. coef_scaled = ((5 * (clock << 24)) / 2) /
  411. channel->center_freq;
  412. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  413. if ((coef_scaled >> coef_exp) & 0x1)
  414. break;
  415. if (!coef_exp)
  416. return -EINVAL;
  417. coef_exp = 14 - (coef_exp - 24);
  418. coef_man = coef_scaled +
  419. (1 << (24 - coef_exp - 1));
  420. ds_coef_man = coef_man >> (24 - coef_exp);
  421. ds_coef_exp = coef_exp - 16;
  422. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  423. AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
  424. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  425. AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
  426. return 0;
  427. }
  428. /**
  429. * ath5k_hw_write_rate_duration - set rate duration during hw resets
  430. *
  431. * @ah: the &struct ath5k_hw
  432. * @mode: one of enum ath5k_driver_mode
  433. *
  434. * Write the rate duration table for the current mode upon hw reset. This
  435. * is a helper for ath5k_hw_reset(). It seems all this is doing is setting
  436. * an ACK timeout for the hardware for the current mode for each rate. The
  437. * rates which are capable of short preamble (802.11b rates 2Mbps, 5.5Mbps,
  438. * and 11Mbps) have another register for the short preamble ACK timeout
  439. * calculation.
  440. *
  441. */
  442. static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
  443. unsigned int mode)
  444. {
  445. struct ath5k_softc *sc = ah->ah_sc;
  446. const struct ath5k_rate_table *rt;
  447. struct ieee80211_rate srate = {};
  448. unsigned int i;
  449. /* Get rate table for the current operating mode */
  450. rt = ath5k_hw_get_rate_table(ah, mode);
  451. /* Write rate duration table */
  452. for (i = 0; i < rt->rate_count; i++) {
  453. const struct ath5k_rate *rate, *control_rate;
  454. u32 reg;
  455. u16 tx_time;
  456. rate = &rt->rates[i];
  457. control_rate = &rt->rates[rate->control_rate];
  458. /* Set ACK timeout */
  459. reg = AR5K_RATE_DUR(rate->rate_code);
  460. srate.bitrate = control_rate->rate_kbps/100;
  461. /* An ACK frame consists of 10 bytes. If you add the FCS,
  462. * which ieee80211_generic_frame_duration() adds,
  463. * its 14 bytes. Note we use the control rate and not the
  464. * actual rate for this rate. See mac80211 tx.c
  465. * ieee80211_duration() for a brief description of
  466. * what rate we should choose to TX ACKs. */
  467. tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
  468. sc->vif, 10, &srate));
  469. ath5k_hw_reg_write(ah, tx_time, reg);
  470. if (!HAS_SHPREAMBLE(i))
  471. continue;
  472. /*
  473. * We're not distinguishing short preamble here,
  474. * This is true, all we'll get is a longer value here
  475. * which is not necessarilly bad. We could use
  476. * export ieee80211_frame_duration() but that needs to be
  477. * fixed first to be properly used by mac802111 drivers:
  478. *
  479. * - remove erp stuff and let the routine figure ofdm
  480. * erp rates
  481. * - remove passing argument ieee80211_local as
  482. * drivers don't have access to it
  483. * - move drivers using ieee80211_generic_frame_duration()
  484. * to this
  485. */
  486. ath5k_hw_reg_write(ah, tx_time,
  487. reg + (AR5K_SET_SHORT_PREAMBLE << 2));
  488. }
  489. }
  490. /*
  491. * Main reset function
  492. */
  493. int ath5k_hw_reset(struct ath5k_hw *ah, enum ieee80211_if_types op_mode,
  494. struct ieee80211_channel *channel, bool change_channel)
  495. {
  496. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  497. u32 data, s_seq, s_ant, s_led[3];
  498. unsigned int i, mode, freq, ee_mode, ant[2];
  499. int ret;
  500. ATH5K_TRACE(ah->ah_sc);
  501. s_seq = 0;
  502. s_ant = 0;
  503. ee_mode = 0;
  504. freq = 0;
  505. mode = 0;
  506. /*
  507. * Save some registers before a reset
  508. */
  509. /*DCU/Antenna selection not available on 5210*/
  510. if (ah->ah_version != AR5K_AR5210) {
  511. if (change_channel == true) {
  512. /* Seq number for queue 0 -do this for all queues ? */
  513. s_seq = ath5k_hw_reg_read(ah,
  514. AR5K_QUEUE_DFS_SEQNUM(0));
  515. /*Default antenna*/
  516. s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  517. }
  518. }
  519. /*GPIOs*/
  520. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) & AR5K_PCICFG_LEDSTATE;
  521. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  522. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  523. if (change_channel == true && ah->ah_rf_banks != NULL)
  524. ath5k_hw_get_rf_gain(ah);
  525. /*Wakeup the device*/
  526. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  527. if (ret)
  528. return ret;
  529. /*
  530. * Initialize operating mode
  531. */
  532. ah->ah_op_mode = op_mode;
  533. /*
  534. * 5111/5112 Settings
  535. * 5210 only comes with RF5110
  536. */
  537. if (ah->ah_version != AR5K_AR5210) {
  538. if (ah->ah_radio != AR5K_RF5111 &&
  539. ah->ah_radio != AR5K_RF5112 &&
  540. ah->ah_radio != AR5K_RF5413) {
  541. ATH5K_ERR(ah->ah_sc,
  542. "invalid phy radio: %u\n", ah->ah_radio);
  543. return -EINVAL;
  544. }
  545. switch (channel->hw_value & CHANNEL_MODES) {
  546. case CHANNEL_A:
  547. mode = AR5K_MODE_11A;
  548. freq = AR5K_INI_RFGAIN_5GHZ;
  549. ee_mode = AR5K_EEPROM_MODE_11A;
  550. break;
  551. case CHANNEL_G:
  552. mode = AR5K_MODE_11G;
  553. freq = AR5K_INI_RFGAIN_2GHZ;
  554. ee_mode = AR5K_EEPROM_MODE_11G;
  555. break;
  556. case CHANNEL_B:
  557. mode = AR5K_MODE_11B;
  558. freq = AR5K_INI_RFGAIN_2GHZ;
  559. ee_mode = AR5K_EEPROM_MODE_11B;
  560. break;
  561. case CHANNEL_T:
  562. mode = AR5K_MODE_11A_TURBO;
  563. freq = AR5K_INI_RFGAIN_5GHZ;
  564. ee_mode = AR5K_EEPROM_MODE_11A;
  565. break;
  566. /*Is this ok on 5211 too ?*/
  567. case CHANNEL_TG:
  568. mode = AR5K_MODE_11G_TURBO;
  569. freq = AR5K_INI_RFGAIN_2GHZ;
  570. ee_mode = AR5K_EEPROM_MODE_11G;
  571. break;
  572. case CHANNEL_XR:
  573. if (ah->ah_version == AR5K_AR5211) {
  574. ATH5K_ERR(ah->ah_sc,
  575. "XR mode not available on 5211");
  576. return -EINVAL;
  577. }
  578. mode = AR5K_MODE_XR;
  579. freq = AR5K_INI_RFGAIN_5GHZ;
  580. ee_mode = AR5K_EEPROM_MODE_11A;
  581. break;
  582. default:
  583. ATH5K_ERR(ah->ah_sc,
  584. "invalid channel: %d\n", channel->center_freq);
  585. return -EINVAL;
  586. }
  587. /* PHY access enable */
  588. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  589. }
  590. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  591. if (ret)
  592. return ret;
  593. /*
  594. * 5211/5212 Specific
  595. */
  596. if (ah->ah_version != AR5K_AR5210) {
  597. /*
  598. * Write initial RF gain settings
  599. * This should work for both 5111/5112
  600. */
  601. ret = ath5k_hw_rfgain(ah, freq);
  602. if (ret)
  603. return ret;
  604. mdelay(1);
  605. /*
  606. * Write some more initial register settings
  607. */
  608. if (ah->ah_version > AR5K_AR5211){ /* found on 5213+ */
  609. ath5k_hw_reg_write(ah, 0x0002a002, AR5K_PHY(11));
  610. if (channel->hw_value == CHANNEL_G)
  611. ath5k_hw_reg_write(ah, 0x00f80d80, AR5K_PHY(83)); /* 0x00fc0ec0 */
  612. else
  613. ath5k_hw_reg_write(ah, 0x00000000, AR5K_PHY(83));
  614. ath5k_hw_reg_write(ah, 0x000001b5, 0xa228); /* 0x000009b5 */
  615. ath5k_hw_reg_write(ah, 0x000009b5, 0xa228);
  616. ath5k_hw_reg_write(ah, 0x0000000f, 0x8060);
  617. ath5k_hw_reg_write(ah, 0x00000000, 0xa254);
  618. ath5k_hw_reg_write(ah, 0x0000000e, AR5K_PHY_SCAL);
  619. }
  620. /* Fix for first revision of the RF5112 RF chipset */
  621. if (ah->ah_radio >= AR5K_RF5112 &&
  622. ah->ah_radio_5ghz_revision <
  623. AR5K_SREV_RAD_5112A) {
  624. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  625. AR5K_PHY_CCKTXCTL);
  626. if (channel->hw_value & CHANNEL_5GHZ)
  627. data = 0xffb81020;
  628. else
  629. data = 0xffb80d20;
  630. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  631. }
  632. /*
  633. * Set TX power (FIXME)
  634. */
  635. ret = ath5k_hw_txpower(ah, channel, AR5K_TUNE_DEFAULT_TXPOWER);
  636. if (ret)
  637. return ret;
  638. /* Write rate duration table only on AR5212 and if
  639. * virtual interface has already been brought up
  640. * XXX: rethink this after new mode changes to
  641. * mac80211 are integrated */
  642. if (ah->ah_version == AR5K_AR5212 &&
  643. ah->ah_sc->vif != NULL)
  644. ath5k_hw_write_rate_duration(ah, mode);
  645. /*
  646. * Write RF registers
  647. * TODO:Does this work on 5211 (5111) ?
  648. */
  649. ret = ath5k_hw_rfregs(ah, channel, mode);
  650. if (ret)
  651. return ret;
  652. /*
  653. * Configure additional registers
  654. */
  655. /* Write OFDM timings on 5212*/
  656. if (ah->ah_version == AR5K_AR5212 &&
  657. channel->hw_value & CHANNEL_OFDM) {
  658. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  659. if (ret)
  660. return ret;
  661. }
  662. /*Enable/disable 802.11b mode on 5111
  663. (enable 2111 frequency converter + CCK)*/
  664. if (ah->ah_radio == AR5K_RF5111) {
  665. if (mode == AR5K_MODE_11B)
  666. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  667. AR5K_TXCFG_B_MODE);
  668. else
  669. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  670. AR5K_TXCFG_B_MODE);
  671. }
  672. /*
  673. * Set channel and calibrate the PHY
  674. */
  675. ret = ath5k_hw_channel(ah, channel);
  676. if (ret)
  677. return ret;
  678. /* Set antenna mode */
  679. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x44),
  680. ah->ah_antenna[ee_mode][0], 0xfffffc06);
  681. /*
  682. * In case a fixed antenna was set as default
  683. * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
  684. * registers.
  685. */
  686. if (s_ant != 0){
  687. if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
  688. ant[0] = ant[1] = AR5K_ANT_FIXED_A;
  689. else /* 2 - Aux */
  690. ant[0] = ant[1] = AR5K_ANT_FIXED_B;
  691. } else {
  692. ant[0] = AR5K_ANT_FIXED_A;
  693. ant[1] = AR5K_ANT_FIXED_B;
  694. }
  695. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
  696. AR5K_PHY_ANT_SWITCH_TABLE_0);
  697. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
  698. AR5K_PHY_ANT_SWITCH_TABLE_1);
  699. /* Commit values from EEPROM */
  700. if (ah->ah_radio == AR5K_RF5111)
  701. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  702. AR5K_PHY_FRAME_CTL_TX_CLIP, ee->ee_tx_clip);
  703. ath5k_hw_reg_write(ah,
  704. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  705. AR5K_PHY(0x5a));
  706. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x11),
  707. (ee->ee_switch_settling[ee_mode] << 7) & 0x3f80,
  708. 0xffffc07f);
  709. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x12),
  710. (ee->ee_ant_tx_rx[ee_mode] << 12) & 0x3f000,
  711. 0xfffc0fff);
  712. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x14),
  713. (ee->ee_adc_desired_size[ee_mode] & 0x00ff) |
  714. ((ee->ee_pga_desired_size[ee_mode] << 8) & 0xff00),
  715. 0xffff0000);
  716. ath5k_hw_reg_write(ah,
  717. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  718. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  719. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  720. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY(0x0d));
  721. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x0a),
  722. ee->ee_tx_end2xlna_enable[ee_mode] << 8, 0xffff00ff);
  723. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x19),
  724. (ee->ee_thr_62[ee_mode] << 12) & 0x7f000, 0xfff80fff);
  725. AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x49), 4, 0xffffff01);
  726. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  727. AR5K_PHY_IQ_CORR_ENABLE |
  728. (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
  729. ee->ee_q_cal[ee_mode]);
  730. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  731. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  732. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  733. ee->ee_margin_tx_rx[ee_mode]);
  734. } else {
  735. mdelay(1);
  736. /* Disable phy and wait */
  737. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  738. mdelay(1);
  739. }
  740. /*
  741. * Restore saved values
  742. */
  743. /*DCU/Antenna selection not available on 5210*/
  744. if (ah->ah_version != AR5K_AR5210) {
  745. ath5k_hw_reg_write(ah, s_seq, AR5K_QUEUE_DFS_SEQNUM(0));
  746. ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
  747. }
  748. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  749. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  750. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  751. /*
  752. * Misc
  753. */
  754. /* XXX: add ah->aid once mac80211 gives this to us */
  755. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  756. ath5k_hw_set_opmode(ah);
  757. /*PISR/SISR Not available on 5210*/
  758. if (ah->ah_version != AR5K_AR5210) {
  759. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  760. /* If we later allow tuning for this, store into sc structure */
  761. data = AR5K_TUNE_RSSI_THRES |
  762. AR5K_TUNE_BMISS_THRES << AR5K_RSSI_THR_BMISS_S;
  763. ath5k_hw_reg_write(ah, data, AR5K_RSSI_THR);
  764. }
  765. /*
  766. * Set Rx/Tx DMA Configuration
  767. *(passing dma size not available on 5210)
  768. */
  769. if (ah->ah_version != AR5K_AR5210) {
  770. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_SDMAMR,
  771. AR5K_DMASIZE_512B | AR5K_TXCFG_DMASIZE);
  772. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_SDMAMW,
  773. AR5K_DMASIZE_512B);
  774. }
  775. /*
  776. * Enable the PHY and wait until completion
  777. */
  778. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  779. /*
  780. * 5111/5112 Specific
  781. */
  782. if (ah->ah_version != AR5K_AR5210) {
  783. data = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  784. AR5K_PHY_RX_DELAY_M;
  785. data = (channel->hw_value & CHANNEL_CCK) ?
  786. ((data << 2) / 22) : (data / 10);
  787. udelay(100 + data);
  788. } else {
  789. mdelay(1);
  790. }
  791. /*
  792. * Enable calibration and wait until completion
  793. */
  794. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  795. AR5K_PHY_AGCCTL_CAL);
  796. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  797. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  798. ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
  799. channel->center_freq);
  800. return -EAGAIN;
  801. }
  802. ret = ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  803. if (ret)
  804. return ret;
  805. ah->ah_calibration = false;
  806. /* A and G modes can use QAM modulation which requires enabling
  807. * I and Q calibration. Don't bother in B mode. */
  808. if (!(mode == AR5K_MODE_11B)) {
  809. ah->ah_calibration = true;
  810. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  811. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  812. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  813. AR5K_PHY_IQ_RUN);
  814. }
  815. /*
  816. * Reset queues and start beacon timers at the end of the reset routine
  817. */
  818. for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
  819. /*No QCU on 5210*/
  820. if (ah->ah_version != AR5K_AR5210)
  821. AR5K_REG_WRITE_Q(ah, AR5K_QUEUE_QCUMASK(i), i);
  822. ret = ath5k_hw_reset_tx_queue(ah, i);
  823. if (ret) {
  824. ATH5K_ERR(ah->ah_sc,
  825. "failed to reset TX queue #%d\n", i);
  826. return ret;
  827. }
  828. }
  829. /* Pre-enable interrupts on 5211/5212*/
  830. if (ah->ah_version != AR5K_AR5210)
  831. ath5k_hw_set_intr(ah, AR5K_INT_RX | AR5K_INT_TX |
  832. AR5K_INT_FATAL);
  833. /*
  834. * Set RF kill flags if supported by the device (read from the EEPROM)
  835. * Disable gpio_intr for now since it results system hang.
  836. * TODO: Handle this in ath5k_intr
  837. */
  838. #if 0
  839. if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
  840. ath5k_hw_set_gpio_input(ah, 0);
  841. ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
  842. if (ah->ah_gpio[0] == 0)
  843. ath5k_hw_set_gpio_intr(ah, 0, 1);
  844. else
  845. ath5k_hw_set_gpio_intr(ah, 0, 0);
  846. }
  847. #endif
  848. /*
  849. * Set the 32MHz reference clock on 5212 phy clock sleep register
  850. */
  851. if (ah->ah_version == AR5K_AR5212) {
  852. ath5k_hw_reg_write(ah, AR5K_PHY_SCR_32MHZ, AR5K_PHY_SCR);
  853. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  854. ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ, AR5K_PHY_SCAL);
  855. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  856. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  857. ath5k_hw_reg_write(ah, ah->ah_radio == AR5K_RF5111 ?
  858. AR5K_PHY_SPENDING_RF5111 : AR5K_PHY_SPENDING_RF5112,
  859. AR5K_PHY_SPENDING);
  860. }
  861. /*
  862. * Disable beacons and reset the register
  863. */
  864. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
  865. AR5K_BEACON_RESET_TSF);
  866. return 0;
  867. }
  868. /*
  869. * Reset chipset
  870. */
  871. static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
  872. {
  873. int ret;
  874. u32 mask = val ? val : ~0U;
  875. ATH5K_TRACE(ah->ah_sc);
  876. /* Read-and-clear RX Descriptor Pointer*/
  877. ath5k_hw_reg_read(ah, AR5K_RXDP);
  878. /*
  879. * Reset the device and wait until success
  880. */
  881. ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
  882. /* Wait at least 128 PCI clocks */
  883. udelay(15);
  884. if (ah->ah_version == AR5K_AR5210) {
  885. val &= AR5K_RESET_CTL_CHIP;
  886. mask &= AR5K_RESET_CTL_CHIP;
  887. } else {
  888. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  889. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  890. }
  891. ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
  892. /*
  893. * Reset configuration register (for hw byte-swap). Note that this
  894. * is only set for big endian. We do the necessary magic in
  895. * AR5K_INIT_CFG.
  896. */
  897. if ((val & AR5K_RESET_CTL_PCU) == 0)
  898. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  899. return ret;
  900. }
  901. /*
  902. * Power management functions
  903. */
  904. /*
  905. * Sleep control
  906. */
  907. int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
  908. bool set_chip, u16 sleep_duration)
  909. {
  910. unsigned int i;
  911. u32 staid;
  912. ATH5K_TRACE(ah->ah_sc);
  913. staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
  914. switch (mode) {
  915. case AR5K_PM_AUTO:
  916. staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
  917. /* fallthrough */
  918. case AR5K_PM_NETWORK_SLEEP:
  919. if (set_chip == true)
  920. ath5k_hw_reg_write(ah,
  921. AR5K_SLEEP_CTL_SLE | sleep_duration,
  922. AR5K_SLEEP_CTL);
  923. staid |= AR5K_STA_ID1_PWR_SV;
  924. break;
  925. case AR5K_PM_FULL_SLEEP:
  926. if (set_chip == true)
  927. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
  928. AR5K_SLEEP_CTL);
  929. staid |= AR5K_STA_ID1_PWR_SV;
  930. break;
  931. case AR5K_PM_AWAKE:
  932. if (set_chip == false)
  933. goto commit;
  934. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_WAKE,
  935. AR5K_SLEEP_CTL);
  936. for (i = 5000; i > 0; i--) {
  937. /* Check if the chip did wake up */
  938. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  939. AR5K_PCICFG_SPWR_DN) == 0)
  940. break;
  941. /* Wait a bit and retry */
  942. udelay(200);
  943. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_WAKE,
  944. AR5K_SLEEP_CTL);
  945. }
  946. /* Fail if the chip didn't wake up */
  947. if (i <= 0)
  948. return -EIO;
  949. staid &= ~AR5K_STA_ID1_PWR_SV;
  950. break;
  951. default:
  952. return -EINVAL;
  953. }
  954. commit:
  955. ah->ah_power_mode = mode;
  956. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  957. return 0;
  958. }
  959. /***********************\
  960. DMA Related Functions
  961. \***********************/
  962. /*
  963. * Receive functions
  964. */
  965. /*
  966. * Start DMA receive
  967. */
  968. void ath5k_hw_start_rx(struct ath5k_hw *ah)
  969. {
  970. ATH5K_TRACE(ah->ah_sc);
  971. ath5k_hw_reg_write(ah, AR5K_CR_RXE, AR5K_CR);
  972. }
  973. /*
  974. * Stop DMA receive
  975. */
  976. int ath5k_hw_stop_rx_dma(struct ath5k_hw *ah)
  977. {
  978. unsigned int i;
  979. ATH5K_TRACE(ah->ah_sc);
  980. ath5k_hw_reg_write(ah, AR5K_CR_RXD, AR5K_CR);
  981. /*
  982. * It may take some time to disable the DMA receive unit
  983. */
  984. for (i = 2000; i > 0 &&
  985. (ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_CR_RXE) != 0;
  986. i--)
  987. udelay(10);
  988. return i ? 0 : -EBUSY;
  989. }
  990. /*
  991. * Get the address of the RX Descriptor
  992. */
  993. u32 ath5k_hw_get_rx_buf(struct ath5k_hw *ah)
  994. {
  995. return ath5k_hw_reg_read(ah, AR5K_RXDP);
  996. }
  997. /*
  998. * Set the address of the RX Descriptor
  999. */
  1000. void ath5k_hw_put_rx_buf(struct ath5k_hw *ah, u32 phys_addr)
  1001. {
  1002. ATH5K_TRACE(ah->ah_sc);
  1003. /*TODO:Shouldn't we check if RX is enabled first ?*/
  1004. ath5k_hw_reg_write(ah, phys_addr, AR5K_RXDP);
  1005. }
  1006. /*
  1007. * Transmit functions
  1008. */
  1009. /*
  1010. * Start DMA transmit for a specific queue
  1011. * (see also QCU/DCU functions)
  1012. */
  1013. int ath5k_hw_tx_start(struct ath5k_hw *ah, unsigned int queue)
  1014. {
  1015. u32 tx_queue;
  1016. ATH5K_TRACE(ah->ah_sc);
  1017. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  1018. /* Return if queue is declared inactive */
  1019. if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
  1020. return -EIO;
  1021. if (ah->ah_version == AR5K_AR5210) {
  1022. tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
  1023. /*
  1024. * Set the queue by type on 5210
  1025. */
  1026. switch (ah->ah_txq[queue].tqi_type) {
  1027. case AR5K_TX_QUEUE_DATA:
  1028. tx_queue |= AR5K_CR_TXE0 & ~AR5K_CR_TXD0;
  1029. break;
  1030. case AR5K_TX_QUEUE_BEACON:
  1031. tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
  1032. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  1033. AR5K_BSR);
  1034. break;
  1035. case AR5K_TX_QUEUE_CAB:
  1036. tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
  1037. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1FV | AR5K_BCR_TQ1V |
  1038. AR5K_BCR_BDMAE, AR5K_BSR);
  1039. break;
  1040. default:
  1041. return -EINVAL;
  1042. }
  1043. /* Start queue */
  1044. ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
  1045. } else {
  1046. /* Return if queue is disabled */
  1047. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXD, queue))
  1048. return -EIO;
  1049. /* Start queue */
  1050. AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXE, queue);
  1051. }
  1052. return 0;
  1053. }
  1054. /*
  1055. * Stop DMA transmit for a specific queue
  1056. * (see also QCU/DCU functions)
  1057. */
  1058. int ath5k_hw_stop_tx_dma(struct ath5k_hw *ah, unsigned int queue)
  1059. {
  1060. unsigned int i = 100;
  1061. u32 tx_queue, pending;
  1062. ATH5K_TRACE(ah->ah_sc);
  1063. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  1064. /* Return if queue is declared inactive */
  1065. if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
  1066. return -EIO;
  1067. if (ah->ah_version == AR5K_AR5210) {
  1068. tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
  1069. /*
  1070. * Set by queue type
  1071. */
  1072. switch (ah->ah_txq[queue].tqi_type) {
  1073. case AR5K_TX_QUEUE_DATA:
  1074. tx_queue |= AR5K_CR_TXD0 & ~AR5K_CR_TXE0;
  1075. break;
  1076. case AR5K_TX_QUEUE_BEACON:
  1077. case AR5K_TX_QUEUE_CAB:
  1078. /* XXX Fix me... */
  1079. tx_queue |= AR5K_CR_TXD1 & ~AR5K_CR_TXD1;
  1080. ath5k_hw_reg_write(ah, 0, AR5K_BSR);
  1081. break;
  1082. default:
  1083. return -EINVAL;
  1084. }
  1085. /* Stop queue */
  1086. ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
  1087. } else {
  1088. /*
  1089. * Schedule TX disable and wait until queue is empty
  1090. */
  1091. AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXD, queue);
  1092. /*Check for pending frames*/
  1093. do {
  1094. pending = ath5k_hw_reg_read(ah,
  1095. AR5K_QUEUE_STATUS(queue)) &
  1096. AR5K_QCU_STS_FRMPENDCNT;
  1097. udelay(100);
  1098. } while (--i && pending);
  1099. /* Clear register */
  1100. ath5k_hw_reg_write(ah, 0, AR5K_QCU_TXD);
  1101. }
  1102. /* TODO: Check for success else return error */
  1103. return 0;
  1104. }
  1105. /*
  1106. * Get the address of the TX Descriptor for a specific queue
  1107. * (see also QCU/DCU functions)
  1108. */
  1109. u32 ath5k_hw_get_tx_buf(struct ath5k_hw *ah, unsigned int queue)
  1110. {
  1111. u16 tx_reg;
  1112. ATH5K_TRACE(ah->ah_sc);
  1113. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  1114. /*
  1115. * Get the transmit queue descriptor pointer from the selected queue
  1116. */
  1117. /*5210 doesn't have QCU*/
  1118. if (ah->ah_version == AR5K_AR5210) {
  1119. switch (ah->ah_txq[queue].tqi_type) {
  1120. case AR5K_TX_QUEUE_DATA:
  1121. tx_reg = AR5K_NOQCU_TXDP0;
  1122. break;
  1123. case AR5K_TX_QUEUE_BEACON:
  1124. case AR5K_TX_QUEUE_CAB:
  1125. tx_reg = AR5K_NOQCU_TXDP1;
  1126. break;
  1127. default:
  1128. return 0xffffffff;
  1129. }
  1130. } else {
  1131. tx_reg = AR5K_QUEUE_TXDP(queue);
  1132. }
  1133. return ath5k_hw_reg_read(ah, tx_reg);
  1134. }
  1135. /*
  1136. * Set the address of the TX Descriptor for a specific queue
  1137. * (see also QCU/DCU functions)
  1138. */
  1139. int ath5k_hw_put_tx_buf(struct ath5k_hw *ah, unsigned int queue, u32 phys_addr)
  1140. {
  1141. u16 tx_reg;
  1142. ATH5K_TRACE(ah->ah_sc);
  1143. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  1144. /*
  1145. * Set the transmit queue descriptor pointer register by type
  1146. * on 5210
  1147. */
  1148. if (ah->ah_version == AR5K_AR5210) {
  1149. switch (ah->ah_txq[queue].tqi_type) {
  1150. case AR5K_TX_QUEUE_DATA:
  1151. tx_reg = AR5K_NOQCU_TXDP0;
  1152. break;
  1153. case AR5K_TX_QUEUE_BEACON:
  1154. case AR5K_TX_QUEUE_CAB:
  1155. tx_reg = AR5K_NOQCU_TXDP1;
  1156. break;
  1157. default:
  1158. return -EINVAL;
  1159. }
  1160. } else {
  1161. /*
  1162. * Set the transmit queue descriptor pointer for
  1163. * the selected queue on QCU for 5211+
  1164. * (this won't work if the queue is still active)
  1165. */
  1166. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue))
  1167. return -EIO;
  1168. tx_reg = AR5K_QUEUE_TXDP(queue);
  1169. }
  1170. /* Set descriptor pointer */
  1171. ath5k_hw_reg_write(ah, phys_addr, tx_reg);
  1172. return 0;
  1173. }
  1174. /*
  1175. * Update tx trigger level
  1176. */
  1177. int ath5k_hw_update_tx_triglevel(struct ath5k_hw *ah, bool increase)
  1178. {
  1179. u32 trigger_level, imr;
  1180. int ret = -EIO;
  1181. ATH5K_TRACE(ah->ah_sc);
  1182. /*
  1183. * Disable interrupts by setting the mask
  1184. */
  1185. imr = ath5k_hw_set_intr(ah, ah->ah_imr & ~AR5K_INT_GLOBAL);
  1186. /*TODO: Boundary check on trigger_level*/
  1187. trigger_level = AR5K_REG_MS(ath5k_hw_reg_read(ah, AR5K_TXCFG),
  1188. AR5K_TXCFG_TXFULL);
  1189. if (increase == false) {
  1190. if (--trigger_level < AR5K_TUNE_MIN_TX_FIFO_THRES)
  1191. goto done;
  1192. } else
  1193. trigger_level +=
  1194. ((AR5K_TUNE_MAX_TX_FIFO_THRES - trigger_level) / 2);
  1195. /*
  1196. * Update trigger level on success
  1197. */
  1198. if (ah->ah_version == AR5K_AR5210)
  1199. ath5k_hw_reg_write(ah, trigger_level, AR5K_TRIG_LVL);
  1200. else
  1201. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  1202. AR5K_TXCFG_TXFULL, trigger_level);
  1203. ret = 0;
  1204. done:
  1205. /*
  1206. * Restore interrupt mask
  1207. */
  1208. ath5k_hw_set_intr(ah, imr);
  1209. return ret;
  1210. }
  1211. /*
  1212. * Interrupt handling
  1213. */
  1214. /*
  1215. * Check if we have pending interrupts
  1216. */
  1217. bool ath5k_hw_is_intr_pending(struct ath5k_hw *ah)
  1218. {
  1219. ATH5K_TRACE(ah->ah_sc);
  1220. return ath5k_hw_reg_read(ah, AR5K_INTPEND);
  1221. }
  1222. /*
  1223. * Get interrupt mask (ISR)
  1224. */
  1225. int ath5k_hw_get_isr(struct ath5k_hw *ah, enum ath5k_int *interrupt_mask)
  1226. {
  1227. u32 data;
  1228. ATH5K_TRACE(ah->ah_sc);
  1229. /*
  1230. * Read interrupt status from the Interrupt Status register
  1231. * on 5210
  1232. */
  1233. if (ah->ah_version == AR5K_AR5210) {
  1234. data = ath5k_hw_reg_read(ah, AR5K_ISR);
  1235. if (unlikely(data == AR5K_INT_NOCARD)) {
  1236. *interrupt_mask = data;
  1237. return -ENODEV;
  1238. }
  1239. } else {
  1240. /*
  1241. * Read interrupt status from the Read-And-Clear shadow register
  1242. * Note: PISR/SISR Not available on 5210
  1243. */
  1244. data = ath5k_hw_reg_read(ah, AR5K_RAC_PISR);
  1245. }
  1246. /*
  1247. * Get abstract interrupt mask (driver-compatible)
  1248. */
  1249. *interrupt_mask = (data & AR5K_INT_COMMON) & ah->ah_imr;
  1250. if (unlikely(data == AR5K_INT_NOCARD))
  1251. return -ENODEV;
  1252. if (data & (AR5K_ISR_RXOK | AR5K_ISR_RXERR))
  1253. *interrupt_mask |= AR5K_INT_RX;
  1254. if (data & (AR5K_ISR_TXOK | AR5K_ISR_TXERR
  1255. | AR5K_ISR_TXDESC | AR5K_ISR_TXEOL))
  1256. *interrupt_mask |= AR5K_INT_TX;
  1257. if (ah->ah_version != AR5K_AR5210) {
  1258. /*HIU = Host Interface Unit (PCI etc)*/
  1259. if (unlikely(data & (AR5K_ISR_HIUERR)))
  1260. *interrupt_mask |= AR5K_INT_FATAL;
  1261. /*Beacon Not Ready*/
  1262. if (unlikely(data & (AR5K_ISR_BNR)))
  1263. *interrupt_mask |= AR5K_INT_BNR;
  1264. }
  1265. /*
  1266. * XXX: BMISS interrupts may occur after association.
  1267. * I found this on 5210 code but it needs testing. If this is
  1268. * true we should disable them before assoc and re-enable them
  1269. * after a successfull assoc + some jiffies.
  1270. */
  1271. #if 0
  1272. interrupt_mask &= ~AR5K_INT_BMISS;
  1273. #endif
  1274. /*
  1275. * In case we didn't handle anything,
  1276. * print the register value.
  1277. */
  1278. if (unlikely(*interrupt_mask == 0 && net_ratelimit()))
  1279. ATH5K_PRINTF("0x%08x\n", data);
  1280. return 0;
  1281. }
  1282. /*
  1283. * Set interrupt mask
  1284. */
  1285. enum ath5k_int ath5k_hw_set_intr(struct ath5k_hw *ah, enum ath5k_int new_mask)
  1286. {
  1287. enum ath5k_int old_mask, int_mask;
  1288. /*
  1289. * Disable card interrupts to prevent any race conditions
  1290. * (they will be re-enabled afterwards).
  1291. */
  1292. ath5k_hw_reg_write(ah, AR5K_IER_DISABLE, AR5K_IER);
  1293. old_mask = ah->ah_imr;
  1294. /*
  1295. * Add additional, chipset-dependent interrupt mask flags
  1296. * and write them to the IMR (interrupt mask register).
  1297. */
  1298. int_mask = new_mask & AR5K_INT_COMMON;
  1299. if (new_mask & AR5K_INT_RX)
  1300. int_mask |= AR5K_IMR_RXOK | AR5K_IMR_RXERR | AR5K_IMR_RXORN |
  1301. AR5K_IMR_RXDESC;
  1302. if (new_mask & AR5K_INT_TX)
  1303. int_mask |= AR5K_IMR_TXOK | AR5K_IMR_TXERR | AR5K_IMR_TXDESC |
  1304. AR5K_IMR_TXURN;
  1305. if (ah->ah_version != AR5K_AR5210) {
  1306. if (new_mask & AR5K_INT_FATAL) {
  1307. int_mask |= AR5K_IMR_HIUERR;
  1308. AR5K_REG_ENABLE_BITS(ah, AR5K_SIMR2, AR5K_SIMR2_MCABT |
  1309. AR5K_SIMR2_SSERR | AR5K_SIMR2_DPERR);
  1310. }
  1311. }
  1312. ath5k_hw_reg_write(ah, int_mask, AR5K_PIMR);
  1313. /* Store new interrupt mask */
  1314. ah->ah_imr = new_mask;
  1315. /* ..re-enable interrupts */
  1316. ath5k_hw_reg_write(ah, AR5K_IER_ENABLE, AR5K_IER);
  1317. return old_mask;
  1318. }
  1319. /*************************\
  1320. EEPROM access functions
  1321. \*************************/
  1322. /*
  1323. * Read from eeprom
  1324. */
  1325. static int ath5k_hw_eeprom_read(struct ath5k_hw *ah, u32 offset, u16 *data)
  1326. {
  1327. u32 status, timeout;
  1328. ATH5K_TRACE(ah->ah_sc);
  1329. /*
  1330. * Initialize EEPROM access
  1331. */
  1332. if (ah->ah_version == AR5K_AR5210) {
  1333. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
  1334. (void)ath5k_hw_reg_read(ah, AR5K_EEPROM_BASE + (4 * offset));
  1335. } else {
  1336. ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
  1337. AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
  1338. AR5K_EEPROM_CMD_READ);
  1339. }
  1340. for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
  1341. status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
  1342. if (status & AR5K_EEPROM_STAT_RDDONE) {
  1343. if (status & AR5K_EEPROM_STAT_RDERR)
  1344. return -EIO;
  1345. *data = (u16)(ath5k_hw_reg_read(ah, AR5K_EEPROM_DATA) &
  1346. 0xffff);
  1347. return 0;
  1348. }
  1349. udelay(15);
  1350. }
  1351. return -ETIMEDOUT;
  1352. }
  1353. /*
  1354. * Write to eeprom - currently disabled, use at your own risk
  1355. */
  1356. #if 0
  1357. static int ath5k_hw_eeprom_write(struct ath5k_hw *ah, u32 offset, u16 data)
  1358. {
  1359. u32 status, timeout;
  1360. ATH5K_TRACE(ah->ah_sc);
  1361. /*
  1362. * Initialize eeprom access
  1363. */
  1364. if (ah->ah_version == AR5K_AR5210) {
  1365. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
  1366. } else {
  1367. AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
  1368. AR5K_EEPROM_CMD_RESET);
  1369. }
  1370. /*
  1371. * Write data to data register
  1372. */
  1373. if (ah->ah_version == AR5K_AR5210) {
  1374. ath5k_hw_reg_write(ah, data, AR5K_EEPROM_BASE + (4 * offset));
  1375. } else {
  1376. ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
  1377. ath5k_hw_reg_write(ah, data, AR5K_EEPROM_DATA);
  1378. AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
  1379. AR5K_EEPROM_CMD_WRITE);
  1380. }
  1381. /*
  1382. * Check status
  1383. */
  1384. for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
  1385. status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
  1386. if (status & AR5K_EEPROM_STAT_WRDONE) {
  1387. if (status & AR5K_EEPROM_STAT_WRERR)
  1388. return EIO;
  1389. return 0;
  1390. }
  1391. udelay(15);
  1392. }
  1393. ATH5K_ERR(ah->ah_sc, "EEPROM Write is disabled!");
  1394. return -EIO;
  1395. }
  1396. #endif
  1397. /*
  1398. * Translate binary channel representation in EEPROM to frequency
  1399. */
  1400. static u16 ath5k_eeprom_bin2freq(struct ath5k_hw *ah, u16 bin, unsigned int mode)
  1401. {
  1402. u16 val;
  1403. if (bin == AR5K_EEPROM_CHANNEL_DIS)
  1404. return bin;
  1405. if (mode == AR5K_EEPROM_MODE_11A) {
  1406. if (ah->ah_ee_version > AR5K_EEPROM_VERSION_3_2)
  1407. val = (5 * bin) + 4800;
  1408. else
  1409. val = bin > 62 ? (10 * 62) + (5 * (bin - 62)) + 5100 :
  1410. (bin * 10) + 5100;
  1411. } else {
  1412. if (ah->ah_ee_version > AR5K_EEPROM_VERSION_3_2)
  1413. val = bin + 2300;
  1414. else
  1415. val = bin + 2400;
  1416. }
  1417. return val;
  1418. }
  1419. /*
  1420. * Read antenna infos from eeprom
  1421. */
  1422. static int ath5k_eeprom_read_ants(struct ath5k_hw *ah, u32 *offset,
  1423. unsigned int mode)
  1424. {
  1425. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1426. u32 o = *offset;
  1427. u16 val;
  1428. int ret, i = 0;
  1429. AR5K_EEPROM_READ(o++, val);
  1430. ee->ee_switch_settling[mode] = (val >> 8) & 0x7f;
  1431. ee->ee_ant_tx_rx[mode] = (val >> 2) & 0x3f;
  1432. ee->ee_ant_control[mode][i] = (val << 4) & 0x3f;
  1433. AR5K_EEPROM_READ(o++, val);
  1434. ee->ee_ant_control[mode][i++] |= (val >> 12) & 0xf;
  1435. ee->ee_ant_control[mode][i++] = (val >> 6) & 0x3f;
  1436. ee->ee_ant_control[mode][i++] = val & 0x3f;
  1437. AR5K_EEPROM_READ(o++, val);
  1438. ee->ee_ant_control[mode][i++] = (val >> 10) & 0x3f;
  1439. ee->ee_ant_control[mode][i++] = (val >> 4) & 0x3f;
  1440. ee->ee_ant_control[mode][i] = (val << 2) & 0x3f;
  1441. AR5K_EEPROM_READ(o++, val);
  1442. ee->ee_ant_control[mode][i++] |= (val >> 14) & 0x3;
  1443. ee->ee_ant_control[mode][i++] = (val >> 8) & 0x3f;
  1444. ee->ee_ant_control[mode][i++] = (val >> 2) & 0x3f;
  1445. ee->ee_ant_control[mode][i] = (val << 4) & 0x3f;
  1446. AR5K_EEPROM_READ(o++, val);
  1447. ee->ee_ant_control[mode][i++] |= (val >> 12) & 0xf;
  1448. ee->ee_ant_control[mode][i++] = (val >> 6) & 0x3f;
  1449. ee->ee_ant_control[mode][i++] = val & 0x3f;
  1450. /* Get antenna modes */
  1451. ah->ah_antenna[mode][0] =
  1452. (ee->ee_ant_control[mode][0] << 4) | 0x1;
  1453. ah->ah_antenna[mode][AR5K_ANT_FIXED_A] =
  1454. ee->ee_ant_control[mode][1] |
  1455. (ee->ee_ant_control[mode][2] << 6) |
  1456. (ee->ee_ant_control[mode][3] << 12) |
  1457. (ee->ee_ant_control[mode][4] << 18) |
  1458. (ee->ee_ant_control[mode][5] << 24);
  1459. ah->ah_antenna[mode][AR5K_ANT_FIXED_B] =
  1460. ee->ee_ant_control[mode][6] |
  1461. (ee->ee_ant_control[mode][7] << 6) |
  1462. (ee->ee_ant_control[mode][8] << 12) |
  1463. (ee->ee_ant_control[mode][9] << 18) |
  1464. (ee->ee_ant_control[mode][10] << 24);
  1465. /* return new offset */
  1466. *offset = o;
  1467. return 0;
  1468. }
  1469. /*
  1470. * Read supported modes from eeprom
  1471. */
  1472. static int ath5k_eeprom_read_modes(struct ath5k_hw *ah, u32 *offset,
  1473. unsigned int mode)
  1474. {
  1475. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1476. u32 o = *offset;
  1477. u16 val;
  1478. int ret;
  1479. AR5K_EEPROM_READ(o++, val);
  1480. ee->ee_tx_end2xlna_enable[mode] = (val >> 8) & 0xff;
  1481. ee->ee_thr_62[mode] = val & 0xff;
  1482. if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
  1483. ee->ee_thr_62[mode] = mode == AR5K_EEPROM_MODE_11A ? 15 : 28;
  1484. AR5K_EEPROM_READ(o++, val);
  1485. ee->ee_tx_end2xpa_disable[mode] = (val >> 8) & 0xff;
  1486. ee->ee_tx_frm2xpa_enable[mode] = val & 0xff;
  1487. AR5K_EEPROM_READ(o++, val);
  1488. ee->ee_pga_desired_size[mode] = (val >> 8) & 0xff;
  1489. if ((val & 0xff) & 0x80)
  1490. ee->ee_noise_floor_thr[mode] = -((((val & 0xff) ^ 0xff)) + 1);
  1491. else
  1492. ee->ee_noise_floor_thr[mode] = val & 0xff;
  1493. if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
  1494. ee->ee_noise_floor_thr[mode] =
  1495. mode == AR5K_EEPROM_MODE_11A ? -54 : -1;
  1496. AR5K_EEPROM_READ(o++, val);
  1497. ee->ee_xlna_gain[mode] = (val >> 5) & 0xff;
  1498. ee->ee_x_gain[mode] = (val >> 1) & 0xf;
  1499. ee->ee_xpd[mode] = val & 0x1;
  1500. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0)
  1501. ee->ee_fixed_bias[mode] = (val >> 13) & 0x1;
  1502. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_3) {
  1503. AR5K_EEPROM_READ(o++, val);
  1504. ee->ee_false_detect[mode] = (val >> 6) & 0x7f;
  1505. if (mode == AR5K_EEPROM_MODE_11A)
  1506. ee->ee_xr_power[mode] = val & 0x3f;
  1507. else {
  1508. ee->ee_ob[mode][0] = val & 0x7;
  1509. ee->ee_db[mode][0] = (val >> 3) & 0x7;
  1510. }
  1511. }
  1512. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_4) {
  1513. ee->ee_i_gain[mode] = AR5K_EEPROM_I_GAIN;
  1514. ee->ee_cck_ofdm_power_delta = AR5K_EEPROM_CCK_OFDM_DELTA;
  1515. } else {
  1516. ee->ee_i_gain[mode] = (val >> 13) & 0x7;
  1517. AR5K_EEPROM_READ(o++, val);
  1518. ee->ee_i_gain[mode] |= (val << 3) & 0x38;
  1519. if (mode == AR5K_EEPROM_MODE_11G)
  1520. ee->ee_cck_ofdm_power_delta = (val >> 3) & 0xff;
  1521. }
  1522. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
  1523. mode == AR5K_EEPROM_MODE_11A) {
  1524. ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
  1525. ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
  1526. }
  1527. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_6 &&
  1528. mode == AR5K_EEPROM_MODE_11G)
  1529. ee->ee_scaled_cck_delta = (val >> 11) & 0x1f;
  1530. /* return new offset */
  1531. *offset = o;
  1532. return 0;
  1533. }
  1534. /*
  1535. * Initialize eeprom & capabilities structs
  1536. */
  1537. static int ath5k_eeprom_init(struct ath5k_hw *ah)
  1538. {
  1539. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1540. unsigned int mode, i;
  1541. int ret;
  1542. u32 offset;
  1543. u16 val;
  1544. /* Initial TX thermal adjustment values */
  1545. ee->ee_tx_clip = 4;
  1546. ee->ee_pwd_84 = ee->ee_pwd_90 = 1;
  1547. ee->ee_gain_select = 1;
  1548. /*
  1549. * Read values from EEPROM and store them in the capability structure
  1550. */
  1551. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MAGIC, ee_magic);
  1552. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_PROTECT, ee_protect);
  1553. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_REG_DOMAIN, ee_regdomain);
  1554. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_VERSION, ee_version);
  1555. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_HDR, ee_header);
  1556. /* Return if we have an old EEPROM */
  1557. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_0)
  1558. return 0;
  1559. #ifdef notyet
  1560. /*
  1561. * Validate the checksum of the EEPROM date. There are some
  1562. * devices with invalid EEPROMs.
  1563. */
  1564. for (cksum = 0, offset = 0; offset < AR5K_EEPROM_INFO_MAX; offset++) {
  1565. AR5K_EEPROM_READ(AR5K_EEPROM_INFO(offset), val);
  1566. cksum ^= val;
  1567. }
  1568. if (cksum != AR5K_EEPROM_INFO_CKSUM) {
  1569. ATH5K_ERR(ah->ah_sc, "Invalid EEPROM checksum 0x%04x\n", cksum);
  1570. return -EIO;
  1571. }
  1572. #endif
  1573. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_ANT_GAIN(ah->ah_ee_version),
  1574. ee_ant_gain);
  1575. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  1576. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC0, ee_misc0);
  1577. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC1, ee_misc1);
  1578. }
  1579. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_3) {
  1580. AR5K_EEPROM_READ(AR5K_EEPROM_OBDB0_2GHZ, val);
  1581. ee->ee_ob[AR5K_EEPROM_MODE_11B][0] = val & 0x7;
  1582. ee->ee_db[AR5K_EEPROM_MODE_11B][0] = (val >> 3) & 0x7;
  1583. AR5K_EEPROM_READ(AR5K_EEPROM_OBDB1_2GHZ, val);
  1584. ee->ee_ob[AR5K_EEPROM_MODE_11G][0] = val & 0x7;
  1585. ee->ee_db[AR5K_EEPROM_MODE_11G][0] = (val >> 3) & 0x7;
  1586. }
  1587. /*
  1588. * Get conformance test limit values
  1589. */
  1590. offset = AR5K_EEPROM_CTL(ah->ah_ee_version);
  1591. ee->ee_ctls = AR5K_EEPROM_N_CTLS(ah->ah_ee_version);
  1592. for (i = 0; i < ee->ee_ctls; i++) {
  1593. AR5K_EEPROM_READ(offset++, val);
  1594. ee->ee_ctl[i] = (val >> 8) & 0xff;
  1595. ee->ee_ctl[i + 1] = val & 0xff;
  1596. }
  1597. /*
  1598. * Get values for 802.11a (5GHz)
  1599. */
  1600. mode = AR5K_EEPROM_MODE_11A;
  1601. ee->ee_turbo_max_power[mode] =
  1602. AR5K_EEPROM_HDR_T_5GHZ_DBM(ee->ee_header);
  1603. offset = AR5K_EEPROM_MODES_11A(ah->ah_ee_version);
  1604. ret = ath5k_eeprom_read_ants(ah, &offset, mode);
  1605. if (ret)
  1606. return ret;
  1607. AR5K_EEPROM_READ(offset++, val);
  1608. ee->ee_adc_desired_size[mode] = (s8)((val >> 8) & 0xff);
  1609. ee->ee_ob[mode][3] = (val >> 5) & 0x7;
  1610. ee->ee_db[mode][3] = (val >> 2) & 0x7;
  1611. ee->ee_ob[mode][2] = (val << 1) & 0x7;
  1612. AR5K_EEPROM_READ(offset++, val);
  1613. ee->ee_ob[mode][2] |= (val >> 15) & 0x1;
  1614. ee->ee_db[mode][2] = (val >> 12) & 0x7;
  1615. ee->ee_ob[mode][1] = (val >> 9) & 0x7;
  1616. ee->ee_db[mode][1] = (val >> 6) & 0x7;
  1617. ee->ee_ob[mode][0] = (val >> 3) & 0x7;
  1618. ee->ee_db[mode][0] = val & 0x7;
  1619. ret = ath5k_eeprom_read_modes(ah, &offset, mode);
  1620. if (ret)
  1621. return ret;
  1622. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1) {
  1623. AR5K_EEPROM_READ(offset++, val);
  1624. ee->ee_margin_tx_rx[mode] = val & 0x3f;
  1625. }
  1626. /*
  1627. * Get values for 802.11b (2.4GHz)
  1628. */
  1629. mode = AR5K_EEPROM_MODE_11B;
  1630. offset = AR5K_EEPROM_MODES_11B(ah->ah_ee_version);
  1631. ret = ath5k_eeprom_read_ants(ah, &offset, mode);
  1632. if (ret)
  1633. return ret;
  1634. AR5K_EEPROM_READ(offset++, val);
  1635. ee->ee_adc_desired_size[mode] = (s8)((val >> 8) & 0xff);
  1636. ee->ee_ob[mode][1] = (val >> 4) & 0x7;
  1637. ee->ee_db[mode][1] = val & 0x7;
  1638. ret = ath5k_eeprom_read_modes(ah, &offset, mode);
  1639. if (ret)
  1640. return ret;
  1641. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  1642. AR5K_EEPROM_READ(offset++, val);
  1643. ee->ee_cal_pier[mode][0] =
  1644. ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
  1645. ee->ee_cal_pier[mode][1] =
  1646. ath5k_eeprom_bin2freq(ah, (val >> 8) & 0xff, mode);
  1647. AR5K_EEPROM_READ(offset++, val);
  1648. ee->ee_cal_pier[mode][2] =
  1649. ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
  1650. }
  1651. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  1652. ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
  1653. /*
  1654. * Get values for 802.11g (2.4GHz)
  1655. */
  1656. mode = AR5K_EEPROM_MODE_11G;
  1657. offset = AR5K_EEPROM_MODES_11G(ah->ah_ee_version);
  1658. ret = ath5k_eeprom_read_ants(ah, &offset, mode);
  1659. if (ret)
  1660. return ret;
  1661. AR5K_EEPROM_READ(offset++, val);
  1662. ee->ee_adc_desired_size[mode] = (s8)((val >> 8) & 0xff);
  1663. ee->ee_ob[mode][1] = (val >> 4) & 0x7;
  1664. ee->ee_db[mode][1] = val & 0x7;
  1665. ret = ath5k_eeprom_read_modes(ah, &offset, mode);
  1666. if (ret)
  1667. return ret;
  1668. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  1669. AR5K_EEPROM_READ(offset++, val);
  1670. ee->ee_cal_pier[mode][0] =
  1671. ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
  1672. ee->ee_cal_pier[mode][1] =
  1673. ath5k_eeprom_bin2freq(ah, (val >> 8) & 0xff, mode);
  1674. AR5K_EEPROM_READ(offset++, val);
  1675. ee->ee_turbo_max_power[mode] = val & 0x7f;
  1676. ee->ee_xr_power[mode] = (val >> 7) & 0x3f;
  1677. AR5K_EEPROM_READ(offset++, val);
  1678. ee->ee_cal_pier[mode][2] =
  1679. ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
  1680. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  1681. ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
  1682. AR5K_EEPROM_READ(offset++, val);
  1683. ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
  1684. ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
  1685. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_2) {
  1686. AR5K_EEPROM_READ(offset++, val);
  1687. ee->ee_cck_ofdm_gain_delta = val & 0xff;
  1688. }
  1689. }
  1690. /*
  1691. * Read 5GHz EEPROM channels
  1692. */
  1693. return 0;
  1694. }
  1695. /*
  1696. * Read the MAC address from eeprom
  1697. */
  1698. static int ath5k_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
  1699. {
  1700. u8 mac_d[ETH_ALEN];
  1701. u32 total, offset;
  1702. u16 data;
  1703. int octet, ret;
  1704. memset(mac, 0, ETH_ALEN);
  1705. memset(mac_d, 0, ETH_ALEN);
  1706. ret = ath5k_hw_eeprom_read(ah, 0x20, &data);
  1707. if (ret)
  1708. return ret;
  1709. for (offset = 0x1f, octet = 0, total = 0; offset >= 0x1d; offset--) {
  1710. ret = ath5k_hw_eeprom_read(ah, offset, &data);
  1711. if (ret)
  1712. return ret;
  1713. total += data;
  1714. mac_d[octet + 1] = data & 0xff;
  1715. mac_d[octet] = data >> 8;
  1716. octet += 2;
  1717. }
  1718. memcpy(mac, mac_d, ETH_ALEN);
  1719. if (!total || total == 3 * 0xffff)
  1720. return -EINVAL;
  1721. return 0;
  1722. }
  1723. /*
  1724. * Fill the capabilities struct
  1725. */
  1726. static int ath5k_hw_get_capabilities(struct ath5k_hw *ah)
  1727. {
  1728. u16 ee_header;
  1729. ATH5K_TRACE(ah->ah_sc);
  1730. /* Capabilities stored in the EEPROM */
  1731. ee_header = ah->ah_capabilities.cap_eeprom.ee_header;
  1732. if (ah->ah_version == AR5K_AR5210) {
  1733. /*
  1734. * Set radio capabilities
  1735. * (The AR5110 only supports the middle 5GHz band)
  1736. */
  1737. ah->ah_capabilities.cap_range.range_5ghz_min = 5120;
  1738. ah->ah_capabilities.cap_range.range_5ghz_max = 5430;
  1739. ah->ah_capabilities.cap_range.range_2ghz_min = 0;
  1740. ah->ah_capabilities.cap_range.range_2ghz_max = 0;
  1741. /* Set supported modes */
  1742. __set_bit(AR5K_MODE_11A, ah->ah_capabilities.cap_mode);
  1743. __set_bit(AR5K_MODE_11A_TURBO, ah->ah_capabilities.cap_mode);
  1744. } else {
  1745. /*
  1746. * XXX The tranceiver supports frequencies from 4920 to 6100GHz
  1747. * XXX and from 2312 to 2732GHz. There are problems with the
  1748. * XXX current ieee80211 implementation because the IEEE
  1749. * XXX channel mapping does not support negative channel
  1750. * XXX numbers (2312MHz is channel -19). Of course, this
  1751. * XXX doesn't matter because these channels are out of range
  1752. * XXX but some regulation domains like MKK (Japan) will
  1753. * XXX support frequencies somewhere around 4.8GHz.
  1754. */
  1755. /*
  1756. * Set radio capabilities
  1757. */
  1758. if (AR5K_EEPROM_HDR_11A(ee_header)) {
  1759. ah->ah_capabilities.cap_range.range_5ghz_min = 5005; /* 4920 */
  1760. ah->ah_capabilities.cap_range.range_5ghz_max = 6100;
  1761. /* Set supported modes */
  1762. __set_bit(AR5K_MODE_11A,
  1763. ah->ah_capabilities.cap_mode);
  1764. __set_bit(AR5K_MODE_11A_TURBO,
  1765. ah->ah_capabilities.cap_mode);
  1766. if (ah->ah_version == AR5K_AR5212)
  1767. __set_bit(AR5K_MODE_11G_TURBO,
  1768. ah->ah_capabilities.cap_mode);
  1769. }
  1770. /* Enable 802.11b if a 2GHz capable radio (2111/5112) is
  1771. * connected */
  1772. if (AR5K_EEPROM_HDR_11B(ee_header) ||
  1773. AR5K_EEPROM_HDR_11G(ee_header)) {
  1774. ah->ah_capabilities.cap_range.range_2ghz_min = 2412; /* 2312 */
  1775. ah->ah_capabilities.cap_range.range_2ghz_max = 2732;
  1776. if (AR5K_EEPROM_HDR_11B(ee_header))
  1777. __set_bit(AR5K_MODE_11B,
  1778. ah->ah_capabilities.cap_mode);
  1779. if (AR5K_EEPROM_HDR_11G(ee_header))
  1780. __set_bit(AR5K_MODE_11G,
  1781. ah->ah_capabilities.cap_mode);
  1782. }
  1783. }
  1784. /* GPIO */
  1785. ah->ah_gpio_npins = AR5K_NUM_GPIO;
  1786. /* Set number of supported TX queues */
  1787. if (ah->ah_version == AR5K_AR5210)
  1788. ah->ah_capabilities.cap_queues.q_tx_num =
  1789. AR5K_NUM_TX_QUEUES_NOQCU;
  1790. else
  1791. ah->ah_capabilities.cap_queues.q_tx_num = AR5K_NUM_TX_QUEUES;
  1792. return 0;
  1793. }
  1794. /*********************************\
  1795. Protocol Control Unit Functions
  1796. \*********************************/
  1797. /*
  1798. * Set Operation mode
  1799. */
  1800. int ath5k_hw_set_opmode(struct ath5k_hw *ah)
  1801. {
  1802. u32 pcu_reg, beacon_reg, low_id, high_id;
  1803. pcu_reg = 0;
  1804. beacon_reg = 0;
  1805. ATH5K_TRACE(ah->ah_sc);
  1806. switch (ah->ah_op_mode) {
  1807. case IEEE80211_IF_TYPE_IBSS:
  1808. pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_DESC_ANTENNA |
  1809. (ah->ah_version == AR5K_AR5210 ?
  1810. AR5K_STA_ID1_NO_PSPOLL : 0);
  1811. beacon_reg |= AR5K_BCR_ADHOC;
  1812. break;
  1813. case IEEE80211_IF_TYPE_AP:
  1814. pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_RTS_DEF_ANTENNA |
  1815. (ah->ah_version == AR5K_AR5210 ?
  1816. AR5K_STA_ID1_NO_PSPOLL : 0);
  1817. beacon_reg |= AR5K_BCR_AP;
  1818. break;
  1819. case IEEE80211_IF_TYPE_STA:
  1820. pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
  1821. (ah->ah_version == AR5K_AR5210 ?
  1822. AR5K_STA_ID1_PWR_SV : 0);
  1823. case IEEE80211_IF_TYPE_MNTR:
  1824. pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
  1825. (ah->ah_version == AR5K_AR5210 ?
  1826. AR5K_STA_ID1_NO_PSPOLL : 0);
  1827. break;
  1828. default:
  1829. return -EINVAL;
  1830. }
  1831. /*
  1832. * Set PCU registers
  1833. */
  1834. low_id = AR5K_LOW_ID(ah->ah_sta_id);
  1835. high_id = AR5K_HIGH_ID(ah->ah_sta_id);
  1836. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  1837. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  1838. /*
  1839. * Set Beacon Control Register on 5210
  1840. */
  1841. if (ah->ah_version == AR5K_AR5210)
  1842. ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
  1843. return 0;
  1844. }
  1845. /*
  1846. * BSSID Functions
  1847. */
  1848. /*
  1849. * Get station id
  1850. */
  1851. void ath5k_hw_get_lladdr(struct ath5k_hw *ah, u8 *mac)
  1852. {
  1853. ATH5K_TRACE(ah->ah_sc);
  1854. memcpy(mac, ah->ah_sta_id, ETH_ALEN);
  1855. }
  1856. /*
  1857. * Set station id
  1858. */
  1859. int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
  1860. {
  1861. u32 low_id, high_id;
  1862. ATH5K_TRACE(ah->ah_sc);
  1863. /* Set new station ID */
  1864. memcpy(ah->ah_sta_id, mac, ETH_ALEN);
  1865. low_id = AR5K_LOW_ID(mac);
  1866. high_id = AR5K_HIGH_ID(mac);
  1867. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  1868. ath5k_hw_reg_write(ah, high_id, AR5K_STA_ID1);
  1869. return 0;
  1870. }
  1871. /*
  1872. * Set BSSID
  1873. */
  1874. void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
  1875. {
  1876. u32 low_id, high_id;
  1877. u16 tim_offset = 0;
  1878. /*
  1879. * Set simple BSSID mask on 5212
  1880. */
  1881. if (ah->ah_version == AR5K_AR5212) {
  1882. ath5k_hw_reg_write(ah, 0xfffffff, AR5K_BSS_IDM0);
  1883. ath5k_hw_reg_write(ah, 0xfffffff, AR5K_BSS_IDM1);
  1884. }
  1885. /*
  1886. * Set BSSID which triggers the "SME Join" operation
  1887. */
  1888. low_id = AR5K_LOW_ID(bssid);
  1889. high_id = AR5K_HIGH_ID(bssid);
  1890. ath5k_hw_reg_write(ah, low_id, AR5K_BSS_ID0);
  1891. ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
  1892. AR5K_BSS_ID1_AID_S), AR5K_BSS_ID1);
  1893. if (assoc_id == 0) {
  1894. ath5k_hw_disable_pspoll(ah);
  1895. return;
  1896. }
  1897. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  1898. tim_offset ? tim_offset + 4 : 0);
  1899. ath5k_hw_enable_pspoll(ah, NULL, 0);
  1900. }
  1901. /**
  1902. * ath5k_hw_set_bssid_mask - set common bits we should listen to
  1903. *
  1904. * The bssid_mask is a utility used by AR5212 hardware to inform the hardware
  1905. * which bits of the interface's MAC address should be looked at when trying
  1906. * to decide which packets to ACK. In station mode every bit matters. In AP
  1907. * mode with a single BSS every bit matters as well. In AP mode with
  1908. * multiple BSSes not every bit matters.
  1909. *
  1910. * @ah: the &struct ath5k_hw
  1911. * @mask: the bssid_mask, a u8 array of size ETH_ALEN
  1912. *
  1913. * Note that this is a simple filter and *does* not filter out all
  1914. * relevant frames. Some non-relevant frames will get through, probability
  1915. * jocks are welcomed to compute.
  1916. *
  1917. * When handling multiple BSSes (or VAPs) you can get the BSSID mask by
  1918. * computing the set of:
  1919. *
  1920. * ~ ( MAC XOR BSSID )
  1921. *
  1922. * When you do this you are essentially computing the common bits. Later it
  1923. * is assumed the harware will "and" (&) the BSSID mask with the MAC address
  1924. * to obtain the relevant bits which should match on the destination frame.
  1925. *
  1926. * Simple example: on your card you have have two BSSes you have created with
  1927. * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
  1928. * There is another BSSID-03 but you are not part of it. For simplicity's sake,
  1929. * assuming only 4 bits for a mac address and for BSSIDs you can then have:
  1930. *
  1931. * \
  1932. * MAC: 0001 |
  1933. * BSSID-01: 0100 | --> Belongs to us
  1934. * BSSID-02: 1001 |
  1935. * /
  1936. * -------------------
  1937. * BSSID-03: 0110 | --> External
  1938. * -------------------
  1939. *
  1940. * Our bssid_mask would then be:
  1941. *
  1942. * On loop iteration for BSSID-01:
  1943. * ~(0001 ^ 0100) -> ~(0101)
  1944. * -> 1010
  1945. * bssid_mask = 1010
  1946. *
  1947. * On loop iteration for BSSID-02:
  1948. * bssid_mask &= ~(0001 ^ 1001)
  1949. * bssid_mask = (1010) & ~(0001 ^ 1001)
  1950. * bssid_mask = (1010) & ~(1001)
  1951. * bssid_mask = (1010) & (0110)
  1952. * bssid_mask = 0010
  1953. *
  1954. * A bssid_mask of 0010 means "only pay attention to the second least
  1955. * significant bit". This is because its the only bit common
  1956. * amongst the MAC and all BSSIDs we support. To findout what the real
  1957. * common bit is we can simply "&" the bssid_mask now with any BSSID we have
  1958. * or our MAC address (we assume the hardware uses the MAC address).
  1959. *
  1960. * Now, suppose there's an incoming frame for BSSID-03:
  1961. *
  1962. * IFRAME-01: 0110
  1963. *
  1964. * An easy eye-inspeciton of this already should tell you that this frame
  1965. * will not pass our check. This is beacuse the bssid_mask tells the
  1966. * hardware to only look at the second least significant bit and the
  1967. * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
  1968. * as 1, which does not match 0.
  1969. *
  1970. * So with IFRAME-01 we *assume* the hardware will do:
  1971. *
  1972. * allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
  1973. * --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
  1974. * --> allow = (0010) == 0000 ? 1 : 0;
  1975. * --> allow = 0
  1976. *
  1977. * Lets now test a frame that should work:
  1978. *
  1979. * IFRAME-02: 0001 (we should allow)
  1980. *
  1981. * allow = (0001 & 1010) == 1010
  1982. *
  1983. * allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
  1984. * --> allow = (0001 & 0010) == (0010 & 0001) ? 1 :0;
  1985. * --> allow = (0010) == (0010)
  1986. * --> allow = 1
  1987. *
  1988. * Other examples:
  1989. *
  1990. * IFRAME-03: 0100 --> allowed
  1991. * IFRAME-04: 1001 --> allowed
  1992. * IFRAME-05: 1101 --> allowed but its not for us!!!
  1993. *
  1994. */
  1995. int ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  1996. {
  1997. u32 low_id, high_id;
  1998. ATH5K_TRACE(ah->ah_sc);
  1999. if (ah->ah_version == AR5K_AR5212) {
  2000. low_id = AR5K_LOW_ID(mask);
  2001. high_id = AR5K_HIGH_ID(mask);
  2002. ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
  2003. ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
  2004. return 0;
  2005. }
  2006. return -EIO;
  2007. }
  2008. /*
  2009. * Receive start/stop functions
  2010. */
  2011. /*
  2012. * Start receive on PCU
  2013. */
  2014. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  2015. {
  2016. ATH5K_TRACE(ah->ah_sc);
  2017. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  2018. }
  2019. /*
  2020. * Stop receive on PCU
  2021. */
  2022. void ath5k_hw_stop_pcu_recv(struct ath5k_hw *ah)
  2023. {
  2024. ATH5K_TRACE(ah->ah_sc);
  2025. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  2026. }
  2027. /*
  2028. * RX Filter functions
  2029. */
  2030. /*
  2031. * Set multicast filter
  2032. */
  2033. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  2034. {
  2035. ATH5K_TRACE(ah->ah_sc);
  2036. /* Set the multicat filter */
  2037. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  2038. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  2039. }
  2040. /*
  2041. * Set multicast filter by index
  2042. */
  2043. int ath5k_hw_set_mcast_filterindex(struct ath5k_hw *ah, u32 index)
  2044. {
  2045. ATH5K_TRACE(ah->ah_sc);
  2046. if (index >= 64)
  2047. return -EINVAL;
  2048. else if (index >= 32)
  2049. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
  2050. (1 << (index - 32)));
  2051. else
  2052. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  2053. return 0;
  2054. }
  2055. /*
  2056. * Clear Multicast filter by index
  2057. */
  2058. int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  2059. {
  2060. ATH5K_TRACE(ah->ah_sc);
  2061. if (index >= 64)
  2062. return -EINVAL;
  2063. else if (index >= 32)
  2064. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
  2065. (1 << (index - 32)));
  2066. else
  2067. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  2068. return 0;
  2069. }
  2070. /*
  2071. * Get current rx filter
  2072. */
  2073. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  2074. {
  2075. u32 data, filter = 0;
  2076. ATH5K_TRACE(ah->ah_sc);
  2077. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  2078. /*Radar detection for 5212*/
  2079. if (ah->ah_version == AR5K_AR5212) {
  2080. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  2081. if (data & AR5K_PHY_ERR_FIL_RADAR)
  2082. filter |= AR5K_RX_FILTER_RADARERR;
  2083. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  2084. filter |= AR5K_RX_FILTER_PHYERR;
  2085. }
  2086. return filter;
  2087. }
  2088. /*
  2089. * Set rx filter
  2090. */
  2091. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  2092. {
  2093. u32 data = 0;
  2094. ATH5K_TRACE(ah->ah_sc);
  2095. /* Set PHY error filter register on 5212*/
  2096. if (ah->ah_version == AR5K_AR5212) {
  2097. if (filter & AR5K_RX_FILTER_RADARERR)
  2098. data |= AR5K_PHY_ERR_FIL_RADAR;
  2099. if (filter & AR5K_RX_FILTER_PHYERR)
  2100. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  2101. }
  2102. /*
  2103. * The AR5210 uses promiscous mode to detect radar activity
  2104. */
  2105. if (ah->ah_version == AR5K_AR5210 &&
  2106. (filter & AR5K_RX_FILTER_RADARERR)) {
  2107. filter &= ~AR5K_RX_FILTER_RADARERR;
  2108. filter |= AR5K_RX_FILTER_PROM;
  2109. }
  2110. /*Zero length DMA*/
  2111. if (data)
  2112. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  2113. else
  2114. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  2115. /*Write RX Filter register*/
  2116. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  2117. /*Write PHY error filter register on 5212*/
  2118. if (ah->ah_version == AR5K_AR5212)
  2119. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  2120. }
  2121. /*
  2122. * Beacon related functions
  2123. */
  2124. /*
  2125. * Get a 32bit TSF
  2126. */
  2127. u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
  2128. {
  2129. ATH5K_TRACE(ah->ah_sc);
  2130. return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  2131. }
  2132. /*
  2133. * Get the full 64bit TSF
  2134. */
  2135. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  2136. {
  2137. u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  2138. ATH5K_TRACE(ah->ah_sc);
  2139. return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
  2140. }
  2141. /*
  2142. * Force a TSF reset
  2143. */
  2144. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  2145. {
  2146. ATH5K_TRACE(ah->ah_sc);
  2147. AR5K_REG_ENABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_RESET_TSF);
  2148. }
  2149. /*
  2150. * Initialize beacon timers
  2151. */
  2152. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  2153. {
  2154. u32 timer1, timer2, timer3;
  2155. ATH5K_TRACE(ah->ah_sc);
  2156. /*
  2157. * Set the additional timers by mode
  2158. */
  2159. switch (ah->ah_op_mode) {
  2160. case IEEE80211_IF_TYPE_STA:
  2161. if (ah->ah_version == AR5K_AR5210) {
  2162. timer1 = 0xffffffff;
  2163. timer2 = 0xffffffff;
  2164. } else {
  2165. timer1 = 0x0000ffff;
  2166. timer2 = 0x0007ffff;
  2167. }
  2168. break;
  2169. default:
  2170. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  2171. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  2172. }
  2173. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  2174. /*
  2175. * Set the beacon register and enable all timers.
  2176. * (next beacon, DMA beacon, software beacon, ATIM window time)
  2177. */
  2178. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  2179. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  2180. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  2181. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  2182. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  2183. AR5K_BEACON_RESET_TSF | AR5K_BEACON_ENABLE),
  2184. AR5K_BEACON);
  2185. }
  2186. #if 0
  2187. /*
  2188. * Set beacon timers
  2189. */
  2190. int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
  2191. const struct ath5k_beacon_state *state)
  2192. {
  2193. u32 cfp_period, next_cfp, dtim, interval, next_beacon;
  2194. /*
  2195. * TODO: should be changed through *state
  2196. * review struct ath5k_beacon_state struct
  2197. *
  2198. * XXX: These are used for cfp period bellow, are they
  2199. * ok ? Is it O.K. for tsf here to be 0 or should we use
  2200. * get_tsf ?
  2201. */
  2202. u32 dtim_count = 0; /* XXX */
  2203. u32 cfp_count = 0; /* XXX */
  2204. u32 tsf = 0; /* XXX */
  2205. ATH5K_TRACE(ah->ah_sc);
  2206. /* Return on an invalid beacon state */
  2207. if (state->bs_interval < 1)
  2208. return -EINVAL;
  2209. interval = state->bs_interval;
  2210. dtim = state->bs_dtim_period;
  2211. /*
  2212. * PCF support?
  2213. */
  2214. if (state->bs_cfp_period > 0) {
  2215. /*
  2216. * Enable PCF mode and set the CFP
  2217. * (Contention Free Period) and timer registers
  2218. */
  2219. cfp_period = state->bs_cfp_period * state->bs_dtim_period *
  2220. state->bs_interval;
  2221. next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
  2222. state->bs_interval;
  2223. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  2224. AR5K_STA_ID1_DEFAULT_ANTENNA |
  2225. AR5K_STA_ID1_PCF);
  2226. ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
  2227. ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
  2228. AR5K_CFP_DUR);
  2229. ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
  2230. next_cfp)) << 3, AR5K_TIMER2);
  2231. } else {
  2232. /* Disable PCF mode */
  2233. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  2234. AR5K_STA_ID1_DEFAULT_ANTENNA |
  2235. AR5K_STA_ID1_PCF);
  2236. }
  2237. /*
  2238. * Enable the beacon timer register
  2239. */
  2240. ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
  2241. /*
  2242. * Start the beacon timers
  2243. */
  2244. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &~
  2245. (AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
  2246. AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
  2247. AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
  2248. AR5K_BEACON_PERIOD), AR5K_BEACON);
  2249. /*
  2250. * Write new beacon miss threshold, if it appears to be valid
  2251. * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
  2252. * and return if its not in range. We can test this by reading value and
  2253. * setting value to a largest value and seeing which values register.
  2254. */
  2255. AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
  2256. state->bs_bmiss_threshold);
  2257. /*
  2258. * Set sleep control register
  2259. * XXX: Didn't find this in 5210 code but since this register
  2260. * exists also in ar5k's 5210 headers i leave it as common code.
  2261. */
  2262. AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
  2263. (state->bs_sleep_duration - 3) << 3);
  2264. /*
  2265. * Set enhanced sleep registers on 5212
  2266. */
  2267. if (ah->ah_version == AR5K_AR5212) {
  2268. if (state->bs_sleep_duration > state->bs_interval &&
  2269. roundup(state->bs_sleep_duration, interval) ==
  2270. state->bs_sleep_duration)
  2271. interval = state->bs_sleep_duration;
  2272. if (state->bs_sleep_duration > dtim && (dtim == 0 ||
  2273. roundup(state->bs_sleep_duration, dtim) ==
  2274. state->bs_sleep_duration))
  2275. dtim = state->bs_sleep_duration;
  2276. if (interval > dtim)
  2277. return -EINVAL;
  2278. next_beacon = interval == dtim ? state->bs_next_dtim :
  2279. state->bs_next_beacon;
  2280. ath5k_hw_reg_write(ah,
  2281. AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
  2282. AR5K_SLEEP0_NEXT_DTIM) |
  2283. AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
  2284. AR5K_SLEEP0_ENH_SLEEP_EN |
  2285. AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
  2286. ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
  2287. AR5K_SLEEP1_NEXT_TIM) |
  2288. AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
  2289. ath5k_hw_reg_write(ah,
  2290. AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
  2291. AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
  2292. }
  2293. return 0;
  2294. }
  2295. /*
  2296. * Reset beacon timers
  2297. */
  2298. void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
  2299. {
  2300. ATH5K_TRACE(ah->ah_sc);
  2301. /*
  2302. * Disable beacon timer
  2303. */
  2304. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  2305. /*
  2306. * Disable some beacon register values
  2307. */
  2308. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  2309. AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
  2310. ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
  2311. }
  2312. /*
  2313. * Wait for beacon queue to finish
  2314. */
  2315. int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
  2316. {
  2317. unsigned int i;
  2318. int ret;
  2319. ATH5K_TRACE(ah->ah_sc);
  2320. /* 5210 doesn't have QCU*/
  2321. if (ah->ah_version == AR5K_AR5210) {
  2322. /*
  2323. * Wait for beaconn queue to finish by checking
  2324. * Control Register and Beacon Status Register.
  2325. */
  2326. for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
  2327. if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
  2328. ||
  2329. !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
  2330. break;
  2331. udelay(10);
  2332. }
  2333. /* Timeout... */
  2334. if (i <= 0) {
  2335. /*
  2336. * Re-schedule the beacon queue
  2337. */
  2338. ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
  2339. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  2340. AR5K_BCR);
  2341. return -EIO;
  2342. }
  2343. ret = 0;
  2344. } else {
  2345. /*5211/5212*/
  2346. ret = ath5k_hw_register_timeout(ah,
  2347. AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
  2348. AR5K_QCU_STS_FRMPENDCNT, 0, false);
  2349. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
  2350. return -EIO;
  2351. }
  2352. return ret;
  2353. }
  2354. #endif
  2355. /*
  2356. * Update mib counters (statistics)
  2357. */
  2358. void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
  2359. struct ath5k_mib_stats *statistics)
  2360. {
  2361. ATH5K_TRACE(ah->ah_sc);
  2362. /* Read-And-Clear */
  2363. statistics->ackrcv_bad += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
  2364. statistics->rts_bad += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
  2365. statistics->rts_good += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
  2366. statistics->fcs_bad += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
  2367. statistics->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
  2368. /* Reset profile count registers on 5212*/
  2369. if (ah->ah_version == AR5K_AR5212) {
  2370. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
  2371. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
  2372. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
  2373. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
  2374. }
  2375. }
  2376. /** ath5k_hw_set_ack_bitrate - set bitrate for ACKs
  2377. *
  2378. * @ah: the &struct ath5k_hw
  2379. * @high: determines if to use low bit rate or now
  2380. */
  2381. void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
  2382. {
  2383. if (ah->ah_version != AR5K_AR5212)
  2384. return;
  2385. else {
  2386. u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
  2387. if (high)
  2388. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  2389. else
  2390. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
  2391. }
  2392. }
  2393. /*
  2394. * ACK/CTS Timeouts
  2395. */
  2396. /*
  2397. * Set ACK timeout on PCU
  2398. */
  2399. int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
  2400. {
  2401. ATH5K_TRACE(ah->ah_sc);
  2402. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
  2403. ah->ah_turbo) <= timeout)
  2404. return -EINVAL;
  2405. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
  2406. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  2407. return 0;
  2408. }
  2409. /*
  2410. * Read the ACK timeout from PCU
  2411. */
  2412. unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
  2413. {
  2414. ATH5K_TRACE(ah->ah_sc);
  2415. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  2416. AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
  2417. }
  2418. /*
  2419. * Set CTS timeout on PCU
  2420. */
  2421. int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
  2422. {
  2423. ATH5K_TRACE(ah->ah_sc);
  2424. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
  2425. ah->ah_turbo) <= timeout)
  2426. return -EINVAL;
  2427. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
  2428. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  2429. return 0;
  2430. }
  2431. /*
  2432. * Read CTS timeout from PCU
  2433. */
  2434. unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
  2435. {
  2436. ATH5K_TRACE(ah->ah_sc);
  2437. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  2438. AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
  2439. }
  2440. /*
  2441. * Key table (WEP) functions
  2442. */
  2443. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  2444. {
  2445. unsigned int i;
  2446. ATH5K_TRACE(ah->ah_sc);
  2447. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  2448. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  2449. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  2450. /* Set NULL encryption on non-5210*/
  2451. if (ah->ah_version != AR5K_AR5210)
  2452. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  2453. AR5K_KEYTABLE_TYPE(entry));
  2454. return 0;
  2455. }
  2456. int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
  2457. {
  2458. ATH5K_TRACE(ah->ah_sc);
  2459. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  2460. /* Check the validation flag at the end of the entry */
  2461. return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
  2462. AR5K_KEYTABLE_VALID;
  2463. }
  2464. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  2465. const struct ieee80211_key_conf *key, const u8 *mac)
  2466. {
  2467. unsigned int i;
  2468. __le32 key_v[5] = {};
  2469. u32 keytype;
  2470. ATH5K_TRACE(ah->ah_sc);
  2471. /* key->keylen comes in from mac80211 in bytes */
  2472. if (key->keylen > AR5K_KEYTABLE_SIZE / 8)
  2473. return -EOPNOTSUPP;
  2474. switch (key->keylen) {
  2475. /* WEP 40-bit = 40-bit entered key + 24 bit IV = 64-bit */
  2476. case 40 / 8:
  2477. memcpy(&key_v[0], key->key, 5);
  2478. keytype = AR5K_KEYTABLE_TYPE_40;
  2479. break;
  2480. /* WEP 104-bit = 104-bit entered key + 24-bit IV = 128-bit */
  2481. case 104 / 8:
  2482. memcpy(&key_v[0], &key->key[0], 6);
  2483. memcpy(&key_v[2], &key->key[6], 6);
  2484. memcpy(&key_v[4], &key->key[12], 1);
  2485. keytype = AR5K_KEYTABLE_TYPE_104;
  2486. break;
  2487. /* WEP 128-bit = 128-bit entered key + 24 bit IV = 152-bit */
  2488. case 128 / 8:
  2489. memcpy(&key_v[0], &key->key[0], 6);
  2490. memcpy(&key_v[2], &key->key[6], 6);
  2491. memcpy(&key_v[4], &key->key[12], 4);
  2492. keytype = AR5K_KEYTABLE_TYPE_128;
  2493. break;
  2494. default:
  2495. return -EINVAL; /* shouldn't happen */
  2496. }
  2497. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  2498. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  2499. AR5K_KEYTABLE_OFF(entry, i));
  2500. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  2501. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  2502. }
  2503. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  2504. {
  2505. u32 low_id, high_id;
  2506. ATH5K_TRACE(ah->ah_sc);
  2507. /* Invalid entry (key table overflow) */
  2508. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  2509. /* MAC may be NULL if it's a broadcast key. In this case no need to
  2510. * to compute AR5K_LOW_ID and AR5K_HIGH_ID as we already know it. */
  2511. if (unlikely(mac == NULL)) {
  2512. low_id = 0xffffffff;
  2513. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  2514. } else {
  2515. low_id = AR5K_LOW_ID(mac);
  2516. high_id = AR5K_HIGH_ID(mac) | AR5K_KEYTABLE_VALID;
  2517. }
  2518. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  2519. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  2520. return 0;
  2521. }
  2522. /********************************************\
  2523. Queue Control Unit, DFS Control Unit Functions
  2524. \********************************************/
  2525. /*
  2526. * Initialize a transmit queue
  2527. */
  2528. int ath5k_hw_setup_tx_queue(struct ath5k_hw *ah, enum ath5k_tx_queue queue_type,
  2529. struct ath5k_txq_info *queue_info)
  2530. {
  2531. unsigned int queue;
  2532. int ret;
  2533. ATH5K_TRACE(ah->ah_sc);
  2534. /*
  2535. * Get queue by type
  2536. */
  2537. /*5210 only has 2 queues*/
  2538. if (ah->ah_version == AR5K_AR5210) {
  2539. switch (queue_type) {
  2540. case AR5K_TX_QUEUE_DATA:
  2541. queue = AR5K_TX_QUEUE_ID_NOQCU_DATA;
  2542. break;
  2543. case AR5K_TX_QUEUE_BEACON:
  2544. case AR5K_TX_QUEUE_CAB:
  2545. queue = AR5K_TX_QUEUE_ID_NOQCU_BEACON;
  2546. break;
  2547. default:
  2548. return -EINVAL;
  2549. }
  2550. } else {
  2551. switch (queue_type) {
  2552. case AR5K_TX_QUEUE_DATA:
  2553. for (queue = AR5K_TX_QUEUE_ID_DATA_MIN;
  2554. ah->ah_txq[queue].tqi_type !=
  2555. AR5K_TX_QUEUE_INACTIVE; queue++) {
  2556. if (queue > AR5K_TX_QUEUE_ID_DATA_MAX)
  2557. return -EINVAL;
  2558. }
  2559. break;
  2560. case AR5K_TX_QUEUE_UAPSD:
  2561. queue = AR5K_TX_QUEUE_ID_UAPSD;
  2562. break;
  2563. case AR5K_TX_QUEUE_BEACON:
  2564. queue = AR5K_TX_QUEUE_ID_BEACON;
  2565. break;
  2566. case AR5K_TX_QUEUE_CAB:
  2567. queue = AR5K_TX_QUEUE_ID_CAB;
  2568. break;
  2569. case AR5K_TX_QUEUE_XR_DATA:
  2570. if (ah->ah_version != AR5K_AR5212)
  2571. ATH5K_ERR(ah->ah_sc,
  2572. "XR data queues only supported in"
  2573. " 5212!\n");
  2574. queue = AR5K_TX_QUEUE_ID_XR_DATA;
  2575. break;
  2576. default:
  2577. return -EINVAL;
  2578. }
  2579. }
  2580. /*
  2581. * Setup internal queue structure
  2582. */
  2583. memset(&ah->ah_txq[queue], 0, sizeof(struct ath5k_txq_info));
  2584. ah->ah_txq[queue].tqi_type = queue_type;
  2585. if (queue_info != NULL) {
  2586. queue_info->tqi_type = queue_type;
  2587. ret = ath5k_hw_setup_tx_queueprops(ah, queue, queue_info);
  2588. if (ret)
  2589. return ret;
  2590. }
  2591. /*
  2592. * We use ah_txq_status to hold a temp value for
  2593. * the Secondary interrupt mask registers on 5211+
  2594. * check out ath5k_hw_reset_tx_queue
  2595. */
  2596. AR5K_Q_ENABLE_BITS(ah->ah_txq_status, queue);
  2597. return queue;
  2598. }
  2599. /*
  2600. * Setup a transmit queue
  2601. */
  2602. int ath5k_hw_setup_tx_queueprops(struct ath5k_hw *ah, int queue,
  2603. const struct ath5k_txq_info *queue_info)
  2604. {
  2605. ATH5K_TRACE(ah->ah_sc);
  2606. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  2607. if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
  2608. return -EIO;
  2609. memcpy(&ah->ah_txq[queue], queue_info, sizeof(struct ath5k_txq_info));
  2610. /*XXX: Is this supported on 5210 ?*/
  2611. if ((queue_info->tqi_type == AR5K_TX_QUEUE_DATA &&
  2612. ((queue_info->tqi_subtype == AR5K_WME_AC_VI) ||
  2613. (queue_info->tqi_subtype == AR5K_WME_AC_VO))) ||
  2614. queue_info->tqi_type == AR5K_TX_QUEUE_UAPSD)
  2615. ah->ah_txq[queue].tqi_flags |= AR5K_TXQ_FLAG_POST_FR_BKOFF_DIS;
  2616. return 0;
  2617. }
  2618. /*
  2619. * Get properties for a specific transmit queue
  2620. */
  2621. int ath5k_hw_get_tx_queueprops(struct ath5k_hw *ah, int queue,
  2622. struct ath5k_txq_info *queue_info)
  2623. {
  2624. ATH5K_TRACE(ah->ah_sc);
  2625. memcpy(queue_info, &ah->ah_txq[queue], sizeof(struct ath5k_txq_info));
  2626. return 0;
  2627. }
  2628. /*
  2629. * Set a transmit queue inactive
  2630. */
  2631. void ath5k_hw_release_tx_queue(struct ath5k_hw *ah, unsigned int queue)
  2632. {
  2633. ATH5K_TRACE(ah->ah_sc);
  2634. if (WARN_ON(queue >= ah->ah_capabilities.cap_queues.q_tx_num))
  2635. return;
  2636. /* This queue will be skipped in further operations */
  2637. ah->ah_txq[queue].tqi_type = AR5K_TX_QUEUE_INACTIVE;
  2638. /*For SIMR setup*/
  2639. AR5K_Q_DISABLE_BITS(ah->ah_txq_status, queue);
  2640. }
  2641. /*
  2642. * Set DFS params for a transmit queue
  2643. */
  2644. int ath5k_hw_reset_tx_queue(struct ath5k_hw *ah, unsigned int queue)
  2645. {
  2646. u32 cw_min, cw_max, retry_lg, retry_sh;
  2647. struct ath5k_txq_info *tq = &ah->ah_txq[queue];
  2648. ATH5K_TRACE(ah->ah_sc);
  2649. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  2650. tq = &ah->ah_txq[queue];
  2651. if (tq->tqi_type == AR5K_TX_QUEUE_INACTIVE)
  2652. return 0;
  2653. if (ah->ah_version == AR5K_AR5210) {
  2654. /* Only handle data queues, others will be ignored */
  2655. if (tq->tqi_type != AR5K_TX_QUEUE_DATA)
  2656. return 0;
  2657. /* Set Slot time */
  2658. ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
  2659. AR5K_INIT_SLOT_TIME_TURBO : AR5K_INIT_SLOT_TIME,
  2660. AR5K_SLOT_TIME);
  2661. /* Set ACK_CTS timeout */
  2662. ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
  2663. AR5K_INIT_ACK_CTS_TIMEOUT_TURBO :
  2664. AR5K_INIT_ACK_CTS_TIMEOUT, AR5K_SLOT_TIME);
  2665. /* Set Transmit Latency */
  2666. ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
  2667. AR5K_INIT_TRANSMIT_LATENCY_TURBO :
  2668. AR5K_INIT_TRANSMIT_LATENCY, AR5K_USEC_5210);
  2669. /* Set IFS0 */
  2670. if (ah->ah_turbo == true)
  2671. ath5k_hw_reg_write(ah, ((AR5K_INIT_SIFS_TURBO +
  2672. (ah->ah_aifs + tq->tqi_aifs) *
  2673. AR5K_INIT_SLOT_TIME_TURBO) <<
  2674. AR5K_IFS0_DIFS_S) | AR5K_INIT_SIFS_TURBO,
  2675. AR5K_IFS0);
  2676. else
  2677. ath5k_hw_reg_write(ah, ((AR5K_INIT_SIFS +
  2678. (ah->ah_aifs + tq->tqi_aifs) *
  2679. AR5K_INIT_SLOT_TIME) << AR5K_IFS0_DIFS_S) |
  2680. AR5K_INIT_SIFS, AR5K_IFS0);
  2681. /* Set IFS1 */
  2682. ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
  2683. AR5K_INIT_PROTO_TIME_CNTRL_TURBO :
  2684. AR5K_INIT_PROTO_TIME_CNTRL, AR5K_IFS1);
  2685. /* Set PHY register 0x9844 (??) */
  2686. ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
  2687. (ath5k_hw_reg_read(ah, AR5K_PHY(17)) & ~0x7F) | 0x38 :
  2688. (ath5k_hw_reg_read(ah, AR5K_PHY(17)) & ~0x7F) | 0x1C,
  2689. AR5K_PHY(17));
  2690. /* Set Frame Control Register */
  2691. ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
  2692. (AR5K_PHY_FRAME_CTL_INI | AR5K_PHY_TURBO_MODE |
  2693. AR5K_PHY_TURBO_SHORT | 0x2020) :
  2694. (AR5K_PHY_FRAME_CTL_INI | 0x1020),
  2695. AR5K_PHY_FRAME_CTL_5210);
  2696. }
  2697. /*
  2698. * Calculate cwmin/max by channel mode
  2699. */
  2700. cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN;
  2701. cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX;
  2702. ah->ah_aifs = AR5K_TUNE_AIFS;
  2703. /*XR is only supported on 5212*/
  2704. if (IS_CHAN_XR(ah->ah_current_channel) &&
  2705. ah->ah_version == AR5K_AR5212) {
  2706. cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN_XR;
  2707. cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX_XR;
  2708. ah->ah_aifs = AR5K_TUNE_AIFS_XR;
  2709. /*B mode is not supported on 5210*/
  2710. } else if (IS_CHAN_B(ah->ah_current_channel) &&
  2711. ah->ah_version != AR5K_AR5210) {
  2712. cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN_11B;
  2713. cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX_11B;
  2714. ah->ah_aifs = AR5K_TUNE_AIFS_11B;
  2715. }
  2716. cw_min = 1;
  2717. while (cw_min < ah->ah_cw_min)
  2718. cw_min = (cw_min << 1) | 1;
  2719. cw_min = tq->tqi_cw_min < 0 ? (cw_min >> (-tq->tqi_cw_min)) :
  2720. ((cw_min << tq->tqi_cw_min) + (1 << tq->tqi_cw_min) - 1);
  2721. cw_max = tq->tqi_cw_max < 0 ? (cw_max >> (-tq->tqi_cw_max)) :
  2722. ((cw_max << tq->tqi_cw_max) + (1 << tq->tqi_cw_max) - 1);
  2723. /*
  2724. * Calculate and set retry limits
  2725. */
  2726. if (ah->ah_software_retry == true) {
  2727. /* XXX Need to test this */
  2728. retry_lg = ah->ah_limit_tx_retries;
  2729. retry_sh = retry_lg = retry_lg > AR5K_DCU_RETRY_LMT_SH_RETRY ?
  2730. AR5K_DCU_RETRY_LMT_SH_RETRY : retry_lg;
  2731. } else {
  2732. retry_lg = AR5K_INIT_LG_RETRY;
  2733. retry_sh = AR5K_INIT_SH_RETRY;
  2734. }
  2735. /*No QCU/DCU [5210]*/
  2736. if (ah->ah_version == AR5K_AR5210) {
  2737. ath5k_hw_reg_write(ah,
  2738. (cw_min << AR5K_NODCU_RETRY_LMT_CW_MIN_S)
  2739. | AR5K_REG_SM(AR5K_INIT_SLG_RETRY,
  2740. AR5K_NODCU_RETRY_LMT_SLG_RETRY)
  2741. | AR5K_REG_SM(AR5K_INIT_SSH_RETRY,
  2742. AR5K_NODCU_RETRY_LMT_SSH_RETRY)
  2743. | AR5K_REG_SM(retry_lg, AR5K_NODCU_RETRY_LMT_LG_RETRY)
  2744. | AR5K_REG_SM(retry_sh, AR5K_NODCU_RETRY_LMT_SH_RETRY),
  2745. AR5K_NODCU_RETRY_LMT);
  2746. } else {
  2747. /*QCU/DCU [5211+]*/
  2748. ath5k_hw_reg_write(ah,
  2749. AR5K_REG_SM(AR5K_INIT_SLG_RETRY,
  2750. AR5K_DCU_RETRY_LMT_SLG_RETRY) |
  2751. AR5K_REG_SM(AR5K_INIT_SSH_RETRY,
  2752. AR5K_DCU_RETRY_LMT_SSH_RETRY) |
  2753. AR5K_REG_SM(retry_lg, AR5K_DCU_RETRY_LMT_LG_RETRY) |
  2754. AR5K_REG_SM(retry_sh, AR5K_DCU_RETRY_LMT_SH_RETRY),
  2755. AR5K_QUEUE_DFS_RETRY_LIMIT(queue));
  2756. /*===Rest is also for QCU/DCU only [5211+]===*/
  2757. /*
  2758. * Set initial content window (cw_min/cw_max)
  2759. * and arbitrated interframe space (aifs)...
  2760. */
  2761. ath5k_hw_reg_write(ah,
  2762. AR5K_REG_SM(cw_min, AR5K_DCU_LCL_IFS_CW_MIN) |
  2763. AR5K_REG_SM(cw_max, AR5K_DCU_LCL_IFS_CW_MAX) |
  2764. AR5K_REG_SM(ah->ah_aifs + tq->tqi_aifs,
  2765. AR5K_DCU_LCL_IFS_AIFS),
  2766. AR5K_QUEUE_DFS_LOCAL_IFS(queue));
  2767. /*
  2768. * Set misc registers
  2769. */
  2770. ath5k_hw_reg_write(ah, AR5K_QCU_MISC_DCU_EARLY,
  2771. AR5K_QUEUE_MISC(queue));
  2772. if (tq->tqi_cbr_period) {
  2773. ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_cbr_period,
  2774. AR5K_QCU_CBRCFG_INTVAL) |
  2775. AR5K_REG_SM(tq->tqi_cbr_overflow_limit,
  2776. AR5K_QCU_CBRCFG_ORN_THRES),
  2777. AR5K_QUEUE_CBRCFG(queue));
  2778. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
  2779. AR5K_QCU_MISC_FRSHED_CBR);
  2780. if (tq->tqi_cbr_overflow_limit)
  2781. AR5K_REG_ENABLE_BITS(ah,
  2782. AR5K_QUEUE_MISC(queue),
  2783. AR5K_QCU_MISC_CBR_THRES_ENABLE);
  2784. }
  2785. if (tq->tqi_ready_time)
  2786. ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_ready_time,
  2787. AR5K_QCU_RDYTIMECFG_INTVAL) |
  2788. AR5K_QCU_RDYTIMECFG_ENABLE,
  2789. AR5K_QUEUE_RDYTIMECFG(queue));
  2790. if (tq->tqi_burst_time) {
  2791. ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_burst_time,
  2792. AR5K_DCU_CHAN_TIME_DUR) |
  2793. AR5K_DCU_CHAN_TIME_ENABLE,
  2794. AR5K_QUEUE_DFS_CHANNEL_TIME(queue));
  2795. if (tq->tqi_flags & AR5K_TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)
  2796. AR5K_REG_ENABLE_BITS(ah,
  2797. AR5K_QUEUE_MISC(queue),
  2798. AR5K_QCU_MISC_TXE);
  2799. }
  2800. if (tq->tqi_flags & AR5K_TXQ_FLAG_BACKOFF_DISABLE)
  2801. ath5k_hw_reg_write(ah, AR5K_DCU_MISC_POST_FR_BKOFF_DIS,
  2802. AR5K_QUEUE_DFS_MISC(queue));
  2803. if (tq->tqi_flags & AR5K_TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE)
  2804. ath5k_hw_reg_write(ah, AR5K_DCU_MISC_BACKOFF_FRAG,
  2805. AR5K_QUEUE_DFS_MISC(queue));
  2806. /*
  2807. * Set registers by queue type
  2808. */
  2809. switch (tq->tqi_type) {
  2810. case AR5K_TX_QUEUE_BEACON:
  2811. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
  2812. AR5K_QCU_MISC_FRSHED_DBA_GT |
  2813. AR5K_QCU_MISC_CBREXP_BCN |
  2814. AR5K_QCU_MISC_BCN_ENABLE);
  2815. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_DFS_MISC(queue),
  2816. (AR5K_DCU_MISC_ARBLOCK_CTL_GLOBAL <<
  2817. AR5K_DCU_MISC_ARBLOCK_CTL_S) |
  2818. AR5K_DCU_MISC_POST_FR_BKOFF_DIS |
  2819. AR5K_DCU_MISC_BCN_ENABLE);
  2820. ath5k_hw_reg_write(ah, ((AR5K_TUNE_BEACON_INTERVAL -
  2821. (AR5K_TUNE_SW_BEACON_RESP -
  2822. AR5K_TUNE_DMA_BEACON_RESP) -
  2823. AR5K_TUNE_ADDITIONAL_SWBA_BACKOFF) * 1024) |
  2824. AR5K_QCU_RDYTIMECFG_ENABLE,
  2825. AR5K_QUEUE_RDYTIMECFG(queue));
  2826. break;
  2827. case AR5K_TX_QUEUE_CAB:
  2828. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
  2829. AR5K_QCU_MISC_FRSHED_DBA_GT |
  2830. AR5K_QCU_MISC_CBREXP |
  2831. AR5K_QCU_MISC_CBREXP_BCN);
  2832. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_DFS_MISC(queue),
  2833. (AR5K_DCU_MISC_ARBLOCK_CTL_GLOBAL <<
  2834. AR5K_DCU_MISC_ARBLOCK_CTL_S));
  2835. break;
  2836. case AR5K_TX_QUEUE_UAPSD:
  2837. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
  2838. AR5K_QCU_MISC_CBREXP);
  2839. break;
  2840. case AR5K_TX_QUEUE_DATA:
  2841. default:
  2842. break;
  2843. }
  2844. /*
  2845. * Enable interrupts for this tx queue
  2846. * in the secondary interrupt mask registers
  2847. */
  2848. if (tq->tqi_flags & AR5K_TXQ_FLAG_TXOKINT_ENABLE)
  2849. AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txok, queue);
  2850. if (tq->tqi_flags & AR5K_TXQ_FLAG_TXERRINT_ENABLE)
  2851. AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txerr, queue);
  2852. if (tq->tqi_flags & AR5K_TXQ_FLAG_TXURNINT_ENABLE)
  2853. AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txurn, queue);
  2854. if (tq->tqi_flags & AR5K_TXQ_FLAG_TXDESCINT_ENABLE)
  2855. AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txdesc, queue);
  2856. if (tq->tqi_flags & AR5K_TXQ_FLAG_TXEOLINT_ENABLE)
  2857. AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txeol, queue);
  2858. /* Update secondary interrupt mask registers */
  2859. ah->ah_txq_imr_txok &= ah->ah_txq_status;
  2860. ah->ah_txq_imr_txerr &= ah->ah_txq_status;
  2861. ah->ah_txq_imr_txurn &= ah->ah_txq_status;
  2862. ah->ah_txq_imr_txdesc &= ah->ah_txq_status;
  2863. ah->ah_txq_imr_txeol &= ah->ah_txq_status;
  2864. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txok,
  2865. AR5K_SIMR0_QCU_TXOK) |
  2866. AR5K_REG_SM(ah->ah_txq_imr_txdesc,
  2867. AR5K_SIMR0_QCU_TXDESC), AR5K_SIMR0);
  2868. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txerr,
  2869. AR5K_SIMR1_QCU_TXERR) |
  2870. AR5K_REG_SM(ah->ah_txq_imr_txeol,
  2871. AR5K_SIMR1_QCU_TXEOL), AR5K_SIMR1);
  2872. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txurn,
  2873. AR5K_SIMR2_QCU_TXURN), AR5K_SIMR2);
  2874. }
  2875. return 0;
  2876. }
  2877. /*
  2878. * Get number of pending frames
  2879. * for a specific queue [5211+]
  2880. */
  2881. u32 ath5k_hw_num_tx_pending(struct ath5k_hw *ah, unsigned int queue) {
  2882. ATH5K_TRACE(ah->ah_sc);
  2883. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  2884. /* Return if queue is declared inactive */
  2885. if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
  2886. return false;
  2887. /* XXX: How about AR5K_CFG_TXCNT ? */
  2888. if (ah->ah_version == AR5K_AR5210)
  2889. return false;
  2890. return AR5K_QUEUE_STATUS(queue) & AR5K_QCU_STS_FRMPENDCNT;
  2891. }
  2892. /*
  2893. * Set slot time
  2894. */
  2895. int ath5k_hw_set_slot_time(struct ath5k_hw *ah, unsigned int slot_time)
  2896. {
  2897. ATH5K_TRACE(ah->ah_sc);
  2898. if (slot_time < AR5K_SLOT_TIME_9 || slot_time > AR5K_SLOT_TIME_MAX)
  2899. return -EINVAL;
  2900. if (ah->ah_version == AR5K_AR5210)
  2901. ath5k_hw_reg_write(ah, ath5k_hw_htoclock(slot_time,
  2902. ah->ah_turbo), AR5K_SLOT_TIME);
  2903. else
  2904. ath5k_hw_reg_write(ah, slot_time, AR5K_DCU_GBL_IFS_SLOT);
  2905. return 0;
  2906. }
  2907. /*
  2908. * Get slot time
  2909. */
  2910. unsigned int ath5k_hw_get_slot_time(struct ath5k_hw *ah)
  2911. {
  2912. ATH5K_TRACE(ah->ah_sc);
  2913. if (ah->ah_version == AR5K_AR5210)
  2914. return ath5k_hw_clocktoh(ath5k_hw_reg_read(ah,
  2915. AR5K_SLOT_TIME) & 0xffff, ah->ah_turbo);
  2916. else
  2917. return ath5k_hw_reg_read(ah, AR5K_DCU_GBL_IFS_SLOT) & 0xffff;
  2918. }
  2919. /******************************\
  2920. Hardware Descriptor Functions
  2921. \******************************/
  2922. /*
  2923. * TX Descriptor
  2924. */
  2925. /*
  2926. * Initialize the 2-word tx descriptor on 5210/5211
  2927. */
  2928. static int
  2929. ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
  2930. unsigned int pkt_len, unsigned int hdr_len, enum ath5k_pkt_type type,
  2931. unsigned int tx_power, unsigned int tx_rate0, unsigned int tx_tries0,
  2932. unsigned int key_index, unsigned int antenna_mode, unsigned int flags,
  2933. unsigned int rtscts_rate, unsigned int rtscts_duration)
  2934. {
  2935. u32 frame_type;
  2936. struct ath5k_hw_2w_tx_desc *tx_desc;
  2937. unsigned int frame_len;
  2938. tx_desc = (struct ath5k_hw_2w_tx_desc *)&desc->ds_ctl0;
  2939. /*
  2940. * Validate input
  2941. * - Zero retries don't make sense.
  2942. * - A zero rate will put the HW into a mode where it continously sends
  2943. * noise on the channel, so it is important to avoid this.
  2944. */
  2945. if (unlikely(tx_tries0 == 0)) {
  2946. ATH5K_ERR(ah->ah_sc, "zero retries\n");
  2947. WARN_ON(1);
  2948. return -EINVAL;
  2949. }
  2950. if (unlikely(tx_rate0 == 0)) {
  2951. ATH5K_ERR(ah->ah_sc, "zero rate\n");
  2952. WARN_ON(1);
  2953. return -EINVAL;
  2954. }
  2955. /* Clear status descriptor */
  2956. memset(desc->ds_hw, 0, sizeof(struct ath5k_hw_tx_status));
  2957. /* Initialize control descriptor */
  2958. tx_desc->tx_control_0 = 0;
  2959. tx_desc->tx_control_1 = 0;
  2960. /* Setup control descriptor */
  2961. /* Verify and set frame length */
  2962. /* remove padding we might have added before */
  2963. frame_len = pkt_len - (hdr_len & 3) + FCS_LEN;
  2964. if (frame_len & ~AR5K_2W_TX_DESC_CTL0_FRAME_LEN)
  2965. return -EINVAL;
  2966. tx_desc->tx_control_0 = frame_len & AR5K_2W_TX_DESC_CTL0_FRAME_LEN;
  2967. /* Verify and set buffer length */
  2968. /* NB: beacon's BufLen must be a multiple of 4 bytes */
  2969. if(type == AR5K_PKT_TYPE_BEACON)
  2970. pkt_len = roundup(pkt_len, 4);
  2971. if (pkt_len & ~AR5K_2W_TX_DESC_CTL1_BUF_LEN)
  2972. return -EINVAL;
  2973. tx_desc->tx_control_1 = pkt_len & AR5K_2W_TX_DESC_CTL1_BUF_LEN;
  2974. /*
  2975. * Verify and set header length
  2976. * XXX: I only found that on 5210 code, does it work on 5211 ?
  2977. */
  2978. if (ah->ah_version == AR5K_AR5210) {
  2979. if (hdr_len & ~AR5K_2W_TX_DESC_CTL0_HEADER_LEN)
  2980. return -EINVAL;
  2981. tx_desc->tx_control_0 |=
  2982. AR5K_REG_SM(hdr_len, AR5K_2W_TX_DESC_CTL0_HEADER_LEN);
  2983. }
  2984. /*Diferences between 5210-5211*/
  2985. if (ah->ah_version == AR5K_AR5210) {
  2986. switch (type) {
  2987. case AR5K_PKT_TYPE_BEACON:
  2988. case AR5K_PKT_TYPE_PROBE_RESP:
  2989. frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_NO_DELAY;
  2990. case AR5K_PKT_TYPE_PIFS:
  2991. frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_PIFS;
  2992. default:
  2993. frame_type = type /*<< 2 ?*/;
  2994. }
  2995. tx_desc->tx_control_0 |=
  2996. AR5K_REG_SM(frame_type, AR5K_2W_TX_DESC_CTL0_FRAME_TYPE) |
  2997. AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
  2998. } else {
  2999. tx_desc->tx_control_0 |=
  3000. AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE) |
  3001. AR5K_REG_SM(antenna_mode, AR5K_2W_TX_DESC_CTL0_ANT_MODE_XMIT);
  3002. tx_desc->tx_control_1 |=
  3003. AR5K_REG_SM(type, AR5K_2W_TX_DESC_CTL1_FRAME_TYPE);
  3004. }
  3005. #define _TX_FLAGS(_c, _flag) \
  3006. if (flags & AR5K_TXDESC_##_flag) \
  3007. tx_desc->tx_control_##_c |= \
  3008. AR5K_2W_TX_DESC_CTL##_c##_##_flag
  3009. _TX_FLAGS(0, CLRDMASK);
  3010. _TX_FLAGS(0, VEOL);
  3011. _TX_FLAGS(0, INTREQ);
  3012. _TX_FLAGS(0, RTSENA);
  3013. _TX_FLAGS(1, NOACK);
  3014. #undef _TX_FLAGS
  3015. /*
  3016. * WEP crap
  3017. */
  3018. if (key_index != AR5K_TXKEYIX_INVALID) {
  3019. tx_desc->tx_control_0 |=
  3020. AR5K_2W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
  3021. tx_desc->tx_control_1 |=
  3022. AR5K_REG_SM(key_index,
  3023. AR5K_2W_TX_DESC_CTL1_ENCRYPT_KEY_INDEX);
  3024. }
  3025. /*
  3026. * RTS/CTS Duration [5210 ?]
  3027. */
  3028. if ((ah->ah_version == AR5K_AR5210) &&
  3029. (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)))
  3030. tx_desc->tx_control_1 |= rtscts_duration &
  3031. AR5K_2W_TX_DESC_CTL1_RTS_DURATION;
  3032. return 0;
  3033. }
  3034. /*
  3035. * Initialize the 4-word tx descriptor on 5212
  3036. */
  3037. static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *ah,
  3038. struct ath5k_desc *desc, unsigned int pkt_len, unsigned int hdr_len,
  3039. enum ath5k_pkt_type type, unsigned int tx_power, unsigned int tx_rate0,
  3040. unsigned int tx_tries0, unsigned int key_index,
  3041. unsigned int antenna_mode, unsigned int flags, unsigned int rtscts_rate,
  3042. unsigned int rtscts_duration)
  3043. {
  3044. struct ath5k_hw_4w_tx_desc *tx_desc;
  3045. struct ath5k_hw_tx_status *tx_status;
  3046. unsigned int frame_len;
  3047. ATH5K_TRACE(ah->ah_sc);
  3048. tx_desc = (struct ath5k_hw_4w_tx_desc *)&desc->ds_ctl0;
  3049. tx_status = (struct ath5k_hw_tx_status *)&desc->ds_hw[2];
  3050. /*
  3051. * Validate input
  3052. * - Zero retries don't make sense.
  3053. * - A zero rate will put the HW into a mode where it continously sends
  3054. * noise on the channel, so it is important to avoid this.
  3055. */
  3056. if (unlikely(tx_tries0 == 0)) {
  3057. ATH5K_ERR(ah->ah_sc, "zero retries\n");
  3058. WARN_ON(1);
  3059. return -EINVAL;
  3060. }
  3061. if (unlikely(tx_rate0 == 0)) {
  3062. ATH5K_ERR(ah->ah_sc, "zero rate\n");
  3063. WARN_ON(1);
  3064. return -EINVAL;
  3065. }
  3066. /* Clear status descriptor */
  3067. memset(tx_status, 0, sizeof(struct ath5k_hw_tx_status));
  3068. /* Initialize control descriptor */
  3069. tx_desc->tx_control_0 = 0;
  3070. tx_desc->tx_control_1 = 0;
  3071. tx_desc->tx_control_2 = 0;
  3072. tx_desc->tx_control_3 = 0;
  3073. /* Setup control descriptor */
  3074. /* Verify and set frame length */
  3075. /* remove padding we might have added before */
  3076. frame_len = pkt_len - (hdr_len & 3) + FCS_LEN;
  3077. if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
  3078. return -EINVAL;
  3079. tx_desc->tx_control_0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
  3080. /* Verify and set buffer length */
  3081. /* NB: beacon's BufLen must be a multiple of 4 bytes */
  3082. if(type == AR5K_PKT_TYPE_BEACON)
  3083. pkt_len = roundup(pkt_len, 4);
  3084. if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
  3085. return -EINVAL;
  3086. tx_desc->tx_control_1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
  3087. tx_desc->tx_control_0 |=
  3088. AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
  3089. AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
  3090. tx_desc->tx_control_1 |= AR5K_REG_SM(type,
  3091. AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
  3092. tx_desc->tx_control_2 = AR5K_REG_SM(tx_tries0 + AR5K_TUNE_HWTXTRIES,
  3093. AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
  3094. tx_desc->tx_control_3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
  3095. #define _TX_FLAGS(_c, _flag) \
  3096. if (flags & AR5K_TXDESC_##_flag) \
  3097. tx_desc->tx_control_##_c |= \
  3098. AR5K_4W_TX_DESC_CTL##_c##_##_flag
  3099. _TX_FLAGS(0, CLRDMASK);
  3100. _TX_FLAGS(0, VEOL);
  3101. _TX_FLAGS(0, INTREQ);
  3102. _TX_FLAGS(0, RTSENA);
  3103. _TX_FLAGS(0, CTSENA);
  3104. _TX_FLAGS(1, NOACK);
  3105. #undef _TX_FLAGS
  3106. /*
  3107. * WEP crap
  3108. */
  3109. if (key_index != AR5K_TXKEYIX_INVALID) {
  3110. tx_desc->tx_control_0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
  3111. tx_desc->tx_control_1 |= AR5K_REG_SM(key_index,
  3112. AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_INDEX);
  3113. }
  3114. /*
  3115. * RTS/CTS
  3116. */
  3117. if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
  3118. if ((flags & AR5K_TXDESC_RTSENA) &&
  3119. (flags & AR5K_TXDESC_CTSENA))
  3120. return -EINVAL;
  3121. tx_desc->tx_control_2 |= rtscts_duration &
  3122. AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
  3123. tx_desc->tx_control_3 |= AR5K_REG_SM(rtscts_rate,
  3124. AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
  3125. }
  3126. return 0;
  3127. }
  3128. /*
  3129. * Initialize a 4-word multirate tx descriptor on 5212
  3130. */
  3131. static int
  3132. ath5k_hw_setup_xr_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
  3133. unsigned int tx_rate1, u_int tx_tries1, u_int tx_rate2, u_int tx_tries2,
  3134. unsigned int tx_rate3, u_int tx_tries3)
  3135. {
  3136. struct ath5k_hw_4w_tx_desc *tx_desc;
  3137. /*
  3138. * Rates can be 0 as long as the retry count is 0 too.
  3139. * A zero rate and nonzero retry count will put the HW into a mode where
  3140. * it continously sends noise on the channel, so it is important to
  3141. * avoid this.
  3142. */
  3143. if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
  3144. (tx_rate2 == 0 && tx_tries2 != 0) ||
  3145. (tx_rate3 == 0 && tx_tries3 != 0))) {
  3146. ATH5K_ERR(ah->ah_sc, "zero rate\n");
  3147. WARN_ON(1);
  3148. return -EINVAL;
  3149. }
  3150. if (ah->ah_version == AR5K_AR5212) {
  3151. tx_desc = (struct ath5k_hw_4w_tx_desc *)&desc->ds_ctl0;
  3152. #define _XTX_TRIES(_n) \
  3153. if (tx_tries##_n) { \
  3154. tx_desc->tx_control_2 |= \
  3155. AR5K_REG_SM(tx_tries##_n, \
  3156. AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n); \
  3157. tx_desc->tx_control_3 |= \
  3158. AR5K_REG_SM(tx_rate##_n, \
  3159. AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n); \
  3160. }
  3161. _XTX_TRIES(1);
  3162. _XTX_TRIES(2);
  3163. _XTX_TRIES(3);
  3164. #undef _XTX_TRIES
  3165. return 1;
  3166. }
  3167. return 0;
  3168. }
  3169. /*
  3170. * Proccess the tx status descriptor on 5210/5211
  3171. */
  3172. static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
  3173. struct ath5k_desc *desc)
  3174. {
  3175. struct ath5k_hw_tx_status *tx_status;
  3176. struct ath5k_hw_2w_tx_desc *tx_desc;
  3177. tx_desc = (struct ath5k_hw_2w_tx_desc *)&desc->ds_ctl0;
  3178. tx_status = (struct ath5k_hw_tx_status *)&desc->ds_hw[0];
  3179. /* No frame has been send or error */
  3180. if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
  3181. return -EINPROGRESS;
  3182. /*
  3183. * Get descriptor status
  3184. */
  3185. desc->ds_us.tx.ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
  3186. AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
  3187. desc->ds_us.tx.ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
  3188. AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
  3189. desc->ds_us.tx.ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
  3190. AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
  3191. /*TODO: desc->ds_us.tx.ts_virtcol + test*/
  3192. desc->ds_us.tx.ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
  3193. AR5K_DESC_TX_STATUS1_SEQ_NUM);
  3194. desc->ds_us.tx.ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
  3195. AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
  3196. desc->ds_us.tx.ts_antenna = 1;
  3197. desc->ds_us.tx.ts_status = 0;
  3198. desc->ds_us.tx.ts_rate = AR5K_REG_MS(tx_desc->tx_control_0,
  3199. AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
  3200. if ((tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK) == 0){
  3201. if (tx_status->tx_status_0 &
  3202. AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
  3203. desc->ds_us.tx.ts_status |= AR5K_TXERR_XRETRY;
  3204. if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
  3205. desc->ds_us.tx.ts_status |= AR5K_TXERR_FIFO;
  3206. if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
  3207. desc->ds_us.tx.ts_status |= AR5K_TXERR_FILT;
  3208. }
  3209. return 0;
  3210. }
  3211. /*
  3212. * Proccess a tx descriptor on 5212
  3213. */
  3214. static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
  3215. struct ath5k_desc *desc)
  3216. {
  3217. struct ath5k_hw_tx_status *tx_status;
  3218. struct ath5k_hw_4w_tx_desc *tx_desc;
  3219. ATH5K_TRACE(ah->ah_sc);
  3220. tx_desc = (struct ath5k_hw_4w_tx_desc *)&desc->ds_ctl0;
  3221. tx_status = (struct ath5k_hw_tx_status *)&desc->ds_hw[2];
  3222. /* No frame has been send or error */
  3223. if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
  3224. return -EINPROGRESS;
  3225. /*
  3226. * Get descriptor status
  3227. */
  3228. desc->ds_us.tx.ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
  3229. AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
  3230. desc->ds_us.tx.ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
  3231. AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
  3232. desc->ds_us.tx.ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
  3233. AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
  3234. desc->ds_us.tx.ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
  3235. AR5K_DESC_TX_STATUS1_SEQ_NUM);
  3236. desc->ds_us.tx.ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
  3237. AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
  3238. desc->ds_us.tx.ts_antenna = (tx_status->tx_status_1 &
  3239. AR5K_DESC_TX_STATUS1_XMIT_ANTENNA) ? 2 : 1;
  3240. desc->ds_us.tx.ts_status = 0;
  3241. switch (AR5K_REG_MS(tx_status->tx_status_1,
  3242. AR5K_DESC_TX_STATUS1_FINAL_TS_INDEX)) {
  3243. case 0:
  3244. desc->ds_us.tx.ts_rate = tx_desc->tx_control_3 &
  3245. AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
  3246. break;
  3247. case 1:
  3248. desc->ds_us.tx.ts_rate = AR5K_REG_MS(tx_desc->tx_control_3,
  3249. AR5K_4W_TX_DESC_CTL3_XMIT_RATE1);
  3250. desc->ds_us.tx.ts_longretry +=AR5K_REG_MS(tx_desc->tx_control_2,
  3251. AR5K_4W_TX_DESC_CTL2_XMIT_TRIES1);
  3252. break;
  3253. case 2:
  3254. desc->ds_us.tx.ts_rate = AR5K_REG_MS(tx_desc->tx_control_3,
  3255. AR5K_4W_TX_DESC_CTL3_XMIT_RATE2);
  3256. desc->ds_us.tx.ts_longretry +=AR5K_REG_MS(tx_desc->tx_control_2,
  3257. AR5K_4W_TX_DESC_CTL2_XMIT_TRIES2);
  3258. break;
  3259. case 3:
  3260. desc->ds_us.tx.ts_rate = AR5K_REG_MS(tx_desc->tx_control_3,
  3261. AR5K_4W_TX_DESC_CTL3_XMIT_RATE3);
  3262. desc->ds_us.tx.ts_longretry +=AR5K_REG_MS(tx_desc->tx_control_2,
  3263. AR5K_4W_TX_DESC_CTL2_XMIT_TRIES3);
  3264. break;
  3265. }
  3266. if ((tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK) == 0){
  3267. if (tx_status->tx_status_0 &
  3268. AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
  3269. desc->ds_us.tx.ts_status |= AR5K_TXERR_XRETRY;
  3270. if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
  3271. desc->ds_us.tx.ts_status |= AR5K_TXERR_FIFO;
  3272. if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
  3273. desc->ds_us.tx.ts_status |= AR5K_TXERR_FILT;
  3274. }
  3275. return 0;
  3276. }
  3277. /*
  3278. * RX Descriptor
  3279. */
  3280. /*
  3281. * Initialize an rx descriptor
  3282. */
  3283. int ath5k_hw_setup_rx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
  3284. u32 size, unsigned int flags)
  3285. {
  3286. struct ath5k_rx_desc *rx_desc;
  3287. ATH5K_TRACE(ah->ah_sc);
  3288. rx_desc = (struct ath5k_rx_desc *)&desc->ds_ctl0;
  3289. /*
  3290. *Clear ds_hw
  3291. * If we don't clean the status descriptor,
  3292. * while scanning we get too many results,
  3293. * most of them virtual, after some secs
  3294. * of scanning system hangs. M.F.
  3295. */
  3296. memset(desc->ds_hw, 0, sizeof(desc->ds_hw));
  3297. /*Initialize rx descriptor*/
  3298. rx_desc->rx_control_0 = 0;
  3299. rx_desc->rx_control_1 = 0;
  3300. /* Setup descriptor */
  3301. rx_desc->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;
  3302. if (unlikely(rx_desc->rx_control_1 != size))
  3303. return -EINVAL;
  3304. if (flags & AR5K_RXDESC_INTREQ)
  3305. rx_desc->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;
  3306. return 0;
  3307. }
  3308. /*
  3309. * Proccess the rx status descriptor on 5210/5211
  3310. */
  3311. static int ath5k_hw_proc_old_rx_status(struct ath5k_hw *ah,
  3312. struct ath5k_desc *desc)
  3313. {
  3314. struct ath5k_hw_old_rx_status *rx_status;
  3315. rx_status = (struct ath5k_hw_old_rx_status *)&desc->ds_hw[0];
  3316. /* No frame received / not ready */
  3317. if (unlikely((rx_status->rx_status_1 & AR5K_OLD_RX_DESC_STATUS1_DONE)
  3318. == 0))
  3319. return -EINPROGRESS;
  3320. /*
  3321. * Frame receive status
  3322. */
  3323. desc->ds_us.rx.rs_datalen = rx_status->rx_status_0 &
  3324. AR5K_OLD_RX_DESC_STATUS0_DATA_LEN;
  3325. desc->ds_us.rx.rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
  3326. AR5K_OLD_RX_DESC_STATUS0_RECEIVE_SIGNAL);
  3327. desc->ds_us.rx.rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
  3328. AR5K_OLD_RX_DESC_STATUS0_RECEIVE_RATE);
  3329. desc->ds_us.rx.rs_antenna = rx_status->rx_status_0 &
  3330. AR5K_OLD_RX_DESC_STATUS0_RECEIVE_ANTENNA;
  3331. desc->ds_us.rx.rs_more = rx_status->rx_status_0 &
  3332. AR5K_OLD_RX_DESC_STATUS0_MORE;
  3333. desc->ds_us.rx.rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
  3334. AR5K_OLD_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
  3335. desc->ds_us.rx.rs_status = 0;
  3336. /*
  3337. * Key table status
  3338. */
  3339. if (rx_status->rx_status_1 & AR5K_OLD_RX_DESC_STATUS1_KEY_INDEX_VALID)
  3340. desc->ds_us.rx.rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
  3341. AR5K_OLD_RX_DESC_STATUS1_KEY_INDEX);
  3342. else
  3343. desc->ds_us.rx.rs_keyix = AR5K_RXKEYIX_INVALID;
  3344. /*
  3345. * Receive/descriptor errors
  3346. */
  3347. if ((rx_status->rx_status_1 & AR5K_OLD_RX_DESC_STATUS1_FRAME_RECEIVE_OK)
  3348. == 0) {
  3349. if (rx_status->rx_status_1 & AR5K_OLD_RX_DESC_STATUS1_CRC_ERROR)
  3350. desc->ds_us.rx.rs_status |= AR5K_RXERR_CRC;
  3351. if (rx_status->rx_status_1 &
  3352. AR5K_OLD_RX_DESC_STATUS1_FIFO_OVERRUN)
  3353. desc->ds_us.rx.rs_status |= AR5K_RXERR_FIFO;
  3354. if (rx_status->rx_status_1 &
  3355. AR5K_OLD_RX_DESC_STATUS1_PHY_ERROR) {
  3356. desc->ds_us.rx.rs_status |= AR5K_RXERR_PHY;
  3357. desc->ds_us.rx.rs_phyerr =
  3358. AR5K_REG_MS(rx_status->rx_status_1,
  3359. AR5K_OLD_RX_DESC_STATUS1_PHY_ERROR);
  3360. }
  3361. if (rx_status->rx_status_1 &
  3362. AR5K_OLD_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
  3363. desc->ds_us.rx.rs_status |= AR5K_RXERR_DECRYPT;
  3364. }
  3365. return 0;
  3366. }
  3367. /*
  3368. * Proccess the rx status descriptor on 5212
  3369. */
  3370. static int ath5k_hw_proc_new_rx_status(struct ath5k_hw *ah,
  3371. struct ath5k_desc *desc)
  3372. {
  3373. struct ath5k_hw_new_rx_status *rx_status;
  3374. struct ath5k_hw_rx_error *rx_err;
  3375. ATH5K_TRACE(ah->ah_sc);
  3376. rx_status = (struct ath5k_hw_new_rx_status *)&desc->ds_hw[0];
  3377. /* Overlay on error */
  3378. rx_err = (struct ath5k_hw_rx_error *)&desc->ds_hw[0];
  3379. /* No frame received / not ready */
  3380. if (unlikely((rx_status->rx_status_1 & AR5K_NEW_RX_DESC_STATUS1_DONE)
  3381. == 0))
  3382. return -EINPROGRESS;
  3383. /*
  3384. * Frame receive status
  3385. */
  3386. desc->ds_us.rx.rs_datalen = rx_status->rx_status_0 &
  3387. AR5K_NEW_RX_DESC_STATUS0_DATA_LEN;
  3388. desc->ds_us.rx.rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
  3389. AR5K_NEW_RX_DESC_STATUS0_RECEIVE_SIGNAL);
  3390. desc->ds_us.rx.rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
  3391. AR5K_NEW_RX_DESC_STATUS0_RECEIVE_RATE);
  3392. desc->ds_us.rx.rs_antenna = rx_status->rx_status_0 &
  3393. AR5K_NEW_RX_DESC_STATUS0_RECEIVE_ANTENNA;
  3394. desc->ds_us.rx.rs_more = rx_status->rx_status_0 &
  3395. AR5K_NEW_RX_DESC_STATUS0_MORE;
  3396. desc->ds_us.rx.rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
  3397. AR5K_NEW_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
  3398. desc->ds_us.rx.rs_status = 0;
  3399. /*
  3400. * Key table status
  3401. */
  3402. if (rx_status->rx_status_1 & AR5K_NEW_RX_DESC_STATUS1_KEY_INDEX_VALID)
  3403. desc->ds_us.rx.rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
  3404. AR5K_NEW_RX_DESC_STATUS1_KEY_INDEX);
  3405. else
  3406. desc->ds_us.rx.rs_keyix = AR5K_RXKEYIX_INVALID;
  3407. /*
  3408. * Receive/descriptor errors
  3409. */
  3410. if ((rx_status->rx_status_1 &
  3411. AR5K_NEW_RX_DESC_STATUS1_FRAME_RECEIVE_OK) == 0) {
  3412. if (rx_status->rx_status_1 & AR5K_NEW_RX_DESC_STATUS1_CRC_ERROR)
  3413. desc->ds_us.rx.rs_status |= AR5K_RXERR_CRC;
  3414. if (rx_status->rx_status_1 &
  3415. AR5K_NEW_RX_DESC_STATUS1_PHY_ERROR) {
  3416. desc->ds_us.rx.rs_status |= AR5K_RXERR_PHY;
  3417. desc->ds_us.rx.rs_phyerr =
  3418. AR5K_REG_MS(rx_err->rx_error_1,
  3419. AR5K_RX_DESC_ERROR1_PHY_ERROR_CODE);
  3420. }
  3421. if (rx_status->rx_status_1 &
  3422. AR5K_NEW_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
  3423. desc->ds_us.rx.rs_status |= AR5K_RXERR_DECRYPT;
  3424. if (rx_status->rx_status_1 & AR5K_NEW_RX_DESC_STATUS1_MIC_ERROR)
  3425. desc->ds_us.rx.rs_status |= AR5K_RXERR_MIC;
  3426. }
  3427. return 0;
  3428. }
  3429. /****************\
  3430. GPIO Functions
  3431. \****************/
  3432. /*
  3433. * Set led state
  3434. */
  3435. void ath5k_hw_set_ledstate(struct ath5k_hw *ah, unsigned int state)
  3436. {
  3437. u32 led;
  3438. /*5210 has different led mode handling*/
  3439. u32 led_5210;
  3440. ATH5K_TRACE(ah->ah_sc);
  3441. /*Reset led status*/
  3442. if (ah->ah_version != AR5K_AR5210)
  3443. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  3444. AR5K_PCICFG_LEDMODE | AR5K_PCICFG_LED);
  3445. else
  3446. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_LED);
  3447. /*
  3448. * Some blinking values, define at your wish
  3449. */
  3450. switch (state) {
  3451. case AR5K_LED_SCAN:
  3452. case AR5K_LED_AUTH:
  3453. led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_PEND;
  3454. led_5210 = AR5K_PCICFG_LED_PEND | AR5K_PCICFG_LED_BCTL;
  3455. break;
  3456. case AR5K_LED_INIT:
  3457. led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_NONE;
  3458. led_5210 = AR5K_PCICFG_LED_PEND;
  3459. break;
  3460. case AR5K_LED_ASSOC:
  3461. case AR5K_LED_RUN:
  3462. led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_ASSOC;
  3463. led_5210 = AR5K_PCICFG_LED_ASSOC;
  3464. break;
  3465. default:
  3466. led = AR5K_PCICFG_LEDMODE_PROM | AR5K_PCICFG_LED_NONE;
  3467. led_5210 = AR5K_PCICFG_LED_PEND;
  3468. break;
  3469. }
  3470. /*Write new status to the register*/
  3471. if (ah->ah_version != AR5K_AR5210)
  3472. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led);
  3473. else
  3474. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led_5210);
  3475. }
  3476. /*
  3477. * Set GPIO outputs
  3478. */
  3479. int ath5k_hw_set_gpio_output(struct ath5k_hw *ah, u32 gpio)
  3480. {
  3481. ATH5K_TRACE(ah->ah_sc);
  3482. if (gpio > AR5K_NUM_GPIO)
  3483. return -EINVAL;
  3484. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &~
  3485. AR5K_GPIOCR_OUT(gpio)) | AR5K_GPIOCR_OUT(gpio), AR5K_GPIOCR);
  3486. return 0;
  3487. }
  3488. /*
  3489. * Set GPIO inputs
  3490. */
  3491. int ath5k_hw_set_gpio_input(struct ath5k_hw *ah, u32 gpio)
  3492. {
  3493. ATH5K_TRACE(ah->ah_sc);
  3494. if (gpio > AR5K_NUM_GPIO)
  3495. return -EINVAL;
  3496. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &~
  3497. AR5K_GPIOCR_OUT(gpio)) | AR5K_GPIOCR_IN(gpio), AR5K_GPIOCR);
  3498. return 0;
  3499. }
  3500. /*
  3501. * Get GPIO state
  3502. */
  3503. u32 ath5k_hw_get_gpio(struct ath5k_hw *ah, u32 gpio)
  3504. {
  3505. ATH5K_TRACE(ah->ah_sc);
  3506. if (gpio > AR5K_NUM_GPIO)
  3507. return 0xffffffff;
  3508. /* GPIO input magic */
  3509. return ((ath5k_hw_reg_read(ah, AR5K_GPIODI) & AR5K_GPIODI_M) >> gpio) &
  3510. 0x1;
  3511. }
  3512. /*
  3513. * Set GPIO state
  3514. */
  3515. int ath5k_hw_set_gpio(struct ath5k_hw *ah, u32 gpio, u32 val)
  3516. {
  3517. u32 data;
  3518. ATH5K_TRACE(ah->ah_sc);
  3519. if (gpio > AR5K_NUM_GPIO)
  3520. return -EINVAL;
  3521. /* GPIO output magic */
  3522. data = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  3523. data &= ~(1 << gpio);
  3524. data |= (val & 1) << gpio;
  3525. ath5k_hw_reg_write(ah, data, AR5K_GPIODO);
  3526. return 0;
  3527. }
  3528. /*
  3529. * Initialize the GPIO interrupt (RFKill switch)
  3530. */
  3531. void ath5k_hw_set_gpio_intr(struct ath5k_hw *ah, unsigned int gpio,
  3532. u32 interrupt_level)
  3533. {
  3534. u32 data;
  3535. ATH5K_TRACE(ah->ah_sc);
  3536. if (gpio > AR5K_NUM_GPIO)
  3537. return;
  3538. /*
  3539. * Set the GPIO interrupt
  3540. */
  3541. data = (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &
  3542. ~(AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_SELH |
  3543. AR5K_GPIOCR_INT_ENA | AR5K_GPIOCR_OUT(gpio))) |
  3544. (AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_ENA);
  3545. ath5k_hw_reg_write(ah, interrupt_level ? data :
  3546. (data | AR5K_GPIOCR_INT_SELH), AR5K_GPIOCR);
  3547. ah->ah_imr |= AR5K_IMR_GPIO;
  3548. /* Enable GPIO interrupts */
  3549. AR5K_REG_ENABLE_BITS(ah, AR5K_PIMR, AR5K_IMR_GPIO);
  3550. }
  3551. /****************\
  3552. Misc functions
  3553. \****************/
  3554. int ath5k_hw_get_capability(struct ath5k_hw *ah,
  3555. enum ath5k_capability_type cap_type,
  3556. u32 capability, u32 *result)
  3557. {
  3558. ATH5K_TRACE(ah->ah_sc);
  3559. switch (cap_type) {
  3560. case AR5K_CAP_NUM_TXQUEUES:
  3561. if (result) {
  3562. if (ah->ah_version == AR5K_AR5210)
  3563. *result = AR5K_NUM_TX_QUEUES_NOQCU;
  3564. else
  3565. *result = AR5K_NUM_TX_QUEUES;
  3566. goto yes;
  3567. }
  3568. case AR5K_CAP_VEOL:
  3569. goto yes;
  3570. case AR5K_CAP_COMPRESSION:
  3571. if (ah->ah_version == AR5K_AR5212)
  3572. goto yes;
  3573. else
  3574. goto no;
  3575. case AR5K_CAP_BURST:
  3576. goto yes;
  3577. case AR5K_CAP_TPC:
  3578. goto yes;
  3579. case AR5K_CAP_BSSIDMASK:
  3580. if (ah->ah_version == AR5K_AR5212)
  3581. goto yes;
  3582. else
  3583. goto no;
  3584. case AR5K_CAP_XR:
  3585. if (ah->ah_version == AR5K_AR5212)
  3586. goto yes;
  3587. else
  3588. goto no;
  3589. default:
  3590. goto no;
  3591. }
  3592. no:
  3593. return -EINVAL;
  3594. yes:
  3595. return 0;
  3596. }
  3597. static int ath5k_hw_enable_pspoll(struct ath5k_hw *ah, u8 *bssid,
  3598. u16 assoc_id)
  3599. {
  3600. ATH5K_TRACE(ah->ah_sc);
  3601. if (ah->ah_version == AR5K_AR5210) {
  3602. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  3603. AR5K_STA_ID1_NO_PSPOLL | AR5K_STA_ID1_DEFAULT_ANTENNA);
  3604. return 0;
  3605. }
  3606. return -EIO;
  3607. }
  3608. static int ath5k_hw_disable_pspoll(struct ath5k_hw *ah)
  3609. {
  3610. ATH5K_TRACE(ah->ah_sc);
  3611. if (ah->ah_version == AR5K_AR5210) {
  3612. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  3613. AR5K_STA_ID1_NO_PSPOLL | AR5K_STA_ID1_DEFAULT_ANTENNA);
  3614. return 0;
  3615. }
  3616. return -EIO;
  3617. }