rt2800lib.c 155 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883
  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. if (rt2x00_rt(rt2x00dev, RT3572) ||
  336. rt2x00_rt(rt2x00dev, RT5390) ||
  337. rt2x00_rt(rt2x00dev, RT5392)) {
  338. rt2800_register_read(rt2x00dev, AUX_CTRL, &reg);
  339. rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
  340. rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
  341. rt2800_register_write(rt2x00dev, AUX_CTRL, reg);
  342. }
  343. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
  344. }
  345. /*
  346. * Write firmware to the device.
  347. */
  348. rt2800_drv_write_firmware(rt2x00dev, data, len);
  349. /*
  350. * Wait for device to stabilize.
  351. */
  352. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  353. rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
  354. if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
  355. break;
  356. msleep(1);
  357. }
  358. if (i == REGISTER_BUSY_COUNT) {
  359. ERROR(rt2x00dev, "PBF system register not ready.\n");
  360. return -EBUSY;
  361. }
  362. /*
  363. * Disable DMA, will be reenabled later when enabling
  364. * the radio.
  365. */
  366. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  367. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  368. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  369. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  370. /*
  371. * Initialize firmware.
  372. */
  373. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  374. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  375. if (rt2x00_is_usb(rt2x00dev))
  376. rt2800_register_write(rt2x00dev, H2M_INT_SRC, 0);
  377. msleep(1);
  378. return 0;
  379. }
  380. EXPORT_SYMBOL_GPL(rt2800_load_firmware);
  381. void rt2800_write_tx_data(struct queue_entry *entry,
  382. struct txentry_desc *txdesc)
  383. {
  384. __le32 *txwi = rt2800_drv_get_txwi(entry);
  385. u32 word;
  386. /*
  387. * Initialize TX Info descriptor
  388. */
  389. rt2x00_desc_read(txwi, 0, &word);
  390. rt2x00_set_field32(&word, TXWI_W0_FRAG,
  391. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  392. rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
  393. test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
  394. rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
  395. rt2x00_set_field32(&word, TXWI_W0_TS,
  396. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  397. rt2x00_set_field32(&word, TXWI_W0_AMPDU,
  398. test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
  399. rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
  400. txdesc->u.ht.mpdu_density);
  401. rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
  402. rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
  403. rt2x00_set_field32(&word, TXWI_W0_BW,
  404. test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
  405. rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
  406. test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
  407. rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
  408. rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
  409. rt2x00_desc_write(txwi, 0, word);
  410. rt2x00_desc_read(txwi, 1, &word);
  411. rt2x00_set_field32(&word, TXWI_W1_ACK,
  412. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  413. rt2x00_set_field32(&word, TXWI_W1_NSEQ,
  414. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  415. rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
  416. rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
  417. test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
  418. txdesc->key_idx : txdesc->u.ht.wcid);
  419. rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
  420. txdesc->length);
  421. rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
  422. rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
  423. rt2x00_desc_write(txwi, 1, word);
  424. /*
  425. * Always write 0 to IV/EIV fields, hardware will insert the IV
  426. * from the IVEIV register when TXD_W3_WIV is set to 0.
  427. * When TXD_W3_WIV is set to 1 it will use the IV data
  428. * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
  429. * crypto entry in the registers should be used to encrypt the frame.
  430. */
  431. _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
  432. _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
  433. }
  434. EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
  435. static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
  436. {
  437. s8 rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
  438. s8 rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
  439. s8 rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
  440. u16 eeprom;
  441. u8 offset0;
  442. u8 offset1;
  443. u8 offset2;
  444. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
  445. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
  446. offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
  447. offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
  448. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
  449. offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
  450. } else {
  451. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
  452. offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
  453. offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
  454. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
  455. offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
  456. }
  457. /*
  458. * Convert the value from the descriptor into the RSSI value
  459. * If the value in the descriptor is 0, it is considered invalid
  460. * and the default (extremely low) rssi value is assumed
  461. */
  462. rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
  463. rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
  464. rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
  465. /*
  466. * mac80211 only accepts a single RSSI value. Calculating the
  467. * average doesn't deliver a fair answer either since -60:-60 would
  468. * be considered equally good as -50:-70 while the second is the one
  469. * which gives less energy...
  470. */
  471. rssi0 = max(rssi0, rssi1);
  472. return (int)max(rssi0, rssi2);
  473. }
  474. void rt2800_process_rxwi(struct queue_entry *entry,
  475. struct rxdone_entry_desc *rxdesc)
  476. {
  477. __le32 *rxwi = (__le32 *) entry->skb->data;
  478. u32 word;
  479. rt2x00_desc_read(rxwi, 0, &word);
  480. rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
  481. rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
  482. rt2x00_desc_read(rxwi, 1, &word);
  483. if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
  484. rxdesc->flags |= RX_FLAG_SHORT_GI;
  485. if (rt2x00_get_field32(word, RXWI_W1_BW))
  486. rxdesc->flags |= RX_FLAG_40MHZ;
  487. /*
  488. * Detect RX rate, always use MCS as signal type.
  489. */
  490. rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
  491. rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
  492. rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
  493. /*
  494. * Mask of 0x8 bit to remove the short preamble flag.
  495. */
  496. if (rxdesc->rate_mode == RATE_MODE_CCK)
  497. rxdesc->signal &= ~0x8;
  498. rt2x00_desc_read(rxwi, 2, &word);
  499. /*
  500. * Convert descriptor AGC value to RSSI value.
  501. */
  502. rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
  503. /*
  504. * Remove RXWI descriptor from start of buffer.
  505. */
  506. skb_pull(entry->skb, RXWI_DESC_SIZE);
  507. }
  508. EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
  509. void rt2800_txdone_entry(struct queue_entry *entry, u32 status, __le32 *txwi)
  510. {
  511. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  512. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  513. struct txdone_entry_desc txdesc;
  514. u32 word;
  515. u16 mcs, real_mcs;
  516. int aggr, ampdu;
  517. /*
  518. * Obtain the status about this packet.
  519. */
  520. txdesc.flags = 0;
  521. rt2x00_desc_read(txwi, 0, &word);
  522. mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
  523. ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
  524. real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
  525. aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
  526. /*
  527. * If a frame was meant to be sent as a single non-aggregated MPDU
  528. * but ended up in an aggregate the used tx rate doesn't correlate
  529. * with the one specified in the TXWI as the whole aggregate is sent
  530. * with the same rate.
  531. *
  532. * For example: two frames are sent to rt2x00, the first one sets
  533. * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
  534. * and requests MCS15. If the hw aggregates both frames into one
  535. * AMDPU the tx status for both frames will contain MCS7 although
  536. * the frame was sent successfully.
  537. *
  538. * Hence, replace the requested rate with the real tx rate to not
  539. * confuse the rate control algortihm by providing clearly wrong
  540. * data.
  541. */
  542. if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
  543. skbdesc->tx_rate_idx = real_mcs;
  544. mcs = real_mcs;
  545. }
  546. if (aggr == 1 || ampdu == 1)
  547. __set_bit(TXDONE_AMPDU, &txdesc.flags);
  548. /*
  549. * Ralink has a retry mechanism using a global fallback
  550. * table. We setup this fallback table to try the immediate
  551. * lower rate for all rates. In the TX_STA_FIFO, the MCS field
  552. * always contains the MCS used for the last transmission, be
  553. * it successful or not.
  554. */
  555. if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
  556. /*
  557. * Transmission succeeded. The number of retries is
  558. * mcs - real_mcs
  559. */
  560. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  561. txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
  562. } else {
  563. /*
  564. * Transmission failed. The number of retries is
  565. * always 7 in this case (for a total number of 8
  566. * frames sent).
  567. */
  568. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  569. txdesc.retry = rt2x00dev->long_retry;
  570. }
  571. /*
  572. * the frame was retried at least once
  573. * -> hw used fallback rates
  574. */
  575. if (txdesc.retry)
  576. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  577. rt2x00lib_txdone(entry, &txdesc);
  578. }
  579. EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
  580. void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
  581. {
  582. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  583. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  584. unsigned int beacon_base;
  585. unsigned int padding_len;
  586. u32 orig_reg, reg;
  587. /*
  588. * Disable beaconing while we are reloading the beacon data,
  589. * otherwise we might be sending out invalid data.
  590. */
  591. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  592. orig_reg = reg;
  593. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  594. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  595. /*
  596. * Add space for the TXWI in front of the skb.
  597. */
  598. memset(skb_push(entry->skb, TXWI_DESC_SIZE), 0, TXWI_DESC_SIZE);
  599. /*
  600. * Register descriptor details in skb frame descriptor.
  601. */
  602. skbdesc->flags |= SKBDESC_DESC_IN_SKB;
  603. skbdesc->desc = entry->skb->data;
  604. skbdesc->desc_len = TXWI_DESC_SIZE;
  605. /*
  606. * Add the TXWI for the beacon to the skb.
  607. */
  608. rt2800_write_tx_data(entry, txdesc);
  609. /*
  610. * Dump beacon to userspace through debugfs.
  611. */
  612. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
  613. /*
  614. * Write entire beacon with TXWI and padding to register.
  615. */
  616. padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
  617. if (padding_len && skb_pad(entry->skb, padding_len)) {
  618. ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
  619. /* skb freed by skb_pad() on failure */
  620. entry->skb = NULL;
  621. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
  622. return;
  623. }
  624. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  625. rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
  626. entry->skb->len + padding_len);
  627. /*
  628. * Enable beaconing again.
  629. */
  630. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  631. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  632. /*
  633. * Clean up beacon skb.
  634. */
  635. dev_kfree_skb_any(entry->skb);
  636. entry->skb = NULL;
  637. }
  638. EXPORT_SYMBOL_GPL(rt2800_write_beacon);
  639. static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
  640. unsigned int beacon_base)
  641. {
  642. int i;
  643. /*
  644. * For the Beacon base registers we only need to clear
  645. * the whole TXWI which (when set to 0) will invalidate
  646. * the entire beacon.
  647. */
  648. for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
  649. rt2800_register_write(rt2x00dev, beacon_base + i, 0);
  650. }
  651. void rt2800_clear_beacon(struct queue_entry *entry)
  652. {
  653. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  654. u32 reg;
  655. /*
  656. * Disable beaconing while we are reloading the beacon data,
  657. * otherwise we might be sending out invalid data.
  658. */
  659. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  660. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  661. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  662. /*
  663. * Clear beacon.
  664. */
  665. rt2800_clear_beacon_register(rt2x00dev,
  666. HW_BEACON_OFFSET(entry->entry_idx));
  667. /*
  668. * Enabled beaconing again.
  669. */
  670. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  671. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  672. }
  673. EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
  674. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  675. const struct rt2x00debug rt2800_rt2x00debug = {
  676. .owner = THIS_MODULE,
  677. .csr = {
  678. .read = rt2800_register_read,
  679. .write = rt2800_register_write,
  680. .flags = RT2X00DEBUGFS_OFFSET,
  681. .word_base = CSR_REG_BASE,
  682. .word_size = sizeof(u32),
  683. .word_count = CSR_REG_SIZE / sizeof(u32),
  684. },
  685. .eeprom = {
  686. .read = rt2x00_eeprom_read,
  687. .write = rt2x00_eeprom_write,
  688. .word_base = EEPROM_BASE,
  689. .word_size = sizeof(u16),
  690. .word_count = EEPROM_SIZE / sizeof(u16),
  691. },
  692. .bbp = {
  693. .read = rt2800_bbp_read,
  694. .write = rt2800_bbp_write,
  695. .word_base = BBP_BASE,
  696. .word_size = sizeof(u8),
  697. .word_count = BBP_SIZE / sizeof(u8),
  698. },
  699. .rf = {
  700. .read = rt2x00_rf_read,
  701. .write = rt2800_rf_write,
  702. .word_base = RF_BASE,
  703. .word_size = sizeof(u32),
  704. .word_count = RF_SIZE / sizeof(u32),
  705. },
  706. };
  707. EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
  708. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  709. int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  710. {
  711. u32 reg;
  712. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  713. return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
  714. }
  715. EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
  716. #ifdef CONFIG_RT2X00_LIB_LEDS
  717. static void rt2800_brightness_set(struct led_classdev *led_cdev,
  718. enum led_brightness brightness)
  719. {
  720. struct rt2x00_led *led =
  721. container_of(led_cdev, struct rt2x00_led, led_dev);
  722. unsigned int enabled = brightness != LED_OFF;
  723. unsigned int bg_mode =
  724. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  725. unsigned int polarity =
  726. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  727. EEPROM_FREQ_LED_POLARITY);
  728. unsigned int ledmode =
  729. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  730. EEPROM_FREQ_LED_MODE);
  731. u32 reg;
  732. /* Check for SoC (SOC devices don't support MCU requests) */
  733. if (rt2x00_is_soc(led->rt2x00dev)) {
  734. rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
  735. /* Set LED Polarity */
  736. rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, polarity);
  737. /* Set LED Mode */
  738. if (led->type == LED_TYPE_RADIO) {
  739. rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE,
  740. enabled ? 3 : 0);
  741. } else if (led->type == LED_TYPE_ASSOC) {
  742. rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE,
  743. enabled ? 3 : 0);
  744. } else if (led->type == LED_TYPE_QUALITY) {
  745. rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE,
  746. enabled ? 3 : 0);
  747. }
  748. rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
  749. } else {
  750. if (led->type == LED_TYPE_RADIO) {
  751. rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  752. enabled ? 0x20 : 0);
  753. } else if (led->type == LED_TYPE_ASSOC) {
  754. rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  755. enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
  756. } else if (led->type == LED_TYPE_QUALITY) {
  757. /*
  758. * The brightness is divided into 6 levels (0 - 5),
  759. * The specs tell us the following levels:
  760. * 0, 1 ,3, 7, 15, 31
  761. * to determine the level in a simple way we can simply
  762. * work with bitshifting:
  763. * (1 << level) - 1
  764. */
  765. rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
  766. (1 << brightness / (LED_FULL / 6)) - 1,
  767. polarity);
  768. }
  769. }
  770. }
  771. static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
  772. struct rt2x00_led *led, enum led_type type)
  773. {
  774. led->rt2x00dev = rt2x00dev;
  775. led->type = type;
  776. led->led_dev.brightness_set = rt2800_brightness_set;
  777. led->flags = LED_INITIALIZED;
  778. }
  779. #endif /* CONFIG_RT2X00_LIB_LEDS */
  780. /*
  781. * Configuration handlers.
  782. */
  783. static void rt2800_config_wcid(struct rt2x00_dev *rt2x00dev,
  784. const u8 *address,
  785. int wcid)
  786. {
  787. struct mac_wcid_entry wcid_entry;
  788. u32 offset;
  789. offset = MAC_WCID_ENTRY(wcid);
  790. memset(&wcid_entry, 0xff, sizeof(wcid_entry));
  791. if (address)
  792. memcpy(wcid_entry.mac, address, ETH_ALEN);
  793. rt2800_register_multiwrite(rt2x00dev, offset,
  794. &wcid_entry, sizeof(wcid_entry));
  795. }
  796. static void rt2800_delete_wcid_attr(struct rt2x00_dev *rt2x00dev, int wcid)
  797. {
  798. u32 offset;
  799. offset = MAC_WCID_ATTR_ENTRY(wcid);
  800. rt2800_register_write(rt2x00dev, offset, 0);
  801. }
  802. static void rt2800_config_wcid_attr_bssidx(struct rt2x00_dev *rt2x00dev,
  803. int wcid, u32 bssidx)
  804. {
  805. u32 offset = MAC_WCID_ATTR_ENTRY(wcid);
  806. u32 reg;
  807. /*
  808. * The BSS Idx numbers is split in a main value of 3 bits,
  809. * and a extended field for adding one additional bit to the value.
  810. */
  811. rt2800_register_read(rt2x00dev, offset, &reg);
  812. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX, (bssidx & 0x7));
  813. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
  814. (bssidx & 0x8) >> 3);
  815. rt2800_register_write(rt2x00dev, offset, reg);
  816. }
  817. static void rt2800_config_wcid_attr_cipher(struct rt2x00_dev *rt2x00dev,
  818. struct rt2x00lib_crypto *crypto,
  819. struct ieee80211_key_conf *key)
  820. {
  821. struct mac_iveiv_entry iveiv_entry;
  822. u32 offset;
  823. u32 reg;
  824. offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
  825. if (crypto->cmd == SET_KEY) {
  826. rt2800_register_read(rt2x00dev, offset, &reg);
  827. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
  828. !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
  829. /*
  830. * Both the cipher as the BSS Idx numbers are split in a main
  831. * value of 3 bits, and a extended field for adding one additional
  832. * bit to the value.
  833. */
  834. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
  835. (crypto->cipher & 0x7));
  836. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
  837. (crypto->cipher & 0x8) >> 3);
  838. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
  839. rt2800_register_write(rt2x00dev, offset, reg);
  840. } else {
  841. /* Delete the cipher without touching the bssidx */
  842. rt2800_register_read(rt2x00dev, offset, &reg);
  843. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB, 0);
  844. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER, 0);
  845. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT, 0);
  846. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, 0);
  847. rt2800_register_write(rt2x00dev, offset, reg);
  848. }
  849. offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
  850. memset(&iveiv_entry, 0, sizeof(iveiv_entry));
  851. if ((crypto->cipher == CIPHER_TKIP) ||
  852. (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
  853. (crypto->cipher == CIPHER_AES))
  854. iveiv_entry.iv[3] |= 0x20;
  855. iveiv_entry.iv[3] |= key->keyidx << 6;
  856. rt2800_register_multiwrite(rt2x00dev, offset,
  857. &iveiv_entry, sizeof(iveiv_entry));
  858. }
  859. int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
  860. struct rt2x00lib_crypto *crypto,
  861. struct ieee80211_key_conf *key)
  862. {
  863. struct hw_key_entry key_entry;
  864. struct rt2x00_field32 field;
  865. u32 offset;
  866. u32 reg;
  867. if (crypto->cmd == SET_KEY) {
  868. key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
  869. memcpy(key_entry.key, crypto->key,
  870. sizeof(key_entry.key));
  871. memcpy(key_entry.tx_mic, crypto->tx_mic,
  872. sizeof(key_entry.tx_mic));
  873. memcpy(key_entry.rx_mic, crypto->rx_mic,
  874. sizeof(key_entry.rx_mic));
  875. offset = SHARED_KEY_ENTRY(key->hw_key_idx);
  876. rt2800_register_multiwrite(rt2x00dev, offset,
  877. &key_entry, sizeof(key_entry));
  878. }
  879. /*
  880. * The cipher types are stored over multiple registers
  881. * starting with SHARED_KEY_MODE_BASE each word will have
  882. * 32 bits and contains the cipher types for 2 bssidx each.
  883. * Using the correct defines correctly will cause overhead,
  884. * so just calculate the correct offset.
  885. */
  886. field.bit_offset = 4 * (key->hw_key_idx % 8);
  887. field.bit_mask = 0x7 << field.bit_offset;
  888. offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
  889. rt2800_register_read(rt2x00dev, offset, &reg);
  890. rt2x00_set_field32(&reg, field,
  891. (crypto->cmd == SET_KEY) * crypto->cipher);
  892. rt2800_register_write(rt2x00dev, offset, reg);
  893. /*
  894. * Update WCID information
  895. */
  896. rt2800_config_wcid(rt2x00dev, crypto->address, key->hw_key_idx);
  897. rt2800_config_wcid_attr_bssidx(rt2x00dev, key->hw_key_idx,
  898. crypto->bssidx);
  899. rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
  900. return 0;
  901. }
  902. EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
  903. static inline int rt2800_find_wcid(struct rt2x00_dev *rt2x00dev)
  904. {
  905. struct mac_wcid_entry wcid_entry;
  906. int idx;
  907. u32 offset;
  908. /*
  909. * Search for the first free WCID entry and return the corresponding
  910. * index.
  911. *
  912. * Make sure the WCID starts _after_ the last possible shared key
  913. * entry (>32).
  914. *
  915. * Since parts of the pairwise key table might be shared with
  916. * the beacon frame buffers 6 & 7 we should only write into the
  917. * first 222 entries.
  918. */
  919. for (idx = 33; idx <= 222; idx++) {
  920. offset = MAC_WCID_ENTRY(idx);
  921. rt2800_register_multiread(rt2x00dev, offset, &wcid_entry,
  922. sizeof(wcid_entry));
  923. if (is_broadcast_ether_addr(wcid_entry.mac))
  924. return idx;
  925. }
  926. /*
  927. * Use -1 to indicate that we don't have any more space in the WCID
  928. * table.
  929. */
  930. return -1;
  931. }
  932. int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
  933. struct rt2x00lib_crypto *crypto,
  934. struct ieee80211_key_conf *key)
  935. {
  936. struct hw_key_entry key_entry;
  937. u32 offset;
  938. if (crypto->cmd == SET_KEY) {
  939. /*
  940. * Allow key configuration only for STAs that are
  941. * known by the hw.
  942. */
  943. if (crypto->wcid < 0)
  944. return -ENOSPC;
  945. key->hw_key_idx = crypto->wcid;
  946. memcpy(key_entry.key, crypto->key,
  947. sizeof(key_entry.key));
  948. memcpy(key_entry.tx_mic, crypto->tx_mic,
  949. sizeof(key_entry.tx_mic));
  950. memcpy(key_entry.rx_mic, crypto->rx_mic,
  951. sizeof(key_entry.rx_mic));
  952. offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
  953. rt2800_register_multiwrite(rt2x00dev, offset,
  954. &key_entry, sizeof(key_entry));
  955. }
  956. /*
  957. * Update WCID information
  958. */
  959. rt2800_config_wcid_attr_cipher(rt2x00dev, crypto, key);
  960. return 0;
  961. }
  962. EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
  963. int rt2800_sta_add(struct rt2x00_dev *rt2x00dev, struct ieee80211_vif *vif,
  964. struct ieee80211_sta *sta)
  965. {
  966. int wcid;
  967. struct rt2x00_sta *sta_priv = sta_to_rt2x00_sta(sta);
  968. /*
  969. * Find next free WCID.
  970. */
  971. wcid = rt2800_find_wcid(rt2x00dev);
  972. /*
  973. * Store selected wcid even if it is invalid so that we can
  974. * later decide if the STA is uploaded into the hw.
  975. */
  976. sta_priv->wcid = wcid;
  977. /*
  978. * No space left in the device, however, we can still communicate
  979. * with the STA -> No error.
  980. */
  981. if (wcid < 0)
  982. return 0;
  983. /*
  984. * Clean up WCID attributes and write STA address to the device.
  985. */
  986. rt2800_delete_wcid_attr(rt2x00dev, wcid);
  987. rt2800_config_wcid(rt2x00dev, sta->addr, wcid);
  988. rt2800_config_wcid_attr_bssidx(rt2x00dev, wcid,
  989. rt2x00lib_get_bssidx(rt2x00dev, vif));
  990. return 0;
  991. }
  992. EXPORT_SYMBOL_GPL(rt2800_sta_add);
  993. int rt2800_sta_remove(struct rt2x00_dev *rt2x00dev, int wcid)
  994. {
  995. /*
  996. * Remove WCID entry, no need to clean the attributes as they will
  997. * get renewed when the WCID is reused.
  998. */
  999. rt2800_config_wcid(rt2x00dev, NULL, wcid);
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL_GPL(rt2800_sta_remove);
  1003. void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
  1004. const unsigned int filter_flags)
  1005. {
  1006. u32 reg;
  1007. /*
  1008. * Start configuration steps.
  1009. * Note that the version error will always be dropped
  1010. * and broadcast frames will always be accepted since
  1011. * there is no filter for it at this time.
  1012. */
  1013. rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
  1014. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
  1015. !(filter_flags & FIF_FCSFAIL));
  1016. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
  1017. !(filter_flags & FIF_PLCPFAIL));
  1018. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
  1019. !(filter_flags & FIF_PROMISC_IN_BSS));
  1020. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
  1021. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
  1022. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
  1023. !(filter_flags & FIF_ALLMULTI));
  1024. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
  1025. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
  1026. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
  1027. !(filter_flags & FIF_CONTROL));
  1028. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
  1029. !(filter_flags & FIF_CONTROL));
  1030. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
  1031. !(filter_flags & FIF_CONTROL));
  1032. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
  1033. !(filter_flags & FIF_CONTROL));
  1034. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
  1035. !(filter_flags & FIF_CONTROL));
  1036. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
  1037. !(filter_flags & FIF_PSPOLL));
  1038. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA,
  1039. !(filter_flags & FIF_CONTROL));
  1040. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR,
  1041. !(filter_flags & FIF_CONTROL));
  1042. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
  1043. !(filter_flags & FIF_CONTROL));
  1044. rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
  1045. }
  1046. EXPORT_SYMBOL_GPL(rt2800_config_filter);
  1047. void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
  1048. struct rt2x00intf_conf *conf, const unsigned int flags)
  1049. {
  1050. u32 reg;
  1051. bool update_bssid = false;
  1052. if (flags & CONFIG_UPDATE_TYPE) {
  1053. /*
  1054. * Enable synchronisation.
  1055. */
  1056. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1057. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
  1058. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1059. if (conf->sync == TSF_SYNC_AP_NONE) {
  1060. /*
  1061. * Tune beacon queue transmit parameters for AP mode
  1062. */
  1063. rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
  1064. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
  1065. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
  1066. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
  1067. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
  1068. rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
  1069. } else {
  1070. rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
  1071. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
  1072. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
  1073. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
  1074. rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
  1075. rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
  1076. }
  1077. }
  1078. if (flags & CONFIG_UPDATE_MAC) {
  1079. if (flags & CONFIG_UPDATE_TYPE &&
  1080. conf->sync == TSF_SYNC_AP_NONE) {
  1081. /*
  1082. * The BSSID register has to be set to our own mac
  1083. * address in AP mode.
  1084. */
  1085. memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
  1086. update_bssid = true;
  1087. }
  1088. if (!is_zero_ether_addr((const u8 *)conf->mac)) {
  1089. reg = le32_to_cpu(conf->mac[1]);
  1090. rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
  1091. conf->mac[1] = cpu_to_le32(reg);
  1092. }
  1093. rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
  1094. conf->mac, sizeof(conf->mac));
  1095. }
  1096. if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
  1097. if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
  1098. reg = le32_to_cpu(conf->bssid[1]);
  1099. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
  1100. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
  1101. conf->bssid[1] = cpu_to_le32(reg);
  1102. }
  1103. rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
  1104. conf->bssid, sizeof(conf->bssid));
  1105. }
  1106. }
  1107. EXPORT_SYMBOL_GPL(rt2800_config_intf);
  1108. static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
  1109. struct rt2x00lib_erp *erp)
  1110. {
  1111. bool any_sta_nongf = !!(erp->ht_opmode &
  1112. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  1113. u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
  1114. u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
  1115. u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
  1116. u32 reg;
  1117. /* default protection rate for HT20: OFDM 24M */
  1118. mm20_rate = gf20_rate = 0x4004;
  1119. /* default protection rate for HT40: duplicate OFDM 24M */
  1120. mm40_rate = gf40_rate = 0x4084;
  1121. switch (protection) {
  1122. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  1123. /*
  1124. * All STAs in this BSS are HT20/40 but there might be
  1125. * STAs not supporting greenfield mode.
  1126. * => Disable protection for HT transmissions.
  1127. */
  1128. mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
  1129. break;
  1130. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  1131. /*
  1132. * All STAs in this BSS are HT20 or HT20/40 but there
  1133. * might be STAs not supporting greenfield mode.
  1134. * => Protect all HT40 transmissions.
  1135. */
  1136. mm20_mode = gf20_mode = 0;
  1137. mm40_mode = gf40_mode = 2;
  1138. break;
  1139. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  1140. /*
  1141. * Nonmember protection:
  1142. * According to 802.11n we _should_ protect all
  1143. * HT transmissions (but we don't have to).
  1144. *
  1145. * But if cts_protection is enabled we _shall_ protect
  1146. * all HT transmissions using a CCK rate.
  1147. *
  1148. * And if any station is non GF we _shall_ protect
  1149. * GF transmissions.
  1150. *
  1151. * We decide to protect everything
  1152. * -> fall through to mixed mode.
  1153. */
  1154. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  1155. /*
  1156. * Legacy STAs are present
  1157. * => Protect all HT transmissions.
  1158. */
  1159. mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
  1160. /*
  1161. * If erp protection is needed we have to protect HT
  1162. * transmissions with CCK 11M long preamble.
  1163. */
  1164. if (erp->cts_protection) {
  1165. /* don't duplicate RTS/CTS in CCK mode */
  1166. mm20_rate = mm40_rate = 0x0003;
  1167. gf20_rate = gf40_rate = 0x0003;
  1168. }
  1169. break;
  1170. }
  1171. /* check for STAs not supporting greenfield mode */
  1172. if (any_sta_nongf)
  1173. gf20_mode = gf40_mode = 2;
  1174. /* Update HT protection config */
  1175. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  1176. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
  1177. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
  1178. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  1179. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  1180. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
  1181. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
  1182. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  1183. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  1184. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
  1185. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
  1186. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  1187. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  1188. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
  1189. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
  1190. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  1191. }
  1192. void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
  1193. u32 changed)
  1194. {
  1195. u32 reg;
  1196. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1197. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  1198. rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
  1199. !!erp->short_preamble);
  1200. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
  1201. !!erp->short_preamble);
  1202. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  1203. }
  1204. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1205. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  1206. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
  1207. erp->cts_protection ? 2 : 0);
  1208. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  1209. }
  1210. if (changed & BSS_CHANGED_BASIC_RATES) {
  1211. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
  1212. erp->basic_rates);
  1213. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  1214. }
  1215. if (changed & BSS_CHANGED_ERP_SLOT) {
  1216. rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
  1217. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
  1218. erp->slot_time);
  1219. rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
  1220. rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
  1221. rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
  1222. rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
  1223. }
  1224. if (changed & BSS_CHANGED_BEACON_INT) {
  1225. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1226. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
  1227. erp->beacon_int * 16);
  1228. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1229. }
  1230. if (changed & BSS_CHANGED_HT)
  1231. rt2800_config_ht_opmode(rt2x00dev, erp);
  1232. }
  1233. EXPORT_SYMBOL_GPL(rt2800_config_erp);
  1234. static void rt2800_config_3572bt_ant(struct rt2x00_dev *rt2x00dev)
  1235. {
  1236. u32 reg;
  1237. u16 eeprom;
  1238. u8 led_ctrl, led_g_mode, led_r_mode;
  1239. rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
  1240. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  1241. rt2x00_set_field32(&reg, GPIO_SWITCH_0, 1);
  1242. rt2x00_set_field32(&reg, GPIO_SWITCH_1, 1);
  1243. } else {
  1244. rt2x00_set_field32(&reg, GPIO_SWITCH_0, 0);
  1245. rt2x00_set_field32(&reg, GPIO_SWITCH_1, 0);
  1246. }
  1247. rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
  1248. rt2800_register_read(rt2x00dev, LED_CFG, &reg);
  1249. led_g_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 3 : 0;
  1250. led_r_mode = rt2x00_get_field32(reg, LED_CFG_LED_POLAR) ? 0 : 3;
  1251. if (led_g_mode != rt2x00_get_field32(reg, LED_CFG_G_LED_MODE) ||
  1252. led_r_mode != rt2x00_get_field32(reg, LED_CFG_R_LED_MODE)) {
  1253. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  1254. led_ctrl = rt2x00_get_field16(eeprom, EEPROM_FREQ_LED_MODE);
  1255. if (led_ctrl == 0 || led_ctrl > 0x40) {
  1256. rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, led_g_mode);
  1257. rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, led_r_mode);
  1258. rt2800_register_write(rt2x00dev, LED_CFG, reg);
  1259. } else {
  1260. rt2800_mcu_request(rt2x00dev, MCU_BAND_SELECT, 0xff,
  1261. (led_g_mode << 2) | led_r_mode, 1);
  1262. }
  1263. }
  1264. }
  1265. static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
  1266. enum antenna ant)
  1267. {
  1268. u32 reg;
  1269. u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
  1270. u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
  1271. if (rt2x00_is_pci(rt2x00dev)) {
  1272. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  1273. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
  1274. rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
  1275. } else if (rt2x00_is_usb(rt2x00dev))
  1276. rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
  1277. eesk_pin, 0);
  1278. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  1279. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT3, 0);
  1280. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, gpio_bit3);
  1281. rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
  1282. }
  1283. void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
  1284. {
  1285. u8 r1;
  1286. u8 r3;
  1287. u16 eeprom;
  1288. rt2800_bbp_read(rt2x00dev, 1, &r1);
  1289. rt2800_bbp_read(rt2x00dev, 3, &r3);
  1290. if (rt2x00_rt(rt2x00dev, RT3572) &&
  1291. test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
  1292. rt2800_config_3572bt_ant(rt2x00dev);
  1293. /*
  1294. * Configure the TX antenna.
  1295. */
  1296. switch (ant->tx_chain_num) {
  1297. case 1:
  1298. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
  1299. break;
  1300. case 2:
  1301. if (rt2x00_rt(rt2x00dev, RT3572) &&
  1302. test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
  1303. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 1);
  1304. else
  1305. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
  1306. break;
  1307. case 3:
  1308. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
  1309. break;
  1310. }
  1311. /*
  1312. * Configure the RX antenna.
  1313. */
  1314. switch (ant->rx_chain_num) {
  1315. case 1:
  1316. if (rt2x00_rt(rt2x00dev, RT3070) ||
  1317. rt2x00_rt(rt2x00dev, RT3090) ||
  1318. rt2x00_rt(rt2x00dev, RT3390)) {
  1319. rt2x00_eeprom_read(rt2x00dev,
  1320. EEPROM_NIC_CONF1, &eeprom);
  1321. if (rt2x00_get_field16(eeprom,
  1322. EEPROM_NIC_CONF1_ANT_DIVERSITY))
  1323. rt2800_set_ant_diversity(rt2x00dev,
  1324. rt2x00dev->default_ant.rx);
  1325. }
  1326. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
  1327. break;
  1328. case 2:
  1329. if (rt2x00_rt(rt2x00dev, RT3572) &&
  1330. test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
  1331. rt2x00_set_field8(&r3, BBP3_RX_ADC, 1);
  1332. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA,
  1333. rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  1334. rt2800_set_ant_diversity(rt2x00dev, ANTENNA_B);
  1335. } else {
  1336. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
  1337. }
  1338. break;
  1339. case 3:
  1340. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
  1341. break;
  1342. }
  1343. rt2800_bbp_write(rt2x00dev, 3, r3);
  1344. rt2800_bbp_write(rt2x00dev, 1, r1);
  1345. }
  1346. EXPORT_SYMBOL_GPL(rt2800_config_ant);
  1347. static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  1348. struct rt2x00lib_conf *libconf)
  1349. {
  1350. u16 eeprom;
  1351. short lna_gain;
  1352. if (libconf->rf.channel <= 14) {
  1353. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  1354. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
  1355. } else if (libconf->rf.channel <= 64) {
  1356. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  1357. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
  1358. } else if (libconf->rf.channel <= 128) {
  1359. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
  1360. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
  1361. } else {
  1362. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
  1363. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
  1364. }
  1365. rt2x00dev->lna_gain = lna_gain;
  1366. }
  1367. static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
  1368. struct ieee80211_conf *conf,
  1369. struct rf_channel *rf,
  1370. struct channel_info *info)
  1371. {
  1372. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  1373. if (rt2x00dev->default_ant.tx_chain_num == 1)
  1374. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
  1375. if (rt2x00dev->default_ant.rx_chain_num == 1) {
  1376. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
  1377. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  1378. } else if (rt2x00dev->default_ant.rx_chain_num == 2)
  1379. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  1380. if (rf->channel > 14) {
  1381. /*
  1382. * When TX power is below 0, we should increase it by 7 to
  1383. * make it a positive value (Minimum value is -7).
  1384. * However this means that values between 0 and 7 have
  1385. * double meaning, and we should set a 7DBm boost flag.
  1386. */
  1387. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
  1388. (info->default_power1 >= 0));
  1389. if (info->default_power1 < 0)
  1390. info->default_power1 += 7;
  1391. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
  1392. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
  1393. (info->default_power2 >= 0));
  1394. if (info->default_power2 < 0)
  1395. info->default_power2 += 7;
  1396. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
  1397. } else {
  1398. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
  1399. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
  1400. }
  1401. rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
  1402. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  1403. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  1404. rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  1405. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  1406. udelay(200);
  1407. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  1408. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  1409. rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  1410. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  1411. udelay(200);
  1412. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  1413. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  1414. rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  1415. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  1416. }
  1417. static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
  1418. struct ieee80211_conf *conf,
  1419. struct rf_channel *rf,
  1420. struct channel_info *info)
  1421. {
  1422. struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
  1423. u8 rfcsr, calib_tx, calib_rx;
  1424. rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
  1425. rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
  1426. rt2x00_set_field8(&rfcsr, RFCSR3_K, rf->rf3);
  1427. rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
  1428. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  1429. rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
  1430. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  1431. rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
  1432. rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
  1433. rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
  1434. rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
  1435. rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
  1436. rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
  1437. rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
  1438. rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
  1439. rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
  1440. if (rt2x00_rt(rt2x00dev, RT3390)) {
  1441. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD,
  1442. rt2x00dev->default_ant.rx_chain_num == 1);
  1443. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD,
  1444. rt2x00dev->default_ant.tx_chain_num == 1);
  1445. } else {
  1446. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
  1447. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
  1448. rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
  1449. rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
  1450. switch (rt2x00dev->default_ant.tx_chain_num) {
  1451. case 1:
  1452. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
  1453. /* fall through */
  1454. case 2:
  1455. rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
  1456. break;
  1457. }
  1458. switch (rt2x00dev->default_ant.rx_chain_num) {
  1459. case 1:
  1460. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
  1461. /* fall through */
  1462. case 2:
  1463. rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
  1464. break;
  1465. }
  1466. }
  1467. rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
  1468. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  1469. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  1470. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1471. msleep(1);
  1472. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
  1473. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1474. rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
  1475. rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
  1476. rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
  1477. if (rt2x00_rt(rt2x00dev, RT3390)) {
  1478. calib_tx = conf_is_ht40(conf) ? 0x68 : 0x4f;
  1479. calib_rx = conf_is_ht40(conf) ? 0x6f : 0x4f;
  1480. } else {
  1481. if (conf_is_ht40(conf)) {
  1482. calib_tx = drv_data->calibration_bw40;
  1483. calib_rx = drv_data->calibration_bw40;
  1484. } else {
  1485. calib_tx = drv_data->calibration_bw20;
  1486. calib_rx = drv_data->calibration_bw20;
  1487. }
  1488. }
  1489. rt2800_rfcsr_read(rt2x00dev, 24, &rfcsr);
  1490. rt2x00_set_field8(&rfcsr, RFCSR24_TX_CALIB, calib_tx);
  1491. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr);
  1492. rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
  1493. rt2x00_set_field8(&rfcsr, RFCSR31_RX_CALIB, calib_rx);
  1494. rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
  1495. rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
  1496. rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
  1497. rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
  1498. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  1499. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  1500. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1501. msleep(1);
  1502. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
  1503. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1504. }
  1505. static void rt2800_config_channel_rf3052(struct rt2x00_dev *rt2x00dev,
  1506. struct ieee80211_conf *conf,
  1507. struct rf_channel *rf,
  1508. struct channel_info *info)
  1509. {
  1510. struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
  1511. u8 rfcsr;
  1512. u32 reg;
  1513. if (rf->channel <= 14) {
  1514. rt2800_bbp_write(rt2x00dev, 25, drv_data->bbp25);
  1515. rt2800_bbp_write(rt2x00dev, 26, drv_data->bbp26);
  1516. } else {
  1517. rt2800_bbp_write(rt2x00dev, 25, 0x09);
  1518. rt2800_bbp_write(rt2x00dev, 26, 0xff);
  1519. }
  1520. rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
  1521. rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
  1522. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  1523. rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
  1524. if (rf->channel <= 14)
  1525. rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 2);
  1526. else
  1527. rt2x00_set_field8(&rfcsr, RFCSR6_TXDIV, 1);
  1528. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  1529. rt2800_rfcsr_read(rt2x00dev, 5, &rfcsr);
  1530. if (rf->channel <= 14)
  1531. rt2x00_set_field8(&rfcsr, RFCSR5_R1, 1);
  1532. else
  1533. rt2x00_set_field8(&rfcsr, RFCSR5_R1, 2);
  1534. rt2800_rfcsr_write(rt2x00dev, 5, rfcsr);
  1535. rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
  1536. if (rf->channel <= 14) {
  1537. rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 3);
  1538. rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
  1539. info->default_power1);
  1540. } else {
  1541. rt2x00_set_field8(&rfcsr, RFCSR12_DR0, 7);
  1542. rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
  1543. (info->default_power1 & 0x3) |
  1544. ((info->default_power1 & 0xC) << 1));
  1545. }
  1546. rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
  1547. rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
  1548. if (rf->channel <= 14) {
  1549. rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 3);
  1550. rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
  1551. info->default_power2);
  1552. } else {
  1553. rt2x00_set_field8(&rfcsr, RFCSR13_DR0, 7);
  1554. rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER,
  1555. (info->default_power2 & 0x3) |
  1556. ((info->default_power2 & 0xC) << 1));
  1557. }
  1558. rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
  1559. rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
  1560. rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
  1561. rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
  1562. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 0);
  1563. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 0);
  1564. rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 0);
  1565. rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 0);
  1566. if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
  1567. if (rf->channel <= 14) {
  1568. rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
  1569. rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
  1570. }
  1571. rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
  1572. rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
  1573. } else {
  1574. switch (rt2x00dev->default_ant.tx_chain_num) {
  1575. case 1:
  1576. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
  1577. case 2:
  1578. rt2x00_set_field8(&rfcsr, RFCSR1_TX2_PD, 1);
  1579. break;
  1580. }
  1581. switch (rt2x00dev->default_ant.rx_chain_num) {
  1582. case 1:
  1583. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
  1584. case 2:
  1585. rt2x00_set_field8(&rfcsr, RFCSR1_RX2_PD, 1);
  1586. break;
  1587. }
  1588. }
  1589. rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
  1590. rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
  1591. rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
  1592. rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
  1593. if (conf_is_ht40(conf)) {
  1594. rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw40);
  1595. rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw40);
  1596. } else {
  1597. rt2800_rfcsr_write(rt2x00dev, 24, drv_data->calibration_bw20);
  1598. rt2800_rfcsr_write(rt2x00dev, 31, drv_data->calibration_bw20);
  1599. }
  1600. if (rf->channel <= 14) {
  1601. rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
  1602. rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
  1603. rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
  1604. rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
  1605. rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
  1606. rfcsr = 0x4c;
  1607. rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
  1608. drv_data->txmixer_gain_24g);
  1609. rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
  1610. rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
  1611. rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
  1612. rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
  1613. rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
  1614. rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
  1615. rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
  1616. rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
  1617. } else {
  1618. rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
  1619. rt2x00_set_field8(&rfcsr, RFCSR7_BIT2, 1);
  1620. rt2x00_set_field8(&rfcsr, RFCSR7_BIT3, 0);
  1621. rt2x00_set_field8(&rfcsr, RFCSR7_BIT4, 1);
  1622. rt2x00_set_field8(&rfcsr, RFCSR7_BITS67, 0);
  1623. rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
  1624. rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
  1625. rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
  1626. rt2800_rfcsr_write(rt2x00dev, 11, 0x00);
  1627. rt2800_rfcsr_write(rt2x00dev, 15, 0x43);
  1628. rfcsr = 0x7a;
  1629. rt2x00_set_field8(&rfcsr, RFCSR16_TXMIXER_GAIN,
  1630. drv_data->txmixer_gain_5g);
  1631. rt2800_rfcsr_write(rt2x00dev, 16, rfcsr);
  1632. rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
  1633. if (rf->channel <= 64) {
  1634. rt2800_rfcsr_write(rt2x00dev, 19, 0xb7);
  1635. rt2800_rfcsr_write(rt2x00dev, 20, 0xf6);
  1636. rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
  1637. } else if (rf->channel <= 128) {
  1638. rt2800_rfcsr_write(rt2x00dev, 19, 0x74);
  1639. rt2800_rfcsr_write(rt2x00dev, 20, 0xf4);
  1640. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  1641. } else {
  1642. rt2800_rfcsr_write(rt2x00dev, 19, 0x72);
  1643. rt2800_rfcsr_write(rt2x00dev, 20, 0xf3);
  1644. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  1645. }
  1646. rt2800_rfcsr_write(rt2x00dev, 26, 0x87);
  1647. rt2800_rfcsr_write(rt2x00dev, 27, 0x01);
  1648. rt2800_rfcsr_write(rt2x00dev, 29, 0x9f);
  1649. }
  1650. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  1651. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT7, 0);
  1652. if (rf->channel <= 14)
  1653. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT7, 1);
  1654. else
  1655. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT7, 0);
  1656. rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
  1657. rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
  1658. rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
  1659. rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
  1660. }
  1661. #define RT5390_POWER_BOUND 0x27
  1662. #define RT5390_FREQ_OFFSET_BOUND 0x5f
  1663. static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
  1664. struct ieee80211_conf *conf,
  1665. struct rf_channel *rf,
  1666. struct channel_info *info)
  1667. {
  1668. u8 rfcsr;
  1669. rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
  1670. rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
  1671. rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
  1672. rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
  1673. rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
  1674. rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
  1675. if (info->default_power1 > RT5390_POWER_BOUND)
  1676. rt2x00_set_field8(&rfcsr, RFCSR49_TX, RT5390_POWER_BOUND);
  1677. else
  1678. rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
  1679. rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
  1680. rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
  1681. rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
  1682. rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
  1683. rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
  1684. rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
  1685. rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
  1686. rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
  1687. if (rt2x00dev->freq_offset > RT5390_FREQ_OFFSET_BOUND)
  1688. rt2x00_set_field8(&rfcsr, RFCSR17_CODE,
  1689. RT5390_FREQ_OFFSET_BOUND);
  1690. else
  1691. rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
  1692. rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
  1693. if (rf->channel <= 14) {
  1694. int idx = rf->channel-1;
  1695. if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
  1696. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
  1697. /* r55/r59 value array of channel 1~14 */
  1698. static const char r55_bt_rev[] = {0x83, 0x83,
  1699. 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
  1700. 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
  1701. static const char r59_bt_rev[] = {0x0e, 0x0e,
  1702. 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
  1703. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
  1704. rt2800_rfcsr_write(rt2x00dev, 55,
  1705. r55_bt_rev[idx]);
  1706. rt2800_rfcsr_write(rt2x00dev, 59,
  1707. r59_bt_rev[idx]);
  1708. } else {
  1709. static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
  1710. 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
  1711. 0x88, 0x88, 0x86, 0x85, 0x84};
  1712. rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
  1713. }
  1714. } else {
  1715. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
  1716. static const char r55_nonbt_rev[] = {0x23, 0x23,
  1717. 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
  1718. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
  1719. static const char r59_nonbt_rev[] = {0x07, 0x07,
  1720. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  1721. 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
  1722. rt2800_rfcsr_write(rt2x00dev, 55,
  1723. r55_nonbt_rev[idx]);
  1724. rt2800_rfcsr_write(rt2x00dev, 59,
  1725. r59_nonbt_rev[idx]);
  1726. } else if (rt2x00_rt(rt2x00dev, RT5390) ||
  1727. rt2x00_rt(rt2x00dev, RT5392)) {
  1728. static const char r59_non_bt[] = {0x8f, 0x8f,
  1729. 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
  1730. 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
  1731. rt2800_rfcsr_write(rt2x00dev, 59,
  1732. r59_non_bt[idx]);
  1733. }
  1734. }
  1735. }
  1736. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  1737. rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
  1738. rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
  1739. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1740. rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
  1741. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  1742. rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
  1743. }
  1744. static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
  1745. struct ieee80211_conf *conf,
  1746. struct rf_channel *rf,
  1747. struct channel_info *info)
  1748. {
  1749. u32 reg;
  1750. unsigned int tx_pin;
  1751. u8 bbp;
  1752. if (rf->channel <= 14) {
  1753. info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
  1754. info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
  1755. } else {
  1756. info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
  1757. info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
  1758. }
  1759. switch (rt2x00dev->chip.rf) {
  1760. case RF2020:
  1761. case RF3020:
  1762. case RF3021:
  1763. case RF3022:
  1764. case RF3320:
  1765. rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
  1766. break;
  1767. case RF3052:
  1768. rt2800_config_channel_rf3052(rt2x00dev, conf, rf, info);
  1769. break;
  1770. case RF5370:
  1771. case RF5372:
  1772. case RF5390:
  1773. rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
  1774. break;
  1775. default:
  1776. rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
  1777. }
  1778. /*
  1779. * Change BBP settings
  1780. */
  1781. rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
  1782. rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
  1783. rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
  1784. rt2800_bbp_write(rt2x00dev, 86, 0);
  1785. if (rf->channel <= 14) {
  1786. if (!rt2x00_rt(rt2x00dev, RT5390) &&
  1787. !rt2x00_rt(rt2x00dev, RT5392)) {
  1788. if (test_bit(CAPABILITY_EXTERNAL_LNA_BG,
  1789. &rt2x00dev->cap_flags)) {
  1790. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  1791. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  1792. } else {
  1793. rt2800_bbp_write(rt2x00dev, 82, 0x84);
  1794. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  1795. }
  1796. }
  1797. } else {
  1798. if (rt2x00_rt(rt2x00dev, RT3572))
  1799. rt2800_bbp_write(rt2x00dev, 82, 0x94);
  1800. else
  1801. rt2800_bbp_write(rt2x00dev, 82, 0xf2);
  1802. if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
  1803. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  1804. else
  1805. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  1806. }
  1807. rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
  1808. rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
  1809. rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
  1810. rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
  1811. rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
  1812. if (rt2x00_rt(rt2x00dev, RT3572))
  1813. rt2800_rfcsr_write(rt2x00dev, 8, 0);
  1814. tx_pin = 0;
  1815. /* Turn on unused PA or LNA when not using 1T or 1R */
  1816. if (rt2x00dev->default_ant.tx_chain_num == 2) {
  1817. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN,
  1818. rf->channel > 14);
  1819. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN,
  1820. rf->channel <= 14);
  1821. }
  1822. /* Turn on unused PA or LNA when not using 1T or 1R */
  1823. if (rt2x00dev->default_ant.rx_chain_num == 2) {
  1824. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
  1825. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
  1826. }
  1827. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
  1828. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
  1829. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
  1830. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
  1831. if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags))
  1832. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
  1833. else
  1834. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN,
  1835. rf->channel <= 14);
  1836. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
  1837. rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
  1838. if (rt2x00_rt(rt2x00dev, RT3572))
  1839. rt2800_rfcsr_write(rt2x00dev, 8, 0x80);
  1840. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  1841. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
  1842. rt2800_bbp_write(rt2x00dev, 4, bbp);
  1843. rt2800_bbp_read(rt2x00dev, 3, &bbp);
  1844. rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
  1845. rt2800_bbp_write(rt2x00dev, 3, bbp);
  1846. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
  1847. if (conf_is_ht40(conf)) {
  1848. rt2800_bbp_write(rt2x00dev, 69, 0x1a);
  1849. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  1850. rt2800_bbp_write(rt2x00dev, 73, 0x16);
  1851. } else {
  1852. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  1853. rt2800_bbp_write(rt2x00dev, 70, 0x08);
  1854. rt2800_bbp_write(rt2x00dev, 73, 0x11);
  1855. }
  1856. }
  1857. msleep(1);
  1858. /*
  1859. * Clear channel statistic counters
  1860. */
  1861. rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
  1862. rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
  1863. rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
  1864. }
  1865. static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
  1866. {
  1867. u8 tssi_bounds[9];
  1868. u8 current_tssi;
  1869. u16 eeprom;
  1870. u8 step;
  1871. int i;
  1872. /*
  1873. * First check if temperature compensation is supported.
  1874. */
  1875. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  1876. if (!rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC))
  1877. return 0;
  1878. /*
  1879. * Read TSSI boundaries for temperature compensation from
  1880. * the EEPROM.
  1881. *
  1882. * Array idx 0 1 2 3 4 5 6 7 8
  1883. * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
  1884. * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
  1885. */
  1886. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
  1887. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
  1888. tssi_bounds[0] = rt2x00_get_field16(eeprom,
  1889. EEPROM_TSSI_BOUND_BG1_MINUS4);
  1890. tssi_bounds[1] = rt2x00_get_field16(eeprom,
  1891. EEPROM_TSSI_BOUND_BG1_MINUS3);
  1892. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
  1893. tssi_bounds[2] = rt2x00_get_field16(eeprom,
  1894. EEPROM_TSSI_BOUND_BG2_MINUS2);
  1895. tssi_bounds[3] = rt2x00_get_field16(eeprom,
  1896. EEPROM_TSSI_BOUND_BG2_MINUS1);
  1897. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
  1898. tssi_bounds[4] = rt2x00_get_field16(eeprom,
  1899. EEPROM_TSSI_BOUND_BG3_REF);
  1900. tssi_bounds[5] = rt2x00_get_field16(eeprom,
  1901. EEPROM_TSSI_BOUND_BG3_PLUS1);
  1902. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
  1903. tssi_bounds[6] = rt2x00_get_field16(eeprom,
  1904. EEPROM_TSSI_BOUND_BG4_PLUS2);
  1905. tssi_bounds[7] = rt2x00_get_field16(eeprom,
  1906. EEPROM_TSSI_BOUND_BG4_PLUS3);
  1907. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
  1908. tssi_bounds[8] = rt2x00_get_field16(eeprom,
  1909. EEPROM_TSSI_BOUND_BG5_PLUS4);
  1910. step = rt2x00_get_field16(eeprom,
  1911. EEPROM_TSSI_BOUND_BG5_AGC_STEP);
  1912. } else {
  1913. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
  1914. tssi_bounds[0] = rt2x00_get_field16(eeprom,
  1915. EEPROM_TSSI_BOUND_A1_MINUS4);
  1916. tssi_bounds[1] = rt2x00_get_field16(eeprom,
  1917. EEPROM_TSSI_BOUND_A1_MINUS3);
  1918. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
  1919. tssi_bounds[2] = rt2x00_get_field16(eeprom,
  1920. EEPROM_TSSI_BOUND_A2_MINUS2);
  1921. tssi_bounds[3] = rt2x00_get_field16(eeprom,
  1922. EEPROM_TSSI_BOUND_A2_MINUS1);
  1923. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
  1924. tssi_bounds[4] = rt2x00_get_field16(eeprom,
  1925. EEPROM_TSSI_BOUND_A3_REF);
  1926. tssi_bounds[5] = rt2x00_get_field16(eeprom,
  1927. EEPROM_TSSI_BOUND_A3_PLUS1);
  1928. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
  1929. tssi_bounds[6] = rt2x00_get_field16(eeprom,
  1930. EEPROM_TSSI_BOUND_A4_PLUS2);
  1931. tssi_bounds[7] = rt2x00_get_field16(eeprom,
  1932. EEPROM_TSSI_BOUND_A4_PLUS3);
  1933. rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
  1934. tssi_bounds[8] = rt2x00_get_field16(eeprom,
  1935. EEPROM_TSSI_BOUND_A5_PLUS4);
  1936. step = rt2x00_get_field16(eeprom,
  1937. EEPROM_TSSI_BOUND_A5_AGC_STEP);
  1938. }
  1939. /*
  1940. * Check if temperature compensation is supported.
  1941. */
  1942. if (tssi_bounds[4] == 0xff || step == 0xff)
  1943. return 0;
  1944. /*
  1945. * Read current TSSI (BBP 49).
  1946. */
  1947. rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
  1948. /*
  1949. * Compare TSSI value (BBP49) with the compensation boundaries
  1950. * from the EEPROM and increase or decrease tx power.
  1951. */
  1952. for (i = 0; i <= 3; i++) {
  1953. if (current_tssi > tssi_bounds[i])
  1954. break;
  1955. }
  1956. if (i == 4) {
  1957. for (i = 8; i >= 5; i--) {
  1958. if (current_tssi < tssi_bounds[i])
  1959. break;
  1960. }
  1961. }
  1962. return (i - 4) * step;
  1963. }
  1964. static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
  1965. enum ieee80211_band band)
  1966. {
  1967. u16 eeprom;
  1968. u8 comp_en;
  1969. u8 comp_type;
  1970. int comp_value = 0;
  1971. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
  1972. /*
  1973. * HT40 compensation not required.
  1974. */
  1975. if (eeprom == 0xffff ||
  1976. !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
  1977. return 0;
  1978. if (band == IEEE80211_BAND_2GHZ) {
  1979. comp_en = rt2x00_get_field16(eeprom,
  1980. EEPROM_TXPOWER_DELTA_ENABLE_2G);
  1981. if (comp_en) {
  1982. comp_type = rt2x00_get_field16(eeprom,
  1983. EEPROM_TXPOWER_DELTA_TYPE_2G);
  1984. comp_value = rt2x00_get_field16(eeprom,
  1985. EEPROM_TXPOWER_DELTA_VALUE_2G);
  1986. if (!comp_type)
  1987. comp_value = -comp_value;
  1988. }
  1989. } else {
  1990. comp_en = rt2x00_get_field16(eeprom,
  1991. EEPROM_TXPOWER_DELTA_ENABLE_5G);
  1992. if (comp_en) {
  1993. comp_type = rt2x00_get_field16(eeprom,
  1994. EEPROM_TXPOWER_DELTA_TYPE_5G);
  1995. comp_value = rt2x00_get_field16(eeprom,
  1996. EEPROM_TXPOWER_DELTA_VALUE_5G);
  1997. if (!comp_type)
  1998. comp_value = -comp_value;
  1999. }
  2000. }
  2001. return comp_value;
  2002. }
  2003. static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
  2004. enum ieee80211_band band, int power_level,
  2005. u8 txpower, int delta)
  2006. {
  2007. u32 reg;
  2008. u16 eeprom;
  2009. u8 criterion;
  2010. u8 eirp_txpower;
  2011. u8 eirp_txpower_criterion;
  2012. u8 reg_limit;
  2013. if (!((band == IEEE80211_BAND_5GHZ) && is_rate_b))
  2014. return txpower;
  2015. if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags)) {
  2016. /*
  2017. * Check if eirp txpower exceed txpower_limit.
  2018. * We use OFDM 6M as criterion and its eirp txpower
  2019. * is stored at EEPROM_EIRP_MAX_TX_POWER.
  2020. * .11b data rate need add additional 4dbm
  2021. * when calculating eirp txpower.
  2022. */
  2023. rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
  2024. criterion = rt2x00_get_field32(reg, TX_PWR_CFG_0_6MBS);
  2025. rt2x00_eeprom_read(rt2x00dev,
  2026. EEPROM_EIRP_MAX_TX_POWER, &eeprom);
  2027. if (band == IEEE80211_BAND_2GHZ)
  2028. eirp_txpower_criterion = rt2x00_get_field16(eeprom,
  2029. EEPROM_EIRP_MAX_TX_POWER_2GHZ);
  2030. else
  2031. eirp_txpower_criterion = rt2x00_get_field16(eeprom,
  2032. EEPROM_EIRP_MAX_TX_POWER_5GHZ);
  2033. eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
  2034. (is_rate_b ? 4 : 0) + delta;
  2035. reg_limit = (eirp_txpower > power_level) ?
  2036. (eirp_txpower - power_level) : 0;
  2037. } else
  2038. reg_limit = 0;
  2039. return txpower + delta - reg_limit;
  2040. }
  2041. static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
  2042. enum ieee80211_band band,
  2043. int power_level)
  2044. {
  2045. u8 txpower;
  2046. u16 eeprom;
  2047. int i, is_rate_b;
  2048. u32 reg;
  2049. u8 r1;
  2050. u32 offset;
  2051. int delta;
  2052. /*
  2053. * Calculate HT40 compensation delta
  2054. */
  2055. delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
  2056. /*
  2057. * calculate temperature compensation delta
  2058. */
  2059. delta += rt2800_get_gain_calibration_delta(rt2x00dev);
  2060. /*
  2061. * set to normal bbp tx power control mode: +/- 0dBm
  2062. */
  2063. rt2800_bbp_read(rt2x00dev, 1, &r1);
  2064. rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, 0);
  2065. rt2800_bbp_write(rt2x00dev, 1, r1);
  2066. offset = TX_PWR_CFG_0;
  2067. for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
  2068. /* just to be safe */
  2069. if (offset > TX_PWR_CFG_4)
  2070. break;
  2071. rt2800_register_read(rt2x00dev, offset, &reg);
  2072. /* read the next four txpower values */
  2073. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
  2074. &eeprom);
  2075. is_rate_b = i ? 0 : 1;
  2076. /*
  2077. * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
  2078. * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
  2079. * TX_PWR_CFG_4: unknown
  2080. */
  2081. txpower = rt2x00_get_field16(eeprom,
  2082. EEPROM_TXPOWER_BYRATE_RATE0);
  2083. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2084. power_level, txpower, delta);
  2085. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
  2086. /*
  2087. * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
  2088. * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
  2089. * TX_PWR_CFG_4: unknown
  2090. */
  2091. txpower = rt2x00_get_field16(eeprom,
  2092. EEPROM_TXPOWER_BYRATE_RATE1);
  2093. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2094. power_level, txpower, delta);
  2095. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
  2096. /*
  2097. * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
  2098. * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
  2099. * TX_PWR_CFG_4: unknown
  2100. */
  2101. txpower = rt2x00_get_field16(eeprom,
  2102. EEPROM_TXPOWER_BYRATE_RATE2);
  2103. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2104. power_level, txpower, delta);
  2105. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
  2106. /*
  2107. * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
  2108. * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
  2109. * TX_PWR_CFG_4: unknown
  2110. */
  2111. txpower = rt2x00_get_field16(eeprom,
  2112. EEPROM_TXPOWER_BYRATE_RATE3);
  2113. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2114. power_level, txpower, delta);
  2115. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
  2116. /* read the next four txpower values */
  2117. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
  2118. &eeprom);
  2119. is_rate_b = 0;
  2120. /*
  2121. * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
  2122. * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
  2123. * TX_PWR_CFG_4: unknown
  2124. */
  2125. txpower = rt2x00_get_field16(eeprom,
  2126. EEPROM_TXPOWER_BYRATE_RATE0);
  2127. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2128. power_level, txpower, delta);
  2129. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
  2130. /*
  2131. * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
  2132. * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
  2133. * TX_PWR_CFG_4: unknown
  2134. */
  2135. txpower = rt2x00_get_field16(eeprom,
  2136. EEPROM_TXPOWER_BYRATE_RATE1);
  2137. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2138. power_level, txpower, delta);
  2139. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
  2140. /*
  2141. * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
  2142. * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
  2143. * TX_PWR_CFG_4: unknown
  2144. */
  2145. txpower = rt2x00_get_field16(eeprom,
  2146. EEPROM_TXPOWER_BYRATE_RATE2);
  2147. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2148. power_level, txpower, delta);
  2149. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
  2150. /*
  2151. * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
  2152. * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
  2153. * TX_PWR_CFG_4: unknown
  2154. */
  2155. txpower = rt2x00_get_field16(eeprom,
  2156. EEPROM_TXPOWER_BYRATE_RATE3);
  2157. txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
  2158. power_level, txpower, delta);
  2159. rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
  2160. rt2800_register_write(rt2x00dev, offset, reg);
  2161. /* next TX_PWR_CFG register */
  2162. offset += 4;
  2163. }
  2164. }
  2165. void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
  2166. {
  2167. rt2800_config_txpower(rt2x00dev, rt2x00dev->curr_band,
  2168. rt2x00dev->tx_power);
  2169. }
  2170. EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
  2171. void rt2800_vco_calibration(struct rt2x00_dev *rt2x00dev)
  2172. {
  2173. u32 tx_pin;
  2174. u8 rfcsr;
  2175. /*
  2176. * A voltage-controlled oscillator(VCO) is an electronic oscillator
  2177. * designed to be controlled in oscillation frequency by a voltage
  2178. * input. Maybe the temperature will affect the frequency of
  2179. * oscillation to be shifted. The VCO calibration will be called
  2180. * periodically to adjust the frequency to be precision.
  2181. */
  2182. rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
  2183. tx_pin &= TX_PIN_CFG_PA_PE_DISABLE;
  2184. rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
  2185. switch (rt2x00dev->chip.rf) {
  2186. case RF2020:
  2187. case RF3020:
  2188. case RF3021:
  2189. case RF3022:
  2190. case RF3320:
  2191. case RF3052:
  2192. rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
  2193. rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
  2194. rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
  2195. break;
  2196. case RF5370:
  2197. case RF5372:
  2198. case RF5390:
  2199. rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
  2200. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  2201. rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
  2202. break;
  2203. default:
  2204. return;
  2205. }
  2206. mdelay(1);
  2207. rt2800_register_read(rt2x00dev, TX_PIN_CFG, &tx_pin);
  2208. if (rt2x00dev->rf_channel <= 14) {
  2209. switch (rt2x00dev->default_ant.tx_chain_num) {
  2210. case 3:
  2211. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G2_EN, 1);
  2212. /* fall through */
  2213. case 2:
  2214. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
  2215. /* fall through */
  2216. case 1:
  2217. default:
  2218. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, 1);
  2219. break;
  2220. }
  2221. } else {
  2222. switch (rt2x00dev->default_ant.tx_chain_num) {
  2223. case 3:
  2224. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A2_EN, 1);
  2225. /* fall through */
  2226. case 2:
  2227. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
  2228. /* fall through */
  2229. case 1:
  2230. default:
  2231. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, 1);
  2232. break;
  2233. }
  2234. }
  2235. rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
  2236. }
  2237. EXPORT_SYMBOL_GPL(rt2800_vco_calibration);
  2238. static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  2239. struct rt2x00lib_conf *libconf)
  2240. {
  2241. u32 reg;
  2242. rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
  2243. rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
  2244. libconf->conf->short_frame_max_tx_count);
  2245. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
  2246. libconf->conf->long_frame_max_tx_count);
  2247. rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
  2248. }
  2249. static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
  2250. struct rt2x00lib_conf *libconf)
  2251. {
  2252. enum dev_state state =
  2253. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  2254. STATE_SLEEP : STATE_AWAKE;
  2255. u32 reg;
  2256. if (state == STATE_SLEEP) {
  2257. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
  2258. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  2259. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
  2260. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
  2261. libconf->conf->listen_interval - 1);
  2262. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
  2263. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  2264. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  2265. } else {
  2266. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  2267. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
  2268. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
  2269. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
  2270. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  2271. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  2272. }
  2273. }
  2274. void rt2800_config(struct rt2x00_dev *rt2x00dev,
  2275. struct rt2x00lib_conf *libconf,
  2276. const unsigned int flags)
  2277. {
  2278. /* Always recalculate LNA gain before changing configuration */
  2279. rt2800_config_lna_gain(rt2x00dev, libconf);
  2280. if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
  2281. rt2800_config_channel(rt2x00dev, libconf->conf,
  2282. &libconf->rf, &libconf->channel);
  2283. rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
  2284. libconf->conf->power_level);
  2285. }
  2286. if (flags & IEEE80211_CONF_CHANGE_POWER)
  2287. rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
  2288. libconf->conf->power_level);
  2289. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  2290. rt2800_config_retry_limit(rt2x00dev, libconf);
  2291. if (flags & IEEE80211_CONF_CHANGE_PS)
  2292. rt2800_config_ps(rt2x00dev, libconf);
  2293. }
  2294. EXPORT_SYMBOL_GPL(rt2800_config);
  2295. /*
  2296. * Link tuning
  2297. */
  2298. void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
  2299. {
  2300. u32 reg;
  2301. /*
  2302. * Update FCS error count from register.
  2303. */
  2304. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  2305. qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
  2306. }
  2307. EXPORT_SYMBOL_GPL(rt2800_link_stats);
  2308. static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
  2309. {
  2310. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
  2311. if (rt2x00_rt(rt2x00dev, RT3070) ||
  2312. rt2x00_rt(rt2x00dev, RT3071) ||
  2313. rt2x00_rt(rt2x00dev, RT3090) ||
  2314. rt2x00_rt(rt2x00dev, RT3390) ||
  2315. rt2x00_rt(rt2x00dev, RT5390) ||
  2316. rt2x00_rt(rt2x00dev, RT5392))
  2317. return 0x1c + (2 * rt2x00dev->lna_gain);
  2318. else
  2319. return 0x2e + rt2x00dev->lna_gain;
  2320. }
  2321. if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
  2322. return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
  2323. else
  2324. return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
  2325. }
  2326. static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
  2327. struct link_qual *qual, u8 vgc_level)
  2328. {
  2329. if (qual->vgc_level != vgc_level) {
  2330. rt2800_bbp_write(rt2x00dev, 66, vgc_level);
  2331. qual->vgc_level = vgc_level;
  2332. qual->vgc_level_reg = vgc_level;
  2333. }
  2334. }
  2335. void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
  2336. {
  2337. rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
  2338. }
  2339. EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
  2340. void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
  2341. const u32 count)
  2342. {
  2343. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
  2344. return;
  2345. /*
  2346. * When RSSI is better then -80 increase VGC level with 0x10
  2347. */
  2348. rt2800_set_vgc(rt2x00dev, qual,
  2349. rt2800_get_default_vgc(rt2x00dev) +
  2350. ((qual->rssi > -80) * 0x10));
  2351. }
  2352. EXPORT_SYMBOL_GPL(rt2800_link_tuner);
  2353. /*
  2354. * Initialization functions.
  2355. */
  2356. static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
  2357. {
  2358. u32 reg;
  2359. u16 eeprom;
  2360. unsigned int i;
  2361. int ret;
  2362. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  2363. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  2364. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  2365. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  2366. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  2367. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  2368. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  2369. ret = rt2800_drv_init_registers(rt2x00dev);
  2370. if (ret)
  2371. return ret;
  2372. rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
  2373. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
  2374. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
  2375. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
  2376. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
  2377. rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
  2378. rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
  2379. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
  2380. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
  2381. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
  2382. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
  2383. rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
  2384. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
  2385. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  2386. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  2387. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  2388. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
  2389. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
  2390. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
  2391. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
  2392. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  2393. rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
  2394. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  2395. rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
  2396. rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
  2397. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
  2398. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
  2399. rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
  2400. if (rt2x00_rt(rt2x00dev, RT3071) ||
  2401. rt2x00_rt(rt2x00dev, RT3090) ||
  2402. rt2x00_rt(rt2x00dev, RT3390)) {
  2403. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  2404. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
  2405. if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  2406. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
  2407. rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
  2408. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  2409. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
  2410. rt2800_register_write(rt2x00dev, TX_SW_CFG2,
  2411. 0x0000002c);
  2412. else
  2413. rt2800_register_write(rt2x00dev, TX_SW_CFG2,
  2414. 0x0000000f);
  2415. } else {
  2416. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
  2417. }
  2418. } else if (rt2x00_rt(rt2x00dev, RT3070)) {
  2419. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  2420. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
  2421. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
  2422. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
  2423. } else {
  2424. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  2425. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
  2426. }
  2427. } else if (rt2800_is_305x_soc(rt2x00dev)) {
  2428. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  2429. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
  2430. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
  2431. } else if (rt2x00_rt(rt2x00dev, RT3572)) {
  2432. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
  2433. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  2434. } else if (rt2x00_rt(rt2x00dev, RT5390) ||
  2435. rt2x00_rt(rt2x00dev, RT5392)) {
  2436. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
  2437. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  2438. rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
  2439. } else {
  2440. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
  2441. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  2442. }
  2443. rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
  2444. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
  2445. rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
  2446. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
  2447. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
  2448. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
  2449. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
  2450. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
  2451. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
  2452. rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
  2453. rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
  2454. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
  2455. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
  2456. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
  2457. rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
  2458. rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
  2459. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
  2460. if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
  2461. rt2x00_rt(rt2x00dev, RT2883) ||
  2462. rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
  2463. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
  2464. else
  2465. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
  2466. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
  2467. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
  2468. rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
  2469. rt2800_register_read(rt2x00dev, LED_CFG, &reg);
  2470. rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
  2471. rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
  2472. rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
  2473. rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
  2474. rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
  2475. rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
  2476. rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
  2477. rt2800_register_write(rt2x00dev, LED_CFG, reg);
  2478. rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
  2479. rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
  2480. rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
  2481. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
  2482. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
  2483. rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
  2484. rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
  2485. rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
  2486. rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
  2487. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  2488. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
  2489. rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
  2490. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
  2491. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
  2492. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
  2493. rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
  2494. rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
  2495. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  2496. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  2497. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
  2498. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
  2499. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
  2500. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  2501. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  2502. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  2503. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  2504. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  2505. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  2506. rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
  2507. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  2508. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  2509. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
  2510. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
  2511. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
  2512. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  2513. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  2514. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  2515. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  2516. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  2517. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  2518. rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
  2519. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  2520. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  2521. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
  2522. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
  2523. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
  2524. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  2525. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  2526. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  2527. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  2528. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  2529. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  2530. rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
  2531. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  2532. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  2533. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
  2534. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
  2535. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
  2536. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  2537. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  2538. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  2539. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  2540. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  2541. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  2542. rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
  2543. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  2544. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  2545. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
  2546. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
  2547. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
  2548. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  2549. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  2550. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  2551. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  2552. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  2553. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  2554. rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
  2555. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  2556. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  2557. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
  2558. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
  2559. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
  2560. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  2561. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  2562. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  2563. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  2564. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  2565. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  2566. rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
  2567. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  2568. if (rt2x00_is_usb(rt2x00dev)) {
  2569. rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
  2570. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  2571. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  2572. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  2573. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  2574. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  2575. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
  2576. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
  2577. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
  2578. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
  2579. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
  2580. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  2581. }
  2582. /*
  2583. * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
  2584. * although it is reserved.
  2585. */
  2586. rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
  2587. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
  2588. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
  2589. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
  2590. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
  2591. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
  2592. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
  2593. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
  2594. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
  2595. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
  2596. rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
  2597. rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
  2598. rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
  2599. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  2600. rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
  2601. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
  2602. IEEE80211_MAX_RTS_THRESHOLD);
  2603. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
  2604. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  2605. rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
  2606. /*
  2607. * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
  2608. * time should be set to 16. However, the original Ralink driver uses
  2609. * 16 for both and indeed using a value of 10 for CCK SIFS results in
  2610. * connection problems with 11g + CTS protection. Hence, use the same
  2611. * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
  2612. */
  2613. rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
  2614. rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
  2615. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
  2616. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
  2617. rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
  2618. rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
  2619. rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
  2620. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  2621. /*
  2622. * ASIC will keep garbage value after boot, clear encryption keys.
  2623. */
  2624. for (i = 0; i < 4; i++)
  2625. rt2800_register_write(rt2x00dev,
  2626. SHARED_KEY_MODE_ENTRY(i), 0);
  2627. for (i = 0; i < 256; i++) {
  2628. rt2800_config_wcid(rt2x00dev, NULL, i);
  2629. rt2800_delete_wcid_attr(rt2x00dev, i);
  2630. rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
  2631. }
  2632. /*
  2633. * Clear all beacons
  2634. */
  2635. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
  2636. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
  2637. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
  2638. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
  2639. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
  2640. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
  2641. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
  2642. rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
  2643. if (rt2x00_is_usb(rt2x00dev)) {
  2644. rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
  2645. rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
  2646. rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
  2647. } else if (rt2x00_is_pcie(rt2x00dev)) {
  2648. rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
  2649. rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
  2650. rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
  2651. }
  2652. rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
  2653. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
  2654. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
  2655. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
  2656. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
  2657. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
  2658. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
  2659. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
  2660. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
  2661. rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
  2662. rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
  2663. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
  2664. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
  2665. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
  2666. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
  2667. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
  2668. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
  2669. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
  2670. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
  2671. rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
  2672. rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
  2673. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
  2674. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
  2675. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
  2676. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
  2677. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
  2678. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
  2679. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
  2680. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
  2681. rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
  2682. rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
  2683. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
  2684. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
  2685. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
  2686. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
  2687. rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
  2688. /*
  2689. * Do not force the BA window size, we use the TXWI to set it
  2690. */
  2691. rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
  2692. rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
  2693. rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
  2694. rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
  2695. /*
  2696. * We must clear the error counters.
  2697. * These registers are cleared on read,
  2698. * so we may pass a useless variable to store the value.
  2699. */
  2700. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  2701. rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
  2702. rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
  2703. rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
  2704. rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
  2705. rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
  2706. /*
  2707. * Setup leadtime for pre tbtt interrupt to 6ms
  2708. */
  2709. rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
  2710. rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
  2711. rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
  2712. /*
  2713. * Set up channel statistics timer
  2714. */
  2715. rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
  2716. rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
  2717. rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
  2718. rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
  2719. rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
  2720. rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
  2721. rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
  2722. return 0;
  2723. }
  2724. static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
  2725. {
  2726. unsigned int i;
  2727. u32 reg;
  2728. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  2729. rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
  2730. if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
  2731. return 0;
  2732. udelay(REGISTER_BUSY_DELAY);
  2733. }
  2734. ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
  2735. return -EACCES;
  2736. }
  2737. static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  2738. {
  2739. unsigned int i;
  2740. u8 value;
  2741. /*
  2742. * BBP was enabled after firmware was loaded,
  2743. * but we need to reactivate it now.
  2744. */
  2745. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  2746. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  2747. msleep(1);
  2748. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  2749. rt2800_bbp_read(rt2x00dev, 0, &value);
  2750. if ((value != 0xff) && (value != 0x00))
  2751. return 0;
  2752. udelay(REGISTER_BUSY_DELAY);
  2753. }
  2754. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  2755. return -EACCES;
  2756. }
  2757. static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
  2758. {
  2759. unsigned int i;
  2760. u16 eeprom;
  2761. u8 reg_id;
  2762. u8 value;
  2763. if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
  2764. rt2800_wait_bbp_ready(rt2x00dev)))
  2765. return -EACCES;
  2766. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2767. rt2x00_rt(rt2x00dev, RT5392)) {
  2768. rt2800_bbp_read(rt2x00dev, 4, &value);
  2769. rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
  2770. rt2800_bbp_write(rt2x00dev, 4, value);
  2771. }
  2772. if (rt2800_is_305x_soc(rt2x00dev) ||
  2773. rt2x00_rt(rt2x00dev, RT3572) ||
  2774. rt2x00_rt(rt2x00dev, RT5390) ||
  2775. rt2x00_rt(rt2x00dev, RT5392))
  2776. rt2800_bbp_write(rt2x00dev, 31, 0x08);
  2777. rt2800_bbp_write(rt2x00dev, 65, 0x2c);
  2778. rt2800_bbp_write(rt2x00dev, 66, 0x38);
  2779. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2780. rt2x00_rt(rt2x00dev, RT5392))
  2781. rt2800_bbp_write(rt2x00dev, 68, 0x0b);
  2782. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
  2783. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  2784. rt2800_bbp_write(rt2x00dev, 73, 0x12);
  2785. } else if (rt2x00_rt(rt2x00dev, RT5390) ||
  2786. rt2x00_rt(rt2x00dev, RT5392)) {
  2787. rt2800_bbp_write(rt2x00dev, 69, 0x12);
  2788. rt2800_bbp_write(rt2x00dev, 73, 0x13);
  2789. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  2790. rt2800_bbp_write(rt2x00dev, 76, 0x28);
  2791. rt2800_bbp_write(rt2x00dev, 77, 0x59);
  2792. } else {
  2793. rt2800_bbp_write(rt2x00dev, 69, 0x12);
  2794. rt2800_bbp_write(rt2x00dev, 73, 0x10);
  2795. }
  2796. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  2797. if (rt2x00_rt(rt2x00dev, RT3070) ||
  2798. rt2x00_rt(rt2x00dev, RT3071) ||
  2799. rt2x00_rt(rt2x00dev, RT3090) ||
  2800. rt2x00_rt(rt2x00dev, RT3390) ||
  2801. rt2x00_rt(rt2x00dev, RT3572) ||
  2802. rt2x00_rt(rt2x00dev, RT5390) ||
  2803. rt2x00_rt(rt2x00dev, RT5392)) {
  2804. rt2800_bbp_write(rt2x00dev, 79, 0x13);
  2805. rt2800_bbp_write(rt2x00dev, 80, 0x05);
  2806. rt2800_bbp_write(rt2x00dev, 81, 0x33);
  2807. } else if (rt2800_is_305x_soc(rt2x00dev)) {
  2808. rt2800_bbp_write(rt2x00dev, 78, 0x0e);
  2809. rt2800_bbp_write(rt2x00dev, 80, 0x08);
  2810. } else {
  2811. rt2800_bbp_write(rt2x00dev, 81, 0x37);
  2812. }
  2813. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  2814. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2815. rt2x00_rt(rt2x00dev, RT5392))
  2816. rt2800_bbp_write(rt2x00dev, 83, 0x7a);
  2817. else
  2818. rt2800_bbp_write(rt2x00dev, 83, 0x6a);
  2819. if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
  2820. rt2800_bbp_write(rt2x00dev, 84, 0x19);
  2821. else if (rt2x00_rt(rt2x00dev, RT5390) ||
  2822. rt2x00_rt(rt2x00dev, RT5392))
  2823. rt2800_bbp_write(rt2x00dev, 84, 0x9a);
  2824. else
  2825. rt2800_bbp_write(rt2x00dev, 84, 0x99);
  2826. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2827. rt2x00_rt(rt2x00dev, RT5392))
  2828. rt2800_bbp_write(rt2x00dev, 86, 0x38);
  2829. else
  2830. rt2800_bbp_write(rt2x00dev, 86, 0x00);
  2831. if (rt2x00_rt(rt2x00dev, RT5392))
  2832. rt2800_bbp_write(rt2x00dev, 88, 0x90);
  2833. rt2800_bbp_write(rt2x00dev, 91, 0x04);
  2834. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2835. rt2x00_rt(rt2x00dev, RT5392))
  2836. rt2800_bbp_write(rt2x00dev, 92, 0x02);
  2837. else
  2838. rt2800_bbp_write(rt2x00dev, 92, 0x00);
  2839. if (rt2x00_rt(rt2x00dev, RT5392)) {
  2840. rt2800_bbp_write(rt2x00dev, 95, 0x9a);
  2841. rt2800_bbp_write(rt2x00dev, 98, 0x12);
  2842. }
  2843. if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
  2844. rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
  2845. rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
  2846. rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
  2847. rt2x00_rt(rt2x00dev, RT3572) ||
  2848. rt2x00_rt(rt2x00dev, RT5390) ||
  2849. rt2x00_rt(rt2x00dev, RT5392) ||
  2850. rt2800_is_305x_soc(rt2x00dev))
  2851. rt2800_bbp_write(rt2x00dev, 103, 0xc0);
  2852. else
  2853. rt2800_bbp_write(rt2x00dev, 103, 0x00);
  2854. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2855. rt2x00_rt(rt2x00dev, RT5392))
  2856. rt2800_bbp_write(rt2x00dev, 104, 0x92);
  2857. if (rt2800_is_305x_soc(rt2x00dev))
  2858. rt2800_bbp_write(rt2x00dev, 105, 0x01);
  2859. else if (rt2x00_rt(rt2x00dev, RT5390) ||
  2860. rt2x00_rt(rt2x00dev, RT5392))
  2861. rt2800_bbp_write(rt2x00dev, 105, 0x3c);
  2862. else
  2863. rt2800_bbp_write(rt2x00dev, 105, 0x05);
  2864. if (rt2x00_rt(rt2x00dev, RT5390))
  2865. rt2800_bbp_write(rt2x00dev, 106, 0x03);
  2866. else if (rt2x00_rt(rt2x00dev, RT5392))
  2867. rt2800_bbp_write(rt2x00dev, 106, 0x12);
  2868. else
  2869. rt2800_bbp_write(rt2x00dev, 106, 0x35);
  2870. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2871. rt2x00_rt(rt2x00dev, RT5392))
  2872. rt2800_bbp_write(rt2x00dev, 128, 0x12);
  2873. if (rt2x00_rt(rt2x00dev, RT5392)) {
  2874. rt2800_bbp_write(rt2x00dev, 134, 0xd0);
  2875. rt2800_bbp_write(rt2x00dev, 135, 0xf6);
  2876. }
  2877. if (rt2x00_rt(rt2x00dev, RT3071) ||
  2878. rt2x00_rt(rt2x00dev, RT3090) ||
  2879. rt2x00_rt(rt2x00dev, RT3390) ||
  2880. rt2x00_rt(rt2x00dev, RT3572) ||
  2881. rt2x00_rt(rt2x00dev, RT5390) ||
  2882. rt2x00_rt(rt2x00dev, RT5392)) {
  2883. rt2800_bbp_read(rt2x00dev, 138, &value);
  2884. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  2885. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
  2886. value |= 0x20;
  2887. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
  2888. value &= ~0x02;
  2889. rt2800_bbp_write(rt2x00dev, 138, value);
  2890. }
  2891. if (rt2x00_rt(rt2x00dev, RT5390) ||
  2892. rt2x00_rt(rt2x00dev, RT5392)) {
  2893. int ant, div_mode;
  2894. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  2895. div_mode = rt2x00_get_field16(eeprom,
  2896. EEPROM_NIC_CONF1_ANT_DIVERSITY);
  2897. ant = (div_mode == 3) ? 1 : 0;
  2898. /* check if this is a Bluetooth combo card */
  2899. if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
  2900. u32 reg;
  2901. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  2902. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT3, 0);
  2903. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT6, 0);
  2904. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, 0);
  2905. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT6, 0);
  2906. if (ant == 0)
  2907. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, 1);
  2908. else if (ant == 1)
  2909. rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT6, 1);
  2910. rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
  2911. }
  2912. rt2800_bbp_read(rt2x00dev, 152, &value);
  2913. if (ant == 0)
  2914. rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
  2915. else
  2916. rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
  2917. rt2800_bbp_write(rt2x00dev, 152, value);
  2918. /* Init frequency calibration */
  2919. rt2800_bbp_write(rt2x00dev, 142, 1);
  2920. rt2800_bbp_write(rt2x00dev, 143, 57);
  2921. }
  2922. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  2923. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  2924. if (eeprom != 0xffff && eeprom != 0x0000) {
  2925. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  2926. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  2927. rt2800_bbp_write(rt2x00dev, reg_id, value);
  2928. }
  2929. }
  2930. return 0;
  2931. }
  2932. static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
  2933. bool bw40, u8 rfcsr24, u8 filter_target)
  2934. {
  2935. unsigned int i;
  2936. u8 bbp;
  2937. u8 rfcsr;
  2938. u8 passband;
  2939. u8 stopband;
  2940. u8 overtuned = 0;
  2941. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  2942. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  2943. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
  2944. rt2800_bbp_write(rt2x00dev, 4, bbp);
  2945. rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
  2946. rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
  2947. rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
  2948. rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
  2949. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
  2950. rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
  2951. /*
  2952. * Set power & frequency of passband test tone
  2953. */
  2954. rt2800_bbp_write(rt2x00dev, 24, 0);
  2955. for (i = 0; i < 100; i++) {
  2956. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  2957. msleep(1);
  2958. rt2800_bbp_read(rt2x00dev, 55, &passband);
  2959. if (passband)
  2960. break;
  2961. }
  2962. /*
  2963. * Set power & frequency of stopband test tone
  2964. */
  2965. rt2800_bbp_write(rt2x00dev, 24, 0x06);
  2966. for (i = 0; i < 100; i++) {
  2967. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  2968. msleep(1);
  2969. rt2800_bbp_read(rt2x00dev, 55, &stopband);
  2970. if ((passband - stopband) <= filter_target) {
  2971. rfcsr24++;
  2972. overtuned += ((passband - stopband) == filter_target);
  2973. } else
  2974. break;
  2975. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  2976. }
  2977. rfcsr24 -= !!overtuned;
  2978. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  2979. return rfcsr24;
  2980. }
  2981. static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
  2982. {
  2983. struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
  2984. u8 rfcsr;
  2985. u8 bbp;
  2986. u32 reg;
  2987. u16 eeprom;
  2988. if (!rt2x00_rt(rt2x00dev, RT3070) &&
  2989. !rt2x00_rt(rt2x00dev, RT3071) &&
  2990. !rt2x00_rt(rt2x00dev, RT3090) &&
  2991. !rt2x00_rt(rt2x00dev, RT3390) &&
  2992. !rt2x00_rt(rt2x00dev, RT3572) &&
  2993. !rt2x00_rt(rt2x00dev, RT5390) &&
  2994. !rt2x00_rt(rt2x00dev, RT5392) &&
  2995. !rt2800_is_305x_soc(rt2x00dev))
  2996. return 0;
  2997. /*
  2998. * Init RF calibration.
  2999. */
  3000. if (rt2x00_rt(rt2x00dev, RT5390) ||
  3001. rt2x00_rt(rt2x00dev, RT5392)) {
  3002. rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
  3003. rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
  3004. rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
  3005. msleep(1);
  3006. rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 0);
  3007. rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
  3008. } else {
  3009. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  3010. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  3011. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  3012. msleep(1);
  3013. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
  3014. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  3015. }
  3016. if (rt2x00_rt(rt2x00dev, RT3070) ||
  3017. rt2x00_rt(rt2x00dev, RT3071) ||
  3018. rt2x00_rt(rt2x00dev, RT3090)) {
  3019. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  3020. rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
  3021. rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
  3022. rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
  3023. rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
  3024. rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
  3025. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  3026. rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
  3027. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  3028. rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
  3029. rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
  3030. rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
  3031. rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
  3032. rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
  3033. rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
  3034. rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
  3035. rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
  3036. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  3037. rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
  3038. } else if (rt2x00_rt(rt2x00dev, RT3390)) {
  3039. rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
  3040. rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
  3041. rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
  3042. rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
  3043. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  3044. rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
  3045. rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
  3046. rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
  3047. rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
  3048. rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
  3049. rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
  3050. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  3051. rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
  3052. rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
  3053. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  3054. rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
  3055. rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
  3056. rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
  3057. rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
  3058. rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
  3059. rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
  3060. rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
  3061. rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
  3062. rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
  3063. rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
  3064. rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
  3065. rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
  3066. rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
  3067. rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
  3068. rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
  3069. rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
  3070. rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
  3071. } else if (rt2x00_rt(rt2x00dev, RT3572)) {
  3072. rt2800_rfcsr_write(rt2x00dev, 0, 0x70);
  3073. rt2800_rfcsr_write(rt2x00dev, 1, 0x81);
  3074. rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
  3075. rt2800_rfcsr_write(rt2x00dev, 3, 0x02);
  3076. rt2800_rfcsr_write(rt2x00dev, 4, 0x4c);
  3077. rt2800_rfcsr_write(rt2x00dev, 5, 0x05);
  3078. rt2800_rfcsr_write(rt2x00dev, 6, 0x4a);
  3079. rt2800_rfcsr_write(rt2x00dev, 7, 0xd8);
  3080. rt2800_rfcsr_write(rt2x00dev, 9, 0xc3);
  3081. rt2800_rfcsr_write(rt2x00dev, 10, 0xf1);
  3082. rt2800_rfcsr_write(rt2x00dev, 11, 0xb9);
  3083. rt2800_rfcsr_write(rt2x00dev, 12, 0x70);
  3084. rt2800_rfcsr_write(rt2x00dev, 13, 0x65);
  3085. rt2800_rfcsr_write(rt2x00dev, 14, 0xa0);
  3086. rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
  3087. rt2800_rfcsr_write(rt2x00dev, 16, 0x4c);
  3088. rt2800_rfcsr_write(rt2x00dev, 17, 0x23);
  3089. rt2800_rfcsr_write(rt2x00dev, 18, 0xac);
  3090. rt2800_rfcsr_write(rt2x00dev, 19, 0x93);
  3091. rt2800_rfcsr_write(rt2x00dev, 20, 0xb3);
  3092. rt2800_rfcsr_write(rt2x00dev, 21, 0xd0);
  3093. rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
  3094. rt2800_rfcsr_write(rt2x00dev, 23, 0x3c);
  3095. rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
  3096. rt2800_rfcsr_write(rt2x00dev, 25, 0x15);
  3097. rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
  3098. rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
  3099. rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
  3100. rt2800_rfcsr_write(rt2x00dev, 29, 0x9b);
  3101. rt2800_rfcsr_write(rt2x00dev, 30, 0x09);
  3102. rt2800_rfcsr_write(rt2x00dev, 31, 0x10);
  3103. } else if (rt2800_is_305x_soc(rt2x00dev)) {
  3104. rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
  3105. rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
  3106. rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
  3107. rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
  3108. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  3109. rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
  3110. rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
  3111. rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
  3112. rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
  3113. rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
  3114. rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
  3115. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  3116. rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
  3117. rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
  3118. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  3119. rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
  3120. rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
  3121. rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
  3122. rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
  3123. rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
  3124. rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
  3125. rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
  3126. rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
  3127. rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
  3128. rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
  3129. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  3130. rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
  3131. rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
  3132. rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
  3133. rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
  3134. rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
  3135. rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
  3136. return 0;
  3137. } else if (rt2x00_rt(rt2x00dev, RT5390)) {
  3138. rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
  3139. rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
  3140. rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
  3141. rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
  3142. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
  3143. rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
  3144. else
  3145. rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
  3146. rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
  3147. rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
  3148. rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
  3149. rt2800_rfcsr_write(rt2x00dev, 12, 0xc6);
  3150. rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
  3151. rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
  3152. rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
  3153. rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
  3154. rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
  3155. rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
  3156. rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
  3157. rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
  3158. rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
  3159. rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
  3160. rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
  3161. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
  3162. rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
  3163. else
  3164. rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
  3165. rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
  3166. rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
  3167. rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
  3168. rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
  3169. rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
  3170. rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
  3171. rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
  3172. rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
  3173. rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
  3174. rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
  3175. rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
  3176. rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
  3177. rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
  3178. rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
  3179. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
  3180. rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
  3181. else
  3182. rt2800_rfcsr_write(rt2x00dev, 40, 0x4b);
  3183. rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
  3184. rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
  3185. rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
  3186. rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
  3187. rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
  3188. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
  3189. rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
  3190. else
  3191. rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
  3192. rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
  3193. rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
  3194. rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
  3195. rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
  3196. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
  3197. rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
  3198. else
  3199. rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
  3200. rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
  3201. rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
  3202. rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
  3203. rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
  3204. rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
  3205. rt2800_rfcsr_write(rt2x00dev, 59, 0x63);
  3206. rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
  3207. if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
  3208. rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
  3209. else
  3210. rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
  3211. rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
  3212. rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
  3213. } else if (rt2x00_rt(rt2x00dev, RT5392)) {
  3214. rt2800_rfcsr_write(rt2x00dev, 1, 0x17);
  3215. rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
  3216. rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
  3217. rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
  3218. rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
  3219. rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
  3220. rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
  3221. rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
  3222. rt2800_rfcsr_write(rt2x00dev, 12, 0x46);
  3223. rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
  3224. rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
  3225. rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
  3226. rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
  3227. rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
  3228. rt2800_rfcsr_write(rt2x00dev, 19, 0x4d);
  3229. rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
  3230. rt2800_rfcsr_write(rt2x00dev, 21, 0x8d);
  3231. rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
  3232. rt2800_rfcsr_write(rt2x00dev, 23, 0x0b);
  3233. rt2800_rfcsr_write(rt2x00dev, 24, 0x44);
  3234. rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
  3235. rt2800_rfcsr_write(rt2x00dev, 26, 0x82);
  3236. rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
  3237. rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
  3238. rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
  3239. rt2800_rfcsr_write(rt2x00dev, 30, 0x10);
  3240. rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
  3241. rt2800_rfcsr_write(rt2x00dev, 32, 0x20);
  3242. rt2800_rfcsr_write(rt2x00dev, 33, 0xC0);
  3243. rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
  3244. rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
  3245. rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
  3246. rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
  3247. rt2800_rfcsr_write(rt2x00dev, 38, 0x89);
  3248. rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
  3249. rt2800_rfcsr_write(rt2x00dev, 40, 0x0f);
  3250. rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
  3251. rt2800_rfcsr_write(rt2x00dev, 42, 0xd5);
  3252. rt2800_rfcsr_write(rt2x00dev, 43, 0x9b);
  3253. rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
  3254. rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
  3255. rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
  3256. rt2800_rfcsr_write(rt2x00dev, 47, 0x0c);
  3257. rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
  3258. rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
  3259. rt2800_rfcsr_write(rt2x00dev, 50, 0x94);
  3260. rt2800_rfcsr_write(rt2x00dev, 51, 0x3a);
  3261. rt2800_rfcsr_write(rt2x00dev, 52, 0x48);
  3262. rt2800_rfcsr_write(rt2x00dev, 53, 0x44);
  3263. rt2800_rfcsr_write(rt2x00dev, 54, 0x38);
  3264. rt2800_rfcsr_write(rt2x00dev, 55, 0x43);
  3265. rt2800_rfcsr_write(rt2x00dev, 56, 0xa1);
  3266. rt2800_rfcsr_write(rt2x00dev, 57, 0x00);
  3267. rt2800_rfcsr_write(rt2x00dev, 58, 0x39);
  3268. rt2800_rfcsr_write(rt2x00dev, 59, 0x07);
  3269. rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
  3270. rt2800_rfcsr_write(rt2x00dev, 61, 0x91);
  3271. rt2800_rfcsr_write(rt2x00dev, 62, 0x39);
  3272. rt2800_rfcsr_write(rt2x00dev, 63, 0x07);
  3273. }
  3274. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
  3275. rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
  3276. rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
  3277. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
  3278. rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
  3279. } else if (rt2x00_rt(rt2x00dev, RT3071) ||
  3280. rt2x00_rt(rt2x00dev, RT3090)) {
  3281. rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
  3282. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  3283. rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
  3284. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  3285. rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
  3286. rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
  3287. if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  3288. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
  3289. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  3290. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
  3291. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
  3292. else
  3293. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
  3294. }
  3295. rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
  3296. rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
  3297. rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
  3298. rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
  3299. } else if (rt2x00_rt(rt2x00dev, RT3390)) {
  3300. rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
  3301. rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
  3302. rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
  3303. } else if (rt2x00_rt(rt2x00dev, RT3572)) {
  3304. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  3305. rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
  3306. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  3307. rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
  3308. rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
  3309. rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
  3310. rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
  3311. msleep(1);
  3312. rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
  3313. rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
  3314. rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
  3315. }
  3316. /*
  3317. * Set RX Filter calibration for 20MHz and 40MHz
  3318. */
  3319. if (rt2x00_rt(rt2x00dev, RT3070)) {
  3320. drv_data->calibration_bw20 =
  3321. rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
  3322. drv_data->calibration_bw40 =
  3323. rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
  3324. } else if (rt2x00_rt(rt2x00dev, RT3071) ||
  3325. rt2x00_rt(rt2x00dev, RT3090) ||
  3326. rt2x00_rt(rt2x00dev, RT3390) ||
  3327. rt2x00_rt(rt2x00dev, RT3572)) {
  3328. drv_data->calibration_bw20 =
  3329. rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
  3330. drv_data->calibration_bw40 =
  3331. rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
  3332. }
  3333. /*
  3334. * Save BBP 25 & 26 values for later use in channel switching
  3335. */
  3336. rt2800_bbp_read(rt2x00dev, 25, &drv_data->bbp25);
  3337. rt2800_bbp_read(rt2x00dev, 26, &drv_data->bbp26);
  3338. if (!rt2x00_rt(rt2x00dev, RT5390) &&
  3339. !rt2x00_rt(rt2x00dev, RT5392)) {
  3340. /*
  3341. * Set back to initial state
  3342. */
  3343. rt2800_bbp_write(rt2x00dev, 24, 0);
  3344. rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
  3345. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
  3346. rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
  3347. /*
  3348. * Set BBP back to BW20
  3349. */
  3350. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  3351. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
  3352. rt2800_bbp_write(rt2x00dev, 4, bbp);
  3353. }
  3354. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
  3355. rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  3356. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
  3357. rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
  3358. rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
  3359. rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
  3360. rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
  3361. rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
  3362. if (!rt2x00_rt(rt2x00dev, RT5390) &&
  3363. !rt2x00_rt(rt2x00dev, RT5392)) {
  3364. rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
  3365. rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
  3366. if (rt2x00_rt(rt2x00dev, RT3070) ||
  3367. rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
  3368. rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
  3369. rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
  3370. if (!test_bit(CAPABILITY_EXTERNAL_LNA_BG,
  3371. &rt2x00dev->cap_flags))
  3372. rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
  3373. }
  3374. rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
  3375. drv_data->txmixer_gain_24g);
  3376. rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
  3377. }
  3378. if (rt2x00_rt(rt2x00dev, RT3090)) {
  3379. rt2800_bbp_read(rt2x00dev, 138, &bbp);
  3380. /* Turn off unused DAC1 and ADC1 to reduce power consumption */
  3381. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  3382. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
  3383. rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
  3384. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
  3385. rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
  3386. rt2800_bbp_write(rt2x00dev, 138, bbp);
  3387. }
  3388. if (rt2x00_rt(rt2x00dev, RT3071) ||
  3389. rt2x00_rt(rt2x00dev, RT3090) ||
  3390. rt2x00_rt(rt2x00dev, RT3390)) {
  3391. rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
  3392. rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
  3393. rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
  3394. rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
  3395. rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
  3396. rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
  3397. rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
  3398. rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
  3399. rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
  3400. rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
  3401. rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
  3402. rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
  3403. rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
  3404. rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
  3405. rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
  3406. rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
  3407. }
  3408. if (rt2x00_rt(rt2x00dev, RT3070)) {
  3409. rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
  3410. if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
  3411. rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
  3412. else
  3413. rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
  3414. rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
  3415. rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
  3416. rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
  3417. rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
  3418. }
  3419. if (rt2x00_rt(rt2x00dev, RT5390) ||
  3420. rt2x00_rt(rt2x00dev, RT5392)) {
  3421. rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
  3422. rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
  3423. rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
  3424. rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
  3425. rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
  3426. rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
  3427. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  3428. rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
  3429. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  3430. }
  3431. return 0;
  3432. }
  3433. int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
  3434. {
  3435. u32 reg;
  3436. u16 word;
  3437. /*
  3438. * Initialize all registers.
  3439. */
  3440. if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
  3441. rt2800_init_registers(rt2x00dev) ||
  3442. rt2800_init_bbp(rt2x00dev) ||
  3443. rt2800_init_rfcsr(rt2x00dev)))
  3444. return -EIO;
  3445. /*
  3446. * Send signal to firmware during boot time.
  3447. */
  3448. rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
  3449. if (rt2x00_is_usb(rt2x00dev) &&
  3450. (rt2x00_rt(rt2x00dev, RT3070) ||
  3451. rt2x00_rt(rt2x00dev, RT3071) ||
  3452. rt2x00_rt(rt2x00dev, RT3572))) {
  3453. udelay(200);
  3454. rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
  3455. udelay(10);
  3456. }
  3457. /*
  3458. * Enable RX.
  3459. */
  3460. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  3461. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  3462. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  3463. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  3464. udelay(50);
  3465. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  3466. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
  3467. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
  3468. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
  3469. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  3470. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  3471. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  3472. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  3473. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  3474. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  3475. /*
  3476. * Initialize LED control
  3477. */
  3478. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
  3479. rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
  3480. word & 0xff, (word >> 8) & 0xff);
  3481. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
  3482. rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
  3483. word & 0xff, (word >> 8) & 0xff);
  3484. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
  3485. rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
  3486. word & 0xff, (word >> 8) & 0xff);
  3487. return 0;
  3488. }
  3489. EXPORT_SYMBOL_GPL(rt2800_enable_radio);
  3490. void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
  3491. {
  3492. u32 reg;
  3493. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  3494. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  3495. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  3496. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  3497. /* Wait for DMA, ignore error */
  3498. rt2800_wait_wpdma_ready(rt2x00dev);
  3499. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  3500. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
  3501. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  3502. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  3503. }
  3504. EXPORT_SYMBOL_GPL(rt2800_disable_radio);
  3505. int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
  3506. {
  3507. u32 reg;
  3508. rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
  3509. return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
  3510. }
  3511. EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
  3512. static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
  3513. {
  3514. u32 reg;
  3515. mutex_lock(&rt2x00dev->csr_mutex);
  3516. rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
  3517. rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
  3518. rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
  3519. rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
  3520. rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
  3521. /* Wait until the EEPROM has been loaded */
  3522. rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
  3523. /* Apparently the data is read from end to start */
  3524. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3, &reg);
  3525. /* The returned value is in CPU order, but eeprom is le */
  3526. *(u32 *)&rt2x00dev->eeprom[i] = cpu_to_le32(reg);
  3527. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2, &reg);
  3528. *(u32 *)&rt2x00dev->eeprom[i + 2] = cpu_to_le32(reg);
  3529. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1, &reg);
  3530. *(u32 *)&rt2x00dev->eeprom[i + 4] = cpu_to_le32(reg);
  3531. rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0, &reg);
  3532. *(u32 *)&rt2x00dev->eeprom[i + 6] = cpu_to_le32(reg);
  3533. mutex_unlock(&rt2x00dev->csr_mutex);
  3534. }
  3535. void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  3536. {
  3537. unsigned int i;
  3538. for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
  3539. rt2800_efuse_read(rt2x00dev, i);
  3540. }
  3541. EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
  3542. int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  3543. {
  3544. struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
  3545. u16 word;
  3546. u8 *mac;
  3547. u8 default_lna_gain;
  3548. /*
  3549. * Start validation of the data that has been read.
  3550. */
  3551. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  3552. if (!is_valid_ether_addr(mac)) {
  3553. random_ether_addr(mac);
  3554. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  3555. }
  3556. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
  3557. if (word == 0xffff) {
  3558. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
  3559. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
  3560. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
  3561. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
  3562. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  3563. } else if (rt2x00_rt(rt2x00dev, RT2860) ||
  3564. rt2x00_rt(rt2x00dev, RT2872)) {
  3565. /*
  3566. * There is a max of 2 RX streams for RT28x0 series
  3567. */
  3568. if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
  3569. rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
  3570. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
  3571. }
  3572. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
  3573. if (word == 0xffff) {
  3574. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
  3575. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
  3576. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
  3577. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
  3578. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
  3579. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
  3580. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
  3581. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
  3582. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
  3583. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
  3584. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
  3585. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
  3586. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
  3587. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
  3588. rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
  3589. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
  3590. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  3591. }
  3592. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  3593. if ((word & 0x00ff) == 0x00ff) {
  3594. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  3595. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  3596. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  3597. }
  3598. if ((word & 0xff00) == 0xff00) {
  3599. rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
  3600. LED_MODE_TXRX_ACTIVITY);
  3601. rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
  3602. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  3603. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
  3604. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
  3605. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
  3606. EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
  3607. }
  3608. /*
  3609. * During the LNA validation we are going to use
  3610. * lna0 as correct value. Note that EEPROM_LNA
  3611. * is never validated.
  3612. */
  3613. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
  3614. default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
  3615. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
  3616. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
  3617. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
  3618. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
  3619. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
  3620. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
  3621. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &word);
  3622. if ((word & 0x00ff) != 0x00ff) {
  3623. drv_data->txmixer_gain_24g =
  3624. rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_BG_VAL);
  3625. } else {
  3626. drv_data->txmixer_gain_24g = 0;
  3627. }
  3628. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
  3629. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
  3630. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
  3631. if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
  3632. rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
  3633. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
  3634. default_lna_gain);
  3635. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
  3636. rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_A, &word);
  3637. if ((word & 0x00ff) != 0x00ff) {
  3638. drv_data->txmixer_gain_5g =
  3639. rt2x00_get_field16(word, EEPROM_TXMIXER_GAIN_A_VAL);
  3640. } else {
  3641. drv_data->txmixer_gain_5g = 0;
  3642. }
  3643. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
  3644. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
  3645. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
  3646. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
  3647. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
  3648. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
  3649. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
  3650. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
  3651. rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
  3652. if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
  3653. rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
  3654. rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
  3655. default_lna_gain);
  3656. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
  3657. return 0;
  3658. }
  3659. EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
  3660. int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
  3661. {
  3662. u32 reg;
  3663. u16 value;
  3664. u16 eeprom;
  3665. /*
  3666. * Read EEPROM word for configuration.
  3667. */
  3668. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  3669. /*
  3670. * Identify RF chipset by EEPROM value
  3671. * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
  3672. * RT53xx: defined in "EEPROM_CHIP_ID" field
  3673. */
  3674. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  3675. if (rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5390 ||
  3676. rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5392)
  3677. rt2x00_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &value);
  3678. else
  3679. value = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
  3680. rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
  3681. value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
  3682. switch (rt2x00dev->chip.rt) {
  3683. case RT2860:
  3684. case RT2872:
  3685. case RT2883:
  3686. case RT3070:
  3687. case RT3071:
  3688. case RT3090:
  3689. case RT3390:
  3690. case RT3572:
  3691. case RT5390:
  3692. case RT5392:
  3693. break;
  3694. default:
  3695. ERROR(rt2x00dev, "Invalid RT chipset 0x%04x detected.\n", rt2x00dev->chip.rt);
  3696. return -ENODEV;
  3697. }
  3698. switch (rt2x00dev->chip.rf) {
  3699. case RF2820:
  3700. case RF2850:
  3701. case RF2720:
  3702. case RF2750:
  3703. case RF3020:
  3704. case RF2020:
  3705. case RF3021:
  3706. case RF3022:
  3707. case RF3052:
  3708. case RF3320:
  3709. case RF5370:
  3710. case RF5372:
  3711. case RF5390:
  3712. break;
  3713. default:
  3714. ERROR(rt2x00dev, "Invalid RF chipset 0x%04x detected.\n",
  3715. rt2x00dev->chip.rf);
  3716. return -ENODEV;
  3717. }
  3718. /*
  3719. * Identify default antenna configuration.
  3720. */
  3721. rt2x00dev->default_ant.tx_chain_num =
  3722. rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
  3723. rt2x00dev->default_ant.rx_chain_num =
  3724. rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
  3725. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
  3726. if (rt2x00_rt(rt2x00dev, RT3070) ||
  3727. rt2x00_rt(rt2x00dev, RT3090) ||
  3728. rt2x00_rt(rt2x00dev, RT3390)) {
  3729. value = rt2x00_get_field16(eeprom,
  3730. EEPROM_NIC_CONF1_ANT_DIVERSITY);
  3731. switch (value) {
  3732. case 0:
  3733. case 1:
  3734. case 2:
  3735. rt2x00dev->default_ant.tx = ANTENNA_A;
  3736. rt2x00dev->default_ant.rx = ANTENNA_A;
  3737. break;
  3738. case 3:
  3739. rt2x00dev->default_ant.tx = ANTENNA_A;
  3740. rt2x00dev->default_ant.rx = ANTENNA_B;
  3741. break;
  3742. }
  3743. } else {
  3744. rt2x00dev->default_ant.tx = ANTENNA_A;
  3745. rt2x00dev->default_ant.rx = ANTENNA_A;
  3746. }
  3747. /*
  3748. * Determine external LNA informations.
  3749. */
  3750. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
  3751. __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
  3752. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
  3753. __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
  3754. /*
  3755. * Detect if this device has an hardware controlled radio.
  3756. */
  3757. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
  3758. __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
  3759. /*
  3760. * Detect if this device has Bluetooth co-existence.
  3761. */
  3762. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
  3763. __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
  3764. /*
  3765. * Read frequency offset and RF programming sequence.
  3766. */
  3767. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  3768. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  3769. /*
  3770. * Store led settings, for correct led behaviour.
  3771. */
  3772. #ifdef CONFIG_RT2X00_LIB_LEDS
  3773. rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  3774. rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  3775. rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
  3776. rt2x00dev->led_mcu_reg = eeprom;
  3777. #endif /* CONFIG_RT2X00_LIB_LEDS */
  3778. /*
  3779. * Check if support EIRP tx power limit feature.
  3780. */
  3781. rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
  3782. if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
  3783. EIRP_MAX_TX_POWER_LIMIT)
  3784. __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
  3785. return 0;
  3786. }
  3787. EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
  3788. /*
  3789. * RF value list for rt28xx
  3790. * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
  3791. */
  3792. static const struct rf_channel rf_vals[] = {
  3793. { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
  3794. { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
  3795. { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
  3796. { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
  3797. { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
  3798. { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
  3799. { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
  3800. { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
  3801. { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
  3802. { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
  3803. { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
  3804. { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
  3805. { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
  3806. { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
  3807. /* 802.11 UNI / HyperLan 2 */
  3808. { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
  3809. { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
  3810. { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
  3811. { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
  3812. { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
  3813. { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
  3814. { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
  3815. { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
  3816. { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
  3817. { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
  3818. { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
  3819. { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
  3820. /* 802.11 HyperLan 2 */
  3821. { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
  3822. { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
  3823. { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
  3824. { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
  3825. { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
  3826. { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
  3827. { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
  3828. { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
  3829. { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
  3830. { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
  3831. { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
  3832. { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
  3833. { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
  3834. { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
  3835. { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
  3836. { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
  3837. /* 802.11 UNII */
  3838. { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
  3839. { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
  3840. { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
  3841. { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
  3842. { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
  3843. { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
  3844. { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
  3845. { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
  3846. { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
  3847. { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
  3848. { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
  3849. /* 802.11 Japan */
  3850. { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
  3851. { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
  3852. { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
  3853. { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
  3854. { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
  3855. { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
  3856. { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
  3857. };
  3858. /*
  3859. * RF value list for rt3xxx
  3860. * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
  3861. */
  3862. static const struct rf_channel rf_vals_3x[] = {
  3863. {1, 241, 2, 2 },
  3864. {2, 241, 2, 7 },
  3865. {3, 242, 2, 2 },
  3866. {4, 242, 2, 7 },
  3867. {5, 243, 2, 2 },
  3868. {6, 243, 2, 7 },
  3869. {7, 244, 2, 2 },
  3870. {8, 244, 2, 7 },
  3871. {9, 245, 2, 2 },
  3872. {10, 245, 2, 7 },
  3873. {11, 246, 2, 2 },
  3874. {12, 246, 2, 7 },
  3875. {13, 247, 2, 2 },
  3876. {14, 248, 2, 4 },
  3877. /* 802.11 UNI / HyperLan 2 */
  3878. {36, 0x56, 0, 4},
  3879. {38, 0x56, 0, 6},
  3880. {40, 0x56, 0, 8},
  3881. {44, 0x57, 0, 0},
  3882. {46, 0x57, 0, 2},
  3883. {48, 0x57, 0, 4},
  3884. {52, 0x57, 0, 8},
  3885. {54, 0x57, 0, 10},
  3886. {56, 0x58, 0, 0},
  3887. {60, 0x58, 0, 4},
  3888. {62, 0x58, 0, 6},
  3889. {64, 0x58, 0, 8},
  3890. /* 802.11 HyperLan 2 */
  3891. {100, 0x5b, 0, 8},
  3892. {102, 0x5b, 0, 10},
  3893. {104, 0x5c, 0, 0},
  3894. {108, 0x5c, 0, 4},
  3895. {110, 0x5c, 0, 6},
  3896. {112, 0x5c, 0, 8},
  3897. {116, 0x5d, 0, 0},
  3898. {118, 0x5d, 0, 2},
  3899. {120, 0x5d, 0, 4},
  3900. {124, 0x5d, 0, 8},
  3901. {126, 0x5d, 0, 10},
  3902. {128, 0x5e, 0, 0},
  3903. {132, 0x5e, 0, 4},
  3904. {134, 0x5e, 0, 6},
  3905. {136, 0x5e, 0, 8},
  3906. {140, 0x5f, 0, 0},
  3907. /* 802.11 UNII */
  3908. {149, 0x5f, 0, 9},
  3909. {151, 0x5f, 0, 11},
  3910. {153, 0x60, 0, 1},
  3911. {157, 0x60, 0, 5},
  3912. {159, 0x60, 0, 7},
  3913. {161, 0x60, 0, 9},
  3914. {165, 0x61, 0, 1},
  3915. {167, 0x61, 0, 3},
  3916. {169, 0x61, 0, 5},
  3917. {171, 0x61, 0, 7},
  3918. {173, 0x61, 0, 9},
  3919. };
  3920. int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  3921. {
  3922. struct hw_mode_spec *spec = &rt2x00dev->spec;
  3923. struct channel_info *info;
  3924. char *default_power1;
  3925. char *default_power2;
  3926. unsigned int i;
  3927. u16 eeprom;
  3928. /*
  3929. * Disable powersaving as default on PCI devices.
  3930. */
  3931. if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
  3932. rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  3933. /*
  3934. * Initialize all hw fields.
  3935. */
  3936. rt2x00dev->hw->flags =
  3937. IEEE80211_HW_SIGNAL_DBM |
  3938. IEEE80211_HW_SUPPORTS_PS |
  3939. IEEE80211_HW_PS_NULLFUNC_STACK |
  3940. IEEE80211_HW_AMPDU_AGGREGATION |
  3941. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  3942. /*
  3943. * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
  3944. * unless we are capable of sending the buffered frames out after the
  3945. * DTIM transmission using rt2x00lib_beacondone. This will send out
  3946. * multicast and broadcast traffic immediately instead of buffering it
  3947. * infinitly and thus dropping it after some time.
  3948. */
  3949. if (!rt2x00_is_usb(rt2x00dev))
  3950. rt2x00dev->hw->flags |=
  3951. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  3952. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  3953. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  3954. rt2x00_eeprom_addr(rt2x00dev,
  3955. EEPROM_MAC_ADDR_0));
  3956. /*
  3957. * As rt2800 has a global fallback table we cannot specify
  3958. * more then one tx rate per frame but since the hw will
  3959. * try several rates (based on the fallback table) we should
  3960. * initialize max_report_rates to the maximum number of rates
  3961. * we are going to try. Otherwise mac80211 will truncate our
  3962. * reported tx rates and the rc algortihm will end up with
  3963. * incorrect data.
  3964. */
  3965. rt2x00dev->hw->max_rates = 1;
  3966. rt2x00dev->hw->max_report_rates = 7;
  3967. rt2x00dev->hw->max_rate_tries = 1;
  3968. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
  3969. /*
  3970. * Initialize hw_mode information.
  3971. */
  3972. spec->supported_bands = SUPPORT_BAND_2GHZ;
  3973. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  3974. if (rt2x00_rf(rt2x00dev, RF2820) ||
  3975. rt2x00_rf(rt2x00dev, RF2720)) {
  3976. spec->num_channels = 14;
  3977. spec->channels = rf_vals;
  3978. } else if (rt2x00_rf(rt2x00dev, RF2850) ||
  3979. rt2x00_rf(rt2x00dev, RF2750)) {
  3980. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  3981. spec->num_channels = ARRAY_SIZE(rf_vals);
  3982. spec->channels = rf_vals;
  3983. } else if (rt2x00_rf(rt2x00dev, RF3020) ||
  3984. rt2x00_rf(rt2x00dev, RF2020) ||
  3985. rt2x00_rf(rt2x00dev, RF3021) ||
  3986. rt2x00_rf(rt2x00dev, RF3022) ||
  3987. rt2x00_rf(rt2x00dev, RF3320) ||
  3988. rt2x00_rf(rt2x00dev, RF5370) ||
  3989. rt2x00_rf(rt2x00dev, RF5372) ||
  3990. rt2x00_rf(rt2x00dev, RF5390)) {
  3991. spec->num_channels = 14;
  3992. spec->channels = rf_vals_3x;
  3993. } else if (rt2x00_rf(rt2x00dev, RF3052)) {
  3994. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  3995. spec->num_channels = ARRAY_SIZE(rf_vals_3x);
  3996. spec->channels = rf_vals_3x;
  3997. }
  3998. /*
  3999. * Initialize HT information.
  4000. */
  4001. if (!rt2x00_rf(rt2x00dev, RF2020))
  4002. spec->ht.ht_supported = true;
  4003. else
  4004. spec->ht.ht_supported = false;
  4005. spec->ht.cap =
  4006. IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  4007. IEEE80211_HT_CAP_GRN_FLD |
  4008. IEEE80211_HT_CAP_SGI_20 |
  4009. IEEE80211_HT_CAP_SGI_40;
  4010. if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
  4011. spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
  4012. spec->ht.cap |=
  4013. rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
  4014. IEEE80211_HT_CAP_RX_STBC_SHIFT;
  4015. spec->ht.ampdu_factor = 3;
  4016. spec->ht.ampdu_density = 4;
  4017. spec->ht.mcs.tx_params =
  4018. IEEE80211_HT_MCS_TX_DEFINED |
  4019. IEEE80211_HT_MCS_TX_RX_DIFF |
  4020. ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
  4021. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  4022. switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
  4023. case 3:
  4024. spec->ht.mcs.rx_mask[2] = 0xff;
  4025. case 2:
  4026. spec->ht.mcs.rx_mask[1] = 0xff;
  4027. case 1:
  4028. spec->ht.mcs.rx_mask[0] = 0xff;
  4029. spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
  4030. break;
  4031. }
  4032. /*
  4033. * Create channel information array
  4034. */
  4035. info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
  4036. if (!info)
  4037. return -ENOMEM;
  4038. spec->channels_info = info;
  4039. default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
  4040. default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
  4041. for (i = 0; i < 14; i++) {
  4042. info[i].default_power1 = default_power1[i];
  4043. info[i].default_power2 = default_power2[i];
  4044. }
  4045. if (spec->num_channels > 14) {
  4046. default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
  4047. default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
  4048. for (i = 14; i < spec->num_channels; i++) {
  4049. info[i].default_power1 = default_power1[i - 14];
  4050. info[i].default_power2 = default_power2[i - 14];
  4051. }
  4052. }
  4053. switch (rt2x00dev->chip.rf) {
  4054. case RF2020:
  4055. case RF3020:
  4056. case RF3021:
  4057. case RF3022:
  4058. case RF3320:
  4059. case RF3052:
  4060. case RF5370:
  4061. case RF5372:
  4062. case RF5390:
  4063. __set_bit(CAPABILITY_VCO_RECALIBRATION, &rt2x00dev->cap_flags);
  4064. break;
  4065. }
  4066. return 0;
  4067. }
  4068. EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
  4069. /*
  4070. * IEEE80211 stack callback functions.
  4071. */
  4072. void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
  4073. u16 *iv16)
  4074. {
  4075. struct rt2x00_dev *rt2x00dev = hw->priv;
  4076. struct mac_iveiv_entry iveiv_entry;
  4077. u32 offset;
  4078. offset = MAC_IVEIV_ENTRY(hw_key_idx);
  4079. rt2800_register_multiread(rt2x00dev, offset,
  4080. &iveiv_entry, sizeof(iveiv_entry));
  4081. memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
  4082. memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
  4083. }
  4084. EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
  4085. int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  4086. {
  4087. struct rt2x00_dev *rt2x00dev = hw->priv;
  4088. u32 reg;
  4089. bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
  4090. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  4091. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
  4092. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  4093. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  4094. rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
  4095. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  4096. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  4097. rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
  4098. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  4099. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  4100. rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
  4101. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  4102. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  4103. rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
  4104. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  4105. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  4106. rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
  4107. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  4108. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  4109. rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
  4110. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  4111. return 0;
  4112. }
  4113. EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
  4114. int rt2800_conf_tx(struct ieee80211_hw *hw,
  4115. struct ieee80211_vif *vif, u16 queue_idx,
  4116. const struct ieee80211_tx_queue_params *params)
  4117. {
  4118. struct rt2x00_dev *rt2x00dev = hw->priv;
  4119. struct data_queue *queue;
  4120. struct rt2x00_field32 field;
  4121. int retval;
  4122. u32 reg;
  4123. u32 offset;
  4124. /*
  4125. * First pass the configuration through rt2x00lib, that will
  4126. * update the queue settings and validate the input. After that
  4127. * we are free to update the registers based on the value
  4128. * in the queue parameter.
  4129. */
  4130. retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
  4131. if (retval)
  4132. return retval;
  4133. /*
  4134. * We only need to perform additional register initialization
  4135. * for WMM queues/
  4136. */
  4137. if (queue_idx >= 4)
  4138. return 0;
  4139. queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
  4140. /* Update WMM TXOP register */
  4141. offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
  4142. field.bit_offset = (queue_idx & 1) * 16;
  4143. field.bit_mask = 0xffff << field.bit_offset;
  4144. rt2800_register_read(rt2x00dev, offset, &reg);
  4145. rt2x00_set_field32(&reg, field, queue->txop);
  4146. rt2800_register_write(rt2x00dev, offset, reg);
  4147. /* Update WMM registers */
  4148. field.bit_offset = queue_idx * 4;
  4149. field.bit_mask = 0xf << field.bit_offset;
  4150. rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
  4151. rt2x00_set_field32(&reg, field, queue->aifs);
  4152. rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
  4153. rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
  4154. rt2x00_set_field32(&reg, field, queue->cw_min);
  4155. rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
  4156. rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
  4157. rt2x00_set_field32(&reg, field, queue->cw_max);
  4158. rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
  4159. /* Update EDCA registers */
  4160. offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
  4161. rt2800_register_read(rt2x00dev, offset, &reg);
  4162. rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
  4163. rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
  4164. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
  4165. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
  4166. rt2800_register_write(rt2x00dev, offset, reg);
  4167. return 0;
  4168. }
  4169. EXPORT_SYMBOL_GPL(rt2800_conf_tx);
  4170. u64 rt2800_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4171. {
  4172. struct rt2x00_dev *rt2x00dev = hw->priv;
  4173. u64 tsf;
  4174. u32 reg;
  4175. rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
  4176. tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
  4177. rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
  4178. tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
  4179. return tsf;
  4180. }
  4181. EXPORT_SYMBOL_GPL(rt2800_get_tsf);
  4182. int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4183. enum ieee80211_ampdu_mlme_action action,
  4184. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  4185. u8 buf_size)
  4186. {
  4187. struct rt2x00_sta *sta_priv = (struct rt2x00_sta *)sta->drv_priv;
  4188. int ret = 0;
  4189. /*
  4190. * Don't allow aggregation for stations the hardware isn't aware
  4191. * of because tx status reports for frames to an unknown station
  4192. * always contain wcid=255 and thus we can't distinguish between
  4193. * multiple stations which leads to unwanted situations when the
  4194. * hw reorders frames due to aggregation.
  4195. */
  4196. if (sta_priv->wcid < 0)
  4197. return 1;
  4198. switch (action) {
  4199. case IEEE80211_AMPDU_RX_START:
  4200. case IEEE80211_AMPDU_RX_STOP:
  4201. /*
  4202. * The hw itself takes care of setting up BlockAck mechanisms.
  4203. * So, we only have to allow mac80211 to nagotiate a BlockAck
  4204. * agreement. Once that is done, the hw will BlockAck incoming
  4205. * AMPDUs without further setup.
  4206. */
  4207. break;
  4208. case IEEE80211_AMPDU_TX_START:
  4209. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  4210. break;
  4211. case IEEE80211_AMPDU_TX_STOP:
  4212. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  4213. break;
  4214. case IEEE80211_AMPDU_TX_OPERATIONAL:
  4215. break;
  4216. default:
  4217. WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
  4218. }
  4219. return ret;
  4220. }
  4221. EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
  4222. int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
  4223. struct survey_info *survey)
  4224. {
  4225. struct rt2x00_dev *rt2x00dev = hw->priv;
  4226. struct ieee80211_conf *conf = &hw->conf;
  4227. u32 idle, busy, busy_ext;
  4228. if (idx != 0)
  4229. return -ENOENT;
  4230. survey->channel = conf->channel;
  4231. rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
  4232. rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
  4233. rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
  4234. if (idle || busy) {
  4235. survey->filled = SURVEY_INFO_CHANNEL_TIME |
  4236. SURVEY_INFO_CHANNEL_TIME_BUSY |
  4237. SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
  4238. survey->channel_time = (idle + busy) / 1000;
  4239. survey->channel_time_busy = busy / 1000;
  4240. survey->channel_time_ext_busy = busy_ext / 1000;
  4241. }
  4242. if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
  4243. survey->filled |= SURVEY_INFO_IN_USE;
  4244. return 0;
  4245. }
  4246. EXPORT_SYMBOL_GPL(rt2800_get_survey);
  4247. MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
  4248. MODULE_VERSION(DRV_VERSION);
  4249. MODULE_DESCRIPTION("Ralink RT2800 library");
  4250. MODULE_LICENSE("GPL");