rt2800lib.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
  5. Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
  6. Based on the original rt2800pci.c and rt2800usb.c.
  7. Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
  8. Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  9. Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
  10. Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
  11. Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
  12. Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
  13. <http://rt2x00.serialmonkey.com>
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the
  24. Free Software Foundation, Inc.,
  25. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. */
  27. /*
  28. Module: rt2800lib
  29. Abstract: rt2800 generic device routines.
  30. */
  31. #include <linux/crc-ccitt.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include "rt2x00.h"
  36. #include "rt2800lib.h"
  37. #include "rt2800.h"
  38. /*
  39. * Register access.
  40. * All access to the CSR registers will go through the methods
  41. * rt2800_register_read and rt2800_register_write.
  42. * BBP and RF register require indirect register access,
  43. * and use the CSR registers BBPCSR and RFCSR to achieve this.
  44. * These indirect registers work with busy bits,
  45. * and we will try maximal REGISTER_BUSY_COUNT times to access
  46. * the register while taking a REGISTER_BUSY_DELAY us delay
  47. * between each attampt. When the busy bit is still set at that time,
  48. * the access attempt is considered to have failed,
  49. * and we will print an error.
  50. * The _lock versions must be used if you already hold the csr_mutex
  51. */
  52. #define WAIT_FOR_BBP(__dev, __reg) \
  53. rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
  54. #define WAIT_FOR_RFCSR(__dev, __reg) \
  55. rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
  56. #define WAIT_FOR_RF(__dev, __reg) \
  57. rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
  58. #define WAIT_FOR_MCU(__dev, __reg) \
  59. rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
  60. H2M_MAILBOX_CSR_OWNER, (__reg))
  61. static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
  62. {
  63. /* check for rt2872 on SoC */
  64. if (!rt2x00_is_soc(rt2x00dev) ||
  65. !rt2x00_rt(rt2x00dev, RT2872))
  66. return false;
  67. /* we know for sure that these rf chipsets are used on rt305x boards */
  68. if (rt2x00_rf(rt2x00dev, RF3020) ||
  69. rt2x00_rf(rt2x00dev, RF3021) ||
  70. rt2x00_rf(rt2x00dev, RF3022))
  71. return true;
  72. NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
  73. return false;
  74. }
  75. static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
  76. const unsigned int word, const u8 value)
  77. {
  78. u32 reg;
  79. mutex_lock(&rt2x00dev->csr_mutex);
  80. /*
  81. * Wait until the BBP becomes available, afterwards we
  82. * can safely write the new data into the register.
  83. */
  84. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  85. reg = 0;
  86. rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
  87. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  88. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  89. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
  90. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  91. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  92. }
  93. mutex_unlock(&rt2x00dev->csr_mutex);
  94. }
  95. static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
  96. const unsigned int word, u8 *value)
  97. {
  98. u32 reg;
  99. mutex_lock(&rt2x00dev->csr_mutex);
  100. /*
  101. * Wait until the BBP becomes available, afterwards we
  102. * can safely write the read request into the register.
  103. * After the data has been written, we wait until hardware
  104. * returns the correct value, if at any time the register
  105. * doesn't become available in time, reg will be 0xffffffff
  106. * which means we return 0xff to the caller.
  107. */
  108. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  109. reg = 0;
  110. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  111. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  112. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
  113. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  114. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  115. WAIT_FOR_BBP(rt2x00dev, &reg);
  116. }
  117. *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
  118. mutex_unlock(&rt2x00dev->csr_mutex);
  119. }
  120. static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
  121. const unsigned int word, const u8 value)
  122. {
  123. u32 reg;
  124. mutex_lock(&rt2x00dev->csr_mutex);
  125. /*
  126. * Wait until the RFCSR becomes available, afterwards we
  127. * can safely write the new data into the register.
  128. */
  129. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  130. reg = 0;
  131. rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
  132. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  133. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
  134. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  135. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  136. }
  137. mutex_unlock(&rt2x00dev->csr_mutex);
  138. }
  139. static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
  140. const unsigned int word, u8 *value)
  141. {
  142. u32 reg;
  143. mutex_lock(&rt2x00dev->csr_mutex);
  144. /*
  145. * Wait until the RFCSR becomes available, afterwards we
  146. * can safely write the read request into the register.
  147. * After the data has been written, we wait until hardware
  148. * returns the correct value, if at any time the register
  149. * doesn't become available in time, reg will be 0xffffffff
  150. * which means we return 0xff to the caller.
  151. */
  152. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  153. reg = 0;
  154. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  155. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
  156. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  157. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  158. WAIT_FOR_RFCSR(rt2x00dev, &reg);
  159. }
  160. *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
  161. mutex_unlock(&rt2x00dev->csr_mutex);
  162. }
  163. static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
  164. const unsigned int word, const u32 value)
  165. {
  166. u32 reg;
  167. mutex_lock(&rt2x00dev->csr_mutex);
  168. /*
  169. * Wait until the RF becomes available, afterwards we
  170. * can safely write the new data into the register.
  171. */
  172. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  173. reg = 0;
  174. rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
  175. rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
  176. rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
  177. rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
  178. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
  179. rt2x00_rf_write(rt2x00dev, word, value);
  180. }
  181. mutex_unlock(&rt2x00dev->csr_mutex);
  182. }
  183. void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
  184. const u8 command, const u8 token,
  185. const u8 arg0, const u8 arg1)
  186. {
  187. u32 reg;
  188. /*
  189. * SOC devices don't support MCU requests.
  190. */
  191. if (rt2x00_is_soc(rt2x00dev))
  192. return;
  193. mutex_lock(&rt2x00dev->csr_mutex);
  194. /*
  195. * Wait until the MCU becomes available, afterwards we
  196. * can safely write the new data into the register.
  197. */
  198. if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
  199. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  200. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  201. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  202. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  203. rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
  204. reg = 0;
  205. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  206. rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
  207. }
  208. mutex_unlock(&rt2x00dev->csr_mutex);
  209. }
  210. EXPORT_SYMBOL_GPL(rt2800_mcu_request);
  211. int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
  212. {
  213. unsigned int i = 0;
  214. u32 reg;
  215. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  216. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  217. if (reg && reg != ~0)
  218. return 0;
  219. msleep(1);
  220. }
  221. ERROR(rt2x00dev, "Unstable hardware.\n");
  222. return -EBUSY;
  223. }
  224. EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
  225. int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
  226. {
  227. unsigned int i;
  228. u32 reg;
  229. /*
  230. * Some devices are really slow to respond here. Wait a whole second
  231. * before timing out.
  232. */
  233. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  234. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  235. if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
  236. !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
  237. return 0;
  238. msleep(10);
  239. }
  240. ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
  241. return -EACCES;
  242. }
  243. EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
  244. static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
  245. {
  246. u16 fw_crc;
  247. u16 crc;
  248. /*
  249. * The last 2 bytes in the firmware array are the crc checksum itself,
  250. * this means that we should never pass those 2 bytes to the crc
  251. * algorithm.
  252. */
  253. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  254. /*
  255. * Use the crc ccitt algorithm.
  256. * This will return the same value as the legacy driver which
  257. * used bit ordering reversion on the both the firmware bytes
  258. * before input input as well as on the final output.
  259. * Obviously using crc ccitt directly is much more efficient.
  260. */
  261. crc = crc_ccitt(~0, data, len - 2);
  262. /*
  263. * There is a small difference between the crc-itu-t + bitrev and
  264. * the crc-ccitt crc calculation. In the latter method the 2 bytes
  265. * will be swapped, use swab16 to convert the crc to the correct
  266. * value.
  267. */
  268. crc = swab16(crc);
  269. return fw_crc == crc;
  270. }
  271. int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
  272. const u8 *data, const size_t len)
  273. {
  274. size_t offset = 0;
  275. size_t fw_len;
  276. bool multiple;
  277. /*
  278. * PCI(e) & SOC devices require firmware with a length
  279. * of 8kb. USB devices require firmware files with a length
  280. * of 4kb. Certain USB chipsets however require different firmware,
  281. * which Ralink only provides attached to the original firmware
  282. * file. Thus for USB devices, firmware files have a length
  283. * which is a multiple of 4kb.
  284. */
  285. if (rt2x00_is_usb(rt2x00dev)) {
  286. fw_len = 4096;
  287. multiple = true;
  288. } else {
  289. fw_len = 8192;
  290. multiple = true;
  291. }
  292. /*
  293. * Validate the firmware length
  294. */
  295. if (len != fw_len && (!multiple || (len % fw_len) != 0))
  296. return FW_BAD_LENGTH;
  297. /*
  298. * Check if the chipset requires one of the upper parts
  299. * of the firmware.
  300. */
  301. if (rt2x00_is_usb(rt2x00dev) &&
  302. !rt2x00_rt(rt2x00dev, RT2860) &&
  303. !rt2x00_rt(rt2x00dev, RT2872) &&
  304. !rt2x00_rt(rt2x00dev, RT3070) &&
  305. ((len / fw_len) == 1))
  306. return FW_BAD_VERSION;
  307. /*
  308. * 8kb firmware files must be checked as if it were
  309. * 2 separate firmware files.
  310. */
  311. while (offset < len) {
  312. if (!rt2800_check_firmware_crc(data + offset, fw_len))
  313. return FW_BAD_CRC;
  314. offset += fw_len;
  315. }
  316. return FW_OK;
  317. }
  318. EXPORT_SYMBOL_GPL(rt2800_check_firmware);
  319. int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
  320. const u8 *data, const size_t len)
  321. {
  322. unsigned int i;
  323. u32 reg;
  324. /*
  325. * If driver doesn't wake up firmware here,
  326. * rt2800_load_firmware will hang forever when interface is up again.
  327. */
  328. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
  329. /*
  330. * Wait for stable hardware.
  331. */
  332. if (rt2800_wait_csr_ready(rt2x00dev))
  333. return -EBUSY;
  334. if (rt2x00_is_pci(rt2x00dev))
  335. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
  336. /*
  337. * Disable DMA, will be reenabled later when enabling
  338. * the radio.
  339. */
  340. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  341. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  342. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  343. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  344. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  345. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  346. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  347. /*
  348. * Write firmware to the device.
  349. */
  350. rt2800_drv_write_firmware(rt2x00dev, data, len);
  351. /*
  352. * Wait for device to stabilize.
  353. */
  354. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  355. rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
  356. if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
  357. break;
  358. msleep(1);
  359. }
  360. if (i == REGISTER_BUSY_COUNT) {
  361. ERROR(rt2x00dev, "PBF system register not ready.\n");
  362. return -EBUSY;
  363. }
  364. /*
  365. * Initialize firmware.
  366. */
  367. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  368. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  369. msleep(1);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL_GPL(rt2800_load_firmware);
  373. void rt2800_write_tx_data(struct queue_entry *entry,
  374. struct txentry_desc *txdesc)
  375. {
  376. __le32 *txwi = rt2800_drv_get_txwi(entry);
  377. u32 word;
  378. /*
  379. * Initialize TX Info descriptor
  380. */
  381. rt2x00_desc_read(txwi, 0, &word);
  382. rt2x00_set_field32(&word, TXWI_W0_FRAG,
  383. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  384. rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
  385. test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
  386. rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
  387. rt2x00_set_field32(&word, TXWI_W0_TS,
  388. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  389. rt2x00_set_field32(&word, TXWI_W0_AMPDU,
  390. test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
  391. rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
  392. rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->txop);
  393. rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
  394. rt2x00_set_field32(&word, TXWI_W0_BW,
  395. test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
  396. rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
  397. test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
  398. rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
  399. rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
  400. rt2x00_desc_write(txwi, 0, word);
  401. rt2x00_desc_read(txwi, 1, &word);
  402. rt2x00_set_field32(&word, TXWI_W1_ACK,
  403. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  404. rt2x00_set_field32(&word, TXWI_W1_NSEQ,
  405. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  406. rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
  407. rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
  408. test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
  409. txdesc->key_idx : 0xff);
  410. rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
  411. txdesc->length);
  412. rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
  413. rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
  414. rt2x00_desc_write(txwi, 1, word);
  415. /*
  416. * Always write 0 to IV/EIV fields, hardware will insert the IV
  417. * from the IVEIV register when TXD_W3_WIV is set to 0.
  418. * When TXD_W3_WIV is set to 1 it will use the IV data
  419. * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
  420. * crypto entry in the registers should be used to encrypt the frame.
  421. */
  422. _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
  423. _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
  424. }
  425. EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
  426. static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
  427. {
  428. int rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
  429. int rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
  430. int rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
  431. u16 eeprom;
  432. u8 offset0;
  433. u8 offset1;
  434. u8 offset2;
  435. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
  436. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
  437. offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
  438. offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
  439. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
  440. offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
  441. } else {
  442. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
  443. offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
  444. offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
  445. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
  446. offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
  447. }
  448. /*
  449. * Convert the value from the descriptor into the RSSI value
  450. * If the value in the descriptor is 0, it is considered invalid
  451. * and the default (extremely low) rssi value is assumed
  452. */
  453. rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
  454. rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
  455. rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
  456. /*
  457. * mac80211 only accepts a single RSSI value. Calculating the
  458. * average doesn't deliver a fair answer either since -60:-60 would
  459. * be considered equally good as -50:-70 while the second is the one
  460. * which gives less energy...
  461. */
  462. rssi0 = max(rssi0, rssi1);
  463. return max(rssi0, rssi2);
  464. }
  465. void rt2800_process_rxwi(struct queue_entry *entry,
  466. struct rxdone_entry_desc *rxdesc)
  467. {
  468. __le32 *rxwi = (__le32 *) entry->skb->data;
  469. u32 word;
  470. rt2x00_desc_read(rxwi, 0, &word);
  471. rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
  472. rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
  473. rt2x00_desc_read(rxwi, 1, &word);
  474. if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
  475. rxdesc->flags |= RX_FLAG_SHORT_GI;
  476. if (rt2x00_get_field32(word, RXWI_W1_BW))
  477. rxdesc->flags |= RX_FLAG_40MHZ;
  478. /*
  479. * Detect RX rate, always use MCS as signal type.
  480. */
  481. rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
  482. rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
  483. rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
  484. /*
  485. * Mask of 0x8 bit to remove the short preamble flag.
  486. */
  487. if (rxdesc->rate_mode == RATE_MODE_CCK)
  488. rxdesc->signal &= ~0x8;
  489. rt2x00_desc_read(rxwi, 2, &word);
  490. /*
  491. * Convert descriptor AGC value to RSSI value.
  492. */
  493. rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
  494. /*
  495. * Remove RXWI descriptor from start of buffer.
  496. */
  497. skb_pull(entry->skb, RXWI_DESC_SIZE);
  498. }
  499. EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
  500. static bool rt2800_txdone_entry_check(struct queue_entry *entry, u32 reg)
  501. {
  502. __le32 *txwi;
  503. u32 word;
  504. int wcid, ack, pid;
  505. int tx_wcid, tx_ack, tx_pid;
  506. wcid = rt2x00_get_field32(reg, TX_STA_FIFO_WCID);
  507. ack = rt2x00_get_field32(reg, TX_STA_FIFO_TX_ACK_REQUIRED);
  508. pid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE);
  509. /*
  510. * This frames has returned with an IO error,
  511. * so the status report is not intended for this
  512. * frame.
  513. */
  514. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags)) {
  515. rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
  516. return false;
  517. }
  518. /*
  519. * Validate if this TX status report is intended for
  520. * this entry by comparing the WCID/ACK/PID fields.
  521. */
  522. txwi = rt2800_drv_get_txwi(entry);
  523. rt2x00_desc_read(txwi, 1, &word);
  524. tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
  525. tx_ack = rt2x00_get_field32(word, TXWI_W1_ACK);
  526. tx_pid = rt2x00_get_field32(word, TXWI_W1_PACKETID);
  527. if ((wcid != tx_wcid) || (ack != tx_ack) || (pid != tx_pid)) {
  528. WARNING(entry->queue->rt2x00dev,
  529. "TX status report missed for queue %d entry %d\n",
  530. entry->queue->qid, entry->entry_idx);
  531. rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
  532. return false;
  533. }
  534. return true;
  535. }
  536. void rt2800_txdone_entry(struct queue_entry *entry, u32 status)
  537. {
  538. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  539. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  540. struct txdone_entry_desc txdesc;
  541. u32 word;
  542. u16 mcs, real_mcs;
  543. int aggr, ampdu;
  544. __le32 *txwi;
  545. /*
  546. * Obtain the status about this packet.
  547. */
  548. txdesc.flags = 0;
  549. txwi = rt2800_drv_get_txwi(entry);
  550. rt2x00_desc_read(txwi, 0, &word);
  551. mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
  552. ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
  553. real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
  554. aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
  555. /*
  556. * If a frame was meant to be sent as a single non-aggregated MPDU
  557. * but ended up in an aggregate the used tx rate doesn't correlate
  558. * with the one specified in the TXWI as the whole aggregate is sent
  559. * with the same rate.
  560. *
  561. * For example: two frames are sent to rt2x00, the first one sets
  562. * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
  563. * and requests MCS15. If the hw aggregates both frames into one
  564. * AMDPU the tx status for both frames will contain MCS7 although
  565. * the frame was sent successfully.
  566. *
  567. * Hence, replace the requested rate with the real tx rate to not
  568. * confuse the rate control algortihm by providing clearly wrong
  569. * data.
  570. */
  571. if (aggr == 1 && ampdu == 0 && real_mcs != mcs) {
  572. skbdesc->tx_rate_idx = real_mcs;
  573. mcs = real_mcs;
  574. }
  575. /*
  576. * Ralink has a retry mechanism using a global fallback
  577. * table. We setup this fallback table to try the immediate
  578. * lower rate for all rates. In the TX_STA_FIFO, the MCS field
  579. * always contains the MCS used for the last transmission, be
  580. * it successful or not.
  581. */
  582. if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
  583. /*
  584. * Transmission succeeded. The number of retries is
  585. * mcs - real_mcs
  586. */
  587. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  588. txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
  589. } else {
  590. /*
  591. * Transmission failed. The number of retries is
  592. * always 7 in this case (for a total number of 8
  593. * frames sent).
  594. */
  595. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  596. txdesc.retry = rt2x00dev->long_retry;
  597. }
  598. /*
  599. * the frame was retried at least once
  600. * -> hw used fallback rates
  601. */
  602. if (txdesc.retry)
  603. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  604. rt2x00lib_txdone(entry, &txdesc);
  605. }
  606. EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
  607. void rt2800_txdone(struct rt2x00_dev *rt2x00dev)
  608. {
  609. struct data_queue *queue;
  610. struct queue_entry *entry;
  611. u32 reg;
  612. u8 pid;
  613. int i;
  614. /*
  615. * TX_STA_FIFO is a stack of X entries, hence read TX_STA_FIFO
  616. * at most X times and also stop processing once the TX_STA_FIFO_VALID
  617. * flag is not set anymore.
  618. *
  619. * The legacy drivers use X=TX_RING_SIZE but state in a comment
  620. * that the TX_STA_FIFO stack has a size of 16. We stick to our
  621. * tx ring size for now.
  622. */
  623. for (i = 0; i < rt2x00dev->ops->tx->entry_num; i++) {
  624. rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
  625. if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
  626. break;
  627. /*
  628. * Skip this entry when it contains an invalid
  629. * queue identication number.
  630. */
  631. pid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_QUEUE);
  632. if (pid >= QID_RX)
  633. continue;
  634. queue = rt2x00queue_get_queue(rt2x00dev, pid);
  635. if (unlikely(!queue))
  636. continue;
  637. /*
  638. * Inside each queue, we process each entry in a chronological
  639. * order. We first check that the queue is not empty.
  640. */
  641. entry = NULL;
  642. while (!rt2x00queue_empty(queue)) {
  643. entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  644. if (rt2800_txdone_entry_check(entry, reg))
  645. break;
  646. }
  647. if (!entry || rt2x00queue_empty(queue))
  648. break;
  649. rt2800_txdone_entry(entry, reg);
  650. }
  651. }
  652. EXPORT_SYMBOL_GPL(rt2800_txdone);
  653. void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
  654. {
  655. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  656. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  657. unsigned int beacon_base;
  658. unsigned int padding_len;
  659. u32 orig_reg, reg;
  660. /*
  661. * Disable beaconing while we are reloading the beacon data,
  662. * otherwise we might be sending out invalid data.
  663. */
  664. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  665. orig_reg = reg;
  666. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  667. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  668. /*
  669. * Add space for the TXWI in front of the skb.
  670. */
  671. skb_push(entry->skb, TXWI_DESC_SIZE);
  672. memset(entry->skb, 0, TXWI_DESC_SIZE);
  673. /*
  674. * Register descriptor details in skb frame descriptor.
  675. */
  676. skbdesc->flags |= SKBDESC_DESC_IN_SKB;
  677. skbdesc->desc = entry->skb->data;
  678. skbdesc->desc_len = TXWI_DESC_SIZE;
  679. /*
  680. * Add the TXWI for the beacon to the skb.
  681. */
  682. rt2800_write_tx_data(entry, txdesc);
  683. /*
  684. * Dump beacon to userspace through debugfs.
  685. */
  686. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
  687. /*
  688. * Write entire beacon with TXWI and padding to register.
  689. */
  690. padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
  691. if (padding_len && skb_pad(entry->skb, padding_len)) {
  692. ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
  693. /* skb freed by skb_pad() on failure */
  694. entry->skb = NULL;
  695. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
  696. return;
  697. }
  698. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  699. rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
  700. entry->skb->len + padding_len);
  701. /*
  702. * Enable beaconing again.
  703. */
  704. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  705. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  706. /*
  707. * Clean up beacon skb.
  708. */
  709. dev_kfree_skb_any(entry->skb);
  710. entry->skb = NULL;
  711. }
  712. EXPORT_SYMBOL_GPL(rt2800_write_beacon);
  713. static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
  714. unsigned int beacon_base)
  715. {
  716. int i;
  717. /*
  718. * For the Beacon base registers we only need to clear
  719. * the whole TXWI which (when set to 0) will invalidate
  720. * the entire beacon.
  721. */
  722. for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
  723. rt2800_register_write(rt2x00dev, beacon_base + i, 0);
  724. }
  725. void rt2800_clear_beacon(struct queue_entry *entry)
  726. {
  727. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  728. u32 reg;
  729. /*
  730. * Disable beaconing while we are reloading the beacon data,
  731. * otherwise we might be sending out invalid data.
  732. */
  733. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  734. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  735. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  736. /*
  737. * Clear beacon.
  738. */
  739. rt2800_clear_beacon_register(rt2x00dev,
  740. HW_BEACON_OFFSET(entry->entry_idx));
  741. /*
  742. * Enabled beaconing again.
  743. */
  744. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  745. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  746. }
  747. EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
  748. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  749. const struct rt2x00debug rt2800_rt2x00debug = {
  750. .owner = THIS_MODULE,
  751. .csr = {
  752. .read = rt2800_register_read,
  753. .write = rt2800_register_write,
  754. .flags = RT2X00DEBUGFS_OFFSET,
  755. .word_base = CSR_REG_BASE,
  756. .word_size = sizeof(u32),
  757. .word_count = CSR_REG_SIZE / sizeof(u32),
  758. },
  759. .eeprom = {
  760. .read = rt2x00_eeprom_read,
  761. .write = rt2x00_eeprom_write,
  762. .word_base = EEPROM_BASE,
  763. .word_size = sizeof(u16),
  764. .word_count = EEPROM_SIZE / sizeof(u16),
  765. },
  766. .bbp = {
  767. .read = rt2800_bbp_read,
  768. .write = rt2800_bbp_write,
  769. .word_base = BBP_BASE,
  770. .word_size = sizeof(u8),
  771. .word_count = BBP_SIZE / sizeof(u8),
  772. },
  773. .rf = {
  774. .read = rt2x00_rf_read,
  775. .write = rt2800_rf_write,
  776. .word_base = RF_BASE,
  777. .word_size = sizeof(u32),
  778. .word_count = RF_SIZE / sizeof(u32),
  779. },
  780. };
  781. EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
  782. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  783. int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  784. {
  785. u32 reg;
  786. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  787. return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
  788. }
  789. EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
  790. #ifdef CONFIG_RT2X00_LIB_LEDS
  791. static void rt2800_brightness_set(struct led_classdev *led_cdev,
  792. enum led_brightness brightness)
  793. {
  794. struct rt2x00_led *led =
  795. container_of(led_cdev, struct rt2x00_led, led_dev);
  796. unsigned int enabled = brightness != LED_OFF;
  797. unsigned int bg_mode =
  798. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  799. unsigned int polarity =
  800. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  801. EEPROM_FREQ_LED_POLARITY);
  802. unsigned int ledmode =
  803. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  804. EEPROM_FREQ_LED_MODE);
  805. if (led->type == LED_TYPE_RADIO) {
  806. rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  807. enabled ? 0x20 : 0);
  808. } else if (led->type == LED_TYPE_ASSOC) {
  809. rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  810. enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
  811. } else if (led->type == LED_TYPE_QUALITY) {
  812. /*
  813. * The brightness is divided into 6 levels (0 - 5),
  814. * The specs tell us the following levels:
  815. * 0, 1 ,3, 7, 15, 31
  816. * to determine the level in a simple way we can simply
  817. * work with bitshifting:
  818. * (1 << level) - 1
  819. */
  820. rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
  821. (1 << brightness / (LED_FULL / 6)) - 1,
  822. polarity);
  823. }
  824. }
  825. static int rt2800_blink_set(struct led_classdev *led_cdev,
  826. unsigned long *delay_on, unsigned long *delay_off)
  827. {
  828. struct rt2x00_led *led =
  829. container_of(led_cdev, struct rt2x00_led, led_dev);
  830. u32 reg;
  831. rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
  832. rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
  833. rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
  834. rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
  835. return 0;
  836. }
  837. static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
  838. struct rt2x00_led *led, enum led_type type)
  839. {
  840. led->rt2x00dev = rt2x00dev;
  841. led->type = type;
  842. led->led_dev.brightness_set = rt2800_brightness_set;
  843. led->led_dev.blink_set = rt2800_blink_set;
  844. led->flags = LED_INITIALIZED;
  845. }
  846. #endif /* CONFIG_RT2X00_LIB_LEDS */
  847. /*
  848. * Configuration handlers.
  849. */
  850. static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
  851. struct rt2x00lib_crypto *crypto,
  852. struct ieee80211_key_conf *key)
  853. {
  854. struct mac_wcid_entry wcid_entry;
  855. struct mac_iveiv_entry iveiv_entry;
  856. u32 offset;
  857. u32 reg;
  858. offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
  859. if (crypto->cmd == SET_KEY) {
  860. rt2800_register_read(rt2x00dev, offset, &reg);
  861. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
  862. !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
  863. /*
  864. * Both the cipher as the BSS Idx numbers are split in a main
  865. * value of 3 bits, and a extended field for adding one additional
  866. * bit to the value.
  867. */
  868. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
  869. (crypto->cipher & 0x7));
  870. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
  871. (crypto->cipher & 0x8) >> 3);
  872. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
  873. (crypto->bssidx & 0x7));
  874. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
  875. (crypto->bssidx & 0x8) >> 3);
  876. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
  877. rt2800_register_write(rt2x00dev, offset, reg);
  878. } else {
  879. rt2800_register_write(rt2x00dev, offset, 0);
  880. }
  881. offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
  882. memset(&iveiv_entry, 0, sizeof(iveiv_entry));
  883. if ((crypto->cipher == CIPHER_TKIP) ||
  884. (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
  885. (crypto->cipher == CIPHER_AES))
  886. iveiv_entry.iv[3] |= 0x20;
  887. iveiv_entry.iv[3] |= key->keyidx << 6;
  888. rt2800_register_multiwrite(rt2x00dev, offset,
  889. &iveiv_entry, sizeof(iveiv_entry));
  890. offset = MAC_WCID_ENTRY(key->hw_key_idx);
  891. memset(&wcid_entry, 0, sizeof(wcid_entry));
  892. if (crypto->cmd == SET_KEY)
  893. memcpy(wcid_entry.mac, crypto->address, ETH_ALEN);
  894. rt2800_register_multiwrite(rt2x00dev, offset,
  895. &wcid_entry, sizeof(wcid_entry));
  896. }
  897. int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
  898. struct rt2x00lib_crypto *crypto,
  899. struct ieee80211_key_conf *key)
  900. {
  901. struct hw_key_entry key_entry;
  902. struct rt2x00_field32 field;
  903. u32 offset;
  904. u32 reg;
  905. if (crypto->cmd == SET_KEY) {
  906. key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
  907. memcpy(key_entry.key, crypto->key,
  908. sizeof(key_entry.key));
  909. memcpy(key_entry.tx_mic, crypto->tx_mic,
  910. sizeof(key_entry.tx_mic));
  911. memcpy(key_entry.rx_mic, crypto->rx_mic,
  912. sizeof(key_entry.rx_mic));
  913. offset = SHARED_KEY_ENTRY(key->hw_key_idx);
  914. rt2800_register_multiwrite(rt2x00dev, offset,
  915. &key_entry, sizeof(key_entry));
  916. }
  917. /*
  918. * The cipher types are stored over multiple registers
  919. * starting with SHARED_KEY_MODE_BASE each word will have
  920. * 32 bits and contains the cipher types for 2 bssidx each.
  921. * Using the correct defines correctly will cause overhead,
  922. * so just calculate the correct offset.
  923. */
  924. field.bit_offset = 4 * (key->hw_key_idx % 8);
  925. field.bit_mask = 0x7 << field.bit_offset;
  926. offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
  927. rt2800_register_read(rt2x00dev, offset, &reg);
  928. rt2x00_set_field32(&reg, field,
  929. (crypto->cmd == SET_KEY) * crypto->cipher);
  930. rt2800_register_write(rt2x00dev, offset, reg);
  931. /*
  932. * Update WCID information
  933. */
  934. rt2800_config_wcid_attr(rt2x00dev, crypto, key);
  935. return 0;
  936. }
  937. EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
  938. int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
  939. struct rt2x00lib_crypto *crypto,
  940. struct ieee80211_key_conf *key)
  941. {
  942. struct hw_key_entry key_entry;
  943. u32 offset;
  944. if (crypto->cmd == SET_KEY) {
  945. /*
  946. * 1 pairwise key is possible per AID, this means that the AID
  947. * equals our hw_key_idx. Make sure the WCID starts _after_ the
  948. * last possible shared key entry.
  949. *
  950. * Since parts of the pairwise key table might be shared with
  951. * the beacon frame buffers 6 & 7 we should only write into the
  952. * first 222 entries.
  953. */
  954. if (crypto->aid > (222 - 32))
  955. return -ENOSPC;
  956. key->hw_key_idx = 32 + crypto->aid;
  957. memcpy(key_entry.key, crypto->key,
  958. sizeof(key_entry.key));
  959. memcpy(key_entry.tx_mic, crypto->tx_mic,
  960. sizeof(key_entry.tx_mic));
  961. memcpy(key_entry.rx_mic, crypto->rx_mic,
  962. sizeof(key_entry.rx_mic));
  963. offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
  964. rt2800_register_multiwrite(rt2x00dev, offset,
  965. &key_entry, sizeof(key_entry));
  966. }
  967. /*
  968. * Update WCID information
  969. */
  970. rt2800_config_wcid_attr(rt2x00dev, crypto, key);
  971. return 0;
  972. }
  973. EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
  974. void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
  975. const unsigned int filter_flags)
  976. {
  977. u32 reg;
  978. /*
  979. * Start configuration steps.
  980. * Note that the version error will always be dropped
  981. * and broadcast frames will always be accepted since
  982. * there is no filter for it at this time.
  983. */
  984. rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
  985. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
  986. !(filter_flags & FIF_FCSFAIL));
  987. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
  988. !(filter_flags & FIF_PLCPFAIL));
  989. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
  990. !(filter_flags & FIF_PROMISC_IN_BSS));
  991. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
  992. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
  993. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
  994. !(filter_flags & FIF_ALLMULTI));
  995. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
  996. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
  997. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
  998. !(filter_flags & FIF_CONTROL));
  999. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
  1000. !(filter_flags & FIF_CONTROL));
  1001. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
  1002. !(filter_flags & FIF_CONTROL));
  1003. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
  1004. !(filter_flags & FIF_CONTROL));
  1005. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
  1006. !(filter_flags & FIF_CONTROL));
  1007. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
  1008. !(filter_flags & FIF_PSPOLL));
  1009. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
  1010. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
  1011. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
  1012. !(filter_flags & FIF_CONTROL));
  1013. rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
  1014. }
  1015. EXPORT_SYMBOL_GPL(rt2800_config_filter);
  1016. void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
  1017. struct rt2x00intf_conf *conf, const unsigned int flags)
  1018. {
  1019. u32 reg;
  1020. bool update_bssid = false;
  1021. if (flags & CONFIG_UPDATE_TYPE) {
  1022. /*
  1023. * Enable synchronisation.
  1024. */
  1025. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1026. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
  1027. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1028. }
  1029. if (flags & CONFIG_UPDATE_MAC) {
  1030. if (flags & CONFIG_UPDATE_TYPE &&
  1031. conf->sync == TSF_SYNC_AP_NONE) {
  1032. /*
  1033. * The BSSID register has to be set to our own mac
  1034. * address in AP mode.
  1035. */
  1036. memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
  1037. update_bssid = true;
  1038. }
  1039. if (!is_zero_ether_addr((const u8 *)conf->mac)) {
  1040. reg = le32_to_cpu(conf->mac[1]);
  1041. rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
  1042. conf->mac[1] = cpu_to_le32(reg);
  1043. }
  1044. rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
  1045. conf->mac, sizeof(conf->mac));
  1046. }
  1047. if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
  1048. if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
  1049. reg = le32_to_cpu(conf->bssid[1]);
  1050. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
  1051. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
  1052. conf->bssid[1] = cpu_to_le32(reg);
  1053. }
  1054. rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
  1055. conf->bssid, sizeof(conf->bssid));
  1056. }
  1057. }
  1058. EXPORT_SYMBOL_GPL(rt2800_config_intf);
  1059. static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
  1060. struct rt2x00lib_erp *erp)
  1061. {
  1062. bool any_sta_nongf = !!(erp->ht_opmode &
  1063. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  1064. u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
  1065. u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
  1066. u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
  1067. u32 reg;
  1068. /* default protection rate for HT20: OFDM 24M */
  1069. mm20_rate = gf20_rate = 0x4004;
  1070. /* default protection rate for HT40: duplicate OFDM 24M */
  1071. mm40_rate = gf40_rate = 0x4084;
  1072. switch (protection) {
  1073. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  1074. /*
  1075. * All STAs in this BSS are HT20/40 but there might be
  1076. * STAs not supporting greenfield mode.
  1077. * => Disable protection for HT transmissions.
  1078. */
  1079. mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
  1080. break;
  1081. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  1082. /*
  1083. * All STAs in this BSS are HT20 or HT20/40 but there
  1084. * might be STAs not supporting greenfield mode.
  1085. * => Protect all HT40 transmissions.
  1086. */
  1087. mm20_mode = gf20_mode = 0;
  1088. mm40_mode = gf40_mode = 2;
  1089. break;
  1090. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  1091. /*
  1092. * Nonmember protection:
  1093. * According to 802.11n we _should_ protect all
  1094. * HT transmissions (but we don't have to).
  1095. *
  1096. * But if cts_protection is enabled we _shall_ protect
  1097. * all HT transmissions using a CCK rate.
  1098. *
  1099. * And if any station is non GF we _shall_ protect
  1100. * GF transmissions.
  1101. *
  1102. * We decide to protect everything
  1103. * -> fall through to mixed mode.
  1104. */
  1105. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  1106. /*
  1107. * Legacy STAs are present
  1108. * => Protect all HT transmissions.
  1109. */
  1110. mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
  1111. /*
  1112. * If erp protection is needed we have to protect HT
  1113. * transmissions with CCK 11M long preamble.
  1114. */
  1115. if (erp->cts_protection) {
  1116. /* don't duplicate RTS/CTS in CCK mode */
  1117. mm20_rate = mm40_rate = 0x0003;
  1118. gf20_rate = gf40_rate = 0x0003;
  1119. }
  1120. break;
  1121. };
  1122. /* check for STAs not supporting greenfield mode */
  1123. if (any_sta_nongf)
  1124. gf20_mode = gf40_mode = 2;
  1125. /* Update HT protection config */
  1126. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  1127. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
  1128. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
  1129. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  1130. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  1131. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
  1132. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
  1133. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  1134. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  1135. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
  1136. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
  1137. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  1138. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  1139. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
  1140. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
  1141. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  1142. }
  1143. void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
  1144. u32 changed)
  1145. {
  1146. u32 reg;
  1147. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1148. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  1149. rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
  1150. !!erp->short_preamble);
  1151. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
  1152. !!erp->short_preamble);
  1153. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  1154. }
  1155. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1156. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  1157. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
  1158. erp->cts_protection ? 2 : 0);
  1159. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  1160. }
  1161. if (changed & BSS_CHANGED_BASIC_RATES) {
  1162. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
  1163. erp->basic_rates);
  1164. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  1165. }
  1166. if (changed & BSS_CHANGED_ERP_SLOT) {
  1167. rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
  1168. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
  1169. erp->slot_time);
  1170. rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
  1171. rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
  1172. rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
  1173. rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
  1174. }
  1175. if (changed & BSS_CHANGED_BEACON_INT) {
  1176. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1177. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
  1178. erp->beacon_int * 16);
  1179. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1180. }
  1181. if (changed & BSS_CHANGED_HT)
  1182. rt2800_config_ht_opmode(rt2x00dev, erp);
  1183. }
  1184. EXPORT_SYMBOL_GPL(rt2800_config_erp);
  1185. static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
  1186. enum antenna ant)
  1187. {
  1188. u32 reg;
  1189. u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
  1190. u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
  1191. if (rt2x00_is_pci(rt2x00dev)) {
  1192. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  1193. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
  1194. rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
  1195. } else if (rt2x00_is_usb(rt2x00dev))
  1196. rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
  1197. eesk_pin, 0);
  1198. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  1199. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD, 0);
  1200. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, gpio_bit3);
  1201. rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
  1202. }
  1203. void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
  1204. {
  1205. u8 r1;
  1206. u8 r3;
  1207. u16 eeprom;
  1208. rt2800_bbp_read(rt2x00dev, 1, &r1);
  1209. rt2800_bbp_read(rt2x00dev, 3, &r3);
  1210. /*
  1211. * Configure the TX antenna.
  1212. */
  1213. switch (ant->tx_chain_num) {
  1214. case 1:
  1215. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
  1216. break;
  1217. case 2:
  1218. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
  1219. break;
  1220. case 3:
  1221. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
  1222. break;
  1223. }
  1224. /*
  1225. * Configure the RX antenna.
  1226. */
  1227. switch (ant->rx_chain_num) {
  1228. case 1:
  1229. if (rt2x00_rt(rt2x00dev, RT3070) ||
  1230. rt2x00_rt(rt2x00dev, RT3090) ||
  1231. rt2x00_rt(rt2x00dev, RT3390)) {
  1232. rt2x00_eeprom_read(rt2x00dev,
  1233. EEPROM_NIC_CONF1, &eeprom);
  1234. if (rt2x00_get_field16(eeprom,
  1235. EEPROM_NIC_CONF1_ANT_DIVERSITY))
  1236. rt2800_set_ant_diversity(rt2x00dev,
  1237. rt2x00dev->default_ant.rx);
  1238. }
  1239. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
  1240. break;
  1241. case 2:
  1242. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
  1243. break;
  1244. case 3:
  1245. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
  1246. break;
  1247. }
  1248. rt2800_bbp_write(rt2x00dev, 3, r3);
  1249. rt2800_bbp_write(rt2x00dev, 1, r1);
  1250. }
  1251. EXPORT_SYMBOL_GPL(rt2800_config_ant);
  1252. static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  1253. struct rt2x00lib_conf *libconf)
  1254. {
  1255. u16 eeprom;
  1256. short lna_gain;
  1257. if (libconf->rf.channel <= 14) {
  1258. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  1259. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
  1260. } else if (libconf->rf.channel <= 64) {
  1261. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  1262. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
  1263. } else if (libconf->rf.channel <= 128) {
  1264. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
  1265. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
  1266. } else {
  1267. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
  1268. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
  1269. }
  1270. rt2x00dev->lna_gain = lna_gain;
  1271. }
  1272. static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
  1273. struct ieee80211_conf *conf,
  1274. struct rf_channel *rf,
  1275. struct channel_info *info)
  1276. {
  1277. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  1278. if (rt2x00dev->default_ant.tx_chain_num == 1)
  1279. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
  1280. if (rt2x00dev->default_ant.rx_chain_num == 1) {
  1281. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
  1282. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  1283. } else if (rt2x00dev->default_ant.rx_chain_num == 2)
  1284. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  1285. if (rf->channel > 14) {
  1286. /*
  1287. * When TX power is below 0, we should increase it by 7 to
  1288. * make it a positive value (Minumum value is -7).
  1289. * However this means that values between 0 and 7 have
  1290. * double meaning, and we should set a 7DBm boost flag.
  1291. */
  1292. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
  1293. (info->default_power1 >= 0));
  1294. if (info->default_power1 < 0)
  1295. info->default_power1 += 7;
  1296. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
  1297. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
  1298. (info->default_power2 >= 0));
  1299. if (info->default_power2 < 0)
  1300. info->default_power2 += 7;
  1301. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
  1302. } else {
  1303. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
  1304. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
  1305. }
  1306. rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
  1307. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  1308. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  1309. rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  1310. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  1311. udelay(200);
  1312. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  1313. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  1314. rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  1315. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  1316. udelay(200);
  1317. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  1318. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  1319. rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  1320. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  1321. }
  1322. static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
  1323. struct ieee80211_conf *conf,
  1324. struct rf_channel *rf,
  1325. struct channel_info *info)
  1326. {
  1327. u8 rfcsr;
  1328. rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
  1329. rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
  1330. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  1331. rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
  1332. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  1333. rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
  1334. rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
  1335. rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
  1336. rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
  1337. rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
  1338. rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
  1339. rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
  1340. rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
  1341. rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
  1342. rt2800_rfcsr_write(rt2x00dev, 24,
  1343. rt2x00dev->calibration[conf_is_ht40(conf)]);
  1344. rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
  1345. rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
  1346. rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
  1347. }
  1348. static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
  1349. struct ieee80211_conf *conf,
  1350. struct rf_channel *rf,
  1351. struct channel_info *info)
  1352. {
  1353. u32 reg;
  1354. unsigned int tx_pin;
  1355. u8 bbp;
  1356. if (rf->channel <= 14) {
  1357. info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
  1358. info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
  1359. } else {
  1360. info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
  1361. info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
  1362. }
  1363. if (rt2x00_rf(rt2x00dev, RF2020) ||
  1364. rt2x00_rf(rt2x00dev, RF3020) ||
  1365. rt2x00_rf(rt2x00dev, RF3021) ||
  1366. rt2x00_rf(rt2x00dev, RF3022) ||
  1367. rt2x00_rf(rt2x00dev, RF3052) ||
  1368. rt2x00_rf(rt2x00dev, RF3320))
  1369. rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
  1370. else
  1371. rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
  1372. /*
  1373. * Change BBP settings
  1374. */
  1375. rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
  1376. rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
  1377. rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
  1378. rt2800_bbp_write(rt2x00dev, 86, 0);
  1379. if (rf->channel <= 14) {
  1380. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
  1381. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  1382. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  1383. } else {
  1384. rt2800_bbp_write(rt2x00dev, 82, 0x84);
  1385. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  1386. }
  1387. } else {
  1388. rt2800_bbp_write(rt2x00dev, 82, 0xf2);
  1389. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
  1390. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  1391. else
  1392. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  1393. }
  1394. rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
  1395. rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
  1396. rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
  1397. rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
  1398. rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
  1399. tx_pin = 0;
  1400. /* Turn on unused PA or LNA when not using 1T or 1R */
  1401. if (rt2x00dev->default_ant.tx_chain_num == 2) {
  1402. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
  1403. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
  1404. }
  1405. /* Turn on unused PA or LNA when not using 1T or 1R */
  1406. if (rt2x00dev->default_ant.rx_chain_num == 2) {
  1407. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
  1408. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
  1409. }
  1410. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
  1411. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
  1412. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
  1413. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
  1414. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
  1415. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
  1416. rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
  1417. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  1418. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
  1419. rt2800_bbp_write(rt2x00dev, 4, bbp);
  1420. rt2800_bbp_read(rt2x00dev, 3, &bbp);
  1421. rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
  1422. rt2800_bbp_write(rt2x00dev, 3, bbp);
  1423. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
  1424. if (conf_is_ht40(conf)) {
  1425. rt2800_bbp_write(rt2x00dev, 69, 0x1a);
  1426. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  1427. rt2800_bbp_write(rt2x00dev, 73, 0x16);
  1428. } else {
  1429. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  1430. rt2800_bbp_write(rt2x00dev, 70, 0x08);
  1431. rt2800_bbp_write(rt2x00dev, 73, 0x11);
  1432. }
  1433. }
  1434. msleep(1);
  1435. /*
  1436. * Clear channel statistic counters
  1437. */
  1438. rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
  1439. rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
  1440. rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
  1441. }
  1442. static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
  1443. enum ieee80211_band band)
  1444. {
  1445. u16 eeprom;
  1446. u8 comp_en;
  1447. u8 comp_type;
  1448. int comp_value;
  1449. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
  1450. if (eeprom == 0xffff)
  1451. return 0;
  1452. if (band == IEEE80211_BAND_2GHZ) {
  1453. comp_en = rt2x00_get_field16(eeprom,
  1454. EEPROM_TXPOWER_DELTA_ENABLE_2G);
  1455. if (comp_en) {
  1456. comp_type = rt2x00_get_field16(eeprom,
  1457. EEPROM_TXPOWER_DELTA_TYPE_2G);
  1458. comp_value = rt2x00_get_field16(eeprom,
  1459. EEPROM_TXPOWER_DELTA_VALUE_2G);
  1460. if (!comp_type)
  1461. comp_value = -comp_value;
  1462. }
  1463. } else {
  1464. comp_en = rt2x00_get_field16(eeprom,
  1465. EEPROM_TXPOWER_DELTA_ENABLE_5G);
  1466. if (comp_en) {
  1467. comp_type = rt2x00_get_field16(eeprom,
  1468. EEPROM_TXPOWER_DELTA_TYPE_5G);
  1469. comp_value = rt2x00_get_field16(eeprom,
  1470. EEPROM_TXPOWER_DELTA_VALUE_5G);
  1471. if (!comp_type)
  1472. comp_value = -comp_value;
  1473. }
  1474. }
  1475. return comp_value;
  1476. }
  1477. static u8 rt2800_compesate_txpower(struct rt2x00_dev *rt2x00dev,
  1478. int is_rate_b,
  1479. enum ieee80211_band band,
  1480. int power_level,
  1481. u8 txpower)
  1482. {
  1483. u32 reg;
  1484. u16 eeprom;
  1485. u8 criterion;
  1486. u8 eirp_txpower;
  1487. u8 eirp_txpower_criterion;
  1488. u8 reg_limit;
  1489. int bw_comp = 0;
  1490. if (!((band == IEEE80211_BAND_5GHZ) && is_rate_b))
  1491. return txpower;
  1492. if (test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
  1493. bw_comp = rt2800_get_txpower_bw_comp(rt2x00dev, band);
  1494. if (test_bit(CONFIG_SUPPORT_POWER_LIMIT, &rt2x00dev->flags)) {
  1495. /*
  1496. * Check if eirp txpower exceed txpower_limit.
  1497. * We use OFDM 6M as criterion and its eirp txpower
  1498. * is stored at EEPROM_EIRP_MAX_TX_POWER.
  1499. * .11b data rate need add additional 4dbm
  1500. * when calculating eirp txpower.
  1501. */
  1502. rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
  1503. criterion = rt2x00_get_field32(reg, TX_PWR_CFG_0_6MBS);
  1504. rt2x00_eeprom_read(rt2x00dev,
  1505. EEPROM_EIRP_MAX_TX_POWER, &eeprom);
  1506. if (band == IEEE80211_BAND_2GHZ)
  1507. eirp_txpower_criterion = rt2x00_get_field16(eeprom,
  1508. EEPROM_EIRP_MAX_TX_POWER_2GHZ);
  1509. else
  1510. eirp_txpower_criterion = rt2x00_get_field16(eeprom,
  1511. EEPROM_EIRP_MAX_TX_POWER_5GHZ);
  1512. eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
  1513. (is_rate_b ? 4 : 0) + bw_comp;
  1514. reg_limit = (eirp_txpower > power_level) ?
  1515. (eirp_txpower - power_level) : 0;
  1516. } else
  1517. reg_limit = 0;
  1518. return txpower + bw_comp - reg_limit;
  1519. }
  1520. static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
  1521. struct ieee80211_conf *conf)
  1522. {
  1523. u8 txpower;
  1524. u16 eeprom;
  1525. int i, is_rate_b;
  1526. u32 reg;
  1527. u8 r1;
  1528. u32 offset;
  1529. enum ieee80211_band band = conf->channel->band;
  1530. int power_level = conf->power_level;
  1531. /*
  1532. * set to normal bbp tx power control mode: +/- 0dBm
  1533. */
  1534. rt2800_bbp_read(rt2x00dev, 1, &r1);
  1535. rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, 0);
  1536. rt2800_bbp_write(rt2x00dev, 1, r1);
  1537. offset = TX_PWR_CFG_0;
  1538. for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
  1539. /* just to be safe */
  1540. if (offset > TX_PWR_CFG_4)
  1541. break;
  1542. rt2800_register_read(rt2x00dev, offset, &reg);
  1543. /* read the next four txpower values */
  1544. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
  1545. &eeprom);
  1546. is_rate_b = i ? 0 : 1;
  1547. /*
  1548. * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
  1549. * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
  1550. * TX_PWR_CFG_4: unknown
  1551. */
  1552. txpower = rt2x00_get_field16(eeprom,
  1553. EEPROM_TXPOWER_BYRATE_RATE0);
  1554. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1555. power_level, txpower);
  1556. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
  1557. /*
  1558. * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
  1559. * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
  1560. * TX_PWR_CFG_4: unknown
  1561. */
  1562. txpower = rt2x00_get_field16(eeprom,
  1563. EEPROM_TXPOWER_BYRATE_RATE1);
  1564. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1565. power_level, txpower);
  1566. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
  1567. /*
  1568. * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
  1569. * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
  1570. * TX_PWR_CFG_4: unknown
  1571. */
  1572. txpower = rt2x00_get_field16(eeprom,
  1573. EEPROM_TXPOWER_BYRATE_RATE2);
  1574. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1575. power_level, txpower);
  1576. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
  1577. /*
  1578. * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
  1579. * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
  1580. * TX_PWR_CFG_4: unknown
  1581. */
  1582. txpower = rt2x00_get_field16(eeprom,
  1583. EEPROM_TXPOWER_BYRATE_RATE3);
  1584. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1585. power_level, txpower);
  1586. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
  1587. /* read the next four txpower values */
  1588. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
  1589. &eeprom);
  1590. is_rate_b = 0;
  1591. /*
  1592. * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
  1593. * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
  1594. * TX_PWR_CFG_4: unknown
  1595. */
  1596. txpower = rt2x00_get_field16(eeprom,
  1597. EEPROM_TXPOWER_BYRATE_RATE0);
  1598. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1599. power_level, txpower);
  1600. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
  1601. /*
  1602. * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
  1603. * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
  1604. * TX_PWR_CFG_4: unknown
  1605. */
  1606. txpower = rt2x00_get_field16(eeprom,
  1607. EEPROM_TXPOWER_BYRATE_RATE1);
  1608. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1609. power_level, txpower);
  1610. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
  1611. /*
  1612. * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
  1613. * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
  1614. * TX_PWR_CFG_4: unknown
  1615. */
  1616. txpower = rt2x00_get_field16(eeprom,
  1617. EEPROM_TXPOWER_BYRATE_RATE2);
  1618. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1619. power_level, txpower);
  1620. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
  1621. /*
  1622. * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
  1623. * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
  1624. * TX_PWR_CFG_4: unknown
  1625. */
  1626. txpower = rt2x00_get_field16(eeprom,
  1627. EEPROM_TXPOWER_BYRATE_RATE3);
  1628. txpower = rt2800_compesate_txpower(rt2x00dev, is_rate_b, band,
  1629. power_level, txpower);
  1630. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
  1631. rt2800_register_write(rt2x00dev, offset, reg);
  1632. /* next TX_PWR_CFG register */
  1633. offset += 4;
  1634. }
  1635. }
  1636. static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  1637. struct rt2x00lib_conf *libconf)
  1638. {
  1639. u32 reg;
  1640. rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
  1641. rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
  1642. libconf->conf->short_frame_max_tx_count);
  1643. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
  1644. libconf->conf->long_frame_max_tx_count);
  1645. rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
  1646. }
  1647. static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
  1648. struct rt2x00lib_conf *libconf)
  1649. {
  1650. enum dev_state state =
  1651. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  1652. STATE_SLEEP : STATE_AWAKE;
  1653. u32 reg;
  1654. if (state == STATE_SLEEP) {
  1655. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
  1656. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  1657. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
  1658. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
  1659. libconf->conf->listen_interval - 1);
  1660. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
  1661. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  1662. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  1663. } else {
  1664. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  1665. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
  1666. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
  1667. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
  1668. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  1669. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  1670. }
  1671. }
  1672. void rt2800_config(struct rt2x00_dev *rt2x00dev,
  1673. struct rt2x00lib_conf *libconf,
  1674. const unsigned int flags)
  1675. {
  1676. /* Always recalculate LNA gain before changing configuration */
  1677. rt2800_config_lna_gain(rt2x00dev, libconf);
  1678. if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
  1679. rt2800_config_channel(rt2x00dev, libconf->conf,
  1680. &libconf->rf, &libconf->channel);
  1681. rt2800_config_txpower(rt2x00dev, libconf->conf);
  1682. }
  1683. if (flags & IEEE80211_CONF_CHANGE_POWER)
  1684. rt2800_config_txpower(rt2x00dev, libconf->conf);
  1685. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  1686. rt2800_config_retry_limit(rt2x00dev, libconf);
  1687. if (flags & IEEE80211_CONF_CHANGE_PS)
  1688. rt2800_config_ps(rt2x00dev, libconf);
  1689. }
  1690. EXPORT_SYMBOL_GPL(rt2800_config);
  1691. /*
  1692. * Link tuning
  1693. */
  1694. void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
  1695. {
  1696. u32 reg;
  1697. /*
  1698. * Update FCS error count from register.
  1699. */
  1700. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  1701. qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
  1702. }
  1703. EXPORT_SYMBOL_GPL(rt2800_link_stats);
  1704. static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
  1705. {
  1706. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
  1707. if (rt2x00_rt(rt2x00dev, RT3070) ||
  1708. rt2x00_rt(rt2x00dev, RT3071) ||
  1709. rt2x00_rt(rt2x00dev, RT3090) ||
  1710. rt2x00_rt(rt2x00dev, RT3390))
  1711. return 0x1c + (2 * rt2x00dev->lna_gain);
  1712. else
  1713. return 0x2e + rt2x00dev->lna_gain;
  1714. }
  1715. if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
  1716. return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
  1717. else
  1718. return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
  1719. }
  1720. static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
  1721. struct link_qual *qual, u8 vgc_level)
  1722. {
  1723. if (qual->vgc_level != vgc_level) {
  1724. rt2800_bbp_write(rt2x00dev, 66, vgc_level);
  1725. qual->vgc_level = vgc_level;
  1726. qual->vgc_level_reg = vgc_level;
  1727. }
  1728. }
  1729. void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
  1730. {
  1731. rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
  1732. }
  1733. EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
  1734. void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
  1735. const u32 count)
  1736. {
  1737. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
  1738. return;
  1739. /*
  1740. * When RSSI is better then -80 increase VGC level with 0x10
  1741. */
  1742. rt2800_set_vgc(rt2x00dev, qual,
  1743. rt2800_get_default_vgc(rt2x00dev) +
  1744. ((qual->rssi > -80) * 0x10));
  1745. }
  1746. EXPORT_SYMBOL_GPL(rt2800_link_tuner);
  1747. /*
  1748. * Initialization functions.
  1749. */
  1750. static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
  1751. {
  1752. u32 reg;
  1753. u16 eeprom;
  1754. unsigned int i;
  1755. int ret;
  1756. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1757. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1758. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  1759. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1760. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  1761. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1762. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1763. ret = rt2800_drv_init_registers(rt2x00dev);
  1764. if (ret)
  1765. return ret;
  1766. rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
  1767. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
  1768. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
  1769. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
  1770. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
  1771. rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
  1772. rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
  1773. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
  1774. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
  1775. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
  1776. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
  1777. rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
  1778. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
  1779. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  1780. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  1781. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1782. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
  1783. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
  1784. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
  1785. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
  1786. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  1787. rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
  1788. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1789. rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
  1790. rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
  1791. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
  1792. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
  1793. rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
  1794. if (rt2x00_rt(rt2x00dev, RT3071) ||
  1795. rt2x00_rt(rt2x00dev, RT3090) ||
  1796. rt2x00_rt(rt2x00dev, RT3390)) {
  1797. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  1798. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
  1799. if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  1800. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
  1801. rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
  1802. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  1803. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
  1804. rt2800_register_write(rt2x00dev, TX_SW_CFG2,
  1805. 0x0000002c);
  1806. else
  1807. rt2800_register_write(rt2x00dev, TX_SW_CFG2,
  1808. 0x0000000f);
  1809. } else {
  1810. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
  1811. }
  1812. } else if (rt2x00_rt(rt2x00dev, RT3070)) {
  1813. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  1814. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
  1815. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
  1816. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
  1817. } else {
  1818. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  1819. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
  1820. }
  1821. } else if (rt2800_is_305x_soc(rt2x00dev)) {
  1822. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  1823. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
  1824. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000001f);
  1825. } else {
  1826. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
  1827. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  1828. }
  1829. rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
  1830. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
  1831. rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
  1832. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
  1833. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
  1834. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
  1835. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
  1836. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
  1837. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
  1838. rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
  1839. rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
  1840. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
  1841. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
  1842. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
  1843. rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
  1844. rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
  1845. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
  1846. if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
  1847. rt2x00_rt(rt2x00dev, RT2883) ||
  1848. rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
  1849. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
  1850. else
  1851. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
  1852. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
  1853. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
  1854. rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
  1855. rt2800_register_read(rt2x00dev, LED_CFG, &reg);
  1856. rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
  1857. rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
  1858. rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
  1859. rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
  1860. rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
  1861. rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
  1862. rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
  1863. rt2800_register_write(rt2x00dev, LED_CFG, reg);
  1864. rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
  1865. rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
  1866. rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
  1867. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
  1868. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
  1869. rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
  1870. rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
  1871. rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
  1872. rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
  1873. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  1874. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
  1875. rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
  1876. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
  1877. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
  1878. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
  1879. rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
  1880. rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
  1881. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  1882. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  1883. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
  1884. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
  1885. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
  1886. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1887. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1888. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1889. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1890. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1891. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1892. rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
  1893. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  1894. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  1895. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
  1896. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
  1897. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
  1898. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1899. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1900. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1901. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1902. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1903. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1904. rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
  1905. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  1906. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  1907. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
  1908. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
  1909. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
  1910. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1911. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1912. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1913. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1914. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1915. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1916. rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
  1917. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  1918. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  1919. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
  1920. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
  1921. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
  1922. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1923. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1924. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1925. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1926. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1927. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1928. rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
  1929. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  1930. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  1931. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
  1932. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
  1933. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
  1934. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1935. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1936. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1937. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1938. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1939. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1940. rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
  1941. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  1942. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  1943. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
  1944. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
  1945. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
  1946. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1947. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1948. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1949. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1950. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1951. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1952. rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
  1953. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  1954. if (rt2x00_is_usb(rt2x00dev)) {
  1955. rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
  1956. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1957. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1958. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  1959. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1960. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  1961. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
  1962. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
  1963. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
  1964. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
  1965. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
  1966. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1967. }
  1968. /*
  1969. * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
  1970. * although it is reserved.
  1971. */
  1972. rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
  1973. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
  1974. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
  1975. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
  1976. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
  1977. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
  1978. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
  1979. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
  1980. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
  1981. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
  1982. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
  1983. rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
  1984. rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
  1985. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  1986. rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
  1987. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
  1988. IEEE80211_MAX_RTS_THRESHOLD);
  1989. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
  1990. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  1991. rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
  1992. /*
  1993. * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
  1994. * time should be set to 16. However, the original Ralink driver uses
  1995. * 16 for both and indeed using a value of 10 for CCK SIFS results in
  1996. * connection problems with 11g + CTS protection. Hence, use the same
  1997. * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
  1998. */
  1999. rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
  2000. rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
  2001. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
  2002. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
  2003. rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
  2004. rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
  2005. rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
  2006. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  2007. /*
  2008. * ASIC will keep garbage value after boot, clear encryption keys.
  2009. */
  2010. for (i = 0; i < 4; i++)
  2011. rt2800_register_write(rt2x00dev,
  2012. SHARED_KEY_MODE_ENTRY(i), 0);
  2013. for (i = 0; i < 256; i++) {
  2014. static const u32 wcid[2] = { 0xffffffff, 0x00ffffff };
  2015. rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
  2016. wcid, sizeof(wcid));
  2017. rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
  2018. rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
  2019. }
  2020. /*
  2021. * Clear all beacons
  2022. */
  2023. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
  2024. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
  2025. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
  2026. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
  2027. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
  2028. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
  2029. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
  2030. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
  2031. if (rt2x00_is_usb(rt2x00dev)) {
  2032. rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
  2033. rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
  2034. rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
  2035. } else if (rt2x00_is_pcie(rt2x00dev)) {
  2036. rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
  2037. rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
  2038. rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
  2039. }
  2040. rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
  2041. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
  2042. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
  2043. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
  2044. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
  2045. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
  2046. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
  2047. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
  2048. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
  2049. rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
  2050. rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
  2051. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
  2052. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
  2053. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
  2054. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
  2055. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
  2056. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
  2057. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
  2058. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
  2059. rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
  2060. rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
  2061. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
  2062. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
  2063. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
  2064. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
  2065. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
  2066. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
  2067. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
  2068. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
  2069. rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
  2070. rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
  2071. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
  2072. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
  2073. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
  2074. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
  2075. rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
  2076. /*
  2077. * Do not force the BA window size, we use the TXWI to set it
  2078. */
  2079. rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
  2080. rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
  2081. rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
  2082. rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
  2083. /*
  2084. * We must clear the error counters.
  2085. * These registers are cleared on read,
  2086. * so we may pass a useless variable to store the value.
  2087. */
  2088. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  2089. rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
  2090. rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
  2091. rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
  2092. rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
  2093. rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
  2094. /*
  2095. * Setup leadtime for pre tbtt interrupt to 6ms
  2096. */
  2097. rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
  2098. rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
  2099. rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
  2100. /*
  2101. * Set up channel statistics timer
  2102. */
  2103. rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
  2104. rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
  2105. rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
  2106. rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
  2107. rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
  2108. rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
  2109. rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
  2110. return 0;
  2111. }
  2112. static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
  2113. {
  2114. unsigned int i;
  2115. u32 reg;
  2116. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  2117. rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
  2118. if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
  2119. return 0;
  2120. udelay(REGISTER_BUSY_DELAY);
  2121. }
  2122. ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
  2123. return -EACCES;
  2124. }
  2125. static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  2126. {
  2127. unsigned int i;
  2128. u8 value;
  2129. /*
  2130. * BBP was enabled after firmware was loaded,
  2131. * but we need to reactivate it now.
  2132. */
  2133. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  2134. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  2135. msleep(1);
  2136. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  2137. rt2800_bbp_read(rt2x00dev, 0, &value);
  2138. if ((value != 0xff) && (value != 0x00))
  2139. return 0;
  2140. udelay(REGISTER_BUSY_DELAY);
  2141. }
  2142. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  2143. return -EACCES;
  2144. }
  2145. static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
  2146. {
  2147. unsigned int i;
  2148. u16 eeprom;
  2149. u8 reg_id;
  2150. u8 value;
  2151. if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
  2152. rt2800_wait_bbp_ready(rt2x00dev)))
  2153. return -EACCES;
  2154. if (rt2800_is_305x_soc(rt2x00dev))
  2155. rt2800_bbp_write(rt2x00dev, 31, 0x08);
  2156. rt2800_bbp_write(rt2x00dev, 65, 0x2c);
  2157. rt2800_bbp_write(rt2x00dev, 66, 0x38);
  2158. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
  2159. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  2160. rt2800_bbp_write(rt2x00dev, 73, 0x12);
  2161. } else {
  2162. rt2800_bbp_write(rt2x00dev, 69, 0x12);
  2163. rt2800_bbp_write(rt2x00dev, 73, 0x10);
  2164. }
  2165. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  2166. if (rt2x00_rt(rt2x00dev, RT3070) ||
  2167. rt2x00_rt(rt2x00dev, RT3071) ||
  2168. rt2x00_rt(rt2x00dev, RT3090) ||
  2169. rt2x00_rt(rt2x00dev, RT3390)) {
  2170. rt2800_bbp_write(rt2x00dev, 79, 0x13);
  2171. rt2800_bbp_write(rt2x00dev, 80, 0x05);
  2172. rt2800_bbp_write(rt2x00dev, 81, 0x33);
  2173. } else if (rt2800_is_305x_soc(rt2x00dev)) {
  2174. rt2800_bbp_write(rt2x00dev, 78, 0x0e);
  2175. rt2800_bbp_write(rt2x00dev, 80, 0x08);
  2176. } else {
  2177. rt2800_bbp_write(rt2x00dev, 81, 0x37);
  2178. }
  2179. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  2180. rt2800_bbp_write(rt2x00dev, 83, 0x6a);
  2181. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
  2182. rt2800_bbp_write(rt2x00dev, 84, 0x19);
  2183. else
  2184. rt2800_bbp_write(rt2x00dev, 84, 0x99);
  2185. rt2800_bbp_write(rt2x00dev, 86, 0x00);
  2186. rt2800_bbp_write(rt2x00dev, 91, 0x04);
  2187. rt2800_bbp_write(rt2x00dev, 92, 0x00);
  2188. if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
  2189. rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
  2190. rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
  2191. rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
  2192. rt2800_is_305x_soc(rt2x00dev))
  2193. rt2800_bbp_write(rt2x00dev, 103, 0xc0);
  2194. else
  2195. rt2800_bbp_write(rt2x00dev, 103, 0x00);
  2196. if (rt2800_is_305x_soc(rt2x00dev))
  2197. rt2800_bbp_write(rt2x00dev, 105, 0x01);
  2198. else
  2199. rt2800_bbp_write(rt2x00dev, 105, 0x05);
  2200. rt2800_bbp_write(rt2x00dev, 106, 0x35);
  2201. if (rt2x00_rt(rt2x00dev, RT3071) ||
  2202. rt2x00_rt(rt2x00dev, RT3090) ||
  2203. rt2x00_rt(rt2x00dev, RT3390)) {
  2204. rt2800_bbp_read(rt2x00dev, 138, &value);
  2205. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  2206. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
  2207. value |= 0x20;
  2208. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
  2209. value &= ~0x02;
  2210. rt2800_bbp_write(rt2x00dev, 138, value);
  2211. }
  2212. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  2213. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  2214. if (eeprom != 0xffff && eeprom != 0x0000) {
  2215. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  2216. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  2217. rt2800_bbp_write(rt2x00dev, reg_id, value);
  2218. }
  2219. }
  2220. return 0;
  2221. }
  2222. static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
  2223. bool bw40, u8 rfcsr24, u8 filter_target)
  2224. {
  2225. unsigned int i;
  2226. u8 bbp;
  2227. u8 rfcsr;
  2228. u8 passband;
  2229. u8 stopband;
  2230. u8 overtuned = 0;
  2231. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  2232. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  2233. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
  2234. rt2800_bbp_write(rt2x00dev, 4, bbp);
  2235. rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
  2236. rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
  2237. rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
  2238. rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
  2239. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
  2240. rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
  2241. /*
  2242. * Set power & frequency of passband test tone
  2243. */
  2244. rt2800_bbp_write(rt2x00dev, 24, 0);
  2245. for (i = 0; i < 100; i++) {
  2246. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  2247. msleep(1);
  2248. rt2800_bbp_read(rt2x00dev, 55, &passband);
  2249. if (passband)
  2250. break;
  2251. }
  2252. /*
  2253. * Set power & frequency of stopband test tone
  2254. */
  2255. rt2800_bbp_write(rt2x00dev, 24, 0x06);
  2256. for (i = 0; i < 100; i++) {
  2257. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  2258. msleep(1);
  2259. rt2800_bbp_read(rt2x00dev, 55, &stopband);
  2260. if ((passband - stopband) <= filter_target) {
  2261. rfcsr24++;
  2262. overtuned += ((passband - stopband) == filter_target);
  2263. } else
  2264. break;
  2265. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  2266. }
  2267. rfcsr24 -= !!overtuned;
  2268. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  2269. return rfcsr24;
  2270. }
  2271. static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
  2272. {
  2273. u8 rfcsr;
  2274. u8 bbp;
  2275. u32 reg;
  2276. u16 eeprom;
  2277. if (!rt2x00_rt(rt2x00dev, RT3070) &&
  2278. !rt2x00_rt(rt2x00dev, RT3071) &&
  2279. !rt2x00_rt(rt2x00dev, RT3090) &&
  2280. !rt2x00_rt(rt2x00dev, RT3390) &&
  2281. !rt2800_is_305x_soc(rt2x00dev))
  2282. return 0;
  2283. /*
  2284. * Init RF calibration.
  2285. */
  2286. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  2287. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  2288. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  2289. msleep(1);
  2290. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
  2291. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  2292. if (rt2x00_rt(rt2x00dev, RT3070) ||
  2293. rt2x00_rt(rt2x00dev, RT3071) ||
  2294. rt2x00_rt(rt2x00dev, RT3090)) {
  2295. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  2296. rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
  2297. rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
  2298. rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
  2299. rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
  2300. rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
  2301. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  2302. rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
  2303. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  2304. rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
  2305. rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
  2306. rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
  2307. rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
  2308. rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
  2309. rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
  2310. rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
  2311. rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
  2312. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  2313. rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
  2314. } else if (rt2x00_rt(rt2x00dev, RT3390)) {
  2315. rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
  2316. rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
  2317. rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
  2318. rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
  2319. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  2320. rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
  2321. rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
  2322. rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
  2323. rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
  2324. rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
  2325. rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
  2326. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  2327. rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
  2328. rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
  2329. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  2330. rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
  2331. rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
  2332. rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
  2333. rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
  2334. rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
  2335. rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
  2336. rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
  2337. rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
  2338. rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
  2339. rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
  2340. rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
  2341. rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
  2342. rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
  2343. rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
  2344. rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
  2345. rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
  2346. rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
  2347. } else if (rt2800_is_305x_soc(rt2x00dev)) {
  2348. rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
  2349. rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
  2350. rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
  2351. rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
  2352. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  2353. rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
  2354. rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
  2355. rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
  2356. rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
  2357. rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
  2358. rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
  2359. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  2360. rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
  2361. rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
  2362. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  2363. rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
  2364. rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
  2365. rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
  2366. rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
  2367. rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
  2368. rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
  2369. rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
  2370. rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
  2371. rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
  2372. rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
  2373. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  2374. rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
  2375. rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
  2376. rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
  2377. rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
  2378. rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
  2379. rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
  2380. return 0;
  2381. }
  2382. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
  2383. rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
  2384. rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
  2385. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
  2386. rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
  2387. } else if (rt2x00_rt(rt2x00dev, RT3071) ||
  2388. rt2x00_rt(rt2x00dev, RT3090)) {
  2389. rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
  2390. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  2391. rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
  2392. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  2393. rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
  2394. rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
  2395. if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  2396. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
  2397. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  2398. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
  2399. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
  2400. else
  2401. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
  2402. }
  2403. rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
  2404. rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
  2405. rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
  2406. rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
  2407. } else if (rt2x00_rt(rt2x00dev, RT3390)) {
  2408. rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
  2409. rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
  2410. rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
  2411. }
  2412. /*
  2413. * Set RX Filter calibration for 20MHz and 40MHz
  2414. */
  2415. if (rt2x00_rt(rt2x00dev, RT3070)) {
  2416. rt2x00dev->calibration[0] =
  2417. rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
  2418. rt2x00dev->calibration[1] =
  2419. rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
  2420. } else if (rt2x00_rt(rt2x00dev, RT3071) ||
  2421. rt2x00_rt(rt2x00dev, RT3090) ||
  2422. rt2x00_rt(rt2x00dev, RT3390)) {
  2423. rt2x00dev->calibration[0] =
  2424. rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
  2425. rt2x00dev->calibration[1] =
  2426. rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
  2427. }
  2428. /*
  2429. * Set back to initial state
  2430. */
  2431. rt2800_bbp_write(rt2x00dev, 24, 0);
  2432. rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
  2433. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
  2434. rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
  2435. /*
  2436. * set BBP back to BW20
  2437. */
  2438. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  2439. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
  2440. rt2800_bbp_write(rt2x00dev, 4, bbp);
  2441. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
  2442. rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  2443. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
  2444. rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
  2445. rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
  2446. rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
  2447. rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
  2448. rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
  2449. rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
  2450. rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
  2451. if (rt2x00_rt(rt2x00dev, RT3070) ||
  2452. rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  2453. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
  2454. rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
  2455. if (!test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
  2456. rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
  2457. }
  2458. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
  2459. if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
  2460. rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
  2461. rt2x00_get_field16(eeprom,
  2462. EEPROM_TXMIXER_GAIN_BG_VAL));
  2463. rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
  2464. if (rt2x00_rt(rt2x00dev, RT3090)) {
  2465. rt2800_bbp_read(rt2x00dev, 138, &bbp);
  2466. /* Turn off unused DAC1 and ADC1 to reduce power consumption */
  2467. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  2468. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
  2469. rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
  2470. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
  2471. rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
  2472. rt2800_bbp_write(rt2x00dev, 138, bbp);
  2473. }
  2474. if (rt2x00_rt(rt2x00dev, RT3071) ||
  2475. rt2x00_rt(rt2x00dev, RT3090) ||
  2476. rt2x00_rt(rt2x00dev, RT3390)) {
  2477. rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
  2478. rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
  2479. rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
  2480. rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
  2481. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
  2482. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
  2483. rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
  2484. rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
  2485. rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
  2486. rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
  2487. rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
  2488. rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
  2489. rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
  2490. rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
  2491. rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
  2492. rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
  2493. }
  2494. if (rt2x00_rt(rt2x00dev, RT3070)) {
  2495. rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
  2496. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
  2497. rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
  2498. else
  2499. rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
  2500. rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
  2501. rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
  2502. rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
  2503. rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
  2504. }
  2505. return 0;
  2506. }
  2507. int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
  2508. {
  2509. u32 reg;
  2510. u16 word;
  2511. /*
  2512. * Initialize all registers.
  2513. */
  2514. if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
  2515. rt2800_init_registers(rt2x00dev) ||
  2516. rt2800_init_bbp(rt2x00dev) ||
  2517. rt2800_init_rfcsr(rt2x00dev)))
  2518. return -EIO;
  2519. /*
  2520. * Send signal to firmware during boot time.
  2521. */
  2522. rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
  2523. if (rt2x00_is_usb(rt2x00dev) &&
  2524. (rt2x00_rt(rt2x00dev, RT3070) ||
  2525. rt2x00_rt(rt2x00dev, RT3071) ||
  2526. rt2x00_rt(rt2x00dev, RT3572))) {
  2527. udelay(200);
  2528. rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
  2529. udelay(10);
  2530. }
  2531. /*
  2532. * Enable RX.
  2533. */
  2534. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  2535. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  2536. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  2537. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  2538. udelay(50);
  2539. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  2540. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
  2541. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
  2542. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
  2543. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  2544. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  2545. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  2546. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  2547. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  2548. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  2549. /*
  2550. * Initialize LED control
  2551. */
  2552. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
  2553. rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
  2554. word & 0xff, (word >> 8) & 0xff);
  2555. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
  2556. rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
  2557. word & 0xff, (word >> 8) & 0xff);
  2558. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
  2559. rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
  2560. word & 0xff, (word >> 8) & 0xff);
  2561. return 0;
  2562. }
  2563. EXPORT_SYMBOL_GPL(rt2800_enable_radio);
  2564. void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
  2565. {
  2566. u32 reg;
  2567. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  2568. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  2569. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  2570. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  2571. /* Wait for DMA, ignore error */
  2572. rt2800_wait_wpdma_ready(rt2x00dev);
  2573. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  2574. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
  2575. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  2576. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  2577. }
  2578. EXPORT_SYMBOL_GPL(rt2800_disable_radio);
  2579. int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
  2580. {
  2581. u32 reg;
  2582. rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
  2583. return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
  2584. }
  2585. EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
  2586. static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
  2587. {
  2588. u32 reg;
  2589. mutex_lock(&rt2x00dev->csr_mutex);
  2590. rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
  2591. rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
  2592. rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
  2593. rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
  2594. rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
  2595. /* Wait until the EEPROM has been loaded */
  2596. rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
  2597. /* Apparently the data is read from end to start */
  2598. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
  2599. (u32 *)&rt2x00dev->eeprom[i]);
  2600. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
  2601. (u32 *)&rt2x00dev->eeprom[i + 2]);
  2602. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
  2603. (u32 *)&rt2x00dev->eeprom[i + 4]);
  2604. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
  2605. (u32 *)&rt2x00dev->eeprom[i + 6]);
  2606. mutex_unlock(&rt2x00dev->csr_mutex);
  2607. }
  2608. void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  2609. {
  2610. unsigned int i;
  2611. for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
  2612. rt2800_efuse_read(rt2x00dev, i);
  2613. }
  2614. EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
  2615. int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  2616. {
  2617. u16 word;
  2618. u8 *mac;
  2619. u8 default_lna_gain;
  2620. /*
  2621. * Start validation of the data that has been read.
  2622. */
  2623. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  2624. if (!is_valid_ether_addr(mac)) {
  2625. random_ether_addr(mac);
  2626. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  2627. }
  2628. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
  2629. if (word == 0xffff) {
  2630. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
  2631. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
  2632. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
  2633. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
  2634. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  2635. } else if (rt2x00_rt(rt2x00dev, RT2860) ||
  2636. rt2x00_rt(rt2x00dev, RT2872)) {
  2637. /*
  2638. * There is a max of 2 RX streams for RT28x0 series
  2639. */
  2640. if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
  2641. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
  2642. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
  2643. }
  2644. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
  2645. if (word == 0xffff) {
  2646. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
  2647. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
  2648. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
  2649. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
  2650. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
  2651. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
  2652. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
  2653. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
  2654. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
  2655. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
  2656. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
  2657. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
  2658. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
  2659. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
  2660. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
  2661. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
  2662. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  2663. }
  2664. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  2665. if ((word & 0x00ff) == 0x00ff) {
  2666. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  2667. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2668. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  2669. }
  2670. if ((word & 0xff00) == 0xff00) {
  2671. rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
  2672. LED_MODE_TXRX_ACTIVITY);
  2673. rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
  2674. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2675. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
  2676. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
  2677. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
  2678. EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
  2679. }
  2680. /*
  2681. * During the LNA validation we are going to use
  2682. * lna0 as correct value. Note that EEPROM_LNA
  2683. * is never validated.
  2684. */
  2685. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
  2686. default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
  2687. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
  2688. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
  2689. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
  2690. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
  2691. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
  2692. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
  2693. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
  2694. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
  2695. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
  2696. if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
  2697. rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
  2698. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
  2699. default_lna_gain);
  2700. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
  2701. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
  2702. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
  2703. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
  2704. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
  2705. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
  2706. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
  2707. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
  2708. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
  2709. rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
  2710. if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
  2711. rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
  2712. rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
  2713. default_lna_gain);
  2714. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
  2715. return 0;
  2716. }
  2717. EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
  2718. int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
  2719. {
  2720. u32 reg;
  2721. u16 value;
  2722. u16 eeprom;
  2723. /*
  2724. * Read EEPROM word for configuration.
  2725. */
  2726. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  2727. /*
  2728. * Identify RF chipset.
  2729. */
  2730. value = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
  2731. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  2732. rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
  2733. value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
  2734. if (!rt2x00_rt(rt2x00dev, RT2860) &&
  2735. !rt2x00_rt(rt2x00dev, RT2872) &&
  2736. !rt2x00_rt(rt2x00dev, RT2883) &&
  2737. !rt2x00_rt(rt2x00dev, RT3070) &&
  2738. !rt2x00_rt(rt2x00dev, RT3071) &&
  2739. !rt2x00_rt(rt2x00dev, RT3090) &&
  2740. !rt2x00_rt(rt2x00dev, RT3390) &&
  2741. !rt2x00_rt(rt2x00dev, RT3572)) {
  2742. ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
  2743. return -ENODEV;
  2744. }
  2745. if (!rt2x00_rf(rt2x00dev, RF2820) &&
  2746. !rt2x00_rf(rt2x00dev, RF2850) &&
  2747. !rt2x00_rf(rt2x00dev, RF2720) &&
  2748. !rt2x00_rf(rt2x00dev, RF2750) &&
  2749. !rt2x00_rf(rt2x00dev, RF3020) &&
  2750. !rt2x00_rf(rt2x00dev, RF2020) &&
  2751. !rt2x00_rf(rt2x00dev, RF3021) &&
  2752. !rt2x00_rf(rt2x00dev, RF3022) &&
  2753. !rt2x00_rf(rt2x00dev, RF3052) &&
  2754. !rt2x00_rf(rt2x00dev, RF3320)) {
  2755. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  2756. return -ENODEV;
  2757. }
  2758. /*
  2759. * Identify default antenna configuration.
  2760. */
  2761. rt2x00dev->default_ant.tx_chain_num =
  2762. rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
  2763. rt2x00dev->default_ant.rx_chain_num =
  2764. rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
  2765. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  2766. if (rt2x00_rt(rt2x00dev, RT3070) ||
  2767. rt2x00_rt(rt2x00dev, RT3090) ||
  2768. rt2x00_rt(rt2x00dev, RT3390)) {
  2769. value = rt2x00_get_field16(eeprom,
  2770. EEPROM_NIC_CONF1_ANT_DIVERSITY);
  2771. switch (value) {
  2772. case 0:
  2773. case 1:
  2774. case 2:
  2775. rt2x00dev->default_ant.tx = ANTENNA_A;
  2776. rt2x00dev->default_ant.rx = ANTENNA_A;
  2777. break;
  2778. case 3:
  2779. rt2x00dev->default_ant.tx = ANTENNA_A;
  2780. rt2x00dev->default_ant.rx = ANTENNA_B;
  2781. break;
  2782. }
  2783. } else {
  2784. rt2x00dev->default_ant.tx = ANTENNA_A;
  2785. rt2x00dev->default_ant.rx = ANTENNA_A;
  2786. }
  2787. /*
  2788. * Read frequency offset and RF programming sequence.
  2789. */
  2790. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  2791. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  2792. /*
  2793. * Read external LNA informations.
  2794. */
  2795. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  2796. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
  2797. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  2798. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
  2799. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  2800. /*
  2801. * Detect if this device has an hardware controlled radio.
  2802. */
  2803. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
  2804. __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
  2805. /*
  2806. * Store led settings, for correct led behaviour.
  2807. */
  2808. #ifdef CONFIG_RT2X00_LIB_LEDS
  2809. rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  2810. rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  2811. rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
  2812. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
  2813. #endif /* CONFIG_RT2X00_LIB_LEDS */
  2814. /*
  2815. * Check if support EIRP tx power limit feature.
  2816. */
  2817. rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
  2818. if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
  2819. EIRP_MAX_TX_POWER_LIMIT)
  2820. __set_bit(CONFIG_SUPPORT_POWER_LIMIT, &rt2x00dev->flags);
  2821. return 0;
  2822. }
  2823. EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
  2824. /*
  2825. * RF value list for rt28xx
  2826. * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
  2827. */
  2828. static const struct rf_channel rf_vals[] = {
  2829. { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
  2830. { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
  2831. { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
  2832. { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
  2833. { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
  2834. { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
  2835. { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
  2836. { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
  2837. { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
  2838. { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
  2839. { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
  2840. { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
  2841. { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
  2842. { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
  2843. /* 802.11 UNI / HyperLan 2 */
  2844. { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
  2845. { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
  2846. { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
  2847. { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
  2848. { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
  2849. { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
  2850. { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
  2851. { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
  2852. { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
  2853. { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
  2854. { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
  2855. { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
  2856. /* 802.11 HyperLan 2 */
  2857. { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
  2858. { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
  2859. { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
  2860. { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
  2861. { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
  2862. { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
  2863. { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
  2864. { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
  2865. { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
  2866. { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
  2867. { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
  2868. { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
  2869. { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
  2870. { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
  2871. { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
  2872. { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
  2873. /* 802.11 UNII */
  2874. { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
  2875. { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
  2876. { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
  2877. { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
  2878. { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
  2879. { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
  2880. { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
  2881. { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
  2882. { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
  2883. { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
  2884. { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
  2885. /* 802.11 Japan */
  2886. { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
  2887. { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
  2888. { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
  2889. { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
  2890. { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
  2891. { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
  2892. { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
  2893. };
  2894. /*
  2895. * RF value list for rt3xxx
  2896. * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
  2897. */
  2898. static const struct rf_channel rf_vals_3x[] = {
  2899. {1, 241, 2, 2 },
  2900. {2, 241, 2, 7 },
  2901. {3, 242, 2, 2 },
  2902. {4, 242, 2, 7 },
  2903. {5, 243, 2, 2 },
  2904. {6, 243, 2, 7 },
  2905. {7, 244, 2, 2 },
  2906. {8, 244, 2, 7 },
  2907. {9, 245, 2, 2 },
  2908. {10, 245, 2, 7 },
  2909. {11, 246, 2, 2 },
  2910. {12, 246, 2, 7 },
  2911. {13, 247, 2, 2 },
  2912. {14, 248, 2, 4 },
  2913. /* 802.11 UNI / HyperLan 2 */
  2914. {36, 0x56, 0, 4},
  2915. {38, 0x56, 0, 6},
  2916. {40, 0x56, 0, 8},
  2917. {44, 0x57, 0, 0},
  2918. {46, 0x57, 0, 2},
  2919. {48, 0x57, 0, 4},
  2920. {52, 0x57, 0, 8},
  2921. {54, 0x57, 0, 10},
  2922. {56, 0x58, 0, 0},
  2923. {60, 0x58, 0, 4},
  2924. {62, 0x58, 0, 6},
  2925. {64, 0x58, 0, 8},
  2926. /* 802.11 HyperLan 2 */
  2927. {100, 0x5b, 0, 8},
  2928. {102, 0x5b, 0, 10},
  2929. {104, 0x5c, 0, 0},
  2930. {108, 0x5c, 0, 4},
  2931. {110, 0x5c, 0, 6},
  2932. {112, 0x5c, 0, 8},
  2933. {116, 0x5d, 0, 0},
  2934. {118, 0x5d, 0, 2},
  2935. {120, 0x5d, 0, 4},
  2936. {124, 0x5d, 0, 8},
  2937. {126, 0x5d, 0, 10},
  2938. {128, 0x5e, 0, 0},
  2939. {132, 0x5e, 0, 4},
  2940. {134, 0x5e, 0, 6},
  2941. {136, 0x5e, 0, 8},
  2942. {140, 0x5f, 0, 0},
  2943. /* 802.11 UNII */
  2944. {149, 0x5f, 0, 9},
  2945. {151, 0x5f, 0, 11},
  2946. {153, 0x60, 0, 1},
  2947. {157, 0x60, 0, 5},
  2948. {159, 0x60, 0, 7},
  2949. {161, 0x60, 0, 9},
  2950. {165, 0x61, 0, 1},
  2951. {167, 0x61, 0, 3},
  2952. {169, 0x61, 0, 5},
  2953. {171, 0x61, 0, 7},
  2954. {173, 0x61, 0, 9},
  2955. };
  2956. int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  2957. {
  2958. struct hw_mode_spec *spec = &rt2x00dev->spec;
  2959. struct channel_info *info;
  2960. char *default_power1;
  2961. char *default_power2;
  2962. unsigned int i;
  2963. u16 eeprom;
  2964. /*
  2965. * Disable powersaving as default on PCI devices.
  2966. */
  2967. if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
  2968. rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  2969. /*
  2970. * Initialize all hw fields.
  2971. */
  2972. rt2x00dev->hw->flags =
  2973. IEEE80211_HW_SIGNAL_DBM |
  2974. IEEE80211_HW_SUPPORTS_PS |
  2975. IEEE80211_HW_PS_NULLFUNC_STACK |
  2976. IEEE80211_HW_AMPDU_AGGREGATION;
  2977. /*
  2978. * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
  2979. * unless we are capable of sending the buffered frames out after the
  2980. * DTIM transmission using rt2x00lib_beacondone. This will send out
  2981. * multicast and broadcast traffic immediately instead of buffering it
  2982. * infinitly and thus dropping it after some time.
  2983. */
  2984. if (!rt2x00_is_usb(rt2x00dev))
  2985. rt2x00dev->hw->flags |=
  2986. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  2987. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  2988. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  2989. rt2x00_eeprom_addr(rt2x00dev,
  2990. EEPROM_MAC_ADDR_0));
  2991. /*
  2992. * As rt2800 has a global fallback table we cannot specify
  2993. * more then one tx rate per frame but since the hw will
  2994. * try several rates (based on the fallback table) we should
  2995. * initialize max_report_rates to the maximum number of rates
  2996. * we are going to try. Otherwise mac80211 will truncate our
  2997. * reported tx rates and the rc algortihm will end up with
  2998. * incorrect data.
  2999. */
  3000. rt2x00dev->hw->max_rates = 1;
  3001. rt2x00dev->hw->max_report_rates = 7;
  3002. rt2x00dev->hw->max_rate_tries = 1;
  3003. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  3004. /*
  3005. * Initialize hw_mode information.
  3006. */
  3007. spec->supported_bands = SUPPORT_BAND_2GHZ;
  3008. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  3009. if (rt2x00_rf(rt2x00dev, RF2820) ||
  3010. rt2x00_rf(rt2x00dev, RF2720)) {
  3011. spec->num_channels = 14;
  3012. spec->channels = rf_vals;
  3013. } else if (rt2x00_rf(rt2x00dev, RF2850) ||
  3014. rt2x00_rf(rt2x00dev, RF2750)) {
  3015. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  3016. spec->num_channels = ARRAY_SIZE(rf_vals);
  3017. spec->channels = rf_vals;
  3018. } else if (rt2x00_rf(rt2x00dev, RF3020) ||
  3019. rt2x00_rf(rt2x00dev, RF2020) ||
  3020. rt2x00_rf(rt2x00dev, RF3021) ||
  3021. rt2x00_rf(rt2x00dev, RF3022) ||
  3022. rt2x00_rf(rt2x00dev, RF3320)) {
  3023. spec->num_channels = 14;
  3024. spec->channels = rf_vals_3x;
  3025. } else if (rt2x00_rf(rt2x00dev, RF3052)) {
  3026. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  3027. spec->num_channels = ARRAY_SIZE(rf_vals_3x);
  3028. spec->channels = rf_vals_3x;
  3029. }
  3030. /*
  3031. * Initialize HT information.
  3032. */
  3033. if (!rt2x00_rf(rt2x00dev, RF2020))
  3034. spec->ht.ht_supported = true;
  3035. else
  3036. spec->ht.ht_supported = false;
  3037. spec->ht.cap =
  3038. IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  3039. IEEE80211_HT_CAP_GRN_FLD |
  3040. IEEE80211_HT_CAP_SGI_20 |
  3041. IEEE80211_HT_CAP_SGI_40;
  3042. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
  3043. spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
  3044. spec->ht.cap |=
  3045. rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
  3046. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  3047. spec->ht.ampdu_factor = 3;
  3048. spec->ht.ampdu_density = 4;
  3049. spec->ht.mcs.tx_params =
  3050. IEEE80211_HT_MCS_TX_DEFINED |
  3051. IEEE80211_HT_MCS_TX_RX_DIFF |
  3052. ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
  3053. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  3054. switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
  3055. case 3:
  3056. spec->ht.mcs.rx_mask[2] = 0xff;
  3057. case 2:
  3058. spec->ht.mcs.rx_mask[1] = 0xff;
  3059. case 1:
  3060. spec->ht.mcs.rx_mask[0] = 0xff;
  3061. spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
  3062. break;
  3063. }
  3064. /*
  3065. * Create channel information array
  3066. */
  3067. info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
  3068. if (!info)
  3069. return -ENOMEM;
  3070. spec->channels_info = info;
  3071. default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
  3072. default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
  3073. for (i = 0; i < 14; i++) {
  3074. info[i].default_power1 = default_power1[i];
  3075. info[i].default_power2 = default_power2[i];
  3076. }
  3077. if (spec->num_channels > 14) {
  3078. default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
  3079. default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
  3080. for (i = 14; i < spec->num_channels; i++) {
  3081. info[i].default_power1 = default_power1[i];
  3082. info[i].default_power2 = default_power2[i];
  3083. }
  3084. }
  3085. return 0;
  3086. }
  3087. EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
  3088. /*
  3089. * IEEE80211 stack callback functions.
  3090. */
  3091. void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
  3092. u16 *iv16)
  3093. {
  3094. struct rt2x00_dev *rt2x00dev = hw->priv;
  3095. struct mac_iveiv_entry iveiv_entry;
  3096. u32 offset;
  3097. offset = MAC_IVEIV_ENTRY(hw_key_idx);
  3098. rt2800_register_multiread(rt2x00dev, offset,
  3099. &iveiv_entry, sizeof(iveiv_entry));
  3100. memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
  3101. memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
  3102. }
  3103. EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
  3104. int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  3105. {
  3106. struct rt2x00_dev *rt2x00dev = hw->priv;
  3107. u32 reg;
  3108. bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
  3109. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  3110. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
  3111. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  3112. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  3113. rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
  3114. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  3115. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  3116. rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
  3117. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  3118. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  3119. rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
  3120. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  3121. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  3122. rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
  3123. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  3124. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  3125. rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
  3126. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  3127. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  3128. rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
  3129. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  3130. return 0;
  3131. }
  3132. EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
  3133. int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
  3134. const struct ieee80211_tx_queue_params *params)
  3135. {
  3136. struct rt2x00_dev *rt2x00dev = hw->priv;
  3137. struct data_queue *queue;
  3138. struct rt2x00_field32 field;
  3139. int retval;
  3140. u32 reg;
  3141. u32 offset;
  3142. /*
  3143. * First pass the configuration through rt2x00lib, that will
  3144. * update the queue settings and validate the input. After that
  3145. * we are free to update the registers based on the value
  3146. * in the queue parameter.
  3147. */
  3148. retval = rt2x00mac_conf_tx(hw, queue_idx, params);
  3149. if (retval)
  3150. return retval;
  3151. /*
  3152. * We only need to perform additional register initialization
  3153. * for WMM queues/
  3154. */
  3155. if (queue_idx >= 4)
  3156. return 0;
  3157. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  3158. /* Update WMM TXOP register */
  3159. offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
  3160. field.bit_offset = (queue_idx & 1) * 16;
  3161. field.bit_mask = 0xffff << field.bit_offset;
  3162. rt2800_register_read(rt2x00dev, offset, &reg);
  3163. rt2x00_set_field32(&reg, field, queue->txop);
  3164. rt2800_register_write(rt2x00dev, offset, reg);
  3165. /* Update WMM registers */
  3166. field.bit_offset = queue_idx * 4;
  3167. field.bit_mask = 0xf << field.bit_offset;
  3168. rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
  3169. rt2x00_set_field32(&reg, field, queue->aifs);
  3170. rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
  3171. rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
  3172. rt2x00_set_field32(&reg, field, queue->cw_min);
  3173. rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
  3174. rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
  3175. rt2x00_set_field32(&reg, field, queue->cw_max);
  3176. rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
  3177. /* Update EDCA registers */
  3178. offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
  3179. rt2800_register_read(rt2x00dev, offset, &reg);
  3180. rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
  3181. rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
  3182. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
  3183. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
  3184. rt2800_register_write(rt2x00dev, offset, reg);
  3185. return 0;
  3186. }
  3187. EXPORT_SYMBOL_GPL(rt2800_conf_tx);
  3188. u64 rt2800_get_tsf(struct ieee80211_hw *hw)
  3189. {
  3190. struct rt2x00_dev *rt2x00dev = hw->priv;
  3191. u64 tsf;
  3192. u32 reg;
  3193. rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
  3194. tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
  3195. rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
  3196. tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
  3197. return tsf;
  3198. }
  3199. EXPORT_SYMBOL_GPL(rt2800_get_tsf);
  3200. int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3201. enum ieee80211_ampdu_mlme_action action,
  3202. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  3203. u8 buf_size)
  3204. {
  3205. int ret = 0;
  3206. switch (action) {
  3207. case IEEE80211_AMPDU_RX_START:
  3208. case IEEE80211_AMPDU_RX_STOP:
  3209. /*
  3210. * The hw itself takes care of setting up BlockAck mechanisms.
  3211. * So, we only have to allow mac80211 to nagotiate a BlockAck
  3212. * agreement. Once that is done, the hw will BlockAck incoming
  3213. * AMPDUs without further setup.
  3214. */
  3215. break;
  3216. case IEEE80211_AMPDU_TX_START:
  3217. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  3218. break;
  3219. case IEEE80211_AMPDU_TX_STOP:
  3220. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  3221. break;
  3222. case IEEE80211_AMPDU_TX_OPERATIONAL:
  3223. break;
  3224. default:
  3225. WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
  3226. }
  3227. return ret;
  3228. }
  3229. EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
  3230. int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
  3231. struct survey_info *survey)
  3232. {
  3233. struct rt2x00_dev *rt2x00dev = hw->priv;
  3234. struct ieee80211_conf *conf = &hw->conf;
  3235. u32 idle, busy, busy_ext;
  3236. if (idx != 0)
  3237. return -ENOENT;
  3238. survey->channel = conf->channel;
  3239. rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
  3240. rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
  3241. rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
  3242. if (idle || busy) {
  3243. survey->filled = SURVEY_INFO_CHANNEL_TIME |
  3244. SURVEY_INFO_CHANNEL_TIME_BUSY |
  3245. SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
  3246. survey->channel_time = (idle + busy) / 1000;
  3247. survey->channel_time_busy = busy / 1000;
  3248. survey->channel_time_ext_busy = busy_ext / 1000;
  3249. }
  3250. return 0;
  3251. }
  3252. EXPORT_SYMBOL_GPL(rt2800_get_survey);
  3253. MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
  3254. MODULE_VERSION(DRV_VERSION);
  3255. MODULE_DESCRIPTION("Ralink RT2800 library");
  3256. MODULE_LICENSE("GPL");