hw.c 222 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577
  1. /*
  2. * Copyright (c) 2008 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/io.h>
  17. #include <asm/unaligned.h>
  18. #include "core.h"
  19. #include "hw.h"
  20. #include "reg.h"
  21. #include "phy.h"
  22. #include "initvals.h"
  23. static void ath9k_hw_iqcal_collect(struct ath_hal *ah);
  24. static void ath9k_hw_iqcalibrate(struct ath_hal *ah, u8 numChains);
  25. static void ath9k_hw_adc_gaincal_collect(struct ath_hal *ah);
  26. static void ath9k_hw_adc_gaincal_calibrate(struct ath_hal *ah,
  27. u8 numChains);
  28. static void ath9k_hw_adc_dccal_collect(struct ath_hal *ah);
  29. static void ath9k_hw_adc_dccal_calibrate(struct ath_hal *ah,
  30. u8 numChains);
  31. static const u8 CLOCK_RATE[] = { 40, 80, 22, 44, 88, 40 };
  32. static const int16_t NOISE_FLOOR[] = { -96, -93, -98, -96, -93, -96 };
  33. static const struct hal_percal_data iq_cal_multi_sample = {
  34. IQ_MISMATCH_CAL,
  35. MAX_CAL_SAMPLES,
  36. PER_MIN_LOG_COUNT,
  37. ath9k_hw_iqcal_collect,
  38. ath9k_hw_iqcalibrate
  39. };
  40. static const struct hal_percal_data iq_cal_single_sample = {
  41. IQ_MISMATCH_CAL,
  42. MIN_CAL_SAMPLES,
  43. PER_MAX_LOG_COUNT,
  44. ath9k_hw_iqcal_collect,
  45. ath9k_hw_iqcalibrate
  46. };
  47. static const struct hal_percal_data adc_gain_cal_multi_sample = {
  48. ADC_GAIN_CAL,
  49. MAX_CAL_SAMPLES,
  50. PER_MIN_LOG_COUNT,
  51. ath9k_hw_adc_gaincal_collect,
  52. ath9k_hw_adc_gaincal_calibrate
  53. };
  54. static const struct hal_percal_data adc_gain_cal_single_sample = {
  55. ADC_GAIN_CAL,
  56. MIN_CAL_SAMPLES,
  57. PER_MAX_LOG_COUNT,
  58. ath9k_hw_adc_gaincal_collect,
  59. ath9k_hw_adc_gaincal_calibrate
  60. };
  61. static const struct hal_percal_data adc_dc_cal_multi_sample = {
  62. ADC_DC_CAL,
  63. MAX_CAL_SAMPLES,
  64. PER_MIN_LOG_COUNT,
  65. ath9k_hw_adc_dccal_collect,
  66. ath9k_hw_adc_dccal_calibrate
  67. };
  68. static const struct hal_percal_data adc_dc_cal_single_sample = {
  69. ADC_DC_CAL,
  70. MIN_CAL_SAMPLES,
  71. PER_MAX_LOG_COUNT,
  72. ath9k_hw_adc_dccal_collect,
  73. ath9k_hw_adc_dccal_calibrate
  74. };
  75. static const struct hal_percal_data adc_init_dc_cal = {
  76. ADC_DC_INIT_CAL,
  77. MIN_CAL_SAMPLES,
  78. INIT_LOG_COUNT,
  79. ath9k_hw_adc_dccal_collect,
  80. ath9k_hw_adc_dccal_calibrate
  81. };
  82. static struct ath9k_rate_table ar5416_11a_table = {
  83. 8,
  84. {0},
  85. {
  86. {true, PHY_OFDM, 6000, 0x0b, 0x00, (0x80 | 12), 0},
  87. {true, PHY_OFDM, 9000, 0x0f, 0x00, 18, 0},
  88. {true, PHY_OFDM, 12000, 0x0a, 0x00, (0x80 | 24), 2},
  89. {true, PHY_OFDM, 18000, 0x0e, 0x00, 36, 2},
  90. {true, PHY_OFDM, 24000, 0x09, 0x00, (0x80 | 48), 4},
  91. {true, PHY_OFDM, 36000, 0x0d, 0x00, 72, 4},
  92. {true, PHY_OFDM, 48000, 0x08, 0x00, 96, 4},
  93. {true, PHY_OFDM, 54000, 0x0c, 0x00, 108, 4}
  94. },
  95. };
  96. static struct ath9k_rate_table ar5416_11b_table = {
  97. 4,
  98. {0},
  99. {
  100. {true, PHY_CCK, 1000, 0x1b, 0x00, (0x80 | 2), 0},
  101. {true, PHY_CCK, 2000, 0x1a, 0x04, (0x80 | 4), 1},
  102. {true, PHY_CCK, 5500, 0x19, 0x04, (0x80 | 11), 1},
  103. {true, PHY_CCK, 11000, 0x18, 0x04, (0x80 | 22), 1}
  104. },
  105. };
  106. static struct ath9k_rate_table ar5416_11g_table = {
  107. 12,
  108. {0},
  109. {
  110. {true, PHY_CCK, 1000, 0x1b, 0x00, (0x80 | 2), 0},
  111. {true, PHY_CCK, 2000, 0x1a, 0x04, (0x80 | 4), 1},
  112. {true, PHY_CCK, 5500, 0x19, 0x04, (0x80 | 11), 2},
  113. {true, PHY_CCK, 11000, 0x18, 0x04, (0x80 | 22), 3},
  114. {false, PHY_OFDM, 6000, 0x0b, 0x00, 12, 4},
  115. {false, PHY_OFDM, 9000, 0x0f, 0x00, 18, 4},
  116. {true, PHY_OFDM, 12000, 0x0a, 0x00, 24, 6},
  117. {true, PHY_OFDM, 18000, 0x0e, 0x00, 36, 6},
  118. {true, PHY_OFDM, 24000, 0x09, 0x00, 48, 8},
  119. {true, PHY_OFDM, 36000, 0x0d, 0x00, 72, 8},
  120. {true, PHY_OFDM, 48000, 0x08, 0x00, 96, 8},
  121. {true, PHY_OFDM, 54000, 0x0c, 0x00, 108, 8}
  122. },
  123. };
  124. static struct ath9k_rate_table ar5416_11ng_table = {
  125. 28,
  126. {0},
  127. {
  128. {true, PHY_CCK, 1000, 0x1b, 0x00, (0x80 | 2), 0},
  129. {true, PHY_CCK, 2000, 0x1a, 0x04, (0x80 | 4), 1},
  130. {true, PHY_CCK, 5500, 0x19, 0x04, (0x80 | 11), 2},
  131. {true, PHY_CCK, 11000, 0x18, 0x04, (0x80 | 22), 3},
  132. {false, PHY_OFDM, 6000, 0x0b, 0x00, 12, 4},
  133. {false, PHY_OFDM, 9000, 0x0f, 0x00, 18, 4},
  134. {true, PHY_OFDM, 12000, 0x0a, 0x00, 24, 6},
  135. {true, PHY_OFDM, 18000, 0x0e, 0x00, 36, 6},
  136. {true, PHY_OFDM, 24000, 0x09, 0x00, 48, 8},
  137. {true, PHY_OFDM, 36000, 0x0d, 0x00, 72, 8},
  138. {true, PHY_OFDM, 48000, 0x08, 0x00, 96, 8},
  139. {true, PHY_OFDM, 54000, 0x0c, 0x00, 108, 8},
  140. {true, PHY_HT, 6500, 0x80, 0x00, 0, 4},
  141. {true, PHY_HT, 13000, 0x81, 0x00, 1, 6},
  142. {true, PHY_HT, 19500, 0x82, 0x00, 2, 6},
  143. {true, PHY_HT, 26000, 0x83, 0x00, 3, 8},
  144. {true, PHY_HT, 39000, 0x84, 0x00, 4, 8},
  145. {true, PHY_HT, 52000, 0x85, 0x00, 5, 8},
  146. {true, PHY_HT, 58500, 0x86, 0x00, 6, 8},
  147. {true, PHY_HT, 65000, 0x87, 0x00, 7, 8},
  148. {true, PHY_HT, 13000, 0x88, 0x00, 8, 4},
  149. {true, PHY_HT, 26000, 0x89, 0x00, 9, 6},
  150. {true, PHY_HT, 39000, 0x8a, 0x00, 10, 6},
  151. {true, PHY_HT, 52000, 0x8b, 0x00, 11, 8},
  152. {true, PHY_HT, 78000, 0x8c, 0x00, 12, 8},
  153. {true, PHY_HT, 104000, 0x8d, 0x00, 13, 8},
  154. {true, PHY_HT, 117000, 0x8e, 0x00, 14, 8},
  155. {true, PHY_HT, 130000, 0x8f, 0x00, 15, 8},
  156. },
  157. };
  158. static struct ath9k_rate_table ar5416_11na_table = {
  159. 24,
  160. {0},
  161. {
  162. {true, PHY_OFDM, 6000, 0x0b, 0x00, (0x80 | 12), 0},
  163. {true, PHY_OFDM, 9000, 0x0f, 0x00, 18, 0},
  164. {true, PHY_OFDM, 12000, 0x0a, 0x00, (0x80 | 24), 2},
  165. {true, PHY_OFDM, 18000, 0x0e, 0x00, 36, 2},
  166. {true, PHY_OFDM, 24000, 0x09, 0x00, (0x80 | 48), 4},
  167. {true, PHY_OFDM, 36000, 0x0d, 0x00, 72, 4},
  168. {true, PHY_OFDM, 48000, 0x08, 0x00, 96, 4},
  169. {true, PHY_OFDM, 54000, 0x0c, 0x00, 108, 4},
  170. {true, PHY_HT, 6500, 0x80, 0x00, 0, 0},
  171. {true, PHY_HT, 13000, 0x81, 0x00, 1, 2},
  172. {true, PHY_HT, 19500, 0x82, 0x00, 2, 2},
  173. {true, PHY_HT, 26000, 0x83, 0x00, 3, 4},
  174. {true, PHY_HT, 39000, 0x84, 0x00, 4, 4},
  175. {true, PHY_HT, 52000, 0x85, 0x00, 5, 4},
  176. {true, PHY_HT, 58500, 0x86, 0x00, 6, 4},
  177. {true, PHY_HT, 65000, 0x87, 0x00, 7, 4},
  178. {true, PHY_HT, 13000, 0x88, 0x00, 8, 0},
  179. {true, PHY_HT, 26000, 0x89, 0x00, 9, 2},
  180. {true, PHY_HT, 39000, 0x8a, 0x00, 10, 2},
  181. {true, PHY_HT, 52000, 0x8b, 0x00, 11, 4},
  182. {true, PHY_HT, 78000, 0x8c, 0x00, 12, 4},
  183. {true, PHY_HT, 104000, 0x8d, 0x00, 13, 4},
  184. {true, PHY_HT, 117000, 0x8e, 0x00, 14, 4},
  185. {true, PHY_HT, 130000, 0x8f, 0x00, 15, 4},
  186. },
  187. };
  188. static enum wireless_mode ath9k_hw_chan2wmode(struct ath_hal *ah,
  189. const struct ath9k_channel *chan)
  190. {
  191. if (IS_CHAN_CCK(chan))
  192. return ATH9K_MODE_11A;
  193. if (IS_CHAN_G(chan))
  194. return ATH9K_MODE_11G;
  195. return ATH9K_MODE_11A;
  196. }
  197. static bool ath9k_hw_wait(struct ath_hal *ah,
  198. u32 reg,
  199. u32 mask,
  200. u32 val)
  201. {
  202. int i;
  203. for (i = 0; i < (AH_TIMEOUT / AH_TIME_QUANTUM); i++) {
  204. if ((REG_READ(ah, reg) & mask) == val)
  205. return true;
  206. udelay(AH_TIME_QUANTUM);
  207. }
  208. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  209. "%s: timeout on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
  210. __func__, reg, REG_READ(ah, reg), mask, val);
  211. return false;
  212. }
  213. static bool ath9k_hw_eeprom_read(struct ath_hal *ah, u32 off,
  214. u16 *data)
  215. {
  216. (void) REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  217. if (!ath9k_hw_wait(ah,
  218. AR_EEPROM_STATUS_DATA,
  219. AR_EEPROM_STATUS_DATA_BUSY |
  220. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0)) {
  221. return false;
  222. }
  223. *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
  224. AR_EEPROM_STATUS_DATA_VAL);
  225. return true;
  226. }
  227. static int ath9k_hw_flash_map(struct ath_hal *ah)
  228. {
  229. struct ath_hal_5416 *ahp = AH5416(ah);
  230. ahp->ah_cal_mem = ioremap(AR5416_EEPROM_START_ADDR, AR5416_EEPROM_MAX);
  231. if (!ahp->ah_cal_mem) {
  232. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  233. "%s: cannot remap eeprom region \n", __func__);
  234. return -EIO;
  235. }
  236. return 0;
  237. }
  238. static bool ath9k_hw_flash_read(struct ath_hal *ah, u32 off,
  239. u16 *data)
  240. {
  241. struct ath_hal_5416 *ahp = AH5416(ah);
  242. *data = ioread16(ahp->ah_cal_mem + off);
  243. return true;
  244. }
  245. static void ath9k_hw_read_revisions(struct ath_hal *ah)
  246. {
  247. u32 val;
  248. val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
  249. if (val == 0xFF) {
  250. val = REG_READ(ah, AR_SREV);
  251. ah->ah_macVersion =
  252. (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
  253. ah->ah_macRev = MS(val, AR_SREV_REVISION2);
  254. ah->ah_isPciExpress =
  255. (val & AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
  256. } else {
  257. if (!AR_SREV_9100(ah))
  258. ah->ah_macVersion = MS(val, AR_SREV_VERSION);
  259. ah->ah_macRev = val & AR_SREV_REVISION;
  260. if (ah->ah_macVersion == AR_SREV_VERSION_5416_PCIE)
  261. ah->ah_isPciExpress = true;
  262. }
  263. }
  264. u32 ath9k_hw_reverse_bits(u32 val, u32 n)
  265. {
  266. u32 retval;
  267. int i;
  268. for (i = 0, retval = 0; i < n; i++) {
  269. retval = (retval << 1) | (val & 1);
  270. val >>= 1;
  271. }
  272. return retval;
  273. }
  274. static void ath9k_hw_set_defaults(struct ath_hal *ah)
  275. {
  276. int i;
  277. ah->ah_config.dma_beacon_response_time = 2;
  278. ah->ah_config.sw_beacon_response_time = 10;
  279. ah->ah_config.additional_swba_backoff = 0;
  280. ah->ah_config.ack_6mb = 0x0;
  281. ah->ah_config.cwm_ignore_extcca = 0;
  282. ah->ah_config.pcie_powersave_enable = 0;
  283. ah->ah_config.pcie_l1skp_enable = 0;
  284. ah->ah_config.pcie_clock_req = 0;
  285. ah->ah_config.pcie_power_reset = 0x100;
  286. ah->ah_config.pcie_restore = 0;
  287. ah->ah_config.pcie_waen = 0;
  288. ah->ah_config.analog_shiftreg = 1;
  289. ah->ah_config.ht_enable = 1;
  290. ah->ah_config.ofdm_trig_low = 200;
  291. ah->ah_config.ofdm_trig_high = 500;
  292. ah->ah_config.cck_trig_high = 200;
  293. ah->ah_config.cck_trig_low = 100;
  294. ah->ah_config.enable_ani = 1;
  295. ah->ah_config.noise_immunity_level = 4;
  296. ah->ah_config.ofdm_weaksignal_det = 1;
  297. ah->ah_config.cck_weaksignal_thr = 0;
  298. ah->ah_config.spur_immunity_level = 2;
  299. ah->ah_config.firstep_level = 0;
  300. ah->ah_config.rssi_thr_high = 40;
  301. ah->ah_config.rssi_thr_low = 7;
  302. ah->ah_config.diversity_control = 0;
  303. ah->ah_config.antenna_switch_swap = 0;
  304. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  305. ah->ah_config.spurchans[i][0] = AR_NO_SPUR;
  306. ah->ah_config.spurchans[i][1] = AR_NO_SPUR;
  307. }
  308. ah->ah_config.intr_mitigation = 0;
  309. }
  310. static void ath9k_hw_override_ini(struct ath_hal *ah,
  311. struct ath9k_channel *chan)
  312. {
  313. if (!AR_SREV_5416_V20_OR_LATER(ah)
  314. || AR_SREV_9280_10_OR_LATER(ah))
  315. return;
  316. REG_WRITE(ah, 0x9800 + (651 << 2), 0x11);
  317. }
  318. static void ath9k_hw_init_bb(struct ath_hal *ah,
  319. struct ath9k_channel *chan)
  320. {
  321. u32 synthDelay;
  322. synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  323. if (IS_CHAN_CCK(chan))
  324. synthDelay = (4 * synthDelay) / 22;
  325. else
  326. synthDelay /= 10;
  327. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  328. udelay(synthDelay + BASE_ACTIVATE_DELAY);
  329. }
  330. static void ath9k_hw_init_interrupt_masks(struct ath_hal *ah,
  331. enum ath9k_opmode opmode)
  332. {
  333. struct ath_hal_5416 *ahp = AH5416(ah);
  334. ahp->ah_maskReg = AR_IMR_TXERR |
  335. AR_IMR_TXURN |
  336. AR_IMR_RXERR |
  337. AR_IMR_RXORN |
  338. AR_IMR_BCNMISC;
  339. if (ahp->ah_intrMitigation)
  340. ahp->ah_maskReg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  341. else
  342. ahp->ah_maskReg |= AR_IMR_RXOK;
  343. ahp->ah_maskReg |= AR_IMR_TXOK;
  344. if (opmode == ATH9K_M_HOSTAP)
  345. ahp->ah_maskReg |= AR_IMR_MIB;
  346. REG_WRITE(ah, AR_IMR, ahp->ah_maskReg);
  347. REG_WRITE(ah, AR_IMR_S2, REG_READ(ah, AR_IMR_S2) | AR_IMR_S2_GTT);
  348. if (!AR_SREV_9100(ah)) {
  349. REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
  350. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, AR_INTR_SYNC_DEFAULT);
  351. REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
  352. }
  353. }
  354. static void ath9k_hw_init_qos(struct ath_hal *ah)
  355. {
  356. REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
  357. REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
  358. REG_WRITE(ah, AR_QOS_NO_ACK,
  359. SM(2, AR_QOS_NO_ACK_TWO_BIT) |
  360. SM(5, AR_QOS_NO_ACK_BIT_OFF) |
  361. SM(0, AR_QOS_NO_ACK_BYTE_OFF));
  362. REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
  363. REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
  364. REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
  365. REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
  366. REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
  367. }
  368. static void ath9k_hw_analog_shift_rmw(struct ath_hal *ah,
  369. u32 reg,
  370. u32 mask,
  371. u32 shift,
  372. u32 val)
  373. {
  374. u32 regVal;
  375. regVal = REG_READ(ah, reg) & ~mask;
  376. regVal |= (val << shift) & mask;
  377. REG_WRITE(ah, reg, regVal);
  378. if (ah->ah_config.analog_shiftreg)
  379. udelay(100);
  380. return;
  381. }
  382. static u8 ath9k_hw_get_num_ant_config(struct ath_hal_5416 *ahp,
  383. enum ieee80211_band freq_band)
  384. {
  385. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  386. struct modal_eep_header *pModal =
  387. &(eep->modalHeader[IEEE80211_BAND_5GHZ == freq_band]);
  388. struct base_eep_header *pBase = &eep->baseEepHeader;
  389. u8 num_ant_config;
  390. num_ant_config = 1;
  391. if (pBase->version >= 0x0E0D)
  392. if (pModal->useAnt1)
  393. num_ant_config += 1;
  394. return num_ant_config;
  395. }
  396. static int
  397. ath9k_hw_get_eeprom_antenna_cfg(struct ath_hal_5416 *ahp,
  398. struct ath9k_channel *chan,
  399. u8 index,
  400. u16 *config)
  401. {
  402. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  403. struct modal_eep_header *pModal =
  404. &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  405. struct base_eep_header *pBase = &eep->baseEepHeader;
  406. switch (index) {
  407. case 0:
  408. *config = pModal->antCtrlCommon & 0xFFFF;
  409. return 0;
  410. case 1:
  411. if (pBase->version >= 0x0E0D) {
  412. if (pModal->useAnt1) {
  413. *config =
  414. ((pModal->antCtrlCommon & 0xFFFF0000) >> 16);
  415. return 0;
  416. }
  417. }
  418. break;
  419. default:
  420. break;
  421. }
  422. return -EINVAL;
  423. }
  424. static inline bool ath9k_hw_nvram_read(struct ath_hal *ah,
  425. u32 off,
  426. u16 *data)
  427. {
  428. if (ath9k_hw_use_flash(ah))
  429. return ath9k_hw_flash_read(ah, off, data);
  430. else
  431. return ath9k_hw_eeprom_read(ah, off, data);
  432. }
  433. static bool ath9k_hw_fill_eeprom(struct ath_hal *ah)
  434. {
  435. struct ath_hal_5416 *ahp = AH5416(ah);
  436. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  437. u16 *eep_data;
  438. int addr, ar5416_eep_start_loc = 0;
  439. if (!ath9k_hw_use_flash(ah)) {
  440. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  441. "%s: Reading from EEPROM, not flash\n", __func__);
  442. ar5416_eep_start_loc = 256;
  443. }
  444. if (AR_SREV_9100(ah))
  445. ar5416_eep_start_loc = 256;
  446. eep_data = (u16 *) eep;
  447. for (addr = 0;
  448. addr < sizeof(struct ar5416_eeprom) / sizeof(u16);
  449. addr++) {
  450. if (!ath9k_hw_nvram_read(ah, addr + ar5416_eep_start_loc,
  451. eep_data)) {
  452. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  453. "%s: Unable to read eeprom region \n",
  454. __func__);
  455. return false;
  456. }
  457. eep_data++;
  458. }
  459. return true;
  460. }
  461. /* XXX: Clean me up, make me more legible */
  462. static bool
  463. ath9k_hw_eeprom_set_board_values(struct ath_hal *ah,
  464. struct ath9k_channel *chan)
  465. {
  466. struct modal_eep_header *pModal;
  467. int i, regChainOffset;
  468. struct ath_hal_5416 *ahp = AH5416(ah);
  469. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  470. u8 txRxAttenLocal;
  471. u16 ant_config;
  472. pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  473. txRxAttenLocal = IS_CHAN_2GHZ(chan) ? 23 : 44;
  474. ath9k_hw_get_eeprom_antenna_cfg(ahp, chan, 1, &ant_config);
  475. REG_WRITE(ah, AR_PHY_SWITCH_COM, ant_config);
  476. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  477. if (AR_SREV_9280(ah)) {
  478. if (i >= 2)
  479. break;
  480. }
  481. if (AR_SREV_5416_V20_OR_LATER(ah) &&
  482. (ahp->ah_rxchainmask == 5 || ahp->ah_txchainmask == 5)
  483. && (i != 0))
  484. regChainOffset = (i == 1) ? 0x2000 : 0x1000;
  485. else
  486. regChainOffset = i * 0x1000;
  487. REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
  488. pModal->antCtrlChain[i]);
  489. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
  490. (REG_READ(ah,
  491. AR_PHY_TIMING_CTRL4(0) +
  492. regChainOffset) &
  493. ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
  494. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
  495. SM(pModal->iqCalICh[i],
  496. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
  497. SM(pModal->iqCalQCh[i],
  498. AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
  499. if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
  500. if ((eep->baseEepHeader.version &
  501. AR5416_EEP_VER_MINOR_MASK) >=
  502. AR5416_EEP_MINOR_VER_3) {
  503. txRxAttenLocal = pModal->txRxAttenCh[i];
  504. if (AR_SREV_9280_10_OR_LATER(ah)) {
  505. REG_RMW_FIELD(ah,
  506. AR_PHY_GAIN_2GHZ +
  507. regChainOffset,
  508. AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
  509. pModal->
  510. bswMargin[i]);
  511. REG_RMW_FIELD(ah,
  512. AR_PHY_GAIN_2GHZ +
  513. regChainOffset,
  514. AR_PHY_GAIN_2GHZ_XATTEN1_DB,
  515. pModal->
  516. bswAtten[i]);
  517. REG_RMW_FIELD(ah,
  518. AR_PHY_GAIN_2GHZ +
  519. regChainOffset,
  520. AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
  521. pModal->
  522. xatten2Margin[i]);
  523. REG_RMW_FIELD(ah,
  524. AR_PHY_GAIN_2GHZ +
  525. regChainOffset,
  526. AR_PHY_GAIN_2GHZ_XATTEN2_DB,
  527. pModal->
  528. xatten2Db[i]);
  529. } else {
  530. REG_WRITE(ah,
  531. AR_PHY_GAIN_2GHZ +
  532. regChainOffset,
  533. (REG_READ(ah,
  534. AR_PHY_GAIN_2GHZ +
  535. regChainOffset) &
  536. ~AR_PHY_GAIN_2GHZ_BSW_MARGIN)
  537. | SM(pModal->
  538. bswMargin[i],
  539. AR_PHY_GAIN_2GHZ_BSW_MARGIN));
  540. REG_WRITE(ah,
  541. AR_PHY_GAIN_2GHZ +
  542. regChainOffset,
  543. (REG_READ(ah,
  544. AR_PHY_GAIN_2GHZ +
  545. regChainOffset) &
  546. ~AR_PHY_GAIN_2GHZ_BSW_ATTEN)
  547. | SM(pModal->bswAtten[i],
  548. AR_PHY_GAIN_2GHZ_BSW_ATTEN));
  549. }
  550. }
  551. if (AR_SREV_9280_10_OR_LATER(ah)) {
  552. REG_RMW_FIELD(ah,
  553. AR_PHY_RXGAIN +
  554. regChainOffset,
  555. AR9280_PHY_RXGAIN_TXRX_ATTEN,
  556. txRxAttenLocal);
  557. REG_RMW_FIELD(ah,
  558. AR_PHY_RXGAIN +
  559. regChainOffset,
  560. AR9280_PHY_RXGAIN_TXRX_MARGIN,
  561. pModal->rxTxMarginCh[i]);
  562. } else {
  563. REG_WRITE(ah,
  564. AR_PHY_RXGAIN + regChainOffset,
  565. (REG_READ(ah,
  566. AR_PHY_RXGAIN +
  567. regChainOffset) &
  568. ~AR_PHY_RXGAIN_TXRX_ATTEN) |
  569. SM(txRxAttenLocal,
  570. AR_PHY_RXGAIN_TXRX_ATTEN));
  571. REG_WRITE(ah,
  572. AR_PHY_GAIN_2GHZ +
  573. regChainOffset,
  574. (REG_READ(ah,
  575. AR_PHY_GAIN_2GHZ +
  576. regChainOffset) &
  577. ~AR_PHY_GAIN_2GHZ_RXTX_MARGIN) |
  578. SM(pModal->rxTxMarginCh[i],
  579. AR_PHY_GAIN_2GHZ_RXTX_MARGIN));
  580. }
  581. }
  582. }
  583. if (AR_SREV_9280_10_OR_LATER(ah)) {
  584. if (IS_CHAN_2GHZ(chan)) {
  585. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
  586. AR_AN_RF2G1_CH0_OB,
  587. AR_AN_RF2G1_CH0_OB_S,
  588. pModal->ob);
  589. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
  590. AR_AN_RF2G1_CH0_DB,
  591. AR_AN_RF2G1_CH0_DB_S,
  592. pModal->db);
  593. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
  594. AR_AN_RF2G1_CH1_OB,
  595. AR_AN_RF2G1_CH1_OB_S,
  596. pModal->ob_ch1);
  597. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
  598. AR_AN_RF2G1_CH1_DB,
  599. AR_AN_RF2G1_CH1_DB_S,
  600. pModal->db_ch1);
  601. } else {
  602. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
  603. AR_AN_RF5G1_CH0_OB5,
  604. AR_AN_RF5G1_CH0_OB5_S,
  605. pModal->ob);
  606. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
  607. AR_AN_RF5G1_CH0_DB5,
  608. AR_AN_RF5G1_CH0_DB5_S,
  609. pModal->db);
  610. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
  611. AR_AN_RF5G1_CH1_OB5,
  612. AR_AN_RF5G1_CH1_OB5_S,
  613. pModal->ob_ch1);
  614. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
  615. AR_AN_RF5G1_CH1_DB5,
  616. AR_AN_RF5G1_CH1_DB5_S,
  617. pModal->db_ch1);
  618. }
  619. ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
  620. AR_AN_TOP2_XPABIAS_LVL,
  621. AR_AN_TOP2_XPABIAS_LVL_S,
  622. pModal->xpaBiasLvl);
  623. ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
  624. AR_AN_TOP2_LOCALBIAS,
  625. AR_AN_TOP2_LOCALBIAS_S,
  626. pModal->local_bias);
  627. DPRINTF(ah->ah_sc, ATH_DBG_ANY, "ForceXPAon: %d\n",
  628. pModal->force_xpaon);
  629. REG_RMW_FIELD(ah, AR_PHY_XPA_CFG, AR_PHY_FORCE_XPA_CFG,
  630. pModal->force_xpaon);
  631. }
  632. REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
  633. pModal->switchSettling);
  634. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
  635. pModal->adcDesiredSize);
  636. if (!AR_SREV_9280_10_OR_LATER(ah))
  637. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
  638. AR_PHY_DESIRED_SZ_PGA,
  639. pModal->pgaDesiredSize);
  640. REG_WRITE(ah, AR_PHY_RF_CTL4,
  641. SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF)
  642. | SM(pModal->txEndToXpaOff,
  643. AR_PHY_RF_CTL4_TX_END_XPAB_OFF)
  644. | SM(pModal->txFrameToXpaOn,
  645. AR_PHY_RF_CTL4_FRAME_XPAA_ON)
  646. | SM(pModal->txFrameToXpaOn,
  647. AR_PHY_RF_CTL4_FRAME_XPAB_ON));
  648. REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
  649. pModal->txEndToRxOn);
  650. if (AR_SREV_9280_10_OR_LATER(ah)) {
  651. REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
  652. pModal->thresh62);
  653. REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0,
  654. AR_PHY_EXT_CCA0_THRESH62,
  655. pModal->thresh62);
  656. } else {
  657. REG_RMW_FIELD(ah, AR_PHY_CCA, AR_PHY_CCA_THRESH62,
  658. pModal->thresh62);
  659. REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
  660. AR_PHY_EXT_CCA_THRESH62,
  661. pModal->thresh62);
  662. }
  663. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  664. AR5416_EEP_MINOR_VER_2) {
  665. REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
  666. AR_PHY_TX_END_DATA_START,
  667. pModal->txFrameToDataStart);
  668. REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
  669. pModal->txFrameToPaOn);
  670. }
  671. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  672. AR5416_EEP_MINOR_VER_3) {
  673. if (IS_CHAN_HT40(chan))
  674. REG_RMW_FIELD(ah, AR_PHY_SETTLING,
  675. AR_PHY_SETTLING_SWITCH,
  676. pModal->swSettleHt40);
  677. }
  678. return true;
  679. }
  680. static int ath9k_hw_check_eeprom(struct ath_hal *ah)
  681. {
  682. u32 sum = 0, el;
  683. u16 *eepdata;
  684. int i;
  685. struct ath_hal_5416 *ahp = AH5416(ah);
  686. bool need_swap = false;
  687. struct ar5416_eeprom *eep =
  688. (struct ar5416_eeprom *) &ahp->ah_eeprom;
  689. if (!ath9k_hw_use_flash(ah)) {
  690. u16 magic, magic2;
  691. int addr;
  692. if (!ath9k_hw_nvram_read(ah, AR5416_EEPROM_MAGIC_OFFSET,
  693. &magic)) {
  694. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  695. "%s: Reading Magic # failed\n", __func__);
  696. return false;
  697. }
  698. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "%s: Read Magic = 0x%04X\n",
  699. __func__, magic);
  700. if (magic != AR5416_EEPROM_MAGIC) {
  701. magic2 = swab16(magic);
  702. if (magic2 == AR5416_EEPROM_MAGIC) {
  703. need_swap = true;
  704. eepdata = (u16 *) (&ahp->ah_eeprom);
  705. for (addr = 0;
  706. addr <
  707. sizeof(struct ar5416_eeprom) /
  708. sizeof(u16); addr++) {
  709. u16 temp;
  710. temp = swab16(*eepdata);
  711. *eepdata = temp;
  712. eepdata++;
  713. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  714. "0x%04X ", *eepdata);
  715. if (((addr + 1) % 6) == 0)
  716. DPRINTF(ah->ah_sc,
  717. ATH_DBG_EEPROM,
  718. "\n");
  719. }
  720. } else {
  721. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  722. "Invalid EEPROM Magic. "
  723. "endianness missmatch.\n");
  724. return -EINVAL;
  725. }
  726. }
  727. }
  728. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "need_swap = %s.\n",
  729. need_swap ? "True" : "False");
  730. if (need_swap)
  731. el = swab16(ahp->ah_eeprom.baseEepHeader.length);
  732. else
  733. el = ahp->ah_eeprom.baseEepHeader.length;
  734. if (el > sizeof(struct ar5416_eeprom))
  735. el = sizeof(struct ar5416_eeprom) / sizeof(u16);
  736. else
  737. el = el / sizeof(u16);
  738. eepdata = (u16 *) (&ahp->ah_eeprom);
  739. for (i = 0; i < el; i++)
  740. sum ^= *eepdata++;
  741. if (need_swap) {
  742. u32 integer, j;
  743. u16 word;
  744. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  745. "EEPROM Endianness is not native.. Changing \n");
  746. word = swab16(eep->baseEepHeader.length);
  747. eep->baseEepHeader.length = word;
  748. word = swab16(eep->baseEepHeader.checksum);
  749. eep->baseEepHeader.checksum = word;
  750. word = swab16(eep->baseEepHeader.version);
  751. eep->baseEepHeader.version = word;
  752. word = swab16(eep->baseEepHeader.regDmn[0]);
  753. eep->baseEepHeader.regDmn[0] = word;
  754. word = swab16(eep->baseEepHeader.regDmn[1]);
  755. eep->baseEepHeader.regDmn[1] = word;
  756. word = swab16(eep->baseEepHeader.rfSilent);
  757. eep->baseEepHeader.rfSilent = word;
  758. word = swab16(eep->baseEepHeader.blueToothOptions);
  759. eep->baseEepHeader.blueToothOptions = word;
  760. word = swab16(eep->baseEepHeader.deviceCap);
  761. eep->baseEepHeader.deviceCap = word;
  762. for (j = 0; j < ARRAY_SIZE(eep->modalHeader); j++) {
  763. struct modal_eep_header *pModal =
  764. &eep->modalHeader[j];
  765. integer = swab32(pModal->antCtrlCommon);
  766. pModal->antCtrlCommon = integer;
  767. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  768. integer = swab32(pModal->antCtrlChain[i]);
  769. pModal->antCtrlChain[i] = integer;
  770. }
  771. for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
  772. word = swab16(pModal->spurChans[i].spurChan);
  773. pModal->spurChans[i].spurChan = word;
  774. }
  775. }
  776. }
  777. if (sum != 0xffff || ar5416_get_eep_ver(ahp) != AR5416_EEP_VER ||
  778. ar5416_get_eep_rev(ahp) < AR5416_EEP_NO_BACK_VER) {
  779. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  780. "Bad EEPROM checksum 0x%x or revision 0x%04x\n",
  781. sum, ar5416_get_eep_ver(ahp));
  782. return -EINVAL;
  783. }
  784. return 0;
  785. }
  786. static bool ath9k_hw_chip_test(struct ath_hal *ah)
  787. {
  788. u32 regAddr[2] = { AR_STA_ID0, AR_PHY_BASE + (8 << 2) };
  789. u32 regHold[2];
  790. u32 patternData[4] = { 0x55555555,
  791. 0xaaaaaaaa,
  792. 0x66666666,
  793. 0x99999999 };
  794. int i, j;
  795. for (i = 0; i < 2; i++) {
  796. u32 addr = regAddr[i];
  797. u32 wrData, rdData;
  798. regHold[i] = REG_READ(ah, addr);
  799. for (j = 0; j < 0x100; j++) {
  800. wrData = (j << 16) | j;
  801. REG_WRITE(ah, addr, wrData);
  802. rdData = REG_READ(ah, addr);
  803. if (rdData != wrData) {
  804. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  805. "%s: address test failed "
  806. "addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
  807. __func__, addr, wrData, rdData);
  808. return false;
  809. }
  810. }
  811. for (j = 0; j < 4; j++) {
  812. wrData = patternData[j];
  813. REG_WRITE(ah, addr, wrData);
  814. rdData = REG_READ(ah, addr);
  815. if (wrData != rdData) {
  816. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  817. "%s: address test failed "
  818. "addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
  819. __func__, addr, wrData, rdData);
  820. return false;
  821. }
  822. }
  823. REG_WRITE(ah, regAddr[i], regHold[i]);
  824. }
  825. udelay(100);
  826. return true;
  827. }
  828. u32 ath9k_hw_getrxfilter(struct ath_hal *ah)
  829. {
  830. u32 bits = REG_READ(ah, AR_RX_FILTER);
  831. u32 phybits = REG_READ(ah, AR_PHY_ERR);
  832. if (phybits & AR_PHY_ERR_RADAR)
  833. bits |= ATH9K_RX_FILTER_PHYRADAR;
  834. if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
  835. bits |= ATH9K_RX_FILTER_PHYERR;
  836. return bits;
  837. }
  838. void ath9k_hw_setrxfilter(struct ath_hal *ah, u32 bits)
  839. {
  840. u32 phybits;
  841. REG_WRITE(ah, AR_RX_FILTER, (bits & 0xffff) | AR_RX_COMPR_BAR);
  842. phybits = 0;
  843. if (bits & ATH9K_RX_FILTER_PHYRADAR)
  844. phybits |= AR_PHY_ERR_RADAR;
  845. if (bits & ATH9K_RX_FILTER_PHYERR)
  846. phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
  847. REG_WRITE(ah, AR_PHY_ERR, phybits);
  848. if (phybits)
  849. REG_WRITE(ah, AR_RXCFG,
  850. REG_READ(ah, AR_RXCFG) | AR_RXCFG_ZLFDMA);
  851. else
  852. REG_WRITE(ah, AR_RXCFG,
  853. REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_ZLFDMA);
  854. }
  855. bool ath9k_hw_setcapability(struct ath_hal *ah,
  856. enum ath9k_capability_type type,
  857. u32 capability,
  858. u32 setting,
  859. int *status)
  860. {
  861. struct ath_hal_5416 *ahp = AH5416(ah);
  862. u32 v;
  863. switch (type) {
  864. case ATH9K_CAP_TKIP_MIC:
  865. if (setting)
  866. ahp->ah_staId1Defaults |=
  867. AR_STA_ID1_CRPT_MIC_ENABLE;
  868. else
  869. ahp->ah_staId1Defaults &=
  870. ~AR_STA_ID1_CRPT_MIC_ENABLE;
  871. return true;
  872. case ATH9K_CAP_DIVERSITY:
  873. v = REG_READ(ah, AR_PHY_CCK_DETECT);
  874. if (setting)
  875. v |= AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
  876. else
  877. v &= ~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
  878. REG_WRITE(ah, AR_PHY_CCK_DETECT, v);
  879. return true;
  880. case ATH9K_CAP_MCAST_KEYSRCH:
  881. if (setting)
  882. ahp->ah_staId1Defaults |= AR_STA_ID1_MCAST_KSRCH;
  883. else
  884. ahp->ah_staId1Defaults &= ~AR_STA_ID1_MCAST_KSRCH;
  885. return true;
  886. case ATH9K_CAP_TSF_ADJUST:
  887. if (setting)
  888. ahp->ah_miscMode |= AR_PCU_TX_ADD_TSF;
  889. else
  890. ahp->ah_miscMode &= ~AR_PCU_TX_ADD_TSF;
  891. return true;
  892. default:
  893. return false;
  894. }
  895. }
  896. void ath9k_hw_dmaRegDump(struct ath_hal *ah)
  897. {
  898. u32 val[ATH9K_NUM_DMA_DEBUG_REGS];
  899. int qcuOffset = 0, dcuOffset = 0;
  900. u32 *qcuBase = &val[0], *dcuBase = &val[4];
  901. int i;
  902. REG_WRITE(ah, AR_MACMISC,
  903. ((AR_MACMISC_DMA_OBS_LINE_8 << AR_MACMISC_DMA_OBS_S) |
  904. (AR_MACMISC_MISC_OBS_BUS_1 <<
  905. AR_MACMISC_MISC_OBS_BUS_MSB_S)));
  906. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO, "Raw DMA Debug values:\n");
  907. for (i = 0; i < ATH9K_NUM_DMA_DEBUG_REGS; i++) {
  908. if (i % 4 == 0)
  909. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO, "\n");
  910. val[i] = REG_READ(ah, AR_DMADBG_0 + (i * sizeof(u32)));
  911. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO, "%d: %08x ", i, val[i]);
  912. }
  913. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO, "\n\n");
  914. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  915. "Num QCU: chain_st fsp_ok fsp_st DCU: chain_st\n");
  916. for (i = 0; i < ATH9K_NUM_QUEUES;
  917. i++, qcuOffset += 4, dcuOffset += 5) {
  918. if (i == 8) {
  919. qcuOffset = 0;
  920. qcuBase++;
  921. }
  922. if (i == 6) {
  923. dcuOffset = 0;
  924. dcuBase++;
  925. }
  926. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  927. "%2d %2x %1x %2x %2x\n",
  928. i, (*qcuBase & (0x7 << qcuOffset)) >> qcuOffset,
  929. (*qcuBase & (0x8 << qcuOffset)) >> (qcuOffset +
  930. 3),
  931. val[2] & (0x7 << (i * 3)) >> (i * 3),
  932. (*dcuBase & (0x1f << dcuOffset)) >> dcuOffset);
  933. }
  934. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO, "\n");
  935. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  936. "qcu_stitch state: %2x qcu_fetch state: %2x\n",
  937. (val[3] & 0x003c0000) >> 18, (val[3] & 0x03c00000) >> 22);
  938. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  939. "qcu_complete state: %2x dcu_complete state: %2x\n",
  940. (val[3] & 0x1c000000) >> 26, (val[6] & 0x3));
  941. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  942. "dcu_arb state: %2x dcu_fp state: %2x\n",
  943. (val[5] & 0x06000000) >> 25, (val[5] & 0x38000000) >> 27);
  944. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  945. "chan_idle_dur: %3d chan_idle_dur_valid: %1d\n",
  946. (val[6] & 0x000003fc) >> 2, (val[6] & 0x00000400) >> 10);
  947. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  948. "txfifo_valid_0: %1d txfifo_valid_1: %1d\n",
  949. (val[6] & 0x00000800) >> 11, (val[6] & 0x00001000) >> 12);
  950. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  951. "txfifo_dcu_num_0: %2d txfifo_dcu_num_1: %2d\n",
  952. (val[6] & 0x0001e000) >> 13, (val[6] & 0x001e0000) >> 17);
  953. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO, "pcu observe 0x%x \n",
  954. REG_READ(ah, AR_OBS_BUS_1));
  955. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  956. "AR_CR 0x%x \n", REG_READ(ah, AR_CR));
  957. }
  958. u32 ath9k_hw_GetMibCycleCountsPct(struct ath_hal *ah,
  959. u32 *rxc_pcnt,
  960. u32 *rxf_pcnt,
  961. u32 *txf_pcnt)
  962. {
  963. static u32 cycles, rx_clear, rx_frame, tx_frame;
  964. u32 good = 1;
  965. u32 rc = REG_READ(ah, AR_RCCNT);
  966. u32 rf = REG_READ(ah, AR_RFCNT);
  967. u32 tf = REG_READ(ah, AR_TFCNT);
  968. u32 cc = REG_READ(ah, AR_CCCNT);
  969. if (cycles == 0 || cycles > cc) {
  970. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  971. "%s: cycle counter wrap. ExtBusy = 0\n",
  972. __func__);
  973. good = 0;
  974. } else {
  975. u32 cc_d = cc - cycles;
  976. u32 rc_d = rc - rx_clear;
  977. u32 rf_d = rf - rx_frame;
  978. u32 tf_d = tf - tx_frame;
  979. if (cc_d != 0) {
  980. *rxc_pcnt = rc_d * 100 / cc_d;
  981. *rxf_pcnt = rf_d * 100 / cc_d;
  982. *txf_pcnt = tf_d * 100 / cc_d;
  983. } else {
  984. good = 0;
  985. }
  986. }
  987. cycles = cc;
  988. rx_frame = rf;
  989. rx_clear = rc;
  990. tx_frame = tf;
  991. return good;
  992. }
  993. void ath9k_hw_set11nmac2040(struct ath_hal *ah, enum ath9k_ht_macmode mode)
  994. {
  995. u32 macmode;
  996. if (mode == ATH9K_HT_MACMODE_2040 &&
  997. !ah->ah_config.cwm_ignore_extcca)
  998. macmode = AR_2040_JOINED_RX_CLEAR;
  999. else
  1000. macmode = 0;
  1001. REG_WRITE(ah, AR_2040_MODE, macmode);
  1002. }
  1003. static void ath9k_hw_mark_phy_inactive(struct ath_hal *ah)
  1004. {
  1005. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  1006. }
  1007. static struct ath_hal_5416 *ath9k_hw_newstate(u16 devid,
  1008. struct ath_softc *sc,
  1009. void __iomem *mem,
  1010. int *status)
  1011. {
  1012. static const u8 defbssidmask[ETH_ALEN] =
  1013. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1014. struct ath_hal_5416 *ahp;
  1015. struct ath_hal *ah;
  1016. ahp = kzalloc(sizeof(struct ath_hal_5416), GFP_KERNEL);
  1017. if (ahp == NULL) {
  1018. DPRINTF(sc, ATH_DBG_FATAL,
  1019. "%s: cannot allocate memory for state block\n",
  1020. __func__);
  1021. *status = -ENOMEM;
  1022. return NULL;
  1023. }
  1024. ah = &ahp->ah;
  1025. ah->ah_sc = sc;
  1026. ah->ah_sh = mem;
  1027. ah->ah_magic = AR5416_MAGIC;
  1028. ah->ah_countryCode = CTRY_DEFAULT;
  1029. ah->ah_devid = devid;
  1030. ah->ah_subvendorid = 0;
  1031. ah->ah_flags = 0;
  1032. if ((devid == AR5416_AR9100_DEVID))
  1033. ah->ah_macVersion = AR_SREV_VERSION_9100;
  1034. if (!AR_SREV_9100(ah))
  1035. ah->ah_flags = AH_USE_EEPROM;
  1036. ah->ah_powerLimit = MAX_RATE_POWER;
  1037. ah->ah_tpScale = ATH9K_TP_SCALE_MAX;
  1038. ahp->ah_atimWindow = 0;
  1039. ahp->ah_diversityControl = ah->ah_config.diversity_control;
  1040. ahp->ah_antennaSwitchSwap =
  1041. ah->ah_config.antenna_switch_swap;
  1042. ahp->ah_staId1Defaults = AR_STA_ID1_CRPT_MIC_ENABLE;
  1043. ahp->ah_beaconInterval = 100;
  1044. ahp->ah_enable32kHzClock = DONT_USE_32KHZ;
  1045. ahp->ah_slottime = (u32) -1;
  1046. ahp->ah_acktimeout = (u32) -1;
  1047. ahp->ah_ctstimeout = (u32) -1;
  1048. ahp->ah_globaltxtimeout = (u32) -1;
  1049. memcpy(&ahp->ah_bssidmask, defbssidmask, ETH_ALEN);
  1050. ahp->ah_gBeaconRate = 0;
  1051. return ahp;
  1052. }
  1053. static int ath9k_hw_eeprom_attach(struct ath_hal *ah)
  1054. {
  1055. int status;
  1056. if (ath9k_hw_use_flash(ah))
  1057. ath9k_hw_flash_map(ah);
  1058. if (!ath9k_hw_fill_eeprom(ah))
  1059. return -EIO;
  1060. status = ath9k_hw_check_eeprom(ah);
  1061. return status;
  1062. }
  1063. u32 ath9k_hw_get_eeprom(struct ath_hal_5416 *ahp,
  1064. enum eeprom_param param)
  1065. {
  1066. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  1067. struct modal_eep_header *pModal = eep->modalHeader;
  1068. struct base_eep_header *pBase = &eep->baseEepHeader;
  1069. switch (param) {
  1070. case EEP_NFTHRESH_5:
  1071. return -pModal[0].noiseFloorThreshCh[0];
  1072. case EEP_NFTHRESH_2:
  1073. return -pModal[1].noiseFloorThreshCh[0];
  1074. case AR_EEPROM_MAC(0):
  1075. return pBase->macAddr[0] << 8 | pBase->macAddr[1];
  1076. case AR_EEPROM_MAC(1):
  1077. return pBase->macAddr[2] << 8 | pBase->macAddr[3];
  1078. case AR_EEPROM_MAC(2):
  1079. return pBase->macAddr[4] << 8 | pBase->macAddr[5];
  1080. case EEP_REG_0:
  1081. return pBase->regDmn[0];
  1082. case EEP_REG_1:
  1083. return pBase->regDmn[1];
  1084. case EEP_OP_CAP:
  1085. return pBase->deviceCap;
  1086. case EEP_OP_MODE:
  1087. return pBase->opCapFlags;
  1088. case EEP_RF_SILENT:
  1089. return pBase->rfSilent;
  1090. case EEP_OB_5:
  1091. return pModal[0].ob;
  1092. case EEP_DB_5:
  1093. return pModal[0].db;
  1094. case EEP_OB_2:
  1095. return pModal[1].ob;
  1096. case EEP_DB_2:
  1097. return pModal[1].db;
  1098. case EEP_MINOR_REV:
  1099. return pBase->version & AR5416_EEP_VER_MINOR_MASK;
  1100. case EEP_TX_MASK:
  1101. return pBase->txMask;
  1102. case EEP_RX_MASK:
  1103. return pBase->rxMask;
  1104. default:
  1105. return 0;
  1106. }
  1107. }
  1108. static int ath9k_hw_get_radiorev(struct ath_hal *ah)
  1109. {
  1110. u32 val;
  1111. int i;
  1112. REG_WRITE(ah, AR_PHY(0x36), 0x00007058);
  1113. for (i = 0; i < 8; i++)
  1114. REG_WRITE(ah, AR_PHY(0x20), 0x00010000);
  1115. val = (REG_READ(ah, AR_PHY(256)) >> 24) & 0xff;
  1116. val = ((val & 0xf0) >> 4) | ((val & 0x0f) << 4);
  1117. return ath9k_hw_reverse_bits(val, 8);
  1118. }
  1119. static int ath9k_hw_init_macaddr(struct ath_hal *ah)
  1120. {
  1121. u32 sum;
  1122. int i;
  1123. u16 eeval;
  1124. struct ath_hal_5416 *ahp = AH5416(ah);
  1125. DECLARE_MAC_BUF(mac);
  1126. sum = 0;
  1127. for (i = 0; i < 3; i++) {
  1128. eeval = ath9k_hw_get_eeprom(ahp, AR_EEPROM_MAC(i));
  1129. sum += eeval;
  1130. ahp->ah_macaddr[2 * i] = eeval >> 8;
  1131. ahp->ah_macaddr[2 * i + 1] = eeval & 0xff;
  1132. }
  1133. if (sum == 0 || sum == 0xffff * 3) {
  1134. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  1135. "%s: mac address read failed: %s\n", __func__,
  1136. print_mac(mac, ahp->ah_macaddr));
  1137. return -EADDRNOTAVAIL;
  1138. }
  1139. return 0;
  1140. }
  1141. static inline int16_t ath9k_hw_interpolate(u16 target,
  1142. u16 srcLeft,
  1143. u16 srcRight,
  1144. int16_t targetLeft,
  1145. int16_t targetRight)
  1146. {
  1147. int16_t rv;
  1148. if (srcRight == srcLeft) {
  1149. rv = targetLeft;
  1150. } else {
  1151. rv = (int16_t) (((target - srcLeft) * targetRight +
  1152. (srcRight - target) * targetLeft) /
  1153. (srcRight - srcLeft));
  1154. }
  1155. return rv;
  1156. }
  1157. static inline u16 ath9k_hw_fbin2freq(u8 fbin,
  1158. bool is2GHz)
  1159. {
  1160. if (fbin == AR5416_BCHAN_UNUSED)
  1161. return fbin;
  1162. return (u16) ((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
  1163. }
  1164. static u16 ath9k_hw_eeprom_get_spur_chan(struct ath_hal *ah,
  1165. u16 i,
  1166. bool is2GHz)
  1167. {
  1168. struct ath_hal_5416 *ahp = AH5416(ah);
  1169. struct ar5416_eeprom *eep =
  1170. (struct ar5416_eeprom *) &ahp->ah_eeprom;
  1171. u16 spur_val = AR_NO_SPUR;
  1172. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1173. "Getting spur idx %d is2Ghz. %d val %x\n",
  1174. i, is2GHz, ah->ah_config.spurchans[i][is2GHz]);
  1175. switch (ah->ah_config.spurmode) {
  1176. case SPUR_DISABLE:
  1177. break;
  1178. case SPUR_ENABLE_IOCTL:
  1179. spur_val = ah->ah_config.spurchans[i][is2GHz];
  1180. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1181. "Getting spur val from new loc. %d\n", spur_val);
  1182. break;
  1183. case SPUR_ENABLE_EEPROM:
  1184. spur_val = eep->modalHeader[is2GHz].spurChans[i].spurChan;
  1185. break;
  1186. }
  1187. return spur_val;
  1188. }
  1189. static int ath9k_hw_rfattach(struct ath_hal *ah)
  1190. {
  1191. bool rfStatus = false;
  1192. int ecode = 0;
  1193. rfStatus = ath9k_hw_init_rf(ah, &ecode);
  1194. if (!rfStatus) {
  1195. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  1196. "%s: RF setup failed, status %u\n", __func__,
  1197. ecode);
  1198. return ecode;
  1199. }
  1200. return 0;
  1201. }
  1202. static int ath9k_hw_rf_claim(struct ath_hal *ah)
  1203. {
  1204. u32 val;
  1205. REG_WRITE(ah, AR_PHY(0), 0x00000007);
  1206. val = ath9k_hw_get_radiorev(ah);
  1207. switch (val & AR_RADIO_SREV_MAJOR) {
  1208. case 0:
  1209. val = AR_RAD5133_SREV_MAJOR;
  1210. break;
  1211. case AR_RAD5133_SREV_MAJOR:
  1212. case AR_RAD5122_SREV_MAJOR:
  1213. case AR_RAD2133_SREV_MAJOR:
  1214. case AR_RAD2122_SREV_MAJOR:
  1215. break;
  1216. default:
  1217. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  1218. "%s: 5G Radio Chip Rev 0x%02X is not "
  1219. "supported by this driver\n",
  1220. __func__, ah->ah_analog5GhzRev);
  1221. return -EOPNOTSUPP;
  1222. }
  1223. ah->ah_analog5GhzRev = val;
  1224. return 0;
  1225. }
  1226. static void ath9k_hw_init_pll(struct ath_hal *ah,
  1227. struct ath9k_channel *chan)
  1228. {
  1229. u32 pll;
  1230. if (AR_SREV_9100(ah)) {
  1231. if (chan && IS_CHAN_5GHZ(chan))
  1232. pll = 0x1450;
  1233. else
  1234. pll = 0x1458;
  1235. } else {
  1236. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1237. pll = SM(0x5, AR_RTC_9160_PLL_REFDIV);
  1238. if (chan && IS_CHAN_HALF_RATE(chan))
  1239. pll |= SM(0x1, AR_RTC_9160_PLL_CLKSEL);
  1240. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  1241. pll |= SM(0x2, AR_RTC_9160_PLL_CLKSEL);
  1242. if (chan && IS_CHAN_5GHZ(chan)) {
  1243. pll |= SM(0x28, AR_RTC_9160_PLL_DIV);
  1244. if (AR_SREV_9280_20(ah)) {
  1245. if (((chan->channel % 20) == 0)
  1246. || ((chan->channel % 10) == 0))
  1247. pll = 0x2850;
  1248. else
  1249. pll = 0x142c;
  1250. }
  1251. } else {
  1252. pll |= SM(0x2c, AR_RTC_9160_PLL_DIV);
  1253. }
  1254. } else if (AR_SREV_9160_10_OR_LATER(ah)) {
  1255. pll = SM(0x5, AR_RTC_9160_PLL_REFDIV);
  1256. if (chan && IS_CHAN_HALF_RATE(chan))
  1257. pll |= SM(0x1, AR_RTC_9160_PLL_CLKSEL);
  1258. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  1259. pll |= SM(0x2, AR_RTC_9160_PLL_CLKSEL);
  1260. if (chan && IS_CHAN_5GHZ(chan))
  1261. pll |= SM(0x50, AR_RTC_9160_PLL_DIV);
  1262. else
  1263. pll |= SM(0x58, AR_RTC_9160_PLL_DIV);
  1264. } else {
  1265. pll = AR_RTC_PLL_REFDIV_5 | AR_RTC_PLL_DIV2;
  1266. if (chan && IS_CHAN_HALF_RATE(chan))
  1267. pll |= SM(0x1, AR_RTC_PLL_CLKSEL);
  1268. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  1269. pll |= SM(0x2, AR_RTC_PLL_CLKSEL);
  1270. if (chan && IS_CHAN_5GHZ(chan))
  1271. pll |= SM(0xa, AR_RTC_PLL_DIV);
  1272. else
  1273. pll |= SM(0xb, AR_RTC_PLL_DIV);
  1274. }
  1275. }
  1276. REG_WRITE(ah, (u16) (AR_RTC_PLL_CONTROL), pll);
  1277. udelay(RTC_PLL_SETTLE_DELAY);
  1278. REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
  1279. }
  1280. static void ath9k_hw_set_regs(struct ath_hal *ah, struct ath9k_channel *chan,
  1281. enum ath9k_ht_macmode macmode)
  1282. {
  1283. u32 phymode;
  1284. struct ath_hal_5416 *ahp = AH5416(ah);
  1285. phymode = AR_PHY_FC_HT_EN | AR_PHY_FC_SHORT_GI_40
  1286. | AR_PHY_FC_SINGLE_HT_LTF1 | AR_PHY_FC_WALSH;
  1287. if (IS_CHAN_HT40(chan)) {
  1288. phymode |= AR_PHY_FC_DYN2040_EN;
  1289. if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
  1290. (chan->chanmode == CHANNEL_G_HT40PLUS))
  1291. phymode |= AR_PHY_FC_DYN2040_PRI_CH;
  1292. if (ahp->ah_extprotspacing == ATH9K_HT_EXTPROTSPACING_25)
  1293. phymode |= AR_PHY_FC_DYN2040_EXT_CH;
  1294. }
  1295. REG_WRITE(ah, AR_PHY_TURBO, phymode);
  1296. ath9k_hw_set11nmac2040(ah, macmode);
  1297. REG_WRITE(ah, AR_GTXTO, 25 << AR_GTXTO_TIMEOUT_LIMIT_S);
  1298. REG_WRITE(ah, AR_CST, 0xF << AR_CST_TIMEOUT_LIMIT_S);
  1299. }
  1300. static void ath9k_hw_set_operating_mode(struct ath_hal *ah, int opmode)
  1301. {
  1302. u32 val;
  1303. val = REG_READ(ah, AR_STA_ID1);
  1304. val &= ~(AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC);
  1305. switch (opmode) {
  1306. case ATH9K_M_HOSTAP:
  1307. REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_STA_AP
  1308. | AR_STA_ID1_KSRCH_MODE);
  1309. REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  1310. break;
  1311. case ATH9K_M_IBSS:
  1312. REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_ADHOC
  1313. | AR_STA_ID1_KSRCH_MODE);
  1314. REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  1315. break;
  1316. case ATH9K_M_STA:
  1317. case ATH9K_M_MONITOR:
  1318. REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_KSRCH_MODE);
  1319. break;
  1320. }
  1321. }
  1322. static void
  1323. ath9k_hw_set_rfmode(struct ath_hal *ah, struct ath9k_channel *chan)
  1324. {
  1325. u32 rfMode = 0;
  1326. if (chan == NULL)
  1327. return;
  1328. rfMode |= (IS_CHAN_B(chan) || IS_CHAN_G(chan))
  1329. ? AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
  1330. if (!AR_SREV_9280_10_OR_LATER(ah))
  1331. rfMode |= (IS_CHAN_5GHZ(chan)) ? AR_PHY_MODE_RF5GHZ :
  1332. AR_PHY_MODE_RF2GHZ;
  1333. if (AR_SREV_9280_20(ah) && IS_CHAN_A_5MHZ_SPACED(chan))
  1334. rfMode |= (AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE);
  1335. REG_WRITE(ah, AR_PHY_MODE, rfMode);
  1336. }
  1337. static bool ath9k_hw_set_reset(struct ath_hal *ah, int type)
  1338. {
  1339. u32 rst_flags;
  1340. u32 tmpReg;
  1341. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  1342. AR_RTC_FORCE_WAKE_ON_INT);
  1343. if (AR_SREV_9100(ah)) {
  1344. rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
  1345. AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
  1346. } else {
  1347. tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
  1348. if (tmpReg &
  1349. (AR_INTR_SYNC_LOCAL_TIMEOUT |
  1350. AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
  1351. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
  1352. REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
  1353. } else {
  1354. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  1355. }
  1356. rst_flags = AR_RTC_RC_MAC_WARM;
  1357. if (type == ATH9K_RESET_COLD)
  1358. rst_flags |= AR_RTC_RC_MAC_COLD;
  1359. }
  1360. REG_WRITE(ah, (u16) (AR_RTC_RC), rst_flags);
  1361. udelay(50);
  1362. REG_WRITE(ah, (u16) (AR_RTC_RC), 0);
  1363. if (!ath9k_hw_wait(ah, (u16) (AR_RTC_RC), AR_RTC_RC_M, 0)) {
  1364. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  1365. "%s: RTC stuck in MAC reset\n",
  1366. __func__);
  1367. return false;
  1368. }
  1369. if (!AR_SREV_9100(ah))
  1370. REG_WRITE(ah, AR_RC, 0);
  1371. ath9k_hw_init_pll(ah, NULL);
  1372. if (AR_SREV_9100(ah))
  1373. udelay(50);
  1374. return true;
  1375. }
  1376. static bool ath9k_hw_set_reset_power_on(struct ath_hal *ah)
  1377. {
  1378. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  1379. AR_RTC_FORCE_WAKE_ON_INT);
  1380. REG_WRITE(ah, (u16) (AR_RTC_RESET), 0);
  1381. REG_WRITE(ah, (u16) (AR_RTC_RESET), 1);
  1382. if (!ath9k_hw_wait(ah,
  1383. AR_RTC_STATUS,
  1384. AR_RTC_STATUS_M,
  1385. AR_RTC_STATUS_ON)) {
  1386. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: RTC not waking up\n",
  1387. __func__);
  1388. return false;
  1389. }
  1390. ath9k_hw_read_revisions(ah);
  1391. return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
  1392. }
  1393. static bool ath9k_hw_set_reset_reg(struct ath_hal *ah,
  1394. u32 type)
  1395. {
  1396. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  1397. AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
  1398. switch (type) {
  1399. case ATH9K_RESET_POWER_ON:
  1400. return ath9k_hw_set_reset_power_on(ah);
  1401. break;
  1402. case ATH9K_RESET_WARM:
  1403. case ATH9K_RESET_COLD:
  1404. return ath9k_hw_set_reset(ah, type);
  1405. break;
  1406. default:
  1407. return false;
  1408. }
  1409. }
  1410. static
  1411. struct ath9k_channel *ath9k_hw_check_chan(struct ath_hal *ah,
  1412. struct ath9k_channel *chan)
  1413. {
  1414. if (!(IS_CHAN_2GHZ(chan) ^ IS_CHAN_5GHZ(chan))) {
  1415. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  1416. "%s: invalid channel %u/0x%x; not marked as "
  1417. "2GHz or 5GHz\n", __func__, chan->channel,
  1418. chan->channelFlags);
  1419. return NULL;
  1420. }
  1421. if (!IS_CHAN_OFDM(chan) &&
  1422. !IS_CHAN_CCK(chan) &&
  1423. !IS_CHAN_HT20(chan) &&
  1424. !IS_CHAN_HT40(chan)) {
  1425. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  1426. "%s: invalid channel %u/0x%x; not marked as "
  1427. "OFDM or CCK or HT20 or HT40PLUS or HT40MINUS\n",
  1428. __func__, chan->channel, chan->channelFlags);
  1429. return NULL;
  1430. }
  1431. return ath9k_regd_check_channel(ah, chan);
  1432. }
  1433. static inline bool
  1434. ath9k_hw_get_lower_upper_index(u8 target,
  1435. u8 *pList,
  1436. u16 listSize,
  1437. u16 *indexL,
  1438. u16 *indexR)
  1439. {
  1440. u16 i;
  1441. if (target <= pList[0]) {
  1442. *indexL = *indexR = 0;
  1443. return true;
  1444. }
  1445. if (target >= pList[listSize - 1]) {
  1446. *indexL = *indexR = (u16) (listSize - 1);
  1447. return true;
  1448. }
  1449. for (i = 0; i < listSize - 1; i++) {
  1450. if (pList[i] == target) {
  1451. *indexL = *indexR = i;
  1452. return true;
  1453. }
  1454. if (target < pList[i + 1]) {
  1455. *indexL = i;
  1456. *indexR = (u16) (i + 1);
  1457. return false;
  1458. }
  1459. }
  1460. return false;
  1461. }
  1462. static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
  1463. {
  1464. int16_t nfval;
  1465. int16_t sort[ATH9K_NF_CAL_HIST_MAX];
  1466. int i, j;
  1467. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
  1468. sort[i] = nfCalBuffer[i];
  1469. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
  1470. for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
  1471. if (sort[j] > sort[j - 1]) {
  1472. nfval = sort[j];
  1473. sort[j] = sort[j - 1];
  1474. sort[j - 1] = nfval;
  1475. }
  1476. }
  1477. }
  1478. nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
  1479. return nfval;
  1480. }
  1481. static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
  1482. int16_t *nfarray)
  1483. {
  1484. int i;
  1485. for (i = 0; i < NUM_NF_READINGS; i++) {
  1486. h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
  1487. if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
  1488. h[i].currIndex = 0;
  1489. if (h[i].invalidNFcount > 0) {
  1490. if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE
  1491. || nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) {
  1492. h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX;
  1493. } else {
  1494. h[i].invalidNFcount--;
  1495. h[i].privNF = nfarray[i];
  1496. }
  1497. } else {
  1498. h[i].privNF =
  1499. ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
  1500. }
  1501. }
  1502. return;
  1503. }
  1504. static void ar5416GetNoiseFloor(struct ath_hal *ah,
  1505. int16_t nfarray[NUM_NF_READINGS])
  1506. {
  1507. int16_t nf;
  1508. if (AR_SREV_9280_10_OR_LATER(ah))
  1509. nf = MS(REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
  1510. else
  1511. nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
  1512. if (nf & 0x100)
  1513. nf = 0 - ((nf ^ 0x1ff) + 1);
  1514. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  1515. "NF calibrated [ctl] [chain 0] is %d\n", nf);
  1516. nfarray[0] = nf;
  1517. if (AR_SREV_9280_10_OR_LATER(ah))
  1518. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
  1519. AR9280_PHY_CH1_MINCCA_PWR);
  1520. else
  1521. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
  1522. AR_PHY_CH1_MINCCA_PWR);
  1523. if (nf & 0x100)
  1524. nf = 0 - ((nf ^ 0x1ff) + 1);
  1525. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  1526. "NF calibrated [ctl] [chain 1] is %d\n", nf);
  1527. nfarray[1] = nf;
  1528. if (!AR_SREV_9280(ah)) {
  1529. nf = MS(REG_READ(ah, AR_PHY_CH2_CCA),
  1530. AR_PHY_CH2_MINCCA_PWR);
  1531. if (nf & 0x100)
  1532. nf = 0 - ((nf ^ 0x1ff) + 1);
  1533. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  1534. "NF calibrated [ctl] [chain 2] is %d\n", nf);
  1535. nfarray[2] = nf;
  1536. }
  1537. if (AR_SREV_9280_10_OR_LATER(ah))
  1538. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
  1539. AR9280_PHY_EXT_MINCCA_PWR);
  1540. else
  1541. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
  1542. AR_PHY_EXT_MINCCA_PWR);
  1543. if (nf & 0x100)
  1544. nf = 0 - ((nf ^ 0x1ff) + 1);
  1545. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  1546. "NF calibrated [ext] [chain 0] is %d\n", nf);
  1547. nfarray[3] = nf;
  1548. if (AR_SREV_9280_10_OR_LATER(ah))
  1549. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
  1550. AR9280_PHY_CH1_EXT_MINCCA_PWR);
  1551. else
  1552. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
  1553. AR_PHY_CH1_EXT_MINCCA_PWR);
  1554. if (nf & 0x100)
  1555. nf = 0 - ((nf ^ 0x1ff) + 1);
  1556. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  1557. "NF calibrated [ext] [chain 1] is %d\n", nf);
  1558. nfarray[4] = nf;
  1559. if (!AR_SREV_9280(ah)) {
  1560. nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA),
  1561. AR_PHY_CH2_EXT_MINCCA_PWR);
  1562. if (nf & 0x100)
  1563. nf = 0 - ((nf ^ 0x1ff) + 1);
  1564. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  1565. "NF calibrated [ext] [chain 2] is %d\n", nf);
  1566. nfarray[5] = nf;
  1567. }
  1568. }
  1569. static bool
  1570. getNoiseFloorThresh(struct ath_hal *ah,
  1571. const struct ath9k_channel *chan,
  1572. int16_t *nft)
  1573. {
  1574. struct ath_hal_5416 *ahp = AH5416(ah);
  1575. switch (chan->chanmode) {
  1576. case CHANNEL_A:
  1577. case CHANNEL_A_HT20:
  1578. case CHANNEL_A_HT40PLUS:
  1579. case CHANNEL_A_HT40MINUS:
  1580. *nft = (int16_t) ath9k_hw_get_eeprom(ahp, EEP_NFTHRESH_5);
  1581. break;
  1582. case CHANNEL_B:
  1583. case CHANNEL_G:
  1584. case CHANNEL_G_HT20:
  1585. case CHANNEL_G_HT40PLUS:
  1586. case CHANNEL_G_HT40MINUS:
  1587. *nft = (int16_t) ath9k_hw_get_eeprom(ahp, EEP_NFTHRESH_2);
  1588. break;
  1589. default:
  1590. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  1591. "%s: invalid channel flags 0x%x\n", __func__,
  1592. chan->channelFlags);
  1593. return false;
  1594. }
  1595. return true;
  1596. }
  1597. static void ath9k_hw_start_nfcal(struct ath_hal *ah)
  1598. {
  1599. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  1600. AR_PHY_AGC_CONTROL_ENABLE_NF);
  1601. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  1602. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  1603. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  1604. }
  1605. static void
  1606. ath9k_hw_loadnf(struct ath_hal *ah, struct ath9k_channel *chan)
  1607. {
  1608. struct ath9k_nfcal_hist *h;
  1609. int i, j;
  1610. int32_t val;
  1611. const u32 ar5416_cca_regs[6] = {
  1612. AR_PHY_CCA,
  1613. AR_PHY_CH1_CCA,
  1614. AR_PHY_CH2_CCA,
  1615. AR_PHY_EXT_CCA,
  1616. AR_PHY_CH1_EXT_CCA,
  1617. AR_PHY_CH2_EXT_CCA
  1618. };
  1619. u8 chainmask;
  1620. if (AR_SREV_9280(ah))
  1621. chainmask = 0x1B;
  1622. else
  1623. chainmask = 0x3F;
  1624. #ifdef ATH_NF_PER_CHAN
  1625. h = chan->nfCalHist;
  1626. #else
  1627. h = ah->nfCalHist;
  1628. #endif
  1629. for (i = 0; i < NUM_NF_READINGS; i++) {
  1630. if (chainmask & (1 << i)) {
  1631. val = REG_READ(ah, ar5416_cca_regs[i]);
  1632. val &= 0xFFFFFE00;
  1633. val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
  1634. REG_WRITE(ah, ar5416_cca_regs[i], val);
  1635. }
  1636. }
  1637. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  1638. AR_PHY_AGC_CONTROL_ENABLE_NF);
  1639. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  1640. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  1641. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  1642. for (j = 0; j < 1000; j++) {
  1643. if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
  1644. AR_PHY_AGC_CONTROL_NF) == 0)
  1645. break;
  1646. udelay(10);
  1647. }
  1648. for (i = 0; i < NUM_NF_READINGS; i++) {
  1649. if (chainmask & (1 << i)) {
  1650. val = REG_READ(ah, ar5416_cca_regs[i]);
  1651. val &= 0xFFFFFE00;
  1652. val |= (((u32) (-50) << 1) & 0x1ff);
  1653. REG_WRITE(ah, ar5416_cca_regs[i], val);
  1654. }
  1655. }
  1656. }
  1657. static int16_t ath9k_hw_getnf(struct ath_hal *ah,
  1658. struct ath9k_channel *chan)
  1659. {
  1660. int16_t nf, nfThresh;
  1661. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  1662. struct ath9k_nfcal_hist *h;
  1663. u8 chainmask;
  1664. if (AR_SREV_9280(ah))
  1665. chainmask = 0x1B;
  1666. else
  1667. chainmask = 0x3F;
  1668. chan->channelFlags &= (~CHANNEL_CW_INT);
  1669. if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
  1670. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  1671. "%s: NF did not complete in calibration window\n",
  1672. __func__);
  1673. nf = 0;
  1674. chan->rawNoiseFloor = nf;
  1675. return chan->rawNoiseFloor;
  1676. } else {
  1677. ar5416GetNoiseFloor(ah, nfarray);
  1678. nf = nfarray[0];
  1679. if (getNoiseFloorThresh(ah, chan, &nfThresh)
  1680. && nf > nfThresh) {
  1681. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  1682. "%s: noise floor failed detected; "
  1683. "detected %d, threshold %d\n", __func__,
  1684. nf, nfThresh);
  1685. chan->channelFlags |= CHANNEL_CW_INT;
  1686. }
  1687. }
  1688. #ifdef ATH_NF_PER_CHAN
  1689. h = chan->nfCalHist;
  1690. #else
  1691. h = ah->nfCalHist;
  1692. #endif
  1693. ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
  1694. chan->rawNoiseFloor = h[0].privNF;
  1695. return chan->rawNoiseFloor;
  1696. }
  1697. static void ath9k_hw_update_mibstats(struct ath_hal *ah,
  1698. struct ath9k_mib_stats *stats)
  1699. {
  1700. stats->ackrcv_bad += REG_READ(ah, AR_ACK_FAIL);
  1701. stats->rts_bad += REG_READ(ah, AR_RTS_FAIL);
  1702. stats->fcs_bad += REG_READ(ah, AR_FCS_FAIL);
  1703. stats->rts_good += REG_READ(ah, AR_RTS_OK);
  1704. stats->beacons += REG_READ(ah, AR_BEACON_CNT);
  1705. }
  1706. static void ath9k_enable_mib_counters(struct ath_hal *ah)
  1707. {
  1708. struct ath_hal_5416 *ahp = AH5416(ah);
  1709. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Enable mib counters\n");
  1710. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  1711. REG_WRITE(ah, AR_FILT_OFDM, 0);
  1712. REG_WRITE(ah, AR_FILT_CCK, 0);
  1713. REG_WRITE(ah, AR_MIBC,
  1714. ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS)
  1715. & 0x0f);
  1716. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  1717. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  1718. }
  1719. static void ath9k_hw_disable_mib_counters(struct ath_hal *ah)
  1720. {
  1721. struct ath_hal_5416 *ahp = AH5416(ah);
  1722. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Disabling MIB counters\n");
  1723. REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC | AR_MIBC_CMC);
  1724. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  1725. REG_WRITE(ah, AR_FILT_OFDM, 0);
  1726. REG_WRITE(ah, AR_FILT_CCK, 0);
  1727. }
  1728. static int ath9k_hw_get_ani_channel_idx(struct ath_hal *ah,
  1729. struct ath9k_channel *chan)
  1730. {
  1731. struct ath_hal_5416 *ahp = AH5416(ah);
  1732. int i;
  1733. for (i = 0; i < ARRAY_SIZE(ahp->ah_ani); i++) {
  1734. if (ahp->ah_ani[i].c.channel == chan->channel)
  1735. return i;
  1736. if (ahp->ah_ani[i].c.channel == 0) {
  1737. ahp->ah_ani[i].c.channel = chan->channel;
  1738. ahp->ah_ani[i].c.channelFlags = chan->channelFlags;
  1739. return i;
  1740. }
  1741. }
  1742. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1743. "No more channel states left. Using channel 0\n");
  1744. return 0;
  1745. }
  1746. static void ath9k_hw_ani_attach(struct ath_hal *ah)
  1747. {
  1748. struct ath_hal_5416 *ahp = AH5416(ah);
  1749. int i;
  1750. ahp->ah_hasHwPhyCounters = 1;
  1751. memset(ahp->ah_ani, 0, sizeof(ahp->ah_ani));
  1752. for (i = 0; i < ARRAY_SIZE(ahp->ah_ani); i++) {
  1753. ahp->ah_ani[i].ofdmTrigHigh = ATH9K_ANI_OFDM_TRIG_HIGH;
  1754. ahp->ah_ani[i].ofdmTrigLow = ATH9K_ANI_OFDM_TRIG_LOW;
  1755. ahp->ah_ani[i].cckTrigHigh = ATH9K_ANI_CCK_TRIG_HIGH;
  1756. ahp->ah_ani[i].cckTrigLow = ATH9K_ANI_CCK_TRIG_LOW;
  1757. ahp->ah_ani[i].rssiThrHigh = ATH9K_ANI_RSSI_THR_HIGH;
  1758. ahp->ah_ani[i].rssiThrLow = ATH9K_ANI_RSSI_THR_LOW;
  1759. ahp->ah_ani[i].ofdmWeakSigDetectOff =
  1760. !ATH9K_ANI_USE_OFDM_WEAK_SIG;
  1761. ahp->ah_ani[i].cckWeakSigThreshold =
  1762. ATH9K_ANI_CCK_WEAK_SIG_THR;
  1763. ahp->ah_ani[i].spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  1764. ahp->ah_ani[i].firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  1765. if (ahp->ah_hasHwPhyCounters) {
  1766. ahp->ah_ani[i].ofdmPhyErrBase =
  1767. AR_PHY_COUNTMAX - ATH9K_ANI_OFDM_TRIG_HIGH;
  1768. ahp->ah_ani[i].cckPhyErrBase =
  1769. AR_PHY_COUNTMAX - ATH9K_ANI_CCK_TRIG_HIGH;
  1770. }
  1771. }
  1772. if (ahp->ah_hasHwPhyCounters) {
  1773. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1774. "Setting OfdmErrBase = 0x%08x\n",
  1775. ahp->ah_ani[0].ofdmPhyErrBase);
  1776. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Setting cckErrBase = 0x%08x\n",
  1777. ahp->ah_ani[0].cckPhyErrBase);
  1778. REG_WRITE(ah, AR_PHY_ERR_1, ahp->ah_ani[0].ofdmPhyErrBase);
  1779. REG_WRITE(ah, AR_PHY_ERR_2, ahp->ah_ani[0].cckPhyErrBase);
  1780. ath9k_enable_mib_counters(ah);
  1781. }
  1782. ahp->ah_aniPeriod = ATH9K_ANI_PERIOD;
  1783. if (ah->ah_config.enable_ani)
  1784. ahp->ah_procPhyErr |= HAL_PROCESS_ANI;
  1785. }
  1786. static void ath9k_hw_ani_setup(struct ath_hal *ah)
  1787. {
  1788. struct ath_hal_5416 *ahp = AH5416(ah);
  1789. int i;
  1790. const int totalSizeDesired[] = { -55, -55, -55, -55, -62 };
  1791. const int coarseHigh[] = { -14, -14, -14, -14, -12 };
  1792. const int coarseLow[] = { -64, -64, -64, -64, -70 };
  1793. const int firpwr[] = { -78, -78, -78, -78, -80 };
  1794. for (i = 0; i < 5; i++) {
  1795. ahp->ah_totalSizeDesired[i] = totalSizeDesired[i];
  1796. ahp->ah_coarseHigh[i] = coarseHigh[i];
  1797. ahp->ah_coarseLow[i] = coarseLow[i];
  1798. ahp->ah_firpwr[i] = firpwr[i];
  1799. }
  1800. }
  1801. static void ath9k_hw_ani_detach(struct ath_hal *ah)
  1802. {
  1803. struct ath_hal_5416 *ahp = AH5416(ah);
  1804. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Detaching Ani\n");
  1805. if (ahp->ah_hasHwPhyCounters) {
  1806. ath9k_hw_disable_mib_counters(ah);
  1807. REG_WRITE(ah, AR_PHY_ERR_1, 0);
  1808. REG_WRITE(ah, AR_PHY_ERR_2, 0);
  1809. }
  1810. }
  1811. static bool ath9k_hw_ani_control(struct ath_hal *ah,
  1812. enum ath9k_ani_cmd cmd, int param)
  1813. {
  1814. struct ath_hal_5416 *ahp = AH5416(ah);
  1815. struct ar5416AniState *aniState = ahp->ah_curani;
  1816. switch (cmd & ahp->ah_ani_function) {
  1817. case ATH9K_ANI_NOISE_IMMUNITY_LEVEL:{
  1818. u32 level = param;
  1819. if (level >= ARRAY_SIZE(ahp->ah_totalSizeDesired)) {
  1820. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1821. "%s: level out of range (%u > %u)\n",
  1822. __func__, level,
  1823. (unsigned) ARRAY_SIZE(ahp->
  1824. ah_totalSizeDesired));
  1825. return false;
  1826. }
  1827. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
  1828. AR_PHY_DESIRED_SZ_TOT_DES,
  1829. ahp->ah_totalSizeDesired[level]);
  1830. REG_RMW_FIELD(ah, AR_PHY_AGC_CTL1,
  1831. AR_PHY_AGC_CTL1_COARSE_LOW,
  1832. ahp->ah_coarseLow[level]);
  1833. REG_RMW_FIELD(ah, AR_PHY_AGC_CTL1,
  1834. AR_PHY_AGC_CTL1_COARSE_HIGH,
  1835. ahp->ah_coarseHigh[level]);
  1836. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  1837. AR_PHY_FIND_SIG_FIRPWR,
  1838. ahp->ah_firpwr[level]);
  1839. if (level > aniState->noiseImmunityLevel)
  1840. ahp->ah_stats.ast_ani_niup++;
  1841. else if (level < aniState->noiseImmunityLevel)
  1842. ahp->ah_stats.ast_ani_nidown++;
  1843. aniState->noiseImmunityLevel = level;
  1844. break;
  1845. }
  1846. case ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION:{
  1847. const int m1ThreshLow[] = { 127, 50 };
  1848. const int m2ThreshLow[] = { 127, 40 };
  1849. const int m1Thresh[] = { 127, 0x4d };
  1850. const int m2Thresh[] = { 127, 0x40 };
  1851. const int m2CountThr[] = { 31, 16 };
  1852. const int m2CountThrLow[] = { 63, 48 };
  1853. u32 on = param ? 1 : 0;
  1854. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  1855. AR_PHY_SFCORR_LOW_M1_THRESH_LOW,
  1856. m1ThreshLow[on]);
  1857. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  1858. AR_PHY_SFCORR_LOW_M2_THRESH_LOW,
  1859. m2ThreshLow[on]);
  1860. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  1861. AR_PHY_SFCORR_M1_THRESH,
  1862. m1Thresh[on]);
  1863. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  1864. AR_PHY_SFCORR_M2_THRESH,
  1865. m2Thresh[on]);
  1866. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  1867. AR_PHY_SFCORR_M2COUNT_THR,
  1868. m2CountThr[on]);
  1869. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  1870. AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW,
  1871. m2CountThrLow[on]);
  1872. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  1873. AR_PHY_SFCORR_EXT_M1_THRESH_LOW,
  1874. m1ThreshLow[on]);
  1875. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  1876. AR_PHY_SFCORR_EXT_M2_THRESH_LOW,
  1877. m2ThreshLow[on]);
  1878. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  1879. AR_PHY_SFCORR_EXT_M1_THRESH,
  1880. m1Thresh[on]);
  1881. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  1882. AR_PHY_SFCORR_EXT_M2_THRESH,
  1883. m2Thresh[on]);
  1884. if (on)
  1885. REG_SET_BIT(ah, AR_PHY_SFCORR_LOW,
  1886. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  1887. else
  1888. REG_CLR_BIT(ah, AR_PHY_SFCORR_LOW,
  1889. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  1890. if (!on != aniState->ofdmWeakSigDetectOff) {
  1891. if (on)
  1892. ahp->ah_stats.ast_ani_ofdmon++;
  1893. else
  1894. ahp->ah_stats.ast_ani_ofdmoff++;
  1895. aniState->ofdmWeakSigDetectOff = !on;
  1896. }
  1897. break;
  1898. }
  1899. case ATH9K_ANI_CCK_WEAK_SIGNAL_THR:{
  1900. const int weakSigThrCck[] = { 8, 6 };
  1901. u32 high = param ? 1 : 0;
  1902. REG_RMW_FIELD(ah, AR_PHY_CCK_DETECT,
  1903. AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK,
  1904. weakSigThrCck[high]);
  1905. if (high != aniState->cckWeakSigThreshold) {
  1906. if (high)
  1907. ahp->ah_stats.ast_ani_cckhigh++;
  1908. else
  1909. ahp->ah_stats.ast_ani_ccklow++;
  1910. aniState->cckWeakSigThreshold = high;
  1911. }
  1912. break;
  1913. }
  1914. case ATH9K_ANI_FIRSTEP_LEVEL:{
  1915. const int firstep[] = { 0, 4, 8 };
  1916. u32 level = param;
  1917. if (level >= ARRAY_SIZE(firstep)) {
  1918. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1919. "%s: level out of range (%u > %u)\n",
  1920. __func__, level,
  1921. (unsigned) ARRAY_SIZE(firstep));
  1922. return false;
  1923. }
  1924. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  1925. AR_PHY_FIND_SIG_FIRSTEP,
  1926. firstep[level]);
  1927. if (level > aniState->firstepLevel)
  1928. ahp->ah_stats.ast_ani_stepup++;
  1929. else if (level < aniState->firstepLevel)
  1930. ahp->ah_stats.ast_ani_stepdown++;
  1931. aniState->firstepLevel = level;
  1932. break;
  1933. }
  1934. case ATH9K_ANI_SPUR_IMMUNITY_LEVEL:{
  1935. const int cycpwrThr1[] =
  1936. { 2, 4, 6, 8, 10, 12, 14, 16 };
  1937. u32 level = param;
  1938. if (level >= ARRAY_SIZE(cycpwrThr1)) {
  1939. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1940. "%s: level out of range (%u > %u)\n",
  1941. __func__, level,
  1942. (unsigned)
  1943. ARRAY_SIZE(cycpwrThr1));
  1944. return false;
  1945. }
  1946. REG_RMW_FIELD(ah, AR_PHY_TIMING5,
  1947. AR_PHY_TIMING5_CYCPWR_THR1,
  1948. cycpwrThr1[level]);
  1949. if (level > aniState->spurImmunityLevel)
  1950. ahp->ah_stats.ast_ani_spurup++;
  1951. else if (level < aniState->spurImmunityLevel)
  1952. ahp->ah_stats.ast_ani_spurdown++;
  1953. aniState->spurImmunityLevel = level;
  1954. break;
  1955. }
  1956. case ATH9K_ANI_PRESENT:
  1957. break;
  1958. default:
  1959. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1960. "%s: invalid cmd %u\n", __func__, cmd);
  1961. return false;
  1962. }
  1963. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "%s: ANI parameters:\n", __func__);
  1964. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1965. "noiseImmunityLevel=%d, spurImmunityLevel=%d, "
  1966. "ofdmWeakSigDetectOff=%d\n",
  1967. aniState->noiseImmunityLevel, aniState->spurImmunityLevel,
  1968. !aniState->ofdmWeakSigDetectOff);
  1969. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1970. "cckWeakSigThreshold=%d, "
  1971. "firstepLevel=%d, listenTime=%d\n",
  1972. aniState->cckWeakSigThreshold, aniState->firstepLevel,
  1973. aniState->listenTime);
  1974. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1975. "cycleCount=%d, ofdmPhyErrCount=%d, cckPhyErrCount=%d\n\n",
  1976. aniState->cycleCount, aniState->ofdmPhyErrCount,
  1977. aniState->cckPhyErrCount);
  1978. return true;
  1979. }
  1980. static void ath9k_ani_restart(struct ath_hal *ah)
  1981. {
  1982. struct ath_hal_5416 *ahp = AH5416(ah);
  1983. struct ar5416AniState *aniState;
  1984. if (!DO_ANI(ah))
  1985. return;
  1986. aniState = ahp->ah_curani;
  1987. aniState->listenTime = 0;
  1988. if (ahp->ah_hasHwPhyCounters) {
  1989. if (aniState->ofdmTrigHigh > AR_PHY_COUNTMAX) {
  1990. aniState->ofdmPhyErrBase = 0;
  1991. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  1992. "OFDM Trigger is too high for hw counters\n");
  1993. } else {
  1994. aniState->ofdmPhyErrBase =
  1995. AR_PHY_COUNTMAX - aniState->ofdmTrigHigh;
  1996. }
  1997. if (aniState->cckTrigHigh > AR_PHY_COUNTMAX) {
  1998. aniState->cckPhyErrBase = 0;
  1999. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2000. "CCK Trigger is too high for hw counters\n");
  2001. } else {
  2002. aniState->cckPhyErrBase =
  2003. AR_PHY_COUNTMAX - aniState->cckTrigHigh;
  2004. }
  2005. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2006. "%s: Writing ofdmbase=%u cckbase=%u\n",
  2007. __func__, aniState->ofdmPhyErrBase,
  2008. aniState->cckPhyErrBase);
  2009. REG_WRITE(ah, AR_PHY_ERR_1, aniState->ofdmPhyErrBase);
  2010. REG_WRITE(ah, AR_PHY_ERR_2, aniState->cckPhyErrBase);
  2011. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  2012. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  2013. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  2014. }
  2015. aniState->ofdmPhyErrCount = 0;
  2016. aniState->cckPhyErrCount = 0;
  2017. }
  2018. static void ath9k_hw_ani_ofdm_err_trigger(struct ath_hal *ah)
  2019. {
  2020. struct ath_hal_5416 *ahp = AH5416(ah);
  2021. struct ath9k_channel *chan = ah->ah_curchan;
  2022. struct ar5416AniState *aniState;
  2023. enum wireless_mode mode;
  2024. int32_t rssi;
  2025. if (!DO_ANI(ah))
  2026. return;
  2027. aniState = ahp->ah_curani;
  2028. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  2029. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  2030. aniState->noiseImmunityLevel + 1)) {
  2031. return;
  2032. }
  2033. }
  2034. if (aniState->spurImmunityLevel < HAL_SPUR_IMMUNE_MAX) {
  2035. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  2036. aniState->spurImmunityLevel + 1)) {
  2037. return;
  2038. }
  2039. }
  2040. if (ah->ah_opmode == ATH9K_M_HOSTAP) {
  2041. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  2042. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2043. aniState->firstepLevel + 1);
  2044. }
  2045. return;
  2046. }
  2047. rssi = BEACON_RSSI(ahp);
  2048. if (rssi > aniState->rssiThrHigh) {
  2049. if (!aniState->ofdmWeakSigDetectOff) {
  2050. if (ath9k_hw_ani_control(ah,
  2051. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  2052. false)) {
  2053. ath9k_hw_ani_control(ah,
  2054. ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  2055. 0);
  2056. return;
  2057. }
  2058. }
  2059. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  2060. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2061. aniState->firstepLevel + 1);
  2062. return;
  2063. }
  2064. } else if (rssi > aniState->rssiThrLow) {
  2065. if (aniState->ofdmWeakSigDetectOff)
  2066. ath9k_hw_ani_control(ah,
  2067. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  2068. true);
  2069. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  2070. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2071. aniState->firstepLevel + 1);
  2072. return;
  2073. } else {
  2074. mode = ath9k_hw_chan2wmode(ah, chan);
  2075. if (mode == ATH9K_MODE_11G || mode == ATH9K_MODE_11B) {
  2076. if (!aniState->ofdmWeakSigDetectOff)
  2077. ath9k_hw_ani_control(ah,
  2078. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  2079. false);
  2080. if (aniState->firstepLevel > 0)
  2081. ath9k_hw_ani_control(ah,
  2082. ATH9K_ANI_FIRSTEP_LEVEL,
  2083. 0);
  2084. return;
  2085. }
  2086. }
  2087. }
  2088. static void ath9k_hw_ani_cck_err_trigger(struct ath_hal *ah)
  2089. {
  2090. struct ath_hal_5416 *ahp = AH5416(ah);
  2091. struct ath9k_channel *chan = ah->ah_curchan;
  2092. struct ar5416AniState *aniState;
  2093. enum wireless_mode mode;
  2094. int32_t rssi;
  2095. if (!DO_ANI(ah))
  2096. return;
  2097. aniState = ahp->ah_curani;
  2098. if (aniState->noiseImmunityLevel < HAL_NOISE_IMMUNE_MAX) {
  2099. if (ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  2100. aniState->noiseImmunityLevel + 1)) {
  2101. return;
  2102. }
  2103. }
  2104. if (ah->ah_opmode == ATH9K_M_HOSTAP) {
  2105. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX) {
  2106. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2107. aniState->firstepLevel + 1);
  2108. }
  2109. return;
  2110. }
  2111. rssi = BEACON_RSSI(ahp);
  2112. if (rssi > aniState->rssiThrLow) {
  2113. if (aniState->firstepLevel < HAL_FIRST_STEP_MAX)
  2114. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2115. aniState->firstepLevel + 1);
  2116. } else {
  2117. mode = ath9k_hw_chan2wmode(ah, chan);
  2118. if (mode == ATH9K_MODE_11G || mode == ATH9K_MODE_11B) {
  2119. if (aniState->firstepLevel > 0)
  2120. ath9k_hw_ani_control(ah,
  2121. ATH9K_ANI_FIRSTEP_LEVEL,
  2122. 0);
  2123. }
  2124. }
  2125. }
  2126. static void ath9k_ani_reset(struct ath_hal *ah)
  2127. {
  2128. struct ath_hal_5416 *ahp = AH5416(ah);
  2129. struct ar5416AniState *aniState;
  2130. struct ath9k_channel *chan = ah->ah_curchan;
  2131. int index;
  2132. if (!DO_ANI(ah))
  2133. return;
  2134. index = ath9k_hw_get_ani_channel_idx(ah, chan);
  2135. aniState = &ahp->ah_ani[index];
  2136. ahp->ah_curani = aniState;
  2137. if (DO_ANI(ah) && ah->ah_opmode != ATH9K_M_STA
  2138. && ah->ah_opmode != ATH9K_M_IBSS) {
  2139. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2140. "%s: Reset ANI state opmode %u\n", __func__,
  2141. ah->ah_opmode);
  2142. ahp->ah_stats.ast_ani_reset++;
  2143. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL, 0);
  2144. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL, 0);
  2145. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL, 0);
  2146. ath9k_hw_ani_control(ah,
  2147. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  2148. !ATH9K_ANI_USE_OFDM_WEAK_SIG);
  2149. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  2150. ATH9K_ANI_CCK_WEAK_SIG_THR);
  2151. ath9k_hw_setrxfilter(ah,
  2152. ath9k_hw_getrxfilter(ah) |
  2153. ATH9K_RX_FILTER_PHYERR);
  2154. if (ah->ah_opmode == ATH9K_M_HOSTAP) {
  2155. ahp->ah_curani->ofdmTrigHigh =
  2156. ah->ah_config.ofdm_trig_high;
  2157. ahp->ah_curani->ofdmTrigLow =
  2158. ah->ah_config.ofdm_trig_low;
  2159. ahp->ah_curani->cckTrigHigh =
  2160. ah->ah_config.cck_trig_high;
  2161. ahp->ah_curani->cckTrigLow =
  2162. ah->ah_config.cck_trig_low;
  2163. }
  2164. ath9k_ani_restart(ah);
  2165. return;
  2166. }
  2167. if (aniState->noiseImmunityLevel != 0)
  2168. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  2169. aniState->noiseImmunityLevel);
  2170. if (aniState->spurImmunityLevel != 0)
  2171. ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  2172. aniState->spurImmunityLevel);
  2173. if (aniState->ofdmWeakSigDetectOff)
  2174. ath9k_hw_ani_control(ah,
  2175. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  2176. !aniState->ofdmWeakSigDetectOff);
  2177. if (aniState->cckWeakSigThreshold)
  2178. ath9k_hw_ani_control(ah, ATH9K_ANI_CCK_WEAK_SIGNAL_THR,
  2179. aniState->cckWeakSigThreshold);
  2180. if (aniState->firstepLevel != 0)
  2181. ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2182. aniState->firstepLevel);
  2183. if (ahp->ah_hasHwPhyCounters) {
  2184. ath9k_hw_setrxfilter(ah,
  2185. ath9k_hw_getrxfilter(ah) &
  2186. ~ATH9K_RX_FILTER_PHYERR);
  2187. ath9k_ani_restart(ah);
  2188. REG_WRITE(ah, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
  2189. REG_WRITE(ah, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
  2190. } else {
  2191. ath9k_ani_restart(ah);
  2192. ath9k_hw_setrxfilter(ah,
  2193. ath9k_hw_getrxfilter(ah) |
  2194. ATH9K_RX_FILTER_PHYERR);
  2195. }
  2196. }
  2197. /*
  2198. * Process a MIB interrupt. We may potentially be invoked because
  2199. * any of the MIB counters overflow/trigger so don't assume we're
  2200. * here because a PHY error counter triggered.
  2201. */
  2202. void ath9k_hw_procmibevent(struct ath_hal *ah,
  2203. const struct ath9k_node_stats *stats)
  2204. {
  2205. struct ath_hal_5416 *ahp = AH5416(ah);
  2206. u32 phyCnt1, phyCnt2;
  2207. DPRINTF(ah->ah_sc, ATH_DBG_ANI, "Processing Mib Intr\n");
  2208. /* Reset these counters regardless */
  2209. REG_WRITE(ah, AR_FILT_OFDM, 0);
  2210. REG_WRITE(ah, AR_FILT_CCK, 0);
  2211. if (!(REG_READ(ah, AR_SLP_MIB_CTRL) & AR_SLP_MIB_PENDING))
  2212. REG_WRITE(ah, AR_SLP_MIB_CTRL, AR_SLP_MIB_CLEAR);
  2213. /* Clear the mib counters and save them in the stats */
  2214. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  2215. ahp->ah_stats.ast_nodestats = *stats;
  2216. if (!DO_ANI(ah))
  2217. return;
  2218. /* NB: these are not reset-on-read */
  2219. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  2220. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  2221. if (((phyCnt1 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK) ||
  2222. ((phyCnt2 & AR_MIBCNT_INTRMASK) == AR_MIBCNT_INTRMASK)) {
  2223. struct ar5416AniState *aniState = ahp->ah_curani;
  2224. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  2225. /* NB: only use ast_ani_*errs with AH_PRIVATE_DIAG */
  2226. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  2227. ahp->ah_stats.ast_ani_ofdmerrs +=
  2228. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  2229. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  2230. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  2231. ahp->ah_stats.ast_ani_cckerrs +=
  2232. cckPhyErrCnt - aniState->cckPhyErrCount;
  2233. aniState->cckPhyErrCount = cckPhyErrCnt;
  2234. /*
  2235. * NB: figure out which counter triggered. If both
  2236. * trigger we'll only deal with one as the processing
  2237. * clobbers the error counter so the trigger threshold
  2238. * check will never be true.
  2239. */
  2240. if (aniState->ofdmPhyErrCount > aniState->ofdmTrigHigh)
  2241. ath9k_hw_ani_ofdm_err_trigger(ah);
  2242. if (aniState->cckPhyErrCount > aniState->cckTrigHigh)
  2243. ath9k_hw_ani_cck_err_trigger(ah);
  2244. /* NB: always restart to insure the h/w counters are reset */
  2245. ath9k_ani_restart(ah);
  2246. }
  2247. }
  2248. static void ath9k_hw_ani_lower_immunity(struct ath_hal *ah)
  2249. {
  2250. struct ath_hal_5416 *ahp = AH5416(ah);
  2251. struct ar5416AniState *aniState;
  2252. int32_t rssi;
  2253. aniState = ahp->ah_curani;
  2254. if (ah->ah_opmode == ATH9K_M_HOSTAP) {
  2255. if (aniState->firstepLevel > 0) {
  2256. if (ath9k_hw_ani_control(ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2257. aniState->firstepLevel - 1)) {
  2258. return;
  2259. }
  2260. }
  2261. } else {
  2262. rssi = BEACON_RSSI(ahp);
  2263. if (rssi > aniState->rssiThrHigh) {
  2264. /* XXX: Handle me */
  2265. } else if (rssi > aniState->rssiThrLow) {
  2266. if (aniState->ofdmWeakSigDetectOff) {
  2267. if (ath9k_hw_ani_control(ah,
  2268. ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION,
  2269. true) ==
  2270. true) {
  2271. return;
  2272. }
  2273. }
  2274. if (aniState->firstepLevel > 0) {
  2275. if (ath9k_hw_ani_control
  2276. (ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2277. aniState->firstepLevel - 1) ==
  2278. true) {
  2279. return;
  2280. }
  2281. }
  2282. } else {
  2283. if (aniState->firstepLevel > 0) {
  2284. if (ath9k_hw_ani_control
  2285. (ah, ATH9K_ANI_FIRSTEP_LEVEL,
  2286. aniState->firstepLevel - 1) ==
  2287. true) {
  2288. return;
  2289. }
  2290. }
  2291. }
  2292. }
  2293. if (aniState->spurImmunityLevel > 0) {
  2294. if (ath9k_hw_ani_control(ah, ATH9K_ANI_SPUR_IMMUNITY_LEVEL,
  2295. aniState->spurImmunityLevel - 1)) {
  2296. return;
  2297. }
  2298. }
  2299. if (aniState->noiseImmunityLevel > 0) {
  2300. ath9k_hw_ani_control(ah, ATH9K_ANI_NOISE_IMMUNITY_LEVEL,
  2301. aniState->noiseImmunityLevel - 1);
  2302. return;
  2303. }
  2304. }
  2305. static int32_t ath9k_hw_ani_get_listen_time(struct ath_hal *ah)
  2306. {
  2307. struct ath_hal_5416 *ahp = AH5416(ah);
  2308. struct ar5416AniState *aniState;
  2309. u32 txFrameCount, rxFrameCount, cycleCount;
  2310. int32_t listenTime;
  2311. txFrameCount = REG_READ(ah, AR_TFCNT);
  2312. rxFrameCount = REG_READ(ah, AR_RFCNT);
  2313. cycleCount = REG_READ(ah, AR_CCCNT);
  2314. aniState = ahp->ah_curani;
  2315. if (aniState->cycleCount == 0 || aniState->cycleCount > cycleCount) {
  2316. listenTime = 0;
  2317. ahp->ah_stats.ast_ani_lzero++;
  2318. } else {
  2319. int32_t ccdelta = cycleCount - aniState->cycleCount;
  2320. int32_t rfdelta = rxFrameCount - aniState->rxFrameCount;
  2321. int32_t tfdelta = txFrameCount - aniState->txFrameCount;
  2322. listenTime = (ccdelta - rfdelta - tfdelta) / 44000;
  2323. }
  2324. aniState->cycleCount = cycleCount;
  2325. aniState->txFrameCount = txFrameCount;
  2326. aniState->rxFrameCount = rxFrameCount;
  2327. return listenTime;
  2328. }
  2329. void ath9k_hw_ani_monitor(struct ath_hal *ah,
  2330. const struct ath9k_node_stats *stats,
  2331. struct ath9k_channel *chan)
  2332. {
  2333. struct ath_hal_5416 *ahp = AH5416(ah);
  2334. struct ar5416AniState *aniState;
  2335. int32_t listenTime;
  2336. aniState = ahp->ah_curani;
  2337. ahp->ah_stats.ast_nodestats = *stats;
  2338. listenTime = ath9k_hw_ani_get_listen_time(ah);
  2339. if (listenTime < 0) {
  2340. ahp->ah_stats.ast_ani_lneg++;
  2341. ath9k_ani_restart(ah);
  2342. return;
  2343. }
  2344. aniState->listenTime += listenTime;
  2345. if (ahp->ah_hasHwPhyCounters) {
  2346. u32 phyCnt1, phyCnt2;
  2347. u32 ofdmPhyErrCnt, cckPhyErrCnt;
  2348. ath9k_hw_update_mibstats(ah, &ahp->ah_mibStats);
  2349. phyCnt1 = REG_READ(ah, AR_PHY_ERR_1);
  2350. phyCnt2 = REG_READ(ah, AR_PHY_ERR_2);
  2351. if (phyCnt1 < aniState->ofdmPhyErrBase ||
  2352. phyCnt2 < aniState->cckPhyErrBase) {
  2353. if (phyCnt1 < aniState->ofdmPhyErrBase) {
  2354. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2355. "%s: phyCnt1 0x%x, resetting "
  2356. "counter value to 0x%x\n",
  2357. __func__, phyCnt1,
  2358. aniState->ofdmPhyErrBase);
  2359. REG_WRITE(ah, AR_PHY_ERR_1,
  2360. aniState->ofdmPhyErrBase);
  2361. REG_WRITE(ah, AR_PHY_ERR_MASK_1,
  2362. AR_PHY_ERR_OFDM_TIMING);
  2363. }
  2364. if (phyCnt2 < aniState->cckPhyErrBase) {
  2365. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2366. "%s: phyCnt2 0x%x, resetting "
  2367. "counter value to 0x%x\n",
  2368. __func__, phyCnt2,
  2369. aniState->cckPhyErrBase);
  2370. REG_WRITE(ah, AR_PHY_ERR_2,
  2371. aniState->cckPhyErrBase);
  2372. REG_WRITE(ah, AR_PHY_ERR_MASK_2,
  2373. AR_PHY_ERR_CCK_TIMING);
  2374. }
  2375. return;
  2376. }
  2377. ofdmPhyErrCnt = phyCnt1 - aniState->ofdmPhyErrBase;
  2378. ahp->ah_stats.ast_ani_ofdmerrs +=
  2379. ofdmPhyErrCnt - aniState->ofdmPhyErrCount;
  2380. aniState->ofdmPhyErrCount = ofdmPhyErrCnt;
  2381. cckPhyErrCnt = phyCnt2 - aniState->cckPhyErrBase;
  2382. ahp->ah_stats.ast_ani_cckerrs +=
  2383. cckPhyErrCnt - aniState->cckPhyErrCount;
  2384. aniState->cckPhyErrCount = cckPhyErrCnt;
  2385. }
  2386. if (!DO_ANI(ah))
  2387. return;
  2388. if (aniState->listenTime > 5 * ahp->ah_aniPeriod) {
  2389. if (aniState->ofdmPhyErrCount <= aniState->listenTime *
  2390. aniState->ofdmTrigLow / 1000 &&
  2391. aniState->cckPhyErrCount <= aniState->listenTime *
  2392. aniState->cckTrigLow / 1000)
  2393. ath9k_hw_ani_lower_immunity(ah);
  2394. ath9k_ani_restart(ah);
  2395. } else if (aniState->listenTime > ahp->ah_aniPeriod) {
  2396. if (aniState->ofdmPhyErrCount > aniState->listenTime *
  2397. aniState->ofdmTrigHigh / 1000) {
  2398. ath9k_hw_ani_ofdm_err_trigger(ah);
  2399. ath9k_ani_restart(ah);
  2400. } else if (aniState->cckPhyErrCount >
  2401. aniState->listenTime * aniState->cckTrigHigh /
  2402. 1000) {
  2403. ath9k_hw_ani_cck_err_trigger(ah);
  2404. ath9k_ani_restart(ah);
  2405. }
  2406. }
  2407. }
  2408. #ifndef ATH_NF_PER_CHAN
  2409. static void ath9k_init_nfcal_hist_buffer(struct ath_hal *ah)
  2410. {
  2411. int i, j;
  2412. for (i = 0; i < NUM_NF_READINGS; i++) {
  2413. ah->nfCalHist[i].currIndex = 0;
  2414. ah->nfCalHist[i].privNF = AR_PHY_CCA_MAX_GOOD_VALUE;
  2415. ah->nfCalHist[i].invalidNFcount =
  2416. AR_PHY_CCA_FILTERWINDOW_LENGTH;
  2417. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  2418. ah->nfCalHist[i].nfCalBuffer[j] =
  2419. AR_PHY_CCA_MAX_GOOD_VALUE;
  2420. }
  2421. }
  2422. return;
  2423. }
  2424. #endif
  2425. static void ath9k_hw_gpio_cfg_output_mux(struct ath_hal *ah,
  2426. u32 gpio, u32 type)
  2427. {
  2428. int addr;
  2429. u32 gpio_shift, tmp;
  2430. if (gpio > 11)
  2431. addr = AR_GPIO_OUTPUT_MUX3;
  2432. else if (gpio > 5)
  2433. addr = AR_GPIO_OUTPUT_MUX2;
  2434. else
  2435. addr = AR_GPIO_OUTPUT_MUX1;
  2436. gpio_shift = (gpio % 6) * 5;
  2437. if (AR_SREV_9280_20_OR_LATER(ah)
  2438. || (addr != AR_GPIO_OUTPUT_MUX1)) {
  2439. REG_RMW(ah, addr, (type << gpio_shift),
  2440. (0x1f << gpio_shift));
  2441. } else {
  2442. tmp = REG_READ(ah, addr);
  2443. tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
  2444. tmp &= ~(0x1f << gpio_shift);
  2445. tmp |= (type << gpio_shift);
  2446. REG_WRITE(ah, addr, tmp);
  2447. }
  2448. }
  2449. void ath9k_hw_cfg_output(struct ath_hal *ah, u32 gpio,
  2450. u32 ah_signal_type)
  2451. {
  2452. u32 gpio_shift;
  2453. ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
  2454. gpio_shift = 2 * gpio;
  2455. REG_RMW(ah,
  2456. AR_GPIO_OE_OUT,
  2457. (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
  2458. (AR_GPIO_OE_OUT_DRV << gpio_shift));
  2459. }
  2460. void ath9k_hw_set_gpio(struct ath_hal *ah, u32 gpio, u32 val)
  2461. {
  2462. REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
  2463. AR_GPIO_BIT(gpio));
  2464. }
  2465. /*
  2466. * Configure GPIO Input lines
  2467. */
  2468. void ath9k_hw_cfg_gpio_input(struct ath_hal *ah, u32 gpio)
  2469. {
  2470. u32 gpio_shift;
  2471. ASSERT(gpio < ah->ah_caps.num_gpio_pins);
  2472. gpio_shift = gpio << 1;
  2473. REG_RMW(ah,
  2474. AR_GPIO_OE_OUT,
  2475. (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
  2476. (AR_GPIO_OE_OUT_DRV << gpio_shift));
  2477. }
  2478. #ifdef CONFIG_RFKILL
  2479. static void ath9k_enable_rfkill(struct ath_hal *ah)
  2480. {
  2481. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
  2482. AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
  2483. REG_CLR_BIT(ah, AR_GPIO_INPUT_MUX2,
  2484. AR_GPIO_INPUT_MUX2_RFSILENT);
  2485. ath9k_hw_cfg_gpio_input(ah, ah->ah_rfkill_gpio);
  2486. REG_SET_BIT(ah, AR_PHY_TEST, RFSILENT_BB);
  2487. }
  2488. #endif
  2489. u32 ath9k_hw_gpio_get(struct ath_hal *ah, u32 gpio)
  2490. {
  2491. if (gpio >= ah->ah_caps.num_gpio_pins)
  2492. return 0xffffffff;
  2493. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2494. return (MS
  2495. (REG_READ(ah, AR_GPIO_IN_OUT),
  2496. AR928X_GPIO_IN_VAL) & AR_GPIO_BIT(gpio)) != 0;
  2497. } else {
  2498. return (MS(REG_READ(ah, AR_GPIO_IN_OUT), AR_GPIO_IN_VAL) &
  2499. AR_GPIO_BIT(gpio)) != 0;
  2500. }
  2501. }
  2502. static int ath9k_hw_post_attach(struct ath_hal *ah)
  2503. {
  2504. int ecode;
  2505. if (!ath9k_hw_chip_test(ah)) {
  2506. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  2507. "%s: hardware self-test failed\n", __func__);
  2508. return -ENODEV;
  2509. }
  2510. ecode = ath9k_hw_rf_claim(ah);
  2511. if (ecode != 0)
  2512. return ecode;
  2513. ecode = ath9k_hw_eeprom_attach(ah);
  2514. if (ecode != 0)
  2515. return ecode;
  2516. ecode = ath9k_hw_rfattach(ah);
  2517. if (ecode != 0)
  2518. return ecode;
  2519. if (!AR_SREV_9100(ah)) {
  2520. ath9k_hw_ani_setup(ah);
  2521. ath9k_hw_ani_attach(ah);
  2522. }
  2523. return 0;
  2524. }
  2525. static u32 ath9k_hw_ini_fixup(struct ath_hal *ah,
  2526. struct ar5416_eeprom *pEepData,
  2527. u32 reg, u32 value)
  2528. {
  2529. struct base_eep_header *pBase = &(pEepData->baseEepHeader);
  2530. switch (ah->ah_devid) {
  2531. case AR9280_DEVID_PCI:
  2532. if (reg == 0x7894) {
  2533. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  2534. "ini VAL: %x EEPROM: %x\n", value,
  2535. (pBase->version & 0xff));
  2536. if ((pBase->version & 0xff) > 0x0a) {
  2537. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  2538. "PWDCLKIND: %d\n",
  2539. pBase->pwdclkind);
  2540. value &= ~AR_AN_TOP2_PWDCLKIND;
  2541. value |= AR_AN_TOP2_PWDCLKIND & (pBase->
  2542. pwdclkind << AR_AN_TOP2_PWDCLKIND_S);
  2543. } else {
  2544. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  2545. "PWDCLKIND Earlier Rev\n");
  2546. }
  2547. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  2548. "final ini VAL: %x\n", value);
  2549. }
  2550. break;
  2551. }
  2552. return value;
  2553. }
  2554. static bool ath9k_hw_fill_cap_info(struct ath_hal *ah)
  2555. {
  2556. struct ath_hal_5416 *ahp = AH5416(ah);
  2557. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  2558. u16 capField = 0, eeval;
  2559. eeval = ath9k_hw_get_eeprom(ahp, EEP_REG_0);
  2560. ah->ah_currentRD = eeval;
  2561. eeval = ath9k_hw_get_eeprom(ahp, EEP_REG_1);
  2562. ah->ah_currentRDExt = eeval;
  2563. capField = ath9k_hw_get_eeprom(ahp, EEP_OP_CAP);
  2564. if (ah->ah_opmode != ATH9K_M_HOSTAP &&
  2565. ah->ah_subvendorid == AR_SUBVENDOR_ID_NEW_A) {
  2566. if (ah->ah_currentRD == 0x64 || ah->ah_currentRD == 0x65)
  2567. ah->ah_currentRD += 5;
  2568. else if (ah->ah_currentRD == 0x41)
  2569. ah->ah_currentRD = 0x43;
  2570. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  2571. "%s: regdomain mapped to 0x%x\n", __func__,
  2572. ah->ah_currentRD);
  2573. }
  2574. eeval = ath9k_hw_get_eeprom(ahp, EEP_OP_MODE);
  2575. bitmap_zero(pCap->wireless_modes, ATH9K_MODE_MAX);
  2576. if (eeval & AR5416_OPFLAGS_11A) {
  2577. set_bit(ATH9K_MODE_11A, pCap->wireless_modes);
  2578. if (ah->ah_config.ht_enable) {
  2579. if (!(eeval & AR5416_OPFLAGS_N_5G_HT20))
  2580. set_bit(ATH9K_MODE_11NA_HT20,
  2581. pCap->wireless_modes);
  2582. if (!(eeval & AR5416_OPFLAGS_N_5G_HT40)) {
  2583. set_bit(ATH9K_MODE_11NA_HT40PLUS,
  2584. pCap->wireless_modes);
  2585. set_bit(ATH9K_MODE_11NA_HT40MINUS,
  2586. pCap->wireless_modes);
  2587. }
  2588. }
  2589. }
  2590. if (eeval & AR5416_OPFLAGS_11G) {
  2591. set_bit(ATH9K_MODE_11B, pCap->wireless_modes);
  2592. set_bit(ATH9K_MODE_11G, pCap->wireless_modes);
  2593. if (ah->ah_config.ht_enable) {
  2594. if (!(eeval & AR5416_OPFLAGS_N_2G_HT20))
  2595. set_bit(ATH9K_MODE_11NG_HT20,
  2596. pCap->wireless_modes);
  2597. if (!(eeval & AR5416_OPFLAGS_N_2G_HT40)) {
  2598. set_bit(ATH9K_MODE_11NG_HT40PLUS,
  2599. pCap->wireless_modes);
  2600. set_bit(ATH9K_MODE_11NG_HT40MINUS,
  2601. pCap->wireless_modes);
  2602. }
  2603. }
  2604. }
  2605. pCap->tx_chainmask = ath9k_hw_get_eeprom(ahp, EEP_TX_MASK);
  2606. if ((ah->ah_isPciExpress)
  2607. || (eeval & AR5416_OPFLAGS_11A)) {
  2608. pCap->rx_chainmask =
  2609. ath9k_hw_get_eeprom(ahp, EEP_RX_MASK);
  2610. } else {
  2611. pCap->rx_chainmask =
  2612. (ath9k_hw_gpio_get(ah, 0)) ? 0x5 : 0x7;
  2613. }
  2614. if (!(AR_SREV_9280(ah) && (ah->ah_macRev == 0)))
  2615. ahp->ah_miscMode |= AR_PCU_MIC_NEW_LOC_ENA;
  2616. pCap->low_2ghz_chan = 2312;
  2617. pCap->high_2ghz_chan = 2732;
  2618. pCap->low_5ghz_chan = 4920;
  2619. pCap->high_5ghz_chan = 6100;
  2620. pCap->hw_caps &= ~ATH9K_HW_CAP_CIPHER_CKIP;
  2621. pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_TKIP;
  2622. pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_AESCCM;
  2623. pCap->hw_caps &= ~ATH9K_HW_CAP_MIC_CKIP;
  2624. pCap->hw_caps |= ATH9K_HW_CAP_MIC_TKIP;
  2625. pCap->hw_caps |= ATH9K_HW_CAP_MIC_AESCCM;
  2626. pCap->hw_caps |= ATH9K_HW_CAP_CHAN_SPREAD;
  2627. if (ah->ah_config.ht_enable)
  2628. pCap->hw_caps |= ATH9K_HW_CAP_HT;
  2629. else
  2630. pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
  2631. pCap->hw_caps |= ATH9K_HW_CAP_GTT;
  2632. pCap->hw_caps |= ATH9K_HW_CAP_VEOL;
  2633. pCap->hw_caps |= ATH9K_HW_CAP_BSSIDMASK;
  2634. pCap->hw_caps &= ~ATH9K_HW_CAP_MCAST_KEYSEARCH;
  2635. if (capField & AR_EEPROM_EEPCAP_MAXQCU)
  2636. pCap->total_queues =
  2637. MS(capField, AR_EEPROM_EEPCAP_MAXQCU);
  2638. else
  2639. pCap->total_queues = ATH9K_NUM_TX_QUEUES;
  2640. if (capField & AR_EEPROM_EEPCAP_KC_ENTRIES)
  2641. pCap->keycache_size =
  2642. 1 << MS(capField, AR_EEPROM_EEPCAP_KC_ENTRIES);
  2643. else
  2644. pCap->keycache_size = AR_KEYTABLE_SIZE;
  2645. pCap->hw_caps |= ATH9K_HW_CAP_FASTCC;
  2646. pCap->num_mr_retries = 4;
  2647. pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD;
  2648. if (AR_SREV_9280_10_OR_LATER(ah))
  2649. pCap->num_gpio_pins = AR928X_NUM_GPIO;
  2650. else
  2651. pCap->num_gpio_pins = AR_NUM_GPIO;
  2652. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2653. pCap->hw_caps |= ATH9K_HW_CAP_WOW;
  2654. pCap->hw_caps |= ATH9K_HW_CAP_WOW_MATCHPATTERN_EXACT;
  2655. } else {
  2656. pCap->hw_caps &= ~ATH9K_HW_CAP_WOW;
  2657. pCap->hw_caps &= ~ATH9K_HW_CAP_WOW_MATCHPATTERN_EXACT;
  2658. }
  2659. if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
  2660. pCap->hw_caps |= ATH9K_HW_CAP_CST;
  2661. pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
  2662. } else {
  2663. pCap->rts_aggr_limit = (8 * 1024);
  2664. }
  2665. pCap->hw_caps |= ATH9K_HW_CAP_ENHANCEDPM;
  2666. #ifdef CONFIG_RFKILL
  2667. ah->ah_rfsilent = ath9k_hw_get_eeprom(ahp, EEP_RF_SILENT);
  2668. if (ah->ah_rfsilent & EEP_RFSILENT_ENABLED) {
  2669. ah->ah_rfkill_gpio =
  2670. MS(ah->ah_rfsilent, EEP_RFSILENT_GPIO_SEL);
  2671. ah->ah_rfkill_polarity =
  2672. MS(ah->ah_rfsilent, EEP_RFSILENT_POLARITY);
  2673. pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
  2674. }
  2675. #endif
  2676. if ((ah->ah_macVersion == AR_SREV_VERSION_5416_PCI) ||
  2677. (ah->ah_macVersion == AR_SREV_VERSION_5416_PCIE) ||
  2678. (ah->ah_macVersion == AR_SREV_VERSION_9160) ||
  2679. (ah->ah_macVersion == AR_SREV_VERSION_9100) ||
  2680. (ah->ah_macVersion == AR_SREV_VERSION_9280))
  2681. pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
  2682. else
  2683. pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
  2684. if (AR_SREV_9280(ah))
  2685. pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
  2686. else
  2687. pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
  2688. if (ah->ah_currentRDExt & (1 << REG_EXT_JAPAN_MIDBAND)) {
  2689. pCap->reg_cap =
  2690. AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
  2691. AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
  2692. AR_EEPROM_EEREGCAP_EN_KK_U2 |
  2693. AR_EEPROM_EEREGCAP_EN_KK_MIDBAND;
  2694. } else {
  2695. pCap->reg_cap =
  2696. AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
  2697. AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN;
  2698. }
  2699. pCap->reg_cap |= AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND;
  2700. pCap->num_antcfg_5ghz =
  2701. ath9k_hw_get_num_ant_config(ahp, IEEE80211_BAND_5GHZ);
  2702. pCap->num_antcfg_2ghz =
  2703. ath9k_hw_get_num_ant_config(ahp, IEEE80211_BAND_2GHZ);
  2704. return true;
  2705. }
  2706. static void ar5416DisablePciePhy(struct ath_hal *ah)
  2707. {
  2708. if (!AR_SREV_9100(ah))
  2709. return;
  2710. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  2711. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  2712. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
  2713. REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
  2714. REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
  2715. REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
  2716. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  2717. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  2718. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
  2719. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  2720. }
  2721. static void ath9k_set_power_sleep(struct ath_hal *ah, int setChip)
  2722. {
  2723. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  2724. if (setChip) {
  2725. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  2726. AR_RTC_FORCE_WAKE_EN);
  2727. if (!AR_SREV_9100(ah))
  2728. REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
  2729. REG_CLR_BIT(ah, (u16) (AR_RTC_RESET),
  2730. AR_RTC_RESET_EN);
  2731. }
  2732. }
  2733. static void ath9k_set_power_network_sleep(struct ath_hal *ah, int setChip)
  2734. {
  2735. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  2736. if (setChip) {
  2737. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  2738. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  2739. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  2740. AR_RTC_FORCE_WAKE_ON_INT);
  2741. } else {
  2742. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  2743. AR_RTC_FORCE_WAKE_EN);
  2744. }
  2745. }
  2746. }
  2747. static bool ath9k_hw_set_power_awake(struct ath_hal *ah,
  2748. int setChip)
  2749. {
  2750. u32 val;
  2751. int i;
  2752. if (setChip) {
  2753. if ((REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M) ==
  2754. AR_RTC_STATUS_SHUTDOWN) {
  2755. if (ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)
  2756. != true) {
  2757. return false;
  2758. }
  2759. }
  2760. if (AR_SREV_9100(ah))
  2761. REG_SET_BIT(ah, AR_RTC_RESET,
  2762. AR_RTC_RESET_EN);
  2763. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  2764. AR_RTC_FORCE_WAKE_EN);
  2765. udelay(50);
  2766. for (i = POWER_UP_TIME / 50; i > 0; i--) {
  2767. val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
  2768. if (val == AR_RTC_STATUS_ON)
  2769. break;
  2770. udelay(50);
  2771. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  2772. AR_RTC_FORCE_WAKE_EN);
  2773. }
  2774. if (i == 0) {
  2775. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  2776. "%s: Failed to wakeup in %uus\n",
  2777. __func__, POWER_UP_TIME / 20);
  2778. return false;
  2779. }
  2780. }
  2781. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  2782. return true;
  2783. }
  2784. bool ath9k_hw_setpower(struct ath_hal *ah,
  2785. enum ath9k_power_mode mode)
  2786. {
  2787. struct ath_hal_5416 *ahp = AH5416(ah);
  2788. static const char *modes[] = {
  2789. "AWAKE",
  2790. "FULL-SLEEP",
  2791. "NETWORK SLEEP",
  2792. "UNDEFINED"
  2793. };
  2794. int status = true, setChip = true;
  2795. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT, "%s: %s -> %s (%s)\n", __func__,
  2796. modes[ahp->ah_powerMode], modes[mode],
  2797. setChip ? "set chip " : "");
  2798. switch (mode) {
  2799. case ATH9K_PM_AWAKE:
  2800. status = ath9k_hw_set_power_awake(ah, setChip);
  2801. break;
  2802. case ATH9K_PM_FULL_SLEEP:
  2803. ath9k_set_power_sleep(ah, setChip);
  2804. ahp->ah_chipFullSleep = true;
  2805. break;
  2806. case ATH9K_PM_NETWORK_SLEEP:
  2807. ath9k_set_power_network_sleep(ah, setChip);
  2808. break;
  2809. default:
  2810. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  2811. "%s: unknown power mode %u\n", __func__, mode);
  2812. return false;
  2813. }
  2814. ahp->ah_powerMode = mode;
  2815. return status;
  2816. }
  2817. static struct ath_hal *ath9k_hw_do_attach(u16 devid,
  2818. struct ath_softc *sc,
  2819. void __iomem *mem,
  2820. int *status)
  2821. {
  2822. struct ath_hal_5416 *ahp;
  2823. struct ath_hal *ah;
  2824. int ecode;
  2825. #ifndef CONFIG_SLOW_ANT_DIV
  2826. u32 i;
  2827. u32 j;
  2828. #endif
  2829. ahp = ath9k_hw_newstate(devid, sc, mem, status);
  2830. if (ahp == NULL)
  2831. return NULL;
  2832. ah = &ahp->ah;
  2833. ath9k_hw_set_defaults(ah);
  2834. if (ah->ah_config.intr_mitigation != 0)
  2835. ahp->ah_intrMitigation = true;
  2836. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
  2837. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: couldn't reset chip\n",
  2838. __func__);
  2839. ecode = -EIO;
  2840. goto bad;
  2841. }
  2842. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
  2843. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: couldn't wakeup chip\n",
  2844. __func__);
  2845. ecode = -EIO;
  2846. goto bad;
  2847. }
  2848. if (ah->ah_config.serialize_regmode == SER_REG_MODE_AUTO) {
  2849. if (ah->ah_macVersion == AR_SREV_VERSION_5416_PCI) {
  2850. ah->ah_config.serialize_regmode =
  2851. SER_REG_MODE_ON;
  2852. } else {
  2853. ah->ah_config.serialize_regmode =
  2854. SER_REG_MODE_OFF;
  2855. }
  2856. }
  2857. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  2858. "%s: serialize_regmode is %d\n",
  2859. __func__, ah->ah_config.serialize_regmode);
  2860. if ((ah->ah_macVersion != AR_SREV_VERSION_5416_PCI) &&
  2861. (ah->ah_macVersion != AR_SREV_VERSION_5416_PCIE) &&
  2862. (ah->ah_macVersion != AR_SREV_VERSION_9160) &&
  2863. (!AR_SREV_9100(ah)) && (!AR_SREV_9280(ah))) {
  2864. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  2865. "%s: Mac Chip Rev 0x%02x.%x is not supported by "
  2866. "this driver\n", __func__,
  2867. ah->ah_macVersion, ah->ah_macRev);
  2868. ecode = -EOPNOTSUPP;
  2869. goto bad;
  2870. }
  2871. if (AR_SREV_9100(ah)) {
  2872. ahp->ah_iqCalData.calData = &iq_cal_multi_sample;
  2873. ahp->ah_suppCals = IQ_MISMATCH_CAL;
  2874. ah->ah_isPciExpress = false;
  2875. }
  2876. ah->ah_phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
  2877. if (AR_SREV_9160_10_OR_LATER(ah)) {
  2878. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2879. ahp->ah_iqCalData.calData = &iq_cal_single_sample;
  2880. ahp->ah_adcGainCalData.calData =
  2881. &adc_gain_cal_single_sample;
  2882. ahp->ah_adcDcCalData.calData =
  2883. &adc_dc_cal_single_sample;
  2884. ahp->ah_adcDcCalInitData.calData =
  2885. &adc_init_dc_cal;
  2886. } else {
  2887. ahp->ah_iqCalData.calData = &iq_cal_multi_sample;
  2888. ahp->ah_adcGainCalData.calData =
  2889. &adc_gain_cal_multi_sample;
  2890. ahp->ah_adcDcCalData.calData =
  2891. &adc_dc_cal_multi_sample;
  2892. ahp->ah_adcDcCalInitData.calData =
  2893. &adc_init_dc_cal;
  2894. }
  2895. ahp->ah_suppCals =
  2896. ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
  2897. }
  2898. if (AR_SREV_9160(ah)) {
  2899. ah->ah_config.enable_ani = 1;
  2900. ahp->ah_ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  2901. ATH9K_ANI_FIRSTEP_LEVEL);
  2902. } else {
  2903. ahp->ah_ani_function = ATH9K_ANI_ALL;
  2904. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2905. ahp->ah_ani_function &=
  2906. ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
  2907. }
  2908. }
  2909. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  2910. "%s: This Mac Chip Rev 0x%02x.%x is \n", __func__,
  2911. ah->ah_macVersion, ah->ah_macRev);
  2912. if (AR_SREV_9280_20_OR_LATER(ah)) {
  2913. INIT_INI_ARRAY(&ahp->ah_iniModes, ar9280Modes_9280_2,
  2914. ARRAY_SIZE(ar9280Modes_9280_2), 6);
  2915. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar9280Common_9280_2,
  2916. ARRAY_SIZE(ar9280Common_9280_2), 2);
  2917. if (ah->ah_config.pcie_clock_req) {
  2918. INIT_INI_ARRAY(&ahp->ah_iniPcieSerdes,
  2919. ar9280PciePhy_clkreq_off_L1_9280,
  2920. ARRAY_SIZE
  2921. (ar9280PciePhy_clkreq_off_L1_9280),
  2922. 2);
  2923. } else {
  2924. INIT_INI_ARRAY(&ahp->ah_iniPcieSerdes,
  2925. ar9280PciePhy_clkreq_always_on_L1_9280,
  2926. ARRAY_SIZE
  2927. (ar9280PciePhy_clkreq_always_on_L1_9280),
  2928. 2);
  2929. }
  2930. INIT_INI_ARRAY(&ahp->ah_iniModesAdditional,
  2931. ar9280Modes_fast_clock_9280_2,
  2932. ARRAY_SIZE(ar9280Modes_fast_clock_9280_2),
  2933. 3);
  2934. } else if (AR_SREV_9280_10_OR_LATER(ah)) {
  2935. INIT_INI_ARRAY(&ahp->ah_iniModes, ar9280Modes_9280,
  2936. ARRAY_SIZE(ar9280Modes_9280), 6);
  2937. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar9280Common_9280,
  2938. ARRAY_SIZE(ar9280Common_9280), 2);
  2939. } else if (AR_SREV_9160_10_OR_LATER(ah)) {
  2940. INIT_INI_ARRAY(&ahp->ah_iniModes, ar5416Modes_9160,
  2941. ARRAY_SIZE(ar5416Modes_9160), 6);
  2942. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar5416Common_9160,
  2943. ARRAY_SIZE(ar5416Common_9160), 2);
  2944. INIT_INI_ARRAY(&ahp->ah_iniBank0, ar5416Bank0_9160,
  2945. ARRAY_SIZE(ar5416Bank0_9160), 2);
  2946. INIT_INI_ARRAY(&ahp->ah_iniBB_RfGain, ar5416BB_RfGain_9160,
  2947. ARRAY_SIZE(ar5416BB_RfGain_9160), 3);
  2948. INIT_INI_ARRAY(&ahp->ah_iniBank1, ar5416Bank1_9160,
  2949. ARRAY_SIZE(ar5416Bank1_9160), 2);
  2950. INIT_INI_ARRAY(&ahp->ah_iniBank2, ar5416Bank2_9160,
  2951. ARRAY_SIZE(ar5416Bank2_9160), 2);
  2952. INIT_INI_ARRAY(&ahp->ah_iniBank3, ar5416Bank3_9160,
  2953. ARRAY_SIZE(ar5416Bank3_9160), 3);
  2954. INIT_INI_ARRAY(&ahp->ah_iniBank6, ar5416Bank6_9160,
  2955. ARRAY_SIZE(ar5416Bank6_9160), 3);
  2956. INIT_INI_ARRAY(&ahp->ah_iniBank6TPC, ar5416Bank6TPC_9160,
  2957. ARRAY_SIZE(ar5416Bank6TPC_9160), 3);
  2958. INIT_INI_ARRAY(&ahp->ah_iniBank7, ar5416Bank7_9160,
  2959. ARRAY_SIZE(ar5416Bank7_9160), 2);
  2960. if (AR_SREV_9160_11(ah)) {
  2961. INIT_INI_ARRAY(&ahp->ah_iniAddac,
  2962. ar5416Addac_91601_1,
  2963. ARRAY_SIZE(ar5416Addac_91601_1), 2);
  2964. } else {
  2965. INIT_INI_ARRAY(&ahp->ah_iniAddac, ar5416Addac_9160,
  2966. ARRAY_SIZE(ar5416Addac_9160), 2);
  2967. }
  2968. } else if (AR_SREV_9100_OR_LATER(ah)) {
  2969. INIT_INI_ARRAY(&ahp->ah_iniModes, ar5416Modes_9100,
  2970. ARRAY_SIZE(ar5416Modes_9100), 6);
  2971. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar5416Common_9100,
  2972. ARRAY_SIZE(ar5416Common_9100), 2);
  2973. INIT_INI_ARRAY(&ahp->ah_iniBank0, ar5416Bank0_9100,
  2974. ARRAY_SIZE(ar5416Bank0_9100), 2);
  2975. INIT_INI_ARRAY(&ahp->ah_iniBB_RfGain, ar5416BB_RfGain_9100,
  2976. ARRAY_SIZE(ar5416BB_RfGain_9100), 3);
  2977. INIT_INI_ARRAY(&ahp->ah_iniBank1, ar5416Bank1_9100,
  2978. ARRAY_SIZE(ar5416Bank1_9100), 2);
  2979. INIT_INI_ARRAY(&ahp->ah_iniBank2, ar5416Bank2_9100,
  2980. ARRAY_SIZE(ar5416Bank2_9100), 2);
  2981. INIT_INI_ARRAY(&ahp->ah_iniBank3, ar5416Bank3_9100,
  2982. ARRAY_SIZE(ar5416Bank3_9100), 3);
  2983. INIT_INI_ARRAY(&ahp->ah_iniBank6, ar5416Bank6_9100,
  2984. ARRAY_SIZE(ar5416Bank6_9100), 3);
  2985. INIT_INI_ARRAY(&ahp->ah_iniBank6TPC, ar5416Bank6TPC_9100,
  2986. ARRAY_SIZE(ar5416Bank6TPC_9100), 3);
  2987. INIT_INI_ARRAY(&ahp->ah_iniBank7, ar5416Bank7_9100,
  2988. ARRAY_SIZE(ar5416Bank7_9100), 2);
  2989. INIT_INI_ARRAY(&ahp->ah_iniAddac, ar5416Addac_9100,
  2990. ARRAY_SIZE(ar5416Addac_9100), 2);
  2991. } else {
  2992. INIT_INI_ARRAY(&ahp->ah_iniModes, ar5416Modes,
  2993. ARRAY_SIZE(ar5416Modes), 6);
  2994. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar5416Common,
  2995. ARRAY_SIZE(ar5416Common), 2);
  2996. INIT_INI_ARRAY(&ahp->ah_iniBank0, ar5416Bank0,
  2997. ARRAY_SIZE(ar5416Bank0), 2);
  2998. INIT_INI_ARRAY(&ahp->ah_iniBB_RfGain, ar5416BB_RfGain,
  2999. ARRAY_SIZE(ar5416BB_RfGain), 3);
  3000. INIT_INI_ARRAY(&ahp->ah_iniBank1, ar5416Bank1,
  3001. ARRAY_SIZE(ar5416Bank1), 2);
  3002. INIT_INI_ARRAY(&ahp->ah_iniBank2, ar5416Bank2,
  3003. ARRAY_SIZE(ar5416Bank2), 2);
  3004. INIT_INI_ARRAY(&ahp->ah_iniBank3, ar5416Bank3,
  3005. ARRAY_SIZE(ar5416Bank3), 3);
  3006. INIT_INI_ARRAY(&ahp->ah_iniBank6, ar5416Bank6,
  3007. ARRAY_SIZE(ar5416Bank6), 3);
  3008. INIT_INI_ARRAY(&ahp->ah_iniBank6TPC, ar5416Bank6TPC,
  3009. ARRAY_SIZE(ar5416Bank6TPC), 3);
  3010. INIT_INI_ARRAY(&ahp->ah_iniBank7, ar5416Bank7,
  3011. ARRAY_SIZE(ar5416Bank7), 2);
  3012. INIT_INI_ARRAY(&ahp->ah_iniAddac, ar5416Addac,
  3013. ARRAY_SIZE(ar5416Addac), 2);
  3014. }
  3015. if (ah->ah_isPciExpress)
  3016. ath9k_hw_configpcipowersave(ah, 0);
  3017. else
  3018. ar5416DisablePciePhy(ah);
  3019. ecode = ath9k_hw_post_attach(ah);
  3020. if (ecode != 0)
  3021. goto bad;
  3022. #ifndef CONFIG_SLOW_ANT_DIV
  3023. if (ah->ah_devid == AR9280_DEVID_PCI) {
  3024. for (i = 0; i < ahp->ah_iniModes.ia_rows; i++) {
  3025. u32 reg = INI_RA(&ahp->ah_iniModes, i, 0);
  3026. for (j = 1; j < ahp->ah_iniModes.ia_columns; j++) {
  3027. u32 val = INI_RA(&ahp->ah_iniModes, i, j);
  3028. INI_RA(&ahp->ah_iniModes, i, j) =
  3029. ath9k_hw_ini_fixup(ah, &ahp->ah_eeprom,
  3030. reg, val);
  3031. }
  3032. }
  3033. }
  3034. #endif
  3035. if (!ath9k_hw_fill_cap_info(ah)) {
  3036. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  3037. "%s:failed ath9k_hw_fill_cap_info\n", __func__);
  3038. ecode = -EINVAL;
  3039. goto bad;
  3040. }
  3041. ecode = ath9k_hw_init_macaddr(ah);
  3042. if (ecode != 0) {
  3043. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  3044. "%s: failed initializing mac address\n",
  3045. __func__);
  3046. goto bad;
  3047. }
  3048. if (AR_SREV_9285(ah))
  3049. ah->ah_txTrigLevel = (AR_FTRIG_256B >> AR_FTRIG_S);
  3050. else
  3051. ah->ah_txTrigLevel = (AR_FTRIG_512B >> AR_FTRIG_S);
  3052. #ifndef ATH_NF_PER_CHAN
  3053. ath9k_init_nfcal_hist_buffer(ah);
  3054. #endif
  3055. return ah;
  3056. bad:
  3057. if (ahp)
  3058. ath9k_hw_detach((struct ath_hal *) ahp);
  3059. if (status)
  3060. *status = ecode;
  3061. return NULL;
  3062. }
  3063. void ath9k_hw_detach(struct ath_hal *ah)
  3064. {
  3065. if (!AR_SREV_9100(ah))
  3066. ath9k_hw_ani_detach(ah);
  3067. ath9k_hw_rfdetach(ah);
  3068. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  3069. kfree(ah);
  3070. }
  3071. bool ath9k_get_channel_edges(struct ath_hal *ah,
  3072. u16 flags, u16 *low,
  3073. u16 *high)
  3074. {
  3075. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  3076. if (flags & CHANNEL_5GHZ) {
  3077. *low = pCap->low_5ghz_chan;
  3078. *high = pCap->high_5ghz_chan;
  3079. return true;
  3080. }
  3081. if ((flags & CHANNEL_2GHZ)) {
  3082. *low = pCap->low_2ghz_chan;
  3083. *high = pCap->high_2ghz_chan;
  3084. return true;
  3085. }
  3086. return false;
  3087. }
  3088. static inline bool ath9k_hw_fill_vpd_table(u8 pwrMin,
  3089. u8 pwrMax,
  3090. u8 *pPwrList,
  3091. u8 *pVpdList,
  3092. u16
  3093. numIntercepts,
  3094. u8 *pRetVpdList)
  3095. {
  3096. u16 i, k;
  3097. u8 currPwr = pwrMin;
  3098. u16 idxL = 0, idxR = 0;
  3099. for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
  3100. ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
  3101. numIntercepts, &(idxL),
  3102. &(idxR));
  3103. if (idxR < 1)
  3104. idxR = 1;
  3105. if (idxL == numIntercepts - 1)
  3106. idxL = (u16) (numIntercepts - 2);
  3107. if (pPwrList[idxL] == pPwrList[idxR])
  3108. k = pVpdList[idxL];
  3109. else
  3110. k = (u16) (((currPwr -
  3111. pPwrList[idxL]) *
  3112. pVpdList[idxR] +
  3113. (pPwrList[idxR] -
  3114. currPwr) * pVpdList[idxL]) /
  3115. (pPwrList[idxR] -
  3116. pPwrList[idxL]));
  3117. pRetVpdList[i] = (u8) k;
  3118. currPwr += 2;
  3119. }
  3120. return true;
  3121. }
  3122. static void
  3123. ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hal *ah,
  3124. struct ath9k_channel *chan,
  3125. struct cal_data_per_freq *pRawDataSet,
  3126. u8 *bChans,
  3127. u16 availPiers,
  3128. u16 tPdGainOverlap,
  3129. int16_t *pMinCalPower,
  3130. u16 *pPdGainBoundaries,
  3131. u8 *pPDADCValues,
  3132. u16 numXpdGains)
  3133. {
  3134. int i, j, k;
  3135. int16_t ss;
  3136. u16 idxL = 0, idxR = 0, numPiers;
  3137. static u8 vpdTableL[AR5416_NUM_PD_GAINS]
  3138. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  3139. static u8 vpdTableR[AR5416_NUM_PD_GAINS]
  3140. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  3141. static u8 vpdTableI[AR5416_NUM_PD_GAINS]
  3142. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  3143. u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
  3144. u8 minPwrT4[AR5416_NUM_PD_GAINS];
  3145. u8 maxPwrT4[AR5416_NUM_PD_GAINS];
  3146. int16_t vpdStep;
  3147. int16_t tmpVal;
  3148. u16 sizeCurrVpdTable, maxIndex, tgtIndex;
  3149. bool match;
  3150. int16_t minDelta = 0;
  3151. struct chan_centers centers;
  3152. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3153. for (numPiers = 0; numPiers < availPiers; numPiers++) {
  3154. if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
  3155. break;
  3156. }
  3157. match = ath9k_hw_get_lower_upper_index((u8)
  3158. FREQ2FBIN(centers.
  3159. synth_center,
  3160. IS_CHAN_2GHZ
  3161. (chan)), bChans,
  3162. numPiers, &idxL, &idxR);
  3163. if (match) {
  3164. for (i = 0; i < numXpdGains; i++) {
  3165. minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
  3166. maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
  3167. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  3168. pRawDataSet[idxL].
  3169. pwrPdg[i],
  3170. pRawDataSet[idxL].
  3171. vpdPdg[i],
  3172. AR5416_PD_GAIN_ICEPTS,
  3173. vpdTableI[i]);
  3174. }
  3175. } else {
  3176. for (i = 0; i < numXpdGains; i++) {
  3177. pVpdL = pRawDataSet[idxL].vpdPdg[i];
  3178. pPwrL = pRawDataSet[idxL].pwrPdg[i];
  3179. pVpdR = pRawDataSet[idxR].vpdPdg[i];
  3180. pPwrR = pRawDataSet[idxR].pwrPdg[i];
  3181. minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
  3182. maxPwrT4[i] =
  3183. min(pPwrL[AR5416_PD_GAIN_ICEPTS - 1],
  3184. pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);
  3185. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  3186. pPwrL, pVpdL,
  3187. AR5416_PD_GAIN_ICEPTS,
  3188. vpdTableL[i]);
  3189. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  3190. pPwrR, pVpdR,
  3191. AR5416_PD_GAIN_ICEPTS,
  3192. vpdTableR[i]);
  3193. for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
  3194. vpdTableI[i][j] =
  3195. (u8) (ath9k_hw_interpolate
  3196. ((u16)
  3197. FREQ2FBIN(centers.
  3198. synth_center,
  3199. IS_CHAN_2GHZ
  3200. (chan)),
  3201. bChans[idxL],
  3202. bChans[idxR], vpdTableL[i]
  3203. [j], vpdTableR[i]
  3204. [j]));
  3205. }
  3206. }
  3207. }
  3208. *pMinCalPower = (int16_t) (minPwrT4[0] / 2);
  3209. k = 0;
  3210. for (i = 0; i < numXpdGains; i++) {
  3211. if (i == (numXpdGains - 1))
  3212. pPdGainBoundaries[i] =
  3213. (u16) (maxPwrT4[i] / 2);
  3214. else
  3215. pPdGainBoundaries[i] =
  3216. (u16) ((maxPwrT4[i] +
  3217. minPwrT4[i + 1]) / 4);
  3218. pPdGainBoundaries[i] =
  3219. min((u16) AR5416_MAX_RATE_POWER,
  3220. pPdGainBoundaries[i]);
  3221. if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah)) {
  3222. minDelta = pPdGainBoundaries[0] - 23;
  3223. pPdGainBoundaries[0] = 23;
  3224. } else {
  3225. minDelta = 0;
  3226. }
  3227. if (i == 0) {
  3228. if (AR_SREV_9280_10_OR_LATER(ah))
  3229. ss = (int16_t) (0 - (minPwrT4[i] / 2));
  3230. else
  3231. ss = 0;
  3232. } else {
  3233. ss = (int16_t) ((pPdGainBoundaries[i - 1] -
  3234. (minPwrT4[i] / 2)) -
  3235. tPdGainOverlap + 1 + minDelta);
  3236. }
  3237. vpdStep = (int16_t) (vpdTableI[i][1] - vpdTableI[i][0]);
  3238. vpdStep = (int16_t) ((vpdStep < 1) ? 1 : vpdStep);
  3239. while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  3240. tmpVal = (int16_t) (vpdTableI[i][0] + ss * vpdStep);
  3241. pPDADCValues[k++] =
  3242. (u8) ((tmpVal < 0) ? 0 : tmpVal);
  3243. ss++;
  3244. }
  3245. sizeCurrVpdTable =
  3246. (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
  3247. tgtIndex = (u8) (pPdGainBoundaries[i] + tPdGainOverlap -
  3248. (minPwrT4[i] / 2));
  3249. maxIndex = (tgtIndex <
  3250. sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
  3251. while ((ss < maxIndex)
  3252. && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  3253. pPDADCValues[k++] = vpdTableI[i][ss++];
  3254. }
  3255. vpdStep = (int16_t) (vpdTableI[i][sizeCurrVpdTable - 1] -
  3256. vpdTableI[i][sizeCurrVpdTable - 2]);
  3257. vpdStep = (int16_t) ((vpdStep < 1) ? 1 : vpdStep);
  3258. if (tgtIndex > maxIndex) {
  3259. while ((ss <= tgtIndex)
  3260. && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  3261. tmpVal = (int16_t) ((vpdTableI[i]
  3262. [sizeCurrVpdTable -
  3263. 1] + (ss - maxIndex +
  3264. 1) * vpdStep));
  3265. pPDADCValues[k++] = (u8) ((tmpVal >
  3266. 255) ? 255 : tmpVal);
  3267. ss++;
  3268. }
  3269. }
  3270. }
  3271. while (i < AR5416_PD_GAINS_IN_MASK) {
  3272. pPdGainBoundaries[i] = pPdGainBoundaries[i - 1];
  3273. i++;
  3274. }
  3275. while (k < AR5416_NUM_PDADC_VALUES) {
  3276. pPDADCValues[k] = pPDADCValues[k - 1];
  3277. k++;
  3278. }
  3279. return;
  3280. }
  3281. static bool
  3282. ath9k_hw_set_power_cal_table(struct ath_hal *ah,
  3283. struct ar5416_eeprom *pEepData,
  3284. struct ath9k_channel *chan,
  3285. int16_t *pTxPowerIndexOffset)
  3286. {
  3287. struct cal_data_per_freq *pRawDataset;
  3288. u8 *pCalBChans = NULL;
  3289. u16 pdGainOverlap_t2;
  3290. static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
  3291. u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
  3292. u16 numPiers, i, j;
  3293. int16_t tMinCalPower;
  3294. u16 numXpdGain, xpdMask;
  3295. u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
  3296. u32 reg32, regOffset, regChainOffset;
  3297. int16_t modalIdx;
  3298. struct ath_hal_5416 *ahp = AH5416(ah);
  3299. modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
  3300. xpdMask = pEepData->modalHeader[modalIdx].xpdGain;
  3301. if ((pEepData->baseEepHeader.
  3302. version & AR5416_EEP_VER_MINOR_MASK) >=
  3303. AR5416_EEP_MINOR_VER_2) {
  3304. pdGainOverlap_t2 =
  3305. pEepData->modalHeader[modalIdx].pdGainOverlap;
  3306. } else {
  3307. pdGainOverlap_t2 =
  3308. (u16) (MS
  3309. (REG_READ(ah, AR_PHY_TPCRG5),
  3310. AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
  3311. }
  3312. if (IS_CHAN_2GHZ(chan)) {
  3313. pCalBChans = pEepData->calFreqPier2G;
  3314. numPiers = AR5416_NUM_2G_CAL_PIERS;
  3315. } else {
  3316. pCalBChans = pEepData->calFreqPier5G;
  3317. numPiers = AR5416_NUM_5G_CAL_PIERS;
  3318. }
  3319. numXpdGain = 0;
  3320. for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
  3321. if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
  3322. if (numXpdGain >= AR5416_NUM_PD_GAINS)
  3323. break;
  3324. xpdGainValues[numXpdGain] =
  3325. (u16) (AR5416_PD_GAINS_IN_MASK - i);
  3326. numXpdGain++;
  3327. }
  3328. }
  3329. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
  3330. (numXpdGain - 1) & 0x3);
  3331. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
  3332. xpdGainValues[0]);
  3333. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
  3334. xpdGainValues[1]);
  3335. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
  3336. xpdGainValues[2]);
  3337. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  3338. if (AR_SREV_5416_V20_OR_LATER(ah) &&
  3339. (ahp->ah_rxchainmask == 5 || ahp->ah_txchainmask == 5)
  3340. && (i != 0)) {
  3341. regChainOffset = (i == 1) ? 0x2000 : 0x1000;
  3342. } else
  3343. regChainOffset = i * 0x1000;
  3344. if (pEepData->baseEepHeader.txMask & (1 << i)) {
  3345. if (IS_CHAN_2GHZ(chan))
  3346. pRawDataset = pEepData->calPierData2G[i];
  3347. else
  3348. pRawDataset = pEepData->calPierData5G[i];
  3349. ath9k_hw_get_gain_boundaries_pdadcs(ah, chan,
  3350. pRawDataset,
  3351. pCalBChans,
  3352. numPiers,
  3353. pdGainOverlap_t2,
  3354. &tMinCalPower,
  3355. gainBoundaries,
  3356. pdadcValues,
  3357. numXpdGain);
  3358. if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
  3359. REG_WRITE(ah,
  3360. AR_PHY_TPCRG5 + regChainOffset,
  3361. SM(pdGainOverlap_t2,
  3362. AR_PHY_TPCRG5_PD_GAIN_OVERLAP)
  3363. | SM(gainBoundaries[0],
  3364. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1)
  3365. | SM(gainBoundaries[1],
  3366. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2)
  3367. | SM(gainBoundaries[2],
  3368. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3)
  3369. | SM(gainBoundaries[3],
  3370. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
  3371. }
  3372. regOffset =
  3373. AR_PHY_BASE + (672 << 2) + regChainOffset;
  3374. for (j = 0; j < 32; j++) {
  3375. reg32 =
  3376. ((pdadcValues[4 * j + 0] & 0xFF) << 0)
  3377. | ((pdadcValues[4 * j + 1] & 0xFF) <<
  3378. 8) | ((pdadcValues[4 * j + 2] &
  3379. 0xFF) << 16) |
  3380. ((pdadcValues[4 * j + 3] & 0xFF) <<
  3381. 24);
  3382. REG_WRITE(ah, regOffset, reg32);
  3383. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  3384. "PDADC (%d,%4x): %4.4x %8.8x\n",
  3385. i, regChainOffset, regOffset,
  3386. reg32);
  3387. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  3388. "PDADC: Chain %d | PDADC %3d Value %3d | "
  3389. "PDADC %3d Value %3d | PDADC %3d Value %3d | "
  3390. "PDADC %3d Value %3d |\n",
  3391. i, 4 * j, pdadcValues[4 * j],
  3392. 4 * j + 1, pdadcValues[4 * j + 1],
  3393. 4 * j + 2, pdadcValues[4 * j + 2],
  3394. 4 * j + 3,
  3395. pdadcValues[4 * j + 3]);
  3396. regOffset += 4;
  3397. }
  3398. }
  3399. }
  3400. *pTxPowerIndexOffset = 0;
  3401. return true;
  3402. }
  3403. void ath9k_hw_configpcipowersave(struct ath_hal *ah, int restore)
  3404. {
  3405. struct ath_hal_5416 *ahp = AH5416(ah);
  3406. u8 i;
  3407. if (ah->ah_isPciExpress != true)
  3408. return;
  3409. if (ah->ah_config.pcie_powersave_enable == 2)
  3410. return;
  3411. if (restore)
  3412. return;
  3413. if (AR_SREV_9280_20_OR_LATER(ah)) {
  3414. for (i = 0; i < ahp->ah_iniPcieSerdes.ia_rows; i++) {
  3415. REG_WRITE(ah, INI_RA(&ahp->ah_iniPcieSerdes, i, 0),
  3416. INI_RA(&ahp->ah_iniPcieSerdes, i, 1));
  3417. }
  3418. udelay(1000);
  3419. } else if (AR_SREV_9280(ah)
  3420. && (ah->ah_macRev == AR_SREV_REVISION_9280_10)) {
  3421. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fd00);
  3422. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  3423. REG_WRITE(ah, AR_PCIE_SERDES, 0xa8000019);
  3424. REG_WRITE(ah, AR_PCIE_SERDES, 0x13160820);
  3425. REG_WRITE(ah, AR_PCIE_SERDES, 0xe5980560);
  3426. if (ah->ah_config.pcie_clock_req)
  3427. REG_WRITE(ah, AR_PCIE_SERDES, 0x401deffc);
  3428. else
  3429. REG_WRITE(ah, AR_PCIE_SERDES, 0x401deffd);
  3430. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  3431. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  3432. REG_WRITE(ah, AR_PCIE_SERDES, 0x00043007);
  3433. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  3434. udelay(1000);
  3435. } else {
  3436. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  3437. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  3438. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000039);
  3439. REG_WRITE(ah, AR_PCIE_SERDES, 0x53160824);
  3440. REG_WRITE(ah, AR_PCIE_SERDES, 0xe5980579);
  3441. REG_WRITE(ah, AR_PCIE_SERDES, 0x001defff);
  3442. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  3443. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  3444. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e3007);
  3445. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  3446. }
  3447. REG_SET_BIT(ah, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA);
  3448. if (ah->ah_config.pcie_waen) {
  3449. REG_WRITE(ah, AR_WA, ah->ah_config.pcie_waen);
  3450. } else {
  3451. if (AR_SREV_9280(ah))
  3452. REG_WRITE(ah, AR_WA, 0x0040073f);
  3453. else
  3454. REG_WRITE(ah, AR_WA, 0x0000073f);
  3455. }
  3456. }
  3457. static void
  3458. ath9k_hw_get_legacy_target_powers(struct ath_hal *ah,
  3459. struct ath9k_channel *chan,
  3460. struct cal_target_power_leg *powInfo,
  3461. u16 numChannels,
  3462. struct cal_target_power_leg *pNewPower,
  3463. u16 numRates,
  3464. bool isExtTarget)
  3465. {
  3466. u16 clo, chi;
  3467. int i;
  3468. int matchIndex = -1, lowIndex = -1;
  3469. u16 freq;
  3470. struct chan_centers centers;
  3471. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3472. freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;
  3473. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
  3474. IS_CHAN_2GHZ(chan))) {
  3475. matchIndex = 0;
  3476. } else {
  3477. for (i = 0; (i < numChannels)
  3478. && (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  3479. if (freq ==
  3480. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3481. IS_CHAN_2GHZ(chan))) {
  3482. matchIndex = i;
  3483. break;
  3484. } else if ((freq <
  3485. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3486. IS_CHAN_2GHZ(chan)))
  3487. && (freq >
  3488. ath9k_hw_fbin2freq(powInfo[i - 1].
  3489. bChannel,
  3490. IS_CHAN_2GHZ
  3491. (chan)))) {
  3492. lowIndex = i - 1;
  3493. break;
  3494. }
  3495. }
  3496. if ((matchIndex == -1) && (lowIndex == -1))
  3497. matchIndex = i - 1;
  3498. }
  3499. if (matchIndex != -1) {
  3500. *pNewPower = powInfo[matchIndex];
  3501. } else {
  3502. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  3503. IS_CHAN_2GHZ(chan));
  3504. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  3505. IS_CHAN_2GHZ(chan));
  3506. for (i = 0; i < numRates; i++) {
  3507. pNewPower->tPow2x[i] =
  3508. (u8) ath9k_hw_interpolate(freq, clo, chi,
  3509. powInfo
  3510. [lowIndex].
  3511. tPow2x[i],
  3512. powInfo
  3513. [lowIndex +
  3514. 1].tPow2x[i]);
  3515. }
  3516. }
  3517. }
  3518. static void
  3519. ath9k_hw_get_target_powers(struct ath_hal *ah,
  3520. struct ath9k_channel *chan,
  3521. struct cal_target_power_ht *powInfo,
  3522. u16 numChannels,
  3523. struct cal_target_power_ht *pNewPower,
  3524. u16 numRates,
  3525. bool isHt40Target)
  3526. {
  3527. u16 clo, chi;
  3528. int i;
  3529. int matchIndex = -1, lowIndex = -1;
  3530. u16 freq;
  3531. struct chan_centers centers;
  3532. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3533. freq = isHt40Target ? centers.synth_center : centers.ctl_center;
  3534. if (freq <=
  3535. ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
  3536. matchIndex = 0;
  3537. } else {
  3538. for (i = 0; (i < numChannels)
  3539. && (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  3540. if (freq ==
  3541. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3542. IS_CHAN_2GHZ(chan))) {
  3543. matchIndex = i;
  3544. break;
  3545. } else
  3546. if ((freq <
  3547. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3548. IS_CHAN_2GHZ(chan)))
  3549. && (freq >
  3550. ath9k_hw_fbin2freq(powInfo[i - 1].
  3551. bChannel,
  3552. IS_CHAN_2GHZ
  3553. (chan)))) {
  3554. lowIndex = i - 1;
  3555. break;
  3556. }
  3557. }
  3558. if ((matchIndex == -1) && (lowIndex == -1))
  3559. matchIndex = i - 1;
  3560. }
  3561. if (matchIndex != -1) {
  3562. *pNewPower = powInfo[matchIndex];
  3563. } else {
  3564. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  3565. IS_CHAN_2GHZ(chan));
  3566. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  3567. IS_CHAN_2GHZ(chan));
  3568. for (i = 0; i < numRates; i++) {
  3569. pNewPower->tPow2x[i] =
  3570. (u8) ath9k_hw_interpolate(freq, clo, chi,
  3571. powInfo
  3572. [lowIndex].
  3573. tPow2x[i],
  3574. powInfo
  3575. [lowIndex +
  3576. 1].tPow2x[i]);
  3577. }
  3578. }
  3579. }
  3580. static u16
  3581. ath9k_hw_get_max_edge_power(u16 freq,
  3582. struct cal_ctl_edges *pRdEdgesPower,
  3583. bool is2GHz)
  3584. {
  3585. u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  3586. int i;
  3587. for (i = 0; (i < AR5416_NUM_BAND_EDGES)
  3588. && (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  3589. if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
  3590. is2GHz)) {
  3591. twiceMaxEdgePower = pRdEdgesPower[i].tPower;
  3592. break;
  3593. } else if ((i > 0)
  3594. && (freq <
  3595. ath9k_hw_fbin2freq(pRdEdgesPower[i].
  3596. bChannel, is2GHz))) {
  3597. if (ath9k_hw_fbin2freq
  3598. (pRdEdgesPower[i - 1].bChannel, is2GHz) < freq
  3599. && pRdEdgesPower[i - 1].flag) {
  3600. twiceMaxEdgePower =
  3601. pRdEdgesPower[i - 1].tPower;
  3602. }
  3603. break;
  3604. }
  3605. }
  3606. return twiceMaxEdgePower;
  3607. }
  3608. static bool
  3609. ath9k_hw_set_power_per_rate_table(struct ath_hal *ah,
  3610. struct ar5416_eeprom *pEepData,
  3611. struct ath9k_channel *chan,
  3612. int16_t *ratesArray,
  3613. u16 cfgCtl,
  3614. u8 AntennaReduction,
  3615. u8 twiceMaxRegulatoryPower,
  3616. u8 powerLimit)
  3617. {
  3618. u8 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  3619. static const u16 tpScaleReductionTable[5] =
  3620. { 0, 3, 6, 9, AR5416_MAX_RATE_POWER };
  3621. int i;
  3622. int8_t twiceLargestAntenna;
  3623. struct cal_ctl_data *rep;
  3624. struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
  3625. 0, { 0, 0, 0, 0}
  3626. };
  3627. struct cal_target_power_leg targetPowerOfdmExt = {
  3628. 0, { 0, 0, 0, 0} }, targetPowerCckExt = {
  3629. 0, { 0, 0, 0, 0 }
  3630. };
  3631. struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
  3632. 0, {0, 0, 0, 0}
  3633. };
  3634. u8 scaledPower = 0, minCtlPower, maxRegAllowedPower;
  3635. u16 ctlModesFor11a[] =
  3636. { CTL_11A, CTL_5GHT20, CTL_11A_EXT, CTL_5GHT40 };
  3637. u16 ctlModesFor11g[] =
  3638. { CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT,
  3639. CTL_2GHT40
  3640. };
  3641. u16 numCtlModes, *pCtlMode, ctlMode, freq;
  3642. struct chan_centers centers;
  3643. int tx_chainmask;
  3644. u8 twiceMinEdgePower;
  3645. struct ath_hal_5416 *ahp = AH5416(ah);
  3646. tx_chainmask = ahp->ah_txchainmask;
  3647. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3648. twiceLargestAntenna = max(
  3649. pEepData->modalHeader
  3650. [IS_CHAN_2GHZ(chan)].antennaGainCh[0],
  3651. pEepData->modalHeader
  3652. [IS_CHAN_2GHZ(chan)].antennaGainCh[1]);
  3653. twiceLargestAntenna = max((u8) twiceLargestAntenna,
  3654. pEepData->modalHeader
  3655. [IS_CHAN_2GHZ(chan)].antennaGainCh[2]);
  3656. twiceLargestAntenna =
  3657. (int8_t) min(AntennaReduction - twiceLargestAntenna, 0);
  3658. maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;
  3659. if (ah->ah_tpScale != ATH9K_TP_SCALE_MAX) {
  3660. maxRegAllowedPower -=
  3661. (tpScaleReductionTable[(ah->ah_tpScale)] * 2);
  3662. }
  3663. scaledPower = min(powerLimit, maxRegAllowedPower);
  3664. switch (ar5416_get_ntxchains(tx_chainmask)) {
  3665. case 1:
  3666. break;
  3667. case 2:
  3668. scaledPower -=
  3669. pEepData->modalHeader[IS_CHAN_2GHZ(chan)].
  3670. pwrDecreaseFor2Chain;
  3671. break;
  3672. case 3:
  3673. scaledPower -=
  3674. pEepData->modalHeader[IS_CHAN_2GHZ(chan)].
  3675. pwrDecreaseFor3Chain;
  3676. break;
  3677. }
  3678. scaledPower = max(0, (int32_t) scaledPower);
  3679. if (IS_CHAN_2GHZ(chan)) {
  3680. numCtlModes =
  3681. ARRAY_SIZE(ctlModesFor11g) -
  3682. SUB_NUM_CTL_MODES_AT_2G_40;
  3683. pCtlMode = ctlModesFor11g;
  3684. ath9k_hw_get_legacy_target_powers(ah, chan,
  3685. pEepData->
  3686. calTargetPowerCck,
  3687. AR5416_NUM_2G_CCK_TARGET_POWERS,
  3688. &targetPowerCck, 4,
  3689. false);
  3690. ath9k_hw_get_legacy_target_powers(ah, chan,
  3691. pEepData->
  3692. calTargetPower2G,
  3693. AR5416_NUM_2G_20_TARGET_POWERS,
  3694. &targetPowerOfdm, 4,
  3695. false);
  3696. ath9k_hw_get_target_powers(ah, chan,
  3697. pEepData->calTargetPower2GHT20,
  3698. AR5416_NUM_2G_20_TARGET_POWERS,
  3699. &targetPowerHt20, 8, false);
  3700. if (IS_CHAN_HT40(chan)) {
  3701. numCtlModes = ARRAY_SIZE(ctlModesFor11g);
  3702. ath9k_hw_get_target_powers(ah, chan,
  3703. pEepData->
  3704. calTargetPower2GHT40,
  3705. AR5416_NUM_2G_40_TARGET_POWERS,
  3706. &targetPowerHt40, 8,
  3707. true);
  3708. ath9k_hw_get_legacy_target_powers(ah, chan,
  3709. pEepData->
  3710. calTargetPowerCck,
  3711. AR5416_NUM_2G_CCK_TARGET_POWERS,
  3712. &targetPowerCckExt,
  3713. 4, true);
  3714. ath9k_hw_get_legacy_target_powers(ah, chan,
  3715. pEepData->
  3716. calTargetPower2G,
  3717. AR5416_NUM_2G_20_TARGET_POWERS,
  3718. &targetPowerOfdmExt,
  3719. 4, true);
  3720. }
  3721. } else {
  3722. numCtlModes =
  3723. ARRAY_SIZE(ctlModesFor11a) -
  3724. SUB_NUM_CTL_MODES_AT_5G_40;
  3725. pCtlMode = ctlModesFor11a;
  3726. ath9k_hw_get_legacy_target_powers(ah, chan,
  3727. pEepData->
  3728. calTargetPower5G,
  3729. AR5416_NUM_5G_20_TARGET_POWERS,
  3730. &targetPowerOfdm, 4,
  3731. false);
  3732. ath9k_hw_get_target_powers(ah, chan,
  3733. pEepData->calTargetPower5GHT20,
  3734. AR5416_NUM_5G_20_TARGET_POWERS,
  3735. &targetPowerHt20, 8, false);
  3736. if (IS_CHAN_HT40(chan)) {
  3737. numCtlModes = ARRAY_SIZE(ctlModesFor11a);
  3738. ath9k_hw_get_target_powers(ah, chan,
  3739. pEepData->
  3740. calTargetPower5GHT40,
  3741. AR5416_NUM_5G_40_TARGET_POWERS,
  3742. &targetPowerHt40, 8,
  3743. true);
  3744. ath9k_hw_get_legacy_target_powers(ah, chan,
  3745. pEepData->
  3746. calTargetPower5G,
  3747. AR5416_NUM_5G_20_TARGET_POWERS,
  3748. &targetPowerOfdmExt,
  3749. 4, true);
  3750. }
  3751. }
  3752. for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
  3753. bool isHt40CtlMode =
  3754. (pCtlMode[ctlMode] == CTL_5GHT40)
  3755. || (pCtlMode[ctlMode] == CTL_2GHT40);
  3756. if (isHt40CtlMode)
  3757. freq = centers.synth_center;
  3758. else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
  3759. freq = centers.ext_center;
  3760. else
  3761. freq = centers.ctl_center;
  3762. if (ar5416_get_eep_ver(ahp) == 14
  3763. && ar5416_get_eep_rev(ahp) <= 2)
  3764. twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  3765. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3766. "LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d, "
  3767. "EXT_ADDITIVE %d\n",
  3768. ctlMode, numCtlModes, isHt40CtlMode,
  3769. (pCtlMode[ctlMode] & EXT_ADDITIVE));
  3770. for (i = 0; (i < AR5416_NUM_CTLS) && pEepData->ctlIndex[i];
  3771. i++) {
  3772. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3773. " LOOP-Ctlidx %d: cfgCtl 0x%2.2x "
  3774. "pCtlMode 0x%2.2x ctlIndex 0x%2.2x "
  3775. "chan %d\n",
  3776. i, cfgCtl, pCtlMode[ctlMode],
  3777. pEepData->ctlIndex[i], chan->channel);
  3778. if ((((cfgCtl & ~CTL_MODE_M) |
  3779. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  3780. pEepData->ctlIndex[i])
  3781. ||
  3782. (((cfgCtl & ~CTL_MODE_M) |
  3783. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  3784. ((pEepData->
  3785. ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
  3786. rep = &(pEepData->ctlData[i]);
  3787. twiceMinEdgePower =
  3788. ath9k_hw_get_max_edge_power(freq,
  3789. rep->
  3790. ctlEdges
  3791. [ar5416_get_ntxchains
  3792. (tx_chainmask)
  3793. - 1],
  3794. IS_CHAN_2GHZ
  3795. (chan));
  3796. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3797. " MATCH-EE_IDX %d: ch %d is2 %d "
  3798. "2xMinEdge %d chainmask %d chains %d\n",
  3799. i, freq, IS_CHAN_2GHZ(chan),
  3800. twiceMinEdgePower, tx_chainmask,
  3801. ar5416_get_ntxchains
  3802. (tx_chainmask));
  3803. if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
  3804. twiceMaxEdgePower =
  3805. min(twiceMaxEdgePower,
  3806. twiceMinEdgePower);
  3807. } else {
  3808. twiceMaxEdgePower =
  3809. twiceMinEdgePower;
  3810. break;
  3811. }
  3812. }
  3813. }
  3814. minCtlPower = min(twiceMaxEdgePower, scaledPower);
  3815. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3816. " SEL-Min ctlMode %d pCtlMode %d "
  3817. "2xMaxEdge %d sP %d minCtlPwr %d\n",
  3818. ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
  3819. scaledPower, minCtlPower);
  3820. switch (pCtlMode[ctlMode]) {
  3821. case CTL_11B:
  3822. for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x);
  3823. i++) {
  3824. targetPowerCck.tPow2x[i] =
  3825. min(targetPowerCck.tPow2x[i],
  3826. minCtlPower);
  3827. }
  3828. break;
  3829. case CTL_11A:
  3830. case CTL_11G:
  3831. for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x);
  3832. i++) {
  3833. targetPowerOfdm.tPow2x[i] =
  3834. min(targetPowerOfdm.tPow2x[i],
  3835. minCtlPower);
  3836. }
  3837. break;
  3838. case CTL_5GHT20:
  3839. case CTL_2GHT20:
  3840. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x);
  3841. i++) {
  3842. targetPowerHt20.tPow2x[i] =
  3843. min(targetPowerHt20.tPow2x[i],
  3844. minCtlPower);
  3845. }
  3846. break;
  3847. case CTL_11B_EXT:
  3848. targetPowerCckExt.tPow2x[0] =
  3849. min(targetPowerCckExt.tPow2x[0], minCtlPower);
  3850. break;
  3851. case CTL_11A_EXT:
  3852. case CTL_11G_EXT:
  3853. targetPowerOfdmExt.tPow2x[0] =
  3854. min(targetPowerOfdmExt.tPow2x[0], minCtlPower);
  3855. break;
  3856. case CTL_5GHT40:
  3857. case CTL_2GHT40:
  3858. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x);
  3859. i++) {
  3860. targetPowerHt40.tPow2x[i] =
  3861. min(targetPowerHt40.tPow2x[i],
  3862. minCtlPower);
  3863. }
  3864. break;
  3865. default:
  3866. break;
  3867. }
  3868. }
  3869. ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
  3870. ratesArray[rate18mb] = ratesArray[rate24mb] =
  3871. targetPowerOfdm.tPow2x[0];
  3872. ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
  3873. ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
  3874. ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
  3875. ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
  3876. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
  3877. ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
  3878. if (IS_CHAN_2GHZ(chan)) {
  3879. ratesArray[rate1l] = targetPowerCck.tPow2x[0];
  3880. ratesArray[rate2s] = ratesArray[rate2l] =
  3881. targetPowerCck.tPow2x[1];
  3882. ratesArray[rate5_5s] = ratesArray[rate5_5l] =
  3883. targetPowerCck.tPow2x[2];
  3884. ;
  3885. ratesArray[rate11s] = ratesArray[rate11l] =
  3886. targetPowerCck.tPow2x[3];
  3887. ;
  3888. }
  3889. if (IS_CHAN_HT40(chan)) {
  3890. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
  3891. ratesArray[rateHt40_0 + i] =
  3892. targetPowerHt40.tPow2x[i];
  3893. }
  3894. ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
  3895. ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
  3896. ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
  3897. if (IS_CHAN_2GHZ(chan)) {
  3898. ratesArray[rateExtCck] =
  3899. targetPowerCckExt.tPow2x[0];
  3900. }
  3901. }
  3902. return true;
  3903. }
  3904. static int
  3905. ath9k_hw_set_txpower(struct ath_hal *ah,
  3906. struct ar5416_eeprom *pEepData,
  3907. struct ath9k_channel *chan,
  3908. u16 cfgCtl,
  3909. u8 twiceAntennaReduction,
  3910. u8 twiceMaxRegulatoryPower,
  3911. u8 powerLimit)
  3912. {
  3913. struct modal_eep_header *pModal =
  3914. &(pEepData->modalHeader[IS_CHAN_2GHZ(chan)]);
  3915. int16_t ratesArray[Ar5416RateSize];
  3916. int16_t txPowerIndexOffset = 0;
  3917. u8 ht40PowerIncForPdadc = 2;
  3918. int i;
  3919. memset(ratesArray, 0, sizeof(ratesArray));
  3920. if ((pEepData->baseEepHeader.
  3921. version & AR5416_EEP_VER_MINOR_MASK) >=
  3922. AR5416_EEP_MINOR_VER_2) {
  3923. ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
  3924. }
  3925. if (!ath9k_hw_set_power_per_rate_table(ah, pEepData, chan,
  3926. &ratesArray[0], cfgCtl,
  3927. twiceAntennaReduction,
  3928. twiceMaxRegulatoryPower,
  3929. powerLimit)) {
  3930. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  3931. "ath9k_hw_set_txpower: unable to set "
  3932. "tx power per rate table\n");
  3933. return -EIO;
  3934. }
  3935. if (!ath9k_hw_set_power_cal_table
  3936. (ah, pEepData, chan, &txPowerIndexOffset)) {
  3937. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  3938. "ath9k_hw_set_txpower: unable to set power table\n");
  3939. return -EIO;
  3940. }
  3941. for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
  3942. ratesArray[i] =
  3943. (int16_t) (txPowerIndexOffset + ratesArray[i]);
  3944. if (ratesArray[i] > AR5416_MAX_RATE_POWER)
  3945. ratesArray[i] = AR5416_MAX_RATE_POWER;
  3946. }
  3947. if (AR_SREV_9280_10_OR_LATER(ah)) {
  3948. for (i = 0; i < Ar5416RateSize; i++)
  3949. ratesArray[i] -= AR5416_PWR_TABLE_OFFSET * 2;
  3950. }
  3951. REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
  3952. ATH9K_POW_SM(ratesArray[rate18mb], 24)
  3953. | ATH9K_POW_SM(ratesArray[rate12mb], 16)
  3954. | ATH9K_POW_SM(ratesArray[rate9mb], 8)
  3955. | ATH9K_POW_SM(ratesArray[rate6mb], 0)
  3956. );
  3957. REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
  3958. ATH9K_POW_SM(ratesArray[rate54mb], 24)
  3959. | ATH9K_POW_SM(ratesArray[rate48mb], 16)
  3960. | ATH9K_POW_SM(ratesArray[rate36mb], 8)
  3961. | ATH9K_POW_SM(ratesArray[rate24mb], 0)
  3962. );
  3963. if (IS_CHAN_2GHZ(chan)) {
  3964. REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
  3965. ATH9K_POW_SM(ratesArray[rate2s], 24)
  3966. | ATH9K_POW_SM(ratesArray[rate2l], 16)
  3967. | ATH9K_POW_SM(ratesArray[rateXr], 8)
  3968. | ATH9K_POW_SM(ratesArray[rate1l], 0)
  3969. );
  3970. REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
  3971. ATH9K_POW_SM(ratesArray[rate11s], 24)
  3972. | ATH9K_POW_SM(ratesArray[rate11l], 16)
  3973. | ATH9K_POW_SM(ratesArray[rate5_5s], 8)
  3974. | ATH9K_POW_SM(ratesArray[rate5_5l], 0)
  3975. );
  3976. }
  3977. REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
  3978. ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
  3979. | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
  3980. | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
  3981. | ATH9K_POW_SM(ratesArray[rateHt20_0], 0)
  3982. );
  3983. REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
  3984. ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
  3985. | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
  3986. | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
  3987. | ATH9K_POW_SM(ratesArray[rateHt20_4], 0)
  3988. );
  3989. if (IS_CHAN_HT40(chan)) {
  3990. REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
  3991. ATH9K_POW_SM(ratesArray[rateHt40_3] +
  3992. ht40PowerIncForPdadc, 24)
  3993. | ATH9K_POW_SM(ratesArray[rateHt40_2] +
  3994. ht40PowerIncForPdadc, 16)
  3995. | ATH9K_POW_SM(ratesArray[rateHt40_1] +
  3996. ht40PowerIncForPdadc, 8)
  3997. | ATH9K_POW_SM(ratesArray[rateHt40_0] +
  3998. ht40PowerIncForPdadc, 0)
  3999. );
  4000. REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
  4001. ATH9K_POW_SM(ratesArray[rateHt40_7] +
  4002. ht40PowerIncForPdadc, 24)
  4003. | ATH9K_POW_SM(ratesArray[rateHt40_6] +
  4004. ht40PowerIncForPdadc, 16)
  4005. | ATH9K_POW_SM(ratesArray[rateHt40_5] +
  4006. ht40PowerIncForPdadc, 8)
  4007. | ATH9K_POW_SM(ratesArray[rateHt40_4] +
  4008. ht40PowerIncForPdadc, 0)
  4009. );
  4010. REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
  4011. ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
  4012. | ATH9K_POW_SM(ratesArray[rateExtCck], 16)
  4013. | ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
  4014. | ATH9K_POW_SM(ratesArray[rateDupCck], 0)
  4015. );
  4016. }
  4017. REG_WRITE(ah, AR_PHY_POWER_TX_SUB,
  4018. ATH9K_POW_SM(pModal->pwrDecreaseFor3Chain, 6)
  4019. | ATH9K_POW_SM(pModal->pwrDecreaseFor2Chain, 0)
  4020. );
  4021. i = rate6mb;
  4022. if (IS_CHAN_HT40(chan))
  4023. i = rateHt40_0;
  4024. else if (IS_CHAN_HT20(chan))
  4025. i = rateHt20_0;
  4026. if (AR_SREV_9280_10_OR_LATER(ah))
  4027. ah->ah_maxPowerLevel =
  4028. ratesArray[i] + AR5416_PWR_TABLE_OFFSET * 2;
  4029. else
  4030. ah->ah_maxPowerLevel = ratesArray[i];
  4031. return 0;
  4032. }
  4033. static inline void ath9k_hw_get_delta_slope_vals(struct ath_hal *ah,
  4034. u32 coef_scaled,
  4035. u32 *coef_mantissa,
  4036. u32 *coef_exponent)
  4037. {
  4038. u32 coef_exp, coef_man;
  4039. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  4040. if ((coef_scaled >> coef_exp) & 0x1)
  4041. break;
  4042. coef_exp = 14 - (coef_exp - COEF_SCALE_S);
  4043. coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
  4044. *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
  4045. *coef_exponent = coef_exp - 16;
  4046. }
  4047. static void
  4048. ath9k_hw_set_delta_slope(struct ath_hal *ah,
  4049. struct ath9k_channel *chan)
  4050. {
  4051. u32 coef_scaled, ds_coef_exp, ds_coef_man;
  4052. u32 clockMhzScaled = 0x64000000;
  4053. struct chan_centers centers;
  4054. if (IS_CHAN_HALF_RATE(chan))
  4055. clockMhzScaled = clockMhzScaled >> 1;
  4056. else if (IS_CHAN_QUARTER_RATE(chan))
  4057. clockMhzScaled = clockMhzScaled >> 2;
  4058. ath9k_hw_get_channel_centers(ah, chan, &centers);
  4059. coef_scaled = clockMhzScaled / centers.synth_center;
  4060. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  4061. &ds_coef_exp);
  4062. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  4063. AR_PHY_TIMING3_DSC_MAN, ds_coef_man);
  4064. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  4065. AR_PHY_TIMING3_DSC_EXP, ds_coef_exp);
  4066. coef_scaled = (9 * coef_scaled) / 10;
  4067. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  4068. &ds_coef_exp);
  4069. REG_RMW_FIELD(ah, AR_PHY_HALFGI,
  4070. AR_PHY_HALFGI_DSC_MAN, ds_coef_man);
  4071. REG_RMW_FIELD(ah, AR_PHY_HALFGI,
  4072. AR_PHY_HALFGI_DSC_EXP, ds_coef_exp);
  4073. }
  4074. static void ath9k_hw_9280_spur_mitigate(struct ath_hal *ah,
  4075. struct ath9k_channel *chan)
  4076. {
  4077. int bb_spur = AR_NO_SPUR;
  4078. int freq;
  4079. int bin, cur_bin;
  4080. int bb_spur_off, spur_subchannel_sd;
  4081. int spur_freq_sd;
  4082. int spur_delta_phase;
  4083. int denominator;
  4084. int upper, lower, cur_vit_mask;
  4085. int tmp, newVal;
  4086. int i;
  4087. int pilot_mask_reg[4] = { AR_PHY_TIMING7, AR_PHY_TIMING8,
  4088. AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
  4089. };
  4090. int chan_mask_reg[4] = { AR_PHY_TIMING9, AR_PHY_TIMING10,
  4091. AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
  4092. };
  4093. int inc[4] = { 0, 100, 0, 0 };
  4094. struct chan_centers centers;
  4095. int8_t mask_m[123];
  4096. int8_t mask_p[123];
  4097. int8_t mask_amt;
  4098. int tmp_mask;
  4099. int cur_bb_spur;
  4100. bool is2GHz = IS_CHAN_2GHZ(chan);
  4101. memset(&mask_m, 0, sizeof(int8_t) * 123);
  4102. memset(&mask_p, 0, sizeof(int8_t) * 123);
  4103. ath9k_hw_get_channel_centers(ah, chan, &centers);
  4104. freq = centers.synth_center;
  4105. ah->ah_config.spurmode = SPUR_ENABLE_EEPROM;
  4106. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  4107. cur_bb_spur = ath9k_hw_eeprom_get_spur_chan(ah, i, is2GHz);
  4108. if (is2GHz)
  4109. cur_bb_spur = (cur_bb_spur / 10) + AR_BASE_FREQ_2GHZ;
  4110. else
  4111. cur_bb_spur = (cur_bb_spur / 10) + AR_BASE_FREQ_5GHZ;
  4112. if (AR_NO_SPUR == cur_bb_spur)
  4113. break;
  4114. cur_bb_spur = cur_bb_spur - freq;
  4115. if (IS_CHAN_HT40(chan)) {
  4116. if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT40) &&
  4117. (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT40)) {
  4118. bb_spur = cur_bb_spur;
  4119. break;
  4120. }
  4121. } else if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT20) &&
  4122. (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT20)) {
  4123. bb_spur = cur_bb_spur;
  4124. break;
  4125. }
  4126. }
  4127. if (AR_NO_SPUR == bb_spur) {
  4128. REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK,
  4129. AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
  4130. return;
  4131. } else {
  4132. REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK,
  4133. AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
  4134. }
  4135. bin = bb_spur * 320;
  4136. tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
  4137. newVal = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
  4138. AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
  4139. AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
  4140. AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
  4141. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), newVal);
  4142. newVal = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
  4143. AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
  4144. AR_PHY_SPUR_REG_MASK_RATE_SELECT |
  4145. AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
  4146. SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
  4147. REG_WRITE(ah, AR_PHY_SPUR_REG, newVal);
  4148. if (IS_CHAN_HT40(chan)) {
  4149. if (bb_spur < 0) {
  4150. spur_subchannel_sd = 1;
  4151. bb_spur_off = bb_spur + 10;
  4152. } else {
  4153. spur_subchannel_sd = 0;
  4154. bb_spur_off = bb_spur - 10;
  4155. }
  4156. } else {
  4157. spur_subchannel_sd = 0;
  4158. bb_spur_off = bb_spur;
  4159. }
  4160. if (IS_CHAN_HT40(chan))
  4161. spur_delta_phase =
  4162. ((bb_spur * 262144) /
  4163. 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  4164. else
  4165. spur_delta_phase =
  4166. ((bb_spur * 524288) /
  4167. 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  4168. denominator = IS_CHAN_2GHZ(chan) ? 44 : 40;
  4169. spur_freq_sd = ((bb_spur_off * 2048) / denominator) & 0x3ff;
  4170. newVal = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
  4171. SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
  4172. SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
  4173. REG_WRITE(ah, AR_PHY_TIMING11, newVal);
  4174. newVal = spur_subchannel_sd << AR_PHY_SFCORR_SPUR_SUBCHNL_SD_S;
  4175. REG_WRITE(ah, AR_PHY_SFCORR_EXT, newVal);
  4176. cur_bin = -6000;
  4177. upper = bin + 100;
  4178. lower = bin - 100;
  4179. for (i = 0; i < 4; i++) {
  4180. int pilot_mask = 0;
  4181. int chan_mask = 0;
  4182. int bp = 0;
  4183. for (bp = 0; bp < 30; bp++) {
  4184. if ((cur_bin > lower) && (cur_bin < upper)) {
  4185. pilot_mask = pilot_mask | 0x1 << bp;
  4186. chan_mask = chan_mask | 0x1 << bp;
  4187. }
  4188. cur_bin += 100;
  4189. }
  4190. cur_bin += inc[i];
  4191. REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
  4192. REG_WRITE(ah, chan_mask_reg[i], chan_mask);
  4193. }
  4194. cur_vit_mask = 6100;
  4195. upper = bin + 120;
  4196. lower = bin - 120;
  4197. for (i = 0; i < 123; i++) {
  4198. if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
  4199. /* workaround for gcc bug #37014 */
  4200. volatile int tmp = abs(cur_vit_mask - bin);
  4201. if (tmp < 75)
  4202. mask_amt = 1;
  4203. else
  4204. mask_amt = 0;
  4205. if (cur_vit_mask < 0)
  4206. mask_m[abs(cur_vit_mask / 100)] = mask_amt;
  4207. else
  4208. mask_p[cur_vit_mask / 100] = mask_amt;
  4209. }
  4210. cur_vit_mask -= 100;
  4211. }
  4212. tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
  4213. | (mask_m[48] << 26) | (mask_m[49] << 24)
  4214. | (mask_m[50] << 22) | (mask_m[51] << 20)
  4215. | (mask_m[52] << 18) | (mask_m[53] << 16)
  4216. | (mask_m[54] << 14) | (mask_m[55] << 12)
  4217. | (mask_m[56] << 10) | (mask_m[57] << 8)
  4218. | (mask_m[58] << 6) | (mask_m[59] << 4)
  4219. | (mask_m[60] << 2) | (mask_m[61] << 0);
  4220. REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
  4221. REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
  4222. tmp_mask = (mask_m[31] << 28)
  4223. | (mask_m[32] << 26) | (mask_m[33] << 24)
  4224. | (mask_m[34] << 22) | (mask_m[35] << 20)
  4225. | (mask_m[36] << 18) | (mask_m[37] << 16)
  4226. | (mask_m[48] << 14) | (mask_m[39] << 12)
  4227. | (mask_m[40] << 10) | (mask_m[41] << 8)
  4228. | (mask_m[42] << 6) | (mask_m[43] << 4)
  4229. | (mask_m[44] << 2) | (mask_m[45] << 0);
  4230. REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
  4231. REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
  4232. tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
  4233. | (mask_m[18] << 26) | (mask_m[18] << 24)
  4234. | (mask_m[20] << 22) | (mask_m[20] << 20)
  4235. | (mask_m[22] << 18) | (mask_m[22] << 16)
  4236. | (mask_m[24] << 14) | (mask_m[24] << 12)
  4237. | (mask_m[25] << 10) | (mask_m[26] << 8)
  4238. | (mask_m[27] << 6) | (mask_m[28] << 4)
  4239. | (mask_m[29] << 2) | (mask_m[30] << 0);
  4240. REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
  4241. REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
  4242. tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
  4243. | (mask_m[2] << 26) | (mask_m[3] << 24)
  4244. | (mask_m[4] << 22) | (mask_m[5] << 20)
  4245. | (mask_m[6] << 18) | (mask_m[7] << 16)
  4246. | (mask_m[8] << 14) | (mask_m[9] << 12)
  4247. | (mask_m[10] << 10) | (mask_m[11] << 8)
  4248. | (mask_m[12] << 6) | (mask_m[13] << 4)
  4249. | (mask_m[14] << 2) | (mask_m[15] << 0);
  4250. REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
  4251. REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
  4252. tmp_mask = (mask_p[15] << 28)
  4253. | (mask_p[14] << 26) | (mask_p[13] << 24)
  4254. | (mask_p[12] << 22) | (mask_p[11] << 20)
  4255. | (mask_p[10] << 18) | (mask_p[9] << 16)
  4256. | (mask_p[8] << 14) | (mask_p[7] << 12)
  4257. | (mask_p[6] << 10) | (mask_p[5] << 8)
  4258. | (mask_p[4] << 6) | (mask_p[3] << 4)
  4259. | (mask_p[2] << 2) | (mask_p[1] << 0);
  4260. REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
  4261. REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
  4262. tmp_mask = (mask_p[30] << 28)
  4263. | (mask_p[29] << 26) | (mask_p[28] << 24)
  4264. | (mask_p[27] << 22) | (mask_p[26] << 20)
  4265. | (mask_p[25] << 18) | (mask_p[24] << 16)
  4266. | (mask_p[23] << 14) | (mask_p[22] << 12)
  4267. | (mask_p[21] << 10) | (mask_p[20] << 8)
  4268. | (mask_p[19] << 6) | (mask_p[18] << 4)
  4269. | (mask_p[17] << 2) | (mask_p[16] << 0);
  4270. REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
  4271. REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
  4272. tmp_mask = (mask_p[45] << 28)
  4273. | (mask_p[44] << 26) | (mask_p[43] << 24)
  4274. | (mask_p[42] << 22) | (mask_p[41] << 20)
  4275. | (mask_p[40] << 18) | (mask_p[39] << 16)
  4276. | (mask_p[38] << 14) | (mask_p[37] << 12)
  4277. | (mask_p[36] << 10) | (mask_p[35] << 8)
  4278. | (mask_p[34] << 6) | (mask_p[33] << 4)
  4279. | (mask_p[32] << 2) | (mask_p[31] << 0);
  4280. REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
  4281. REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
  4282. tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
  4283. | (mask_p[59] << 26) | (mask_p[58] << 24)
  4284. | (mask_p[57] << 22) | (mask_p[56] << 20)
  4285. | (mask_p[55] << 18) | (mask_p[54] << 16)
  4286. | (mask_p[53] << 14) | (mask_p[52] << 12)
  4287. | (mask_p[51] << 10) | (mask_p[50] << 8)
  4288. | (mask_p[49] << 6) | (mask_p[48] << 4)
  4289. | (mask_p[47] << 2) | (mask_p[46] << 0);
  4290. REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
  4291. REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
  4292. }
  4293. static void ath9k_hw_spur_mitigate(struct ath_hal *ah,
  4294. struct ath9k_channel *chan)
  4295. {
  4296. int bb_spur = AR_NO_SPUR;
  4297. int bin, cur_bin;
  4298. int spur_freq_sd;
  4299. int spur_delta_phase;
  4300. int denominator;
  4301. int upper, lower, cur_vit_mask;
  4302. int tmp, new;
  4303. int i;
  4304. int pilot_mask_reg[4] = { AR_PHY_TIMING7, AR_PHY_TIMING8,
  4305. AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
  4306. };
  4307. int chan_mask_reg[4] = { AR_PHY_TIMING9, AR_PHY_TIMING10,
  4308. AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
  4309. };
  4310. int inc[4] = { 0, 100, 0, 0 };
  4311. int8_t mask_m[123];
  4312. int8_t mask_p[123];
  4313. int8_t mask_amt;
  4314. int tmp_mask;
  4315. int cur_bb_spur;
  4316. bool is2GHz = IS_CHAN_2GHZ(chan);
  4317. memset(&mask_m, 0, sizeof(int8_t) * 123);
  4318. memset(&mask_p, 0, sizeof(int8_t) * 123);
  4319. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  4320. cur_bb_spur = ath9k_hw_eeprom_get_spur_chan(ah, i, is2GHz);
  4321. if (AR_NO_SPUR == cur_bb_spur)
  4322. break;
  4323. cur_bb_spur = cur_bb_spur - (chan->channel * 10);
  4324. if ((cur_bb_spur > -95) && (cur_bb_spur < 95)) {
  4325. bb_spur = cur_bb_spur;
  4326. break;
  4327. }
  4328. }
  4329. if (AR_NO_SPUR == bb_spur)
  4330. return;
  4331. bin = bb_spur * 32;
  4332. tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
  4333. new = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
  4334. AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
  4335. AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
  4336. AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
  4337. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), new);
  4338. new = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
  4339. AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
  4340. AR_PHY_SPUR_REG_MASK_RATE_SELECT |
  4341. AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
  4342. SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
  4343. REG_WRITE(ah, AR_PHY_SPUR_REG, new);
  4344. spur_delta_phase = ((bb_spur * 524288) / 100) &
  4345. AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  4346. denominator = IS_CHAN_2GHZ(chan) ? 440 : 400;
  4347. spur_freq_sd = ((bb_spur * 2048) / denominator) & 0x3ff;
  4348. new = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
  4349. SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
  4350. SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
  4351. REG_WRITE(ah, AR_PHY_TIMING11, new);
  4352. cur_bin = -6000;
  4353. upper = bin + 100;
  4354. lower = bin - 100;
  4355. for (i = 0; i < 4; i++) {
  4356. int pilot_mask = 0;
  4357. int chan_mask = 0;
  4358. int bp = 0;
  4359. for (bp = 0; bp < 30; bp++) {
  4360. if ((cur_bin > lower) && (cur_bin < upper)) {
  4361. pilot_mask = pilot_mask | 0x1 << bp;
  4362. chan_mask = chan_mask | 0x1 << bp;
  4363. }
  4364. cur_bin += 100;
  4365. }
  4366. cur_bin += inc[i];
  4367. REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
  4368. REG_WRITE(ah, chan_mask_reg[i], chan_mask);
  4369. }
  4370. cur_vit_mask = 6100;
  4371. upper = bin + 120;
  4372. lower = bin - 120;
  4373. for (i = 0; i < 123; i++) {
  4374. if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
  4375. /* workaround for gcc bug #37014 */
  4376. volatile int tmp = abs(cur_vit_mask - bin);
  4377. if (tmp < 75)
  4378. mask_amt = 1;
  4379. else
  4380. mask_amt = 0;
  4381. if (cur_vit_mask < 0)
  4382. mask_m[abs(cur_vit_mask / 100)] = mask_amt;
  4383. else
  4384. mask_p[cur_vit_mask / 100] = mask_amt;
  4385. }
  4386. cur_vit_mask -= 100;
  4387. }
  4388. tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
  4389. | (mask_m[48] << 26) | (mask_m[49] << 24)
  4390. | (mask_m[50] << 22) | (mask_m[51] << 20)
  4391. | (mask_m[52] << 18) | (mask_m[53] << 16)
  4392. | (mask_m[54] << 14) | (mask_m[55] << 12)
  4393. | (mask_m[56] << 10) | (mask_m[57] << 8)
  4394. | (mask_m[58] << 6) | (mask_m[59] << 4)
  4395. | (mask_m[60] << 2) | (mask_m[61] << 0);
  4396. REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
  4397. REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
  4398. tmp_mask = (mask_m[31] << 28)
  4399. | (mask_m[32] << 26) | (mask_m[33] << 24)
  4400. | (mask_m[34] << 22) | (mask_m[35] << 20)
  4401. | (mask_m[36] << 18) | (mask_m[37] << 16)
  4402. | (mask_m[48] << 14) | (mask_m[39] << 12)
  4403. | (mask_m[40] << 10) | (mask_m[41] << 8)
  4404. | (mask_m[42] << 6) | (mask_m[43] << 4)
  4405. | (mask_m[44] << 2) | (mask_m[45] << 0);
  4406. REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
  4407. REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
  4408. tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
  4409. | (mask_m[18] << 26) | (mask_m[18] << 24)
  4410. | (mask_m[20] << 22) | (mask_m[20] << 20)
  4411. | (mask_m[22] << 18) | (mask_m[22] << 16)
  4412. | (mask_m[24] << 14) | (mask_m[24] << 12)
  4413. | (mask_m[25] << 10) | (mask_m[26] << 8)
  4414. | (mask_m[27] << 6) | (mask_m[28] << 4)
  4415. | (mask_m[29] << 2) | (mask_m[30] << 0);
  4416. REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
  4417. REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
  4418. tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
  4419. | (mask_m[2] << 26) | (mask_m[3] << 24)
  4420. | (mask_m[4] << 22) | (mask_m[5] << 20)
  4421. | (mask_m[6] << 18) | (mask_m[7] << 16)
  4422. | (mask_m[8] << 14) | (mask_m[9] << 12)
  4423. | (mask_m[10] << 10) | (mask_m[11] << 8)
  4424. | (mask_m[12] << 6) | (mask_m[13] << 4)
  4425. | (mask_m[14] << 2) | (mask_m[15] << 0);
  4426. REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
  4427. REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
  4428. tmp_mask = (mask_p[15] << 28)
  4429. | (mask_p[14] << 26) | (mask_p[13] << 24)
  4430. | (mask_p[12] << 22) | (mask_p[11] << 20)
  4431. | (mask_p[10] << 18) | (mask_p[9] << 16)
  4432. | (mask_p[8] << 14) | (mask_p[7] << 12)
  4433. | (mask_p[6] << 10) | (mask_p[5] << 8)
  4434. | (mask_p[4] << 6) | (mask_p[3] << 4)
  4435. | (mask_p[2] << 2) | (mask_p[1] << 0);
  4436. REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
  4437. REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
  4438. tmp_mask = (mask_p[30] << 28)
  4439. | (mask_p[29] << 26) | (mask_p[28] << 24)
  4440. | (mask_p[27] << 22) | (mask_p[26] << 20)
  4441. | (mask_p[25] << 18) | (mask_p[24] << 16)
  4442. | (mask_p[23] << 14) | (mask_p[22] << 12)
  4443. | (mask_p[21] << 10) | (mask_p[20] << 8)
  4444. | (mask_p[19] << 6) | (mask_p[18] << 4)
  4445. | (mask_p[17] << 2) | (mask_p[16] << 0);
  4446. REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
  4447. REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
  4448. tmp_mask = (mask_p[45] << 28)
  4449. | (mask_p[44] << 26) | (mask_p[43] << 24)
  4450. | (mask_p[42] << 22) | (mask_p[41] << 20)
  4451. | (mask_p[40] << 18) | (mask_p[39] << 16)
  4452. | (mask_p[38] << 14) | (mask_p[37] << 12)
  4453. | (mask_p[36] << 10) | (mask_p[35] << 8)
  4454. | (mask_p[34] << 6) | (mask_p[33] << 4)
  4455. | (mask_p[32] << 2) | (mask_p[31] << 0);
  4456. REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
  4457. REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
  4458. tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
  4459. | (mask_p[59] << 26) | (mask_p[58] << 24)
  4460. | (mask_p[57] << 22) | (mask_p[56] << 20)
  4461. | (mask_p[55] << 18) | (mask_p[54] << 16)
  4462. | (mask_p[53] << 14) | (mask_p[52] << 12)
  4463. | (mask_p[51] << 10) | (mask_p[50] << 8)
  4464. | (mask_p[49] << 6) | (mask_p[48] << 4)
  4465. | (mask_p[47] << 2) | (mask_p[46] << 0);
  4466. REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
  4467. REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
  4468. }
  4469. static void ath9k_hw_init_chain_masks(struct ath_hal *ah)
  4470. {
  4471. struct ath_hal_5416 *ahp = AH5416(ah);
  4472. int rx_chainmask, tx_chainmask;
  4473. rx_chainmask = ahp->ah_rxchainmask;
  4474. tx_chainmask = ahp->ah_txchainmask;
  4475. switch (rx_chainmask) {
  4476. case 0x5:
  4477. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  4478. AR_PHY_SWAP_ALT_CHAIN);
  4479. case 0x3:
  4480. if (((ah)->ah_macVersion <= AR_SREV_VERSION_9160)) {
  4481. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, 0x7);
  4482. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, 0x7);
  4483. break;
  4484. }
  4485. case 0x1:
  4486. case 0x2:
  4487. if (!AR_SREV_9280(ah))
  4488. break;
  4489. case 0x7:
  4490. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
  4491. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
  4492. break;
  4493. default:
  4494. break;
  4495. }
  4496. REG_WRITE(ah, AR_SELFGEN_MASK, tx_chainmask);
  4497. if (tx_chainmask == 0x5) {
  4498. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  4499. AR_PHY_SWAP_ALT_CHAIN);
  4500. }
  4501. if (AR_SREV_9100(ah))
  4502. REG_WRITE(ah, AR_PHY_ANALOG_SWAP,
  4503. REG_READ(ah, AR_PHY_ANALOG_SWAP) | 0x00000001);
  4504. }
  4505. static void ath9k_hw_set_addac(struct ath_hal *ah,
  4506. struct ath9k_channel *chan)
  4507. {
  4508. struct modal_eep_header *pModal;
  4509. struct ath_hal_5416 *ahp = AH5416(ah);
  4510. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  4511. u8 biaslevel;
  4512. if (ah->ah_macVersion != AR_SREV_VERSION_9160)
  4513. return;
  4514. if (ar5416_get_eep_rev(ahp) < AR5416_EEP_MINOR_VER_7)
  4515. return;
  4516. pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  4517. if (pModal->xpaBiasLvl != 0xff) {
  4518. biaslevel = pModal->xpaBiasLvl;
  4519. } else {
  4520. u16 resetFreqBin, freqBin, freqCount = 0;
  4521. struct chan_centers centers;
  4522. ath9k_hw_get_channel_centers(ah, chan, &centers);
  4523. resetFreqBin =
  4524. FREQ2FBIN(centers.synth_center, IS_CHAN_2GHZ(chan));
  4525. freqBin = pModal->xpaBiasLvlFreq[0] & 0xff;
  4526. biaslevel = (u8) (pModal->xpaBiasLvlFreq[0] >> 14);
  4527. freqCount++;
  4528. while (freqCount < 3) {
  4529. if (pModal->xpaBiasLvlFreq[freqCount] == 0x0)
  4530. break;
  4531. freqBin = pModal->xpaBiasLvlFreq[freqCount] & 0xff;
  4532. if (resetFreqBin >= freqBin) {
  4533. biaslevel =
  4534. (u8) (pModal->
  4535. xpaBiasLvlFreq[freqCount]
  4536. >> 14);
  4537. } else {
  4538. break;
  4539. }
  4540. freqCount++;
  4541. }
  4542. }
  4543. if (IS_CHAN_2GHZ(chan)) {
  4544. INI_RA(&ahp->ah_iniAddac, 7, 1) =
  4545. (INI_RA(&ahp->ah_iniAddac, 7, 1) & (~0x18)) | biaslevel
  4546. << 3;
  4547. } else {
  4548. INI_RA(&ahp->ah_iniAddac, 6, 1) =
  4549. (INI_RA(&ahp->ah_iniAddac, 6, 1) & (~0xc0)) | biaslevel
  4550. << 6;
  4551. }
  4552. }
  4553. static u32 ath9k_hw_mac_usec(struct ath_hal *ah, u32 clks)
  4554. {
  4555. if (ah->ah_curchan != NULL)
  4556. return clks /
  4557. CLOCK_RATE[ath9k_hw_chan2wmode(ah, ah->ah_curchan)];
  4558. else
  4559. return clks / CLOCK_RATE[ATH9K_MODE_11B];
  4560. }
  4561. static u32 ath9k_hw_mac_to_usec(struct ath_hal *ah, u32 clks)
  4562. {
  4563. struct ath9k_channel *chan = ah->ah_curchan;
  4564. if (chan && IS_CHAN_HT40(chan))
  4565. return ath9k_hw_mac_usec(ah, clks) / 2;
  4566. else
  4567. return ath9k_hw_mac_usec(ah, clks);
  4568. }
  4569. static u32 ath9k_hw_mac_clks(struct ath_hal *ah, u32 usecs)
  4570. {
  4571. if (ah->ah_curchan != NULL)
  4572. return usecs * CLOCK_RATE[ath9k_hw_chan2wmode(ah,
  4573. ah->ah_curchan)];
  4574. else
  4575. return usecs * CLOCK_RATE[ATH9K_MODE_11B];
  4576. }
  4577. static u32 ath9k_hw_mac_to_clks(struct ath_hal *ah, u32 usecs)
  4578. {
  4579. struct ath9k_channel *chan = ah->ah_curchan;
  4580. if (chan && IS_CHAN_HT40(chan))
  4581. return ath9k_hw_mac_clks(ah, usecs) * 2;
  4582. else
  4583. return ath9k_hw_mac_clks(ah, usecs);
  4584. }
  4585. static bool ath9k_hw_set_ack_timeout(struct ath_hal *ah, u32 us)
  4586. {
  4587. struct ath_hal_5416 *ahp = AH5416(ah);
  4588. if (us > ath9k_hw_mac_to_usec(ah, MS(0xffffffff, AR_TIME_OUT_ACK))) {
  4589. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: bad ack timeout %u\n",
  4590. __func__, us);
  4591. ahp->ah_acktimeout = (u32) -1;
  4592. return false;
  4593. } else {
  4594. REG_RMW_FIELD(ah, AR_TIME_OUT,
  4595. AR_TIME_OUT_ACK, ath9k_hw_mac_to_clks(ah, us));
  4596. ahp->ah_acktimeout = us;
  4597. return true;
  4598. }
  4599. }
  4600. static bool ath9k_hw_set_cts_timeout(struct ath_hal *ah, u32 us)
  4601. {
  4602. struct ath_hal_5416 *ahp = AH5416(ah);
  4603. if (us > ath9k_hw_mac_to_usec(ah, MS(0xffffffff, AR_TIME_OUT_CTS))) {
  4604. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: bad cts timeout %u\n",
  4605. __func__, us);
  4606. ahp->ah_ctstimeout = (u32) -1;
  4607. return false;
  4608. } else {
  4609. REG_RMW_FIELD(ah, AR_TIME_OUT,
  4610. AR_TIME_OUT_CTS, ath9k_hw_mac_to_clks(ah, us));
  4611. ahp->ah_ctstimeout = us;
  4612. return true;
  4613. }
  4614. }
  4615. static bool ath9k_hw_set_global_txtimeout(struct ath_hal *ah,
  4616. u32 tu)
  4617. {
  4618. struct ath_hal_5416 *ahp = AH5416(ah);
  4619. if (tu > 0xFFFF) {
  4620. DPRINTF(ah->ah_sc, ATH_DBG_XMIT,
  4621. "%s: bad global tx timeout %u\n", __func__, tu);
  4622. ahp->ah_globaltxtimeout = (u32) -1;
  4623. return false;
  4624. } else {
  4625. REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
  4626. ahp->ah_globaltxtimeout = tu;
  4627. return true;
  4628. }
  4629. }
  4630. bool ath9k_hw_setslottime(struct ath_hal *ah, u32 us)
  4631. {
  4632. struct ath_hal_5416 *ahp = AH5416(ah);
  4633. if (us < ATH9K_SLOT_TIME_9 || us > ath9k_hw_mac_to_usec(ah, 0xffff)) {
  4634. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: bad slot time %u\n",
  4635. __func__, us);
  4636. ahp->ah_slottime = (u32) -1;
  4637. return false;
  4638. } else {
  4639. REG_WRITE(ah, AR_D_GBL_IFS_SLOT, ath9k_hw_mac_to_clks(ah, us));
  4640. ahp->ah_slottime = us;
  4641. return true;
  4642. }
  4643. }
  4644. static void ath9k_hw_init_user_settings(struct ath_hal *ah)
  4645. {
  4646. struct ath_hal_5416 *ahp = AH5416(ah);
  4647. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "--AP %s ahp->ah_miscMode 0x%x\n",
  4648. __func__, ahp->ah_miscMode);
  4649. if (ahp->ah_miscMode != 0)
  4650. REG_WRITE(ah, AR_PCU_MISC,
  4651. REG_READ(ah, AR_PCU_MISC) | ahp->ah_miscMode);
  4652. if (ahp->ah_slottime != (u32) -1)
  4653. ath9k_hw_setslottime(ah, ahp->ah_slottime);
  4654. if (ahp->ah_acktimeout != (u32) -1)
  4655. ath9k_hw_set_ack_timeout(ah, ahp->ah_acktimeout);
  4656. if (ahp->ah_ctstimeout != (u32) -1)
  4657. ath9k_hw_set_cts_timeout(ah, ahp->ah_ctstimeout);
  4658. if (ahp->ah_globaltxtimeout != (u32) -1)
  4659. ath9k_hw_set_global_txtimeout(ah, ahp->ah_globaltxtimeout);
  4660. }
  4661. static int
  4662. ath9k_hw_process_ini(struct ath_hal *ah,
  4663. struct ath9k_channel *chan,
  4664. enum ath9k_ht_macmode macmode)
  4665. {
  4666. int i, regWrites = 0;
  4667. struct ath_hal_5416 *ahp = AH5416(ah);
  4668. u32 modesIndex, freqIndex;
  4669. int status;
  4670. switch (chan->chanmode) {
  4671. case CHANNEL_A:
  4672. case CHANNEL_A_HT20:
  4673. modesIndex = 1;
  4674. freqIndex = 1;
  4675. break;
  4676. case CHANNEL_A_HT40PLUS:
  4677. case CHANNEL_A_HT40MINUS:
  4678. modesIndex = 2;
  4679. freqIndex = 1;
  4680. break;
  4681. case CHANNEL_G:
  4682. case CHANNEL_G_HT20:
  4683. case CHANNEL_B:
  4684. modesIndex = 4;
  4685. freqIndex = 2;
  4686. break;
  4687. case CHANNEL_G_HT40PLUS:
  4688. case CHANNEL_G_HT40MINUS:
  4689. modesIndex = 3;
  4690. freqIndex = 2;
  4691. break;
  4692. default:
  4693. return -EINVAL;
  4694. }
  4695. REG_WRITE(ah, AR_PHY(0), 0x00000007);
  4696. REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
  4697. ath9k_hw_set_addac(ah, chan);
  4698. if (AR_SREV_5416_V22_OR_LATER(ah)) {
  4699. REG_WRITE_ARRAY(&ahp->ah_iniAddac, 1, regWrites);
  4700. } else {
  4701. struct ar5416IniArray temp;
  4702. u32 addacSize =
  4703. sizeof(u32) * ahp->ah_iniAddac.ia_rows *
  4704. ahp->ah_iniAddac.ia_columns;
  4705. memcpy(ahp->ah_addac5416_21,
  4706. ahp->ah_iniAddac.ia_array, addacSize);
  4707. (ahp->ah_addac5416_21)[31 *
  4708. ahp->ah_iniAddac.ia_columns + 1] = 0;
  4709. temp.ia_array = ahp->ah_addac5416_21;
  4710. temp.ia_columns = ahp->ah_iniAddac.ia_columns;
  4711. temp.ia_rows = ahp->ah_iniAddac.ia_rows;
  4712. REG_WRITE_ARRAY(&temp, 1, regWrites);
  4713. }
  4714. REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
  4715. for (i = 0; i < ahp->ah_iniModes.ia_rows; i++) {
  4716. u32 reg = INI_RA(&ahp->ah_iniModes, i, 0);
  4717. u32 val = INI_RA(&ahp->ah_iniModes, i, modesIndex);
  4718. #ifdef CONFIG_SLOW_ANT_DIV
  4719. if (ah->ah_devid == AR9280_DEVID_PCI)
  4720. val = ath9k_hw_ini_fixup(ah, &ahp->ah_eeprom, reg,
  4721. val);
  4722. #endif
  4723. REG_WRITE(ah, reg, val);
  4724. if (reg >= 0x7800 && reg < 0x78a0
  4725. && ah->ah_config.analog_shiftreg) {
  4726. udelay(100);
  4727. }
  4728. DO_DELAY(regWrites);
  4729. }
  4730. for (i = 0; i < ahp->ah_iniCommon.ia_rows; i++) {
  4731. u32 reg = INI_RA(&ahp->ah_iniCommon, i, 0);
  4732. u32 val = INI_RA(&ahp->ah_iniCommon, i, 1);
  4733. REG_WRITE(ah, reg, val);
  4734. if (reg >= 0x7800 && reg < 0x78a0
  4735. && ah->ah_config.analog_shiftreg) {
  4736. udelay(100);
  4737. }
  4738. DO_DELAY(regWrites);
  4739. }
  4740. ath9k_hw_write_regs(ah, modesIndex, freqIndex, regWrites);
  4741. if (AR_SREV_9280_20(ah) && IS_CHAN_A_5MHZ_SPACED(chan)) {
  4742. REG_WRITE_ARRAY(&ahp->ah_iniModesAdditional, modesIndex,
  4743. regWrites);
  4744. }
  4745. ath9k_hw_override_ini(ah, chan);
  4746. ath9k_hw_set_regs(ah, chan, macmode);
  4747. ath9k_hw_init_chain_masks(ah);
  4748. status = ath9k_hw_set_txpower(ah, &ahp->ah_eeprom, chan,
  4749. ath9k_regd_get_ctl(ah, chan),
  4750. ath9k_regd_get_antenna_allowed(ah,
  4751. chan),
  4752. chan->maxRegTxPower * 2,
  4753. min((u32) MAX_RATE_POWER,
  4754. (u32) ah->ah_powerLimit));
  4755. if (status != 0) {
  4756. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  4757. "%s: error init'ing transmit power\n", __func__);
  4758. return -EIO;
  4759. }
  4760. if (!ath9k_hw_set_rf_regs(ah, chan, freqIndex)) {
  4761. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  4762. "%s: ar5416SetRfRegs failed\n", __func__);
  4763. return -EIO;
  4764. }
  4765. return 0;
  4766. }
  4767. static void ath9k_hw_setup_calibration(struct ath_hal *ah,
  4768. struct hal_cal_list *currCal)
  4769. {
  4770. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(0),
  4771. AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
  4772. currCal->calData->calCountMax);
  4773. switch (currCal->calData->calType) {
  4774. case IQ_MISMATCH_CAL:
  4775. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  4776. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4777. "%s: starting IQ Mismatch Calibration\n",
  4778. __func__);
  4779. break;
  4780. case ADC_GAIN_CAL:
  4781. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
  4782. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4783. "%s: starting ADC Gain Calibration\n", __func__);
  4784. break;
  4785. case ADC_DC_CAL:
  4786. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
  4787. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4788. "%s: starting ADC DC Calibration\n", __func__);
  4789. break;
  4790. case ADC_DC_INIT_CAL:
  4791. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
  4792. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4793. "%s: starting Init ADC DC Calibration\n",
  4794. __func__);
  4795. break;
  4796. }
  4797. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  4798. AR_PHY_TIMING_CTRL4_DO_CAL);
  4799. }
  4800. static void ath9k_hw_reset_calibration(struct ath_hal *ah,
  4801. struct hal_cal_list *currCal)
  4802. {
  4803. struct ath_hal_5416 *ahp = AH5416(ah);
  4804. int i;
  4805. ath9k_hw_setup_calibration(ah, currCal);
  4806. currCal->calState = CAL_RUNNING;
  4807. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  4808. ahp->ah_Meas0.sign[i] = 0;
  4809. ahp->ah_Meas1.sign[i] = 0;
  4810. ahp->ah_Meas2.sign[i] = 0;
  4811. ahp->ah_Meas3.sign[i] = 0;
  4812. }
  4813. ahp->ah_CalSamples = 0;
  4814. }
  4815. static void
  4816. ath9k_hw_per_calibration(struct ath_hal *ah,
  4817. struct ath9k_channel *ichan,
  4818. u8 rxchainmask,
  4819. struct hal_cal_list *currCal,
  4820. bool *isCalDone)
  4821. {
  4822. struct ath_hal_5416 *ahp = AH5416(ah);
  4823. *isCalDone = false;
  4824. if (currCal->calState == CAL_RUNNING) {
  4825. if (!(REG_READ(ah,
  4826. AR_PHY_TIMING_CTRL4(0)) &
  4827. AR_PHY_TIMING_CTRL4_DO_CAL)) {
  4828. currCal->calData->calCollect(ah);
  4829. ahp->ah_CalSamples++;
  4830. if (ahp->ah_CalSamples >=
  4831. currCal->calData->calNumSamples) {
  4832. int i, numChains = 0;
  4833. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  4834. if (rxchainmask & (1 << i))
  4835. numChains++;
  4836. }
  4837. currCal->calData->calPostProc(ah,
  4838. numChains);
  4839. ichan->CalValid |=
  4840. currCal->calData->calType;
  4841. currCal->calState = CAL_DONE;
  4842. *isCalDone = true;
  4843. } else {
  4844. ath9k_hw_setup_calibration(ah, currCal);
  4845. }
  4846. }
  4847. } else if (!(ichan->CalValid & currCal->calData->calType)) {
  4848. ath9k_hw_reset_calibration(ah, currCal);
  4849. }
  4850. }
  4851. static inline bool ath9k_hw_run_init_cals(struct ath_hal *ah,
  4852. int init_cal_count)
  4853. {
  4854. struct ath_hal_5416 *ahp = AH5416(ah);
  4855. struct ath9k_channel ichan;
  4856. bool isCalDone;
  4857. struct hal_cal_list *currCal = ahp->ah_cal_list_curr;
  4858. const struct hal_percal_data *calData = currCal->calData;
  4859. int i;
  4860. if (currCal == NULL)
  4861. return false;
  4862. ichan.CalValid = 0;
  4863. for (i = 0; i < init_cal_count; i++) {
  4864. ath9k_hw_reset_calibration(ah, currCal);
  4865. if (!ath9k_hw_wait(ah, AR_PHY_TIMING_CTRL4(0),
  4866. AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
  4867. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4868. "%s: Cal %d failed to complete in 100ms.\n",
  4869. __func__, calData->calType);
  4870. ahp->ah_cal_list = ahp->ah_cal_list_last =
  4871. ahp->ah_cal_list_curr = NULL;
  4872. return false;
  4873. }
  4874. ath9k_hw_per_calibration(ah, &ichan, ahp->ah_rxchainmask,
  4875. currCal, &isCalDone);
  4876. if (!isCalDone) {
  4877. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4878. "%s: Not able to run Init Cal %d.\n",
  4879. __func__, calData->calType);
  4880. }
  4881. if (currCal->calNext) {
  4882. currCal = currCal->calNext;
  4883. calData = currCal->calData;
  4884. }
  4885. }
  4886. ahp->ah_cal_list = ahp->ah_cal_list_last = ahp->ah_cal_list_curr = NULL;
  4887. return true;
  4888. }
  4889. static bool
  4890. ath9k_hw_channel_change(struct ath_hal *ah,
  4891. struct ath9k_channel *chan,
  4892. enum ath9k_ht_macmode macmode)
  4893. {
  4894. u32 synthDelay, qnum;
  4895. struct ath_hal_5416 *ahp = AH5416(ah);
  4896. for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
  4897. if (ath9k_hw_numtxpending(ah, qnum)) {
  4898. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  4899. "%s: Transmit frames pending on queue %d\n",
  4900. __func__, qnum);
  4901. return false;
  4902. }
  4903. }
  4904. REG_WRITE(ah, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
  4905. if (!ath9k_hw_wait(ah, AR_PHY_RFBUS_GRANT, AR_PHY_RFBUS_GRANT_EN,
  4906. AR_PHY_RFBUS_GRANT_EN)) {
  4907. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  4908. "%s: Could not kill baseband RX\n", __func__);
  4909. return false;
  4910. }
  4911. ath9k_hw_set_regs(ah, chan, macmode);
  4912. if (AR_SREV_9280_10_OR_LATER(ah)) {
  4913. if (!(ath9k_hw_ar9280_set_channel(ah, chan))) {
  4914. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  4915. "%s: failed to set channel\n", __func__);
  4916. return false;
  4917. }
  4918. } else {
  4919. if (!(ath9k_hw_set_channel(ah, chan))) {
  4920. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  4921. "%s: failed to set channel\n", __func__);
  4922. return false;
  4923. }
  4924. }
  4925. if (ath9k_hw_set_txpower(ah, &ahp->ah_eeprom, chan,
  4926. ath9k_regd_get_ctl(ah, chan),
  4927. ath9k_regd_get_antenna_allowed(ah, chan),
  4928. chan->maxRegTxPower * 2,
  4929. min((u32) MAX_RATE_POWER,
  4930. (u32) ah->ah_powerLimit)) != 0) {
  4931. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  4932. "%s: error init'ing transmit power\n", __func__);
  4933. return false;
  4934. }
  4935. synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  4936. if (IS_CHAN_CCK(chan))
  4937. synthDelay = (4 * synthDelay) / 22;
  4938. else
  4939. synthDelay /= 10;
  4940. udelay(synthDelay + BASE_ACTIVATE_DELAY);
  4941. REG_WRITE(ah, AR_PHY_RFBUS_REQ, 0);
  4942. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  4943. ath9k_hw_set_delta_slope(ah, chan);
  4944. if (AR_SREV_9280_10_OR_LATER(ah))
  4945. ath9k_hw_9280_spur_mitigate(ah, chan);
  4946. else
  4947. ath9k_hw_spur_mitigate(ah, chan);
  4948. if (!chan->oneTimeCalsDone)
  4949. chan->oneTimeCalsDone = true;
  4950. return true;
  4951. }
  4952. static bool ath9k_hw_chip_reset(struct ath_hal *ah,
  4953. struct ath9k_channel *chan)
  4954. {
  4955. struct ath_hal_5416 *ahp = AH5416(ah);
  4956. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  4957. return false;
  4958. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  4959. return false;
  4960. ahp->ah_chipFullSleep = false;
  4961. ath9k_hw_init_pll(ah, chan);
  4962. ath9k_hw_set_rfmode(ah, chan);
  4963. return true;
  4964. }
  4965. static inline void ath9k_hw_set_dma(struct ath_hal *ah)
  4966. {
  4967. u32 regval;
  4968. regval = REG_READ(ah, AR_AHB_MODE);
  4969. REG_WRITE(ah, AR_AHB_MODE, regval | AR_AHB_PREFETCH_RD_EN);
  4970. regval = REG_READ(ah, AR_TXCFG) & ~AR_TXCFG_DMASZ_MASK;
  4971. REG_WRITE(ah, AR_TXCFG, regval | AR_TXCFG_DMASZ_128B);
  4972. REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->ah_txTrigLevel);
  4973. regval = REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_DMASZ_MASK;
  4974. REG_WRITE(ah, AR_RXCFG, regval | AR_RXCFG_DMASZ_128B);
  4975. REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
  4976. if (AR_SREV_9285(ah)) {
  4977. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  4978. AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
  4979. } else {
  4980. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  4981. AR_PCU_TXBUF_CTRL_USABLE_SIZE);
  4982. }
  4983. }
  4984. bool ath9k_hw_stopdmarecv(struct ath_hal *ah)
  4985. {
  4986. REG_WRITE(ah, AR_CR, AR_CR_RXD);
  4987. if (!ath9k_hw_wait(ah, AR_CR, AR_CR_RXE, 0)) {
  4988. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  4989. "%s: dma failed to stop in 10ms\n"
  4990. "AR_CR=0x%08x\nAR_DIAG_SW=0x%08x\n",
  4991. __func__,
  4992. REG_READ(ah, AR_CR), REG_READ(ah, AR_DIAG_SW));
  4993. return false;
  4994. } else {
  4995. return true;
  4996. }
  4997. }
  4998. void ath9k_hw_startpcureceive(struct ath_hal *ah)
  4999. {
  5000. REG_CLR_BIT(ah, AR_DIAG_SW,
  5001. (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  5002. ath9k_enable_mib_counters(ah);
  5003. ath9k_ani_reset(ah);
  5004. }
  5005. void ath9k_hw_stoppcurecv(struct ath_hal *ah)
  5006. {
  5007. REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
  5008. ath9k_hw_disable_mib_counters(ah);
  5009. }
  5010. static bool ath9k_hw_iscal_supported(struct ath_hal *ah,
  5011. struct ath9k_channel *chan,
  5012. enum hal_cal_types calType)
  5013. {
  5014. struct ath_hal_5416 *ahp = AH5416(ah);
  5015. bool retval = false;
  5016. switch (calType & ahp->ah_suppCals) {
  5017. case IQ_MISMATCH_CAL:
  5018. if (!IS_CHAN_B(chan))
  5019. retval = true;
  5020. break;
  5021. case ADC_GAIN_CAL:
  5022. case ADC_DC_CAL:
  5023. if (!IS_CHAN_B(chan)
  5024. && !(IS_CHAN_2GHZ(chan) && IS_CHAN_HT20(chan)))
  5025. retval = true;
  5026. break;
  5027. }
  5028. return retval;
  5029. }
  5030. static bool ath9k_hw_init_cal(struct ath_hal *ah,
  5031. struct ath9k_channel *chan)
  5032. {
  5033. struct ath_hal_5416 *ahp = AH5416(ah);
  5034. struct ath9k_channel *ichan =
  5035. ath9k_regd_check_channel(ah, chan);
  5036. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  5037. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  5038. AR_PHY_AGC_CONTROL_CAL);
  5039. if (!ath9k_hw_wait
  5040. (ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
  5041. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5042. "%s: offset calibration failed to complete in 1ms; "
  5043. "noisy environment?\n", __func__);
  5044. return false;
  5045. }
  5046. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  5047. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  5048. AR_PHY_AGC_CONTROL_NF);
  5049. ahp->ah_cal_list = ahp->ah_cal_list_last = ahp->ah_cal_list_curr =
  5050. NULL;
  5051. if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah)) {
  5052. if (ath9k_hw_iscal_supported(ah, chan, ADC_GAIN_CAL)) {
  5053. INIT_CAL(&ahp->ah_adcGainCalData);
  5054. INSERT_CAL(ahp, &ahp->ah_adcGainCalData);
  5055. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5056. "%s: enabling ADC Gain Calibration.\n",
  5057. __func__);
  5058. }
  5059. if (ath9k_hw_iscal_supported(ah, chan, ADC_DC_CAL)) {
  5060. INIT_CAL(&ahp->ah_adcDcCalData);
  5061. INSERT_CAL(ahp, &ahp->ah_adcDcCalData);
  5062. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5063. "%s: enabling ADC DC Calibration.\n",
  5064. __func__);
  5065. }
  5066. if (ath9k_hw_iscal_supported(ah, chan, IQ_MISMATCH_CAL)) {
  5067. INIT_CAL(&ahp->ah_iqCalData);
  5068. INSERT_CAL(ahp, &ahp->ah_iqCalData);
  5069. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5070. "%s: enabling IQ Calibration.\n",
  5071. __func__);
  5072. }
  5073. ahp->ah_cal_list_curr = ahp->ah_cal_list;
  5074. if (ahp->ah_cal_list_curr)
  5075. ath9k_hw_reset_calibration(ah,
  5076. ahp->ah_cal_list_curr);
  5077. }
  5078. ichan->CalValid = 0;
  5079. return true;
  5080. }
  5081. bool ath9k_hw_reset(struct ath_hal *ah,
  5082. struct ath9k_channel *chan,
  5083. enum ath9k_ht_macmode macmode,
  5084. u8 txchainmask, u8 rxchainmask,
  5085. enum ath9k_ht_extprotspacing extprotspacing,
  5086. bool bChannelChange,
  5087. int *status)
  5088. {
  5089. u32 saveLedState;
  5090. struct ath_hal_5416 *ahp = AH5416(ah);
  5091. struct ath9k_channel *curchan = ah->ah_curchan;
  5092. u32 saveDefAntenna;
  5093. u32 macStaId1;
  5094. int ecode;
  5095. int i, rx_chainmask;
  5096. ahp->ah_extprotspacing = extprotspacing;
  5097. ahp->ah_txchainmask = txchainmask;
  5098. ahp->ah_rxchainmask = rxchainmask;
  5099. if (AR_SREV_9280(ah)) {
  5100. ahp->ah_txchainmask &= 0x3;
  5101. ahp->ah_rxchainmask &= 0x3;
  5102. }
  5103. if (ath9k_hw_check_chan(ah, chan) == NULL) {
  5104. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  5105. "%s: invalid channel %u/0x%x; no mapping\n",
  5106. __func__, chan->channel, chan->channelFlags);
  5107. ecode = -EINVAL;
  5108. goto bad;
  5109. }
  5110. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
  5111. ecode = -EIO;
  5112. goto bad;
  5113. }
  5114. if (curchan)
  5115. ath9k_hw_getnf(ah, curchan);
  5116. if (bChannelChange &&
  5117. (ahp->ah_chipFullSleep != true) &&
  5118. (ah->ah_curchan != NULL) &&
  5119. (chan->channel != ah->ah_curchan->channel) &&
  5120. ((chan->channelFlags & CHANNEL_ALL) ==
  5121. (ah->ah_curchan->channelFlags & CHANNEL_ALL)) &&
  5122. (!AR_SREV_9280(ah) || (!IS_CHAN_A_5MHZ_SPACED(chan) &&
  5123. !IS_CHAN_A_5MHZ_SPACED(ah->
  5124. ah_curchan)))) {
  5125. if (ath9k_hw_channel_change(ah, chan, macmode)) {
  5126. ath9k_hw_loadnf(ah, ah->ah_curchan);
  5127. ath9k_hw_start_nfcal(ah);
  5128. return true;
  5129. }
  5130. }
  5131. saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
  5132. if (saveDefAntenna == 0)
  5133. saveDefAntenna = 1;
  5134. macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
  5135. saveLedState = REG_READ(ah, AR_CFG_LED) &
  5136. (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
  5137. AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
  5138. ath9k_hw_mark_phy_inactive(ah);
  5139. if (!ath9k_hw_chip_reset(ah, chan)) {
  5140. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: chip reset failed\n",
  5141. __func__);
  5142. ecode = -EINVAL;
  5143. goto bad;
  5144. }
  5145. if (AR_SREV_9280(ah)) {
  5146. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
  5147. AR_GPIO_JTAG_DISABLE);
  5148. if (test_bit(ATH9K_MODE_11A, ah->ah_caps.wireless_modes)) {
  5149. if (IS_CHAN_5GHZ(chan))
  5150. ath9k_hw_set_gpio(ah, 9, 0);
  5151. else
  5152. ath9k_hw_set_gpio(ah, 9, 1);
  5153. }
  5154. ath9k_hw_cfg_output(ah, 9, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  5155. }
  5156. ecode = ath9k_hw_process_ini(ah, chan, macmode);
  5157. if (ecode != 0) {
  5158. ecode = -EINVAL;
  5159. goto bad;
  5160. }
  5161. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  5162. ath9k_hw_set_delta_slope(ah, chan);
  5163. if (AR_SREV_9280_10_OR_LATER(ah))
  5164. ath9k_hw_9280_spur_mitigate(ah, chan);
  5165. else
  5166. ath9k_hw_spur_mitigate(ah, chan);
  5167. if (!ath9k_hw_eeprom_set_board_values(ah, chan)) {
  5168. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  5169. "%s: error setting board options\n", __func__);
  5170. ecode = -EIO;
  5171. goto bad;
  5172. }
  5173. ath9k_hw_decrease_chain_power(ah, chan);
  5174. REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(ahp->ah_macaddr));
  5175. REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(ahp->ah_macaddr + 4)
  5176. | macStaId1
  5177. | AR_STA_ID1_RTS_USE_DEF
  5178. | (ah->ah_config.
  5179. ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
  5180. | ahp->ah_staId1Defaults);
  5181. ath9k_hw_set_operating_mode(ah, ah->ah_opmode);
  5182. REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(ahp->ah_bssidmask));
  5183. REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(ahp->ah_bssidmask + 4));
  5184. REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
  5185. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(ahp->ah_bssid));
  5186. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(ahp->ah_bssid + 4) |
  5187. ((ahp->ah_assocId & 0x3fff) << AR_BSS_ID1_AID_S));
  5188. REG_WRITE(ah, AR_ISR, ~0);
  5189. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  5190. if (AR_SREV_9280_10_OR_LATER(ah)) {
  5191. if (!(ath9k_hw_ar9280_set_channel(ah, chan))) {
  5192. ecode = -EIO;
  5193. goto bad;
  5194. }
  5195. } else {
  5196. if (!(ath9k_hw_set_channel(ah, chan))) {
  5197. ecode = -EIO;
  5198. goto bad;
  5199. }
  5200. }
  5201. for (i = 0; i < AR_NUM_DCU; i++)
  5202. REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
  5203. ahp->ah_intrTxqs = 0;
  5204. for (i = 0; i < ah->ah_caps.total_queues; i++)
  5205. ath9k_hw_resettxqueue(ah, i);
  5206. ath9k_hw_init_interrupt_masks(ah, ah->ah_opmode);
  5207. ath9k_hw_init_qos(ah);
  5208. #ifdef CONFIG_RFKILL
  5209. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  5210. ath9k_enable_rfkill(ah);
  5211. #endif
  5212. ath9k_hw_init_user_settings(ah);
  5213. REG_WRITE(ah, AR_STA_ID1,
  5214. REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PRESERVE_SEQNUM);
  5215. ath9k_hw_set_dma(ah);
  5216. REG_WRITE(ah, AR_OBS, 8);
  5217. if (ahp->ah_intrMitigation) {
  5218. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
  5219. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
  5220. }
  5221. ath9k_hw_init_bb(ah, chan);
  5222. if (!ath9k_hw_init_cal(ah, chan)){
  5223. ecode = -EIO;;
  5224. goto bad;
  5225. }
  5226. rx_chainmask = ahp->ah_rxchainmask;
  5227. if ((rx_chainmask == 0x5) || (rx_chainmask == 0x3)) {
  5228. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
  5229. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
  5230. }
  5231. REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
  5232. if (AR_SREV_9100(ah)) {
  5233. u32 mask;
  5234. mask = REG_READ(ah, AR_CFG);
  5235. if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
  5236. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  5237. "%s CFG Byte Swap Set 0x%x\n", __func__,
  5238. mask);
  5239. } else {
  5240. mask =
  5241. INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
  5242. REG_WRITE(ah, AR_CFG, mask);
  5243. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  5244. "%s Setting CFG 0x%x\n", __func__,
  5245. REG_READ(ah, AR_CFG));
  5246. }
  5247. } else {
  5248. #ifdef __BIG_ENDIAN
  5249. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  5250. #endif
  5251. }
  5252. return true;
  5253. bad:
  5254. if (status)
  5255. *status = ecode;
  5256. return false;
  5257. }
  5258. bool ath9k_hw_phy_disable(struct ath_hal *ah)
  5259. {
  5260. return ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM);
  5261. }
  5262. bool ath9k_hw_disable(struct ath_hal *ah)
  5263. {
  5264. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  5265. return false;
  5266. return ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD);
  5267. }
  5268. bool
  5269. ath9k_hw_calibrate(struct ath_hal *ah, struct ath9k_channel *chan,
  5270. u8 rxchainmask, bool longcal,
  5271. bool *isCalDone)
  5272. {
  5273. struct ath_hal_5416 *ahp = AH5416(ah);
  5274. struct hal_cal_list *currCal = ahp->ah_cal_list_curr;
  5275. struct ath9k_channel *ichan =
  5276. ath9k_regd_check_channel(ah, chan);
  5277. *isCalDone = true;
  5278. if (ichan == NULL) {
  5279. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  5280. "%s: invalid channel %u/0x%x; no mapping\n",
  5281. __func__, chan->channel, chan->channelFlags);
  5282. return false;
  5283. }
  5284. if (currCal &&
  5285. (currCal->calState == CAL_RUNNING ||
  5286. currCal->calState == CAL_WAITING)) {
  5287. ath9k_hw_per_calibration(ah, ichan, rxchainmask, currCal,
  5288. isCalDone);
  5289. if (*isCalDone) {
  5290. ahp->ah_cal_list_curr = currCal = currCal->calNext;
  5291. if (currCal->calState == CAL_WAITING) {
  5292. *isCalDone = false;
  5293. ath9k_hw_reset_calibration(ah, currCal);
  5294. }
  5295. }
  5296. }
  5297. if (longcal) {
  5298. ath9k_hw_getnf(ah, ichan);
  5299. ath9k_hw_loadnf(ah, ah->ah_curchan);
  5300. ath9k_hw_start_nfcal(ah);
  5301. if ((ichan->channelFlags & CHANNEL_CW_INT) != 0) {
  5302. chan->channelFlags |= CHANNEL_CW_INT;
  5303. ichan->channelFlags &= ~CHANNEL_CW_INT;
  5304. }
  5305. }
  5306. return true;
  5307. }
  5308. static void ath9k_hw_iqcal_collect(struct ath_hal *ah)
  5309. {
  5310. struct ath_hal_5416 *ahp = AH5416(ah);
  5311. int i;
  5312. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  5313. ahp->ah_totalPowerMeasI[i] +=
  5314. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  5315. ahp->ah_totalPowerMeasQ[i] +=
  5316. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  5317. ahp->ah_totalIqCorrMeas[i] +=
  5318. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  5319. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5320. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  5321. ahp->ah_CalSamples, i, ahp->ah_totalPowerMeasI[i],
  5322. ahp->ah_totalPowerMeasQ[i],
  5323. ahp->ah_totalIqCorrMeas[i]);
  5324. }
  5325. }
  5326. static void ath9k_hw_adc_gaincal_collect(struct ath_hal *ah)
  5327. {
  5328. struct ath_hal_5416 *ahp = AH5416(ah);
  5329. int i;
  5330. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  5331. ahp->ah_totalAdcIOddPhase[i] +=
  5332. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  5333. ahp->ah_totalAdcIEvenPhase[i] +=
  5334. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  5335. ahp->ah_totalAdcQOddPhase[i] +=
  5336. REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  5337. ahp->ah_totalAdcQEvenPhase[i] +=
  5338. REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  5339. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5340. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  5341. "oddq=0x%08x; evenq=0x%08x;\n",
  5342. ahp->ah_CalSamples, i,
  5343. ahp->ah_totalAdcIOddPhase[i],
  5344. ahp->ah_totalAdcIEvenPhase[i],
  5345. ahp->ah_totalAdcQOddPhase[i],
  5346. ahp->ah_totalAdcQEvenPhase[i]);
  5347. }
  5348. }
  5349. static void ath9k_hw_adc_dccal_collect(struct ath_hal *ah)
  5350. {
  5351. struct ath_hal_5416 *ahp = AH5416(ah);
  5352. int i;
  5353. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  5354. ahp->ah_totalAdcDcOffsetIOddPhase[i] +=
  5355. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  5356. ahp->ah_totalAdcDcOffsetIEvenPhase[i] +=
  5357. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  5358. ahp->ah_totalAdcDcOffsetQOddPhase[i] +=
  5359. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  5360. ahp->ah_totalAdcDcOffsetQEvenPhase[i] +=
  5361. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  5362. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5363. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  5364. "oddq=0x%08x; evenq=0x%08x;\n",
  5365. ahp->ah_CalSamples, i,
  5366. ahp->ah_totalAdcDcOffsetIOddPhase[i],
  5367. ahp->ah_totalAdcDcOffsetIEvenPhase[i],
  5368. ahp->ah_totalAdcDcOffsetQOddPhase[i],
  5369. ahp->ah_totalAdcDcOffsetQEvenPhase[i]);
  5370. }
  5371. }
  5372. static void ath9k_hw_iqcalibrate(struct ath_hal *ah, u8 numChains)
  5373. {
  5374. struct ath_hal_5416 *ahp = AH5416(ah);
  5375. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  5376. u32 qCoffDenom, iCoffDenom;
  5377. int32_t qCoff, iCoff;
  5378. int iqCorrNeg, i;
  5379. for (i = 0; i < numChains; i++) {
  5380. powerMeasI = ahp->ah_totalPowerMeasI[i];
  5381. powerMeasQ = ahp->ah_totalPowerMeasQ[i];
  5382. iqCorrMeas = ahp->ah_totalIqCorrMeas[i];
  5383. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5384. "Starting IQ Cal and Correction for Chain %d\n",
  5385. i);
  5386. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5387. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  5388. i, ahp->ah_totalIqCorrMeas[i]);
  5389. iqCorrNeg = 0;
  5390. if (iqCorrMeas > 0x80000000) {
  5391. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  5392. iqCorrNeg = 1;
  5393. }
  5394. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5395. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  5396. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5397. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  5398. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  5399. iqCorrNeg);
  5400. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
  5401. qCoffDenom = powerMeasQ / 64;
  5402. if (powerMeasQ != 0) {
  5403. iCoff = iqCorrMeas / iCoffDenom;
  5404. qCoff = powerMeasI / qCoffDenom - 64;
  5405. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5406. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  5407. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5408. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  5409. iCoff = iCoff & 0x3f;
  5410. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5411. "New: Chn %d iCoff = 0x%08x\n", i, iCoff);
  5412. if (iqCorrNeg == 0x0)
  5413. iCoff = 0x40 - iCoff;
  5414. if (qCoff > 15)
  5415. qCoff = 15;
  5416. else if (qCoff <= -16)
  5417. qCoff = 16;
  5418. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5419. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  5420. i, iCoff, qCoff);
  5421. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  5422. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
  5423. iCoff);
  5424. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  5425. AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
  5426. qCoff);
  5427. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5428. "IQ Cal and Correction done for Chain %d\n",
  5429. i);
  5430. }
  5431. }
  5432. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  5433. AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
  5434. }
  5435. static void
  5436. ath9k_hw_adc_gaincal_calibrate(struct ath_hal *ah, u8 numChains)
  5437. {
  5438. struct ath_hal_5416 *ahp = AH5416(ah);
  5439. u32 iOddMeasOffset, iEvenMeasOffset, qOddMeasOffset,
  5440. qEvenMeasOffset;
  5441. u32 qGainMismatch, iGainMismatch, val, i;
  5442. for (i = 0; i < numChains; i++) {
  5443. iOddMeasOffset = ahp->ah_totalAdcIOddPhase[i];
  5444. iEvenMeasOffset = ahp->ah_totalAdcIEvenPhase[i];
  5445. qOddMeasOffset = ahp->ah_totalAdcQOddPhase[i];
  5446. qEvenMeasOffset = ahp->ah_totalAdcQEvenPhase[i];
  5447. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5448. "Starting ADC Gain Cal for Chain %d\n", i);
  5449. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5450. "Chn %d pwr_meas_odd_i = 0x%08x\n", i,
  5451. iOddMeasOffset);
  5452. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5453. "Chn %d pwr_meas_even_i = 0x%08x\n", i,
  5454. iEvenMeasOffset);
  5455. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5456. "Chn %d pwr_meas_odd_q = 0x%08x\n", i,
  5457. qOddMeasOffset);
  5458. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5459. "Chn %d pwr_meas_even_q = 0x%08x\n", i,
  5460. qEvenMeasOffset);
  5461. if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
  5462. iGainMismatch =
  5463. ((iEvenMeasOffset * 32) /
  5464. iOddMeasOffset) & 0x3f;
  5465. qGainMismatch =
  5466. ((qOddMeasOffset * 32) /
  5467. qEvenMeasOffset) & 0x3f;
  5468. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5469. "Chn %d gain_mismatch_i = 0x%08x\n", i,
  5470. iGainMismatch);
  5471. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5472. "Chn %d gain_mismatch_q = 0x%08x\n", i,
  5473. qGainMismatch);
  5474. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  5475. val &= 0xfffff000;
  5476. val |= (qGainMismatch) | (iGainMismatch << 6);
  5477. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  5478. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5479. "ADC Gain Cal done for Chain %d\n", i);
  5480. }
  5481. }
  5482. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  5483. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  5484. AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
  5485. }
  5486. static void
  5487. ath9k_hw_adc_dccal_calibrate(struct ath_hal *ah, u8 numChains)
  5488. {
  5489. struct ath_hal_5416 *ahp = AH5416(ah);
  5490. u32 iOddMeasOffset, iEvenMeasOffset, val, i;
  5491. int32_t qOddMeasOffset, qEvenMeasOffset, qDcMismatch, iDcMismatch;
  5492. const struct hal_percal_data *calData =
  5493. ahp->ah_cal_list_curr->calData;
  5494. u32 numSamples =
  5495. (1 << (calData->calCountMax + 5)) * calData->calNumSamples;
  5496. for (i = 0; i < numChains; i++) {
  5497. iOddMeasOffset = ahp->ah_totalAdcDcOffsetIOddPhase[i];
  5498. iEvenMeasOffset = ahp->ah_totalAdcDcOffsetIEvenPhase[i];
  5499. qOddMeasOffset = ahp->ah_totalAdcDcOffsetQOddPhase[i];
  5500. qEvenMeasOffset = ahp->ah_totalAdcDcOffsetQEvenPhase[i];
  5501. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5502. "Starting ADC DC Offset Cal for Chain %d\n", i);
  5503. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5504. "Chn %d pwr_meas_odd_i = %d\n", i,
  5505. iOddMeasOffset);
  5506. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5507. "Chn %d pwr_meas_even_i = %d\n", i,
  5508. iEvenMeasOffset);
  5509. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5510. "Chn %d pwr_meas_odd_q = %d\n", i,
  5511. qOddMeasOffset);
  5512. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5513. "Chn %d pwr_meas_even_q = %d\n", i,
  5514. qEvenMeasOffset);
  5515. iDcMismatch = (((iEvenMeasOffset - iOddMeasOffset) * 2) /
  5516. numSamples) & 0x1ff;
  5517. qDcMismatch = (((qOddMeasOffset - qEvenMeasOffset) * 2) /
  5518. numSamples) & 0x1ff;
  5519. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5520. "Chn %d dc_offset_mismatch_i = 0x%08x\n", i,
  5521. iDcMismatch);
  5522. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5523. "Chn %d dc_offset_mismatch_q = 0x%08x\n", i,
  5524. qDcMismatch);
  5525. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  5526. val &= 0xc0000fff;
  5527. val |= (qDcMismatch << 12) | (iDcMismatch << 21);
  5528. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  5529. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5530. "ADC DC Offset Cal done for Chain %d\n", i);
  5531. }
  5532. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  5533. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  5534. AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
  5535. }
  5536. bool ath9k_hw_set_txpowerlimit(struct ath_hal *ah, u32 limit)
  5537. {
  5538. struct ath_hal_5416 *ahp = AH5416(ah);
  5539. struct ath9k_channel *chan = ah->ah_curchan;
  5540. ah->ah_powerLimit = min(limit, (u32) MAX_RATE_POWER);
  5541. if (ath9k_hw_set_txpower(ah, &ahp->ah_eeprom, chan,
  5542. ath9k_regd_get_ctl(ah, chan),
  5543. ath9k_regd_get_antenna_allowed(ah,
  5544. chan),
  5545. chan->maxRegTxPower * 2,
  5546. min((u32) MAX_RATE_POWER,
  5547. (u32) ah->ah_powerLimit)) != 0)
  5548. return false;
  5549. return true;
  5550. }
  5551. void
  5552. ath9k_hw_get_channel_centers(struct ath_hal *ah,
  5553. struct ath9k_channel *chan,
  5554. struct chan_centers *centers)
  5555. {
  5556. int8_t extoff;
  5557. struct ath_hal_5416 *ahp = AH5416(ah);
  5558. if (!IS_CHAN_HT40(chan)) {
  5559. centers->ctl_center = centers->ext_center =
  5560. centers->synth_center = chan->channel;
  5561. return;
  5562. }
  5563. if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
  5564. (chan->chanmode == CHANNEL_G_HT40PLUS)) {
  5565. centers->synth_center =
  5566. chan->channel + HT40_CHANNEL_CENTER_SHIFT;
  5567. extoff = 1;
  5568. } else {
  5569. centers->synth_center =
  5570. chan->channel - HT40_CHANNEL_CENTER_SHIFT;
  5571. extoff = -1;
  5572. }
  5573. centers->ctl_center = centers->synth_center - (extoff *
  5574. HT40_CHANNEL_CENTER_SHIFT);
  5575. centers->ext_center = centers->synth_center + (extoff *
  5576. ((ahp->
  5577. ah_extprotspacing
  5578. ==
  5579. ATH9K_HT_EXTPROTSPACING_20)
  5580. ?
  5581. HT40_CHANNEL_CENTER_SHIFT
  5582. : 15));
  5583. }
  5584. void
  5585. ath9k_hw_reset_calvalid(struct ath_hal *ah, struct ath9k_channel *chan,
  5586. bool *isCalDone)
  5587. {
  5588. struct ath_hal_5416 *ahp = AH5416(ah);
  5589. struct ath9k_channel *ichan =
  5590. ath9k_regd_check_channel(ah, chan);
  5591. struct hal_cal_list *currCal = ahp->ah_cal_list_curr;
  5592. *isCalDone = true;
  5593. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  5594. return;
  5595. if (currCal == NULL)
  5596. return;
  5597. if (ichan == NULL) {
  5598. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5599. "%s: invalid channel %u/0x%x; no mapping\n",
  5600. __func__, chan->channel, chan->channelFlags);
  5601. return;
  5602. }
  5603. if (currCal->calState != CAL_DONE) {
  5604. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5605. "%s: Calibration state incorrect, %d\n",
  5606. __func__, currCal->calState);
  5607. return;
  5608. }
  5609. if (!ath9k_hw_iscal_supported(ah, chan, currCal->calData->calType))
  5610. return;
  5611. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5612. "%s: Resetting Cal %d state for channel %u/0x%x\n",
  5613. __func__, currCal->calData->calType, chan->channel,
  5614. chan->channelFlags);
  5615. ichan->CalValid &= ~currCal->calData->calType;
  5616. currCal->calState = CAL_WAITING;
  5617. *isCalDone = false;
  5618. }
  5619. void ath9k_hw_getmac(struct ath_hal *ah, u8 *mac)
  5620. {
  5621. struct ath_hal_5416 *ahp = AH5416(ah);
  5622. memcpy(mac, ahp->ah_macaddr, ETH_ALEN);
  5623. }
  5624. bool ath9k_hw_setmac(struct ath_hal *ah, const u8 *mac)
  5625. {
  5626. struct ath_hal_5416 *ahp = AH5416(ah);
  5627. memcpy(ahp->ah_macaddr, mac, ETH_ALEN);
  5628. return true;
  5629. }
  5630. void ath9k_hw_getbssidmask(struct ath_hal *ah, u8 *mask)
  5631. {
  5632. struct ath_hal_5416 *ahp = AH5416(ah);
  5633. memcpy(mask, ahp->ah_bssidmask, ETH_ALEN);
  5634. }
  5635. bool
  5636. ath9k_hw_setbssidmask(struct ath_hal *ah, const u8 *mask)
  5637. {
  5638. struct ath_hal_5416 *ahp = AH5416(ah);
  5639. memcpy(ahp->ah_bssidmask, mask, ETH_ALEN);
  5640. REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(ahp->ah_bssidmask));
  5641. REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(ahp->ah_bssidmask + 4));
  5642. return true;
  5643. }
  5644. void
  5645. ath9k_hw_write_associd(struct ath_hal *ah, const u8 *bssid,
  5646. u16 assocId)
  5647. {
  5648. struct ath_hal_5416 *ahp = AH5416(ah);
  5649. memcpy(ahp->ah_bssid, bssid, ETH_ALEN);
  5650. ahp->ah_assocId = assocId;
  5651. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(ahp->ah_bssid));
  5652. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(ahp->ah_bssid + 4) |
  5653. ((assocId & 0x3fff) << AR_BSS_ID1_AID_S));
  5654. }
  5655. u64 ath9k_hw_gettsf64(struct ath_hal *ah)
  5656. {
  5657. u64 tsf;
  5658. tsf = REG_READ(ah, AR_TSF_U32);
  5659. tsf = (tsf << 32) | REG_READ(ah, AR_TSF_L32);
  5660. return tsf;
  5661. }
  5662. void ath9k_hw_reset_tsf(struct ath_hal *ah)
  5663. {
  5664. int count;
  5665. count = 0;
  5666. while (REG_READ(ah, AR_SLP32_MODE) & AR_SLP32_TSF_WRITE_STATUS) {
  5667. count++;
  5668. if (count > 10) {
  5669. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  5670. "%s: AR_SLP32_TSF_WRITE_STATUS limit exceeded\n",
  5671. __func__);
  5672. break;
  5673. }
  5674. udelay(10);
  5675. }
  5676. REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
  5677. }
  5678. u32 ath9k_hw_getdefantenna(struct ath_hal *ah)
  5679. {
  5680. return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
  5681. }
  5682. void ath9k_hw_setantenna(struct ath_hal *ah, u32 antenna)
  5683. {
  5684. REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
  5685. }
  5686. bool
  5687. ath9k_hw_setantennaswitch(struct ath_hal *ah,
  5688. enum ath9k_ant_setting settings,
  5689. struct ath9k_channel *chan,
  5690. u8 *tx_chainmask,
  5691. u8 *rx_chainmask,
  5692. u8 *antenna_cfgd)
  5693. {
  5694. struct ath_hal_5416 *ahp = AH5416(ah);
  5695. static u8 tx_chainmask_cfg, rx_chainmask_cfg;
  5696. if (AR_SREV_9280(ah)) {
  5697. if (!tx_chainmask_cfg) {
  5698. tx_chainmask_cfg = *tx_chainmask;
  5699. rx_chainmask_cfg = *rx_chainmask;
  5700. }
  5701. switch (settings) {
  5702. case ATH9K_ANT_FIXED_A:
  5703. *tx_chainmask = ATH9K_ANTENNA0_CHAINMASK;
  5704. *rx_chainmask = ATH9K_ANTENNA0_CHAINMASK;
  5705. *antenna_cfgd = true;
  5706. break;
  5707. case ATH9K_ANT_FIXED_B:
  5708. if (ah->ah_caps.tx_chainmask >
  5709. ATH9K_ANTENNA1_CHAINMASK) {
  5710. *tx_chainmask = ATH9K_ANTENNA1_CHAINMASK;
  5711. }
  5712. *rx_chainmask = ATH9K_ANTENNA1_CHAINMASK;
  5713. *antenna_cfgd = true;
  5714. break;
  5715. case ATH9K_ANT_VARIABLE:
  5716. *tx_chainmask = tx_chainmask_cfg;
  5717. *rx_chainmask = rx_chainmask_cfg;
  5718. *antenna_cfgd = true;
  5719. break;
  5720. default:
  5721. break;
  5722. }
  5723. } else {
  5724. ahp->ah_diversityControl = settings;
  5725. }
  5726. return true;
  5727. }
  5728. void ath9k_hw_setopmode(struct ath_hal *ah)
  5729. {
  5730. ath9k_hw_set_operating_mode(ah, ah->ah_opmode);
  5731. }
  5732. bool
  5733. ath9k_hw_getcapability(struct ath_hal *ah, enum ath9k_capability_type type,
  5734. u32 capability, u32 *result)
  5735. {
  5736. struct ath_hal_5416 *ahp = AH5416(ah);
  5737. const struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5738. switch (type) {
  5739. case ATH9K_CAP_CIPHER:
  5740. switch (capability) {
  5741. case ATH9K_CIPHER_AES_CCM:
  5742. case ATH9K_CIPHER_AES_OCB:
  5743. case ATH9K_CIPHER_TKIP:
  5744. case ATH9K_CIPHER_WEP:
  5745. case ATH9K_CIPHER_MIC:
  5746. case ATH9K_CIPHER_CLR:
  5747. return true;
  5748. default:
  5749. return false;
  5750. }
  5751. case ATH9K_CAP_TKIP_MIC:
  5752. switch (capability) {
  5753. case 0:
  5754. return true;
  5755. case 1:
  5756. return (ahp->ah_staId1Defaults &
  5757. AR_STA_ID1_CRPT_MIC_ENABLE) ? true :
  5758. false;
  5759. }
  5760. case ATH9K_CAP_TKIP_SPLIT:
  5761. return (ahp->ah_miscMode & AR_PCU_MIC_NEW_LOC_ENA) ?
  5762. false : true;
  5763. case ATH9K_CAP_WME_TKIPMIC:
  5764. return 0;
  5765. case ATH9K_CAP_PHYCOUNTERS:
  5766. return ahp->ah_hasHwPhyCounters ? 0 : -ENXIO;
  5767. case ATH9K_CAP_DIVERSITY:
  5768. return (REG_READ(ah, AR_PHY_CCK_DETECT) &
  5769. AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV) ?
  5770. true : false;
  5771. case ATH9K_CAP_PHYDIAG:
  5772. return true;
  5773. case ATH9K_CAP_MCAST_KEYSRCH:
  5774. switch (capability) {
  5775. case 0:
  5776. return true;
  5777. case 1:
  5778. if (REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_ADHOC) {
  5779. return false;
  5780. } else {
  5781. return (ahp->ah_staId1Defaults &
  5782. AR_STA_ID1_MCAST_KSRCH) ? true :
  5783. false;
  5784. }
  5785. }
  5786. return false;
  5787. case ATH9K_CAP_TSF_ADJUST:
  5788. return (ahp->ah_miscMode & AR_PCU_TX_ADD_TSF) ?
  5789. true : false;
  5790. case ATH9K_CAP_RFSILENT:
  5791. if (capability == 3)
  5792. return false;
  5793. case ATH9K_CAP_ANT_CFG_2GHZ:
  5794. *result = pCap->num_antcfg_2ghz;
  5795. return true;
  5796. case ATH9K_CAP_ANT_CFG_5GHZ:
  5797. *result = pCap->num_antcfg_5ghz;
  5798. return true;
  5799. case ATH9K_CAP_TXPOW:
  5800. switch (capability) {
  5801. case 0:
  5802. return 0;
  5803. case 1:
  5804. *result = ah->ah_powerLimit;
  5805. return 0;
  5806. case 2:
  5807. *result = ah->ah_maxPowerLevel;
  5808. return 0;
  5809. case 3:
  5810. *result = ah->ah_tpScale;
  5811. return 0;
  5812. }
  5813. return false;
  5814. default:
  5815. return false;
  5816. }
  5817. }
  5818. int
  5819. ath9k_hw_select_antconfig(struct ath_hal *ah, u32 cfg)
  5820. {
  5821. struct ath_hal_5416 *ahp = AH5416(ah);
  5822. struct ath9k_channel *chan = ah->ah_curchan;
  5823. const struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5824. u16 ant_config;
  5825. u32 halNumAntConfig;
  5826. halNumAntConfig =
  5827. IS_CHAN_2GHZ(chan) ? pCap->num_antcfg_2ghz : pCap->
  5828. num_antcfg_5ghz;
  5829. if (cfg < halNumAntConfig) {
  5830. if (!ath9k_hw_get_eeprom_antenna_cfg(ahp, chan,
  5831. cfg, &ant_config)) {
  5832. REG_WRITE(ah, AR_PHY_SWITCH_COM, ant_config);
  5833. return 0;
  5834. }
  5835. }
  5836. return -EINVAL;
  5837. }
  5838. bool ath9k_hw_intrpend(struct ath_hal *ah)
  5839. {
  5840. u32 host_isr;
  5841. if (AR_SREV_9100(ah))
  5842. return true;
  5843. host_isr = REG_READ(ah, AR_INTR_ASYNC_CAUSE);
  5844. if ((host_isr & AR_INTR_MAC_IRQ) && (host_isr != AR_INTR_SPURIOUS))
  5845. return true;
  5846. host_isr = REG_READ(ah, AR_INTR_SYNC_CAUSE);
  5847. if ((host_isr & AR_INTR_SYNC_DEFAULT)
  5848. && (host_isr != AR_INTR_SPURIOUS))
  5849. return true;
  5850. return false;
  5851. }
  5852. bool ath9k_hw_getisr(struct ath_hal *ah, enum ath9k_int *masked)
  5853. {
  5854. u32 isr = 0;
  5855. u32 mask2 = 0;
  5856. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5857. u32 sync_cause = 0;
  5858. bool fatal_int = false;
  5859. if (!AR_SREV_9100(ah)) {
  5860. if (REG_READ(ah, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) {
  5861. if ((REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M)
  5862. == AR_RTC_STATUS_ON) {
  5863. isr = REG_READ(ah, AR_ISR);
  5864. }
  5865. }
  5866. sync_cause =
  5867. REG_READ(ah,
  5868. AR_INTR_SYNC_CAUSE) & AR_INTR_SYNC_DEFAULT;
  5869. *masked = 0;
  5870. if (!isr && !sync_cause)
  5871. return false;
  5872. } else {
  5873. *masked = 0;
  5874. isr = REG_READ(ah, AR_ISR);
  5875. }
  5876. if (isr) {
  5877. struct ath_hal_5416 *ahp = AH5416(ah);
  5878. if (isr & AR_ISR_BCNMISC) {
  5879. u32 isr2;
  5880. isr2 = REG_READ(ah, AR_ISR_S2);
  5881. if (isr2 & AR_ISR_S2_TIM)
  5882. mask2 |= ATH9K_INT_TIM;
  5883. if (isr2 & AR_ISR_S2_DTIM)
  5884. mask2 |= ATH9K_INT_DTIM;
  5885. if (isr2 & AR_ISR_S2_DTIMSYNC)
  5886. mask2 |= ATH9K_INT_DTIMSYNC;
  5887. if (isr2 & (AR_ISR_S2_CABEND))
  5888. mask2 |= ATH9K_INT_CABEND;
  5889. if (isr2 & AR_ISR_S2_GTT)
  5890. mask2 |= ATH9K_INT_GTT;
  5891. if (isr2 & AR_ISR_S2_CST)
  5892. mask2 |= ATH9K_INT_CST;
  5893. }
  5894. isr = REG_READ(ah, AR_ISR_RAC);
  5895. if (isr == 0xffffffff) {
  5896. *masked = 0;
  5897. return false;
  5898. }
  5899. *masked = isr & ATH9K_INT_COMMON;
  5900. if (ahp->ah_intrMitigation) {
  5901. if (isr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
  5902. *masked |= ATH9K_INT_RX;
  5903. }
  5904. if (isr & (AR_ISR_RXOK | AR_ISR_RXERR))
  5905. *masked |= ATH9K_INT_RX;
  5906. if (isr &
  5907. (AR_ISR_TXOK | AR_ISR_TXDESC | AR_ISR_TXERR |
  5908. AR_ISR_TXEOL)) {
  5909. u32 s0_s, s1_s;
  5910. *masked |= ATH9K_INT_TX;
  5911. s0_s = REG_READ(ah, AR_ISR_S0_S);
  5912. ahp->ah_intrTxqs |= MS(s0_s, AR_ISR_S0_QCU_TXOK);
  5913. ahp->ah_intrTxqs |= MS(s0_s, AR_ISR_S0_QCU_TXDESC);
  5914. s1_s = REG_READ(ah, AR_ISR_S1_S);
  5915. ahp->ah_intrTxqs |= MS(s1_s, AR_ISR_S1_QCU_TXERR);
  5916. ahp->ah_intrTxqs |= MS(s1_s, AR_ISR_S1_QCU_TXEOL);
  5917. }
  5918. if (isr & AR_ISR_RXORN) {
  5919. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  5920. "%s: receive FIFO overrun interrupt\n",
  5921. __func__);
  5922. }
  5923. if (!AR_SREV_9100(ah)) {
  5924. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  5925. u32 isr5 = REG_READ(ah, AR_ISR_S5_S);
  5926. if (isr5 & AR_ISR_S5_TIM_TIMER)
  5927. *masked |= ATH9K_INT_TIM_TIMER;
  5928. }
  5929. }
  5930. *masked |= mask2;
  5931. }
  5932. if (AR_SREV_9100(ah))
  5933. return true;
  5934. if (sync_cause) {
  5935. fatal_int =
  5936. (sync_cause &
  5937. (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR))
  5938. ? true : false;
  5939. if (fatal_int) {
  5940. if (sync_cause & AR_INTR_SYNC_HOST1_FATAL) {
  5941. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  5942. "%s: received PCI FATAL interrupt\n",
  5943. __func__);
  5944. }
  5945. if (sync_cause & AR_INTR_SYNC_HOST1_PERR) {
  5946. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  5947. "%s: received PCI PERR interrupt\n",
  5948. __func__);
  5949. }
  5950. }
  5951. if (sync_cause & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
  5952. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  5953. "%s: AR_INTR_SYNC_RADM_CPL_TIMEOUT\n",
  5954. __func__);
  5955. REG_WRITE(ah, AR_RC, AR_RC_HOSTIF);
  5956. REG_WRITE(ah, AR_RC, 0);
  5957. *masked |= ATH9K_INT_FATAL;
  5958. }
  5959. if (sync_cause & AR_INTR_SYNC_LOCAL_TIMEOUT) {
  5960. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  5961. "%s: AR_INTR_SYNC_LOCAL_TIMEOUT\n",
  5962. __func__);
  5963. }
  5964. REG_WRITE(ah, AR_INTR_SYNC_CAUSE_CLR, sync_cause);
  5965. (void) REG_READ(ah, AR_INTR_SYNC_CAUSE_CLR);
  5966. }
  5967. return true;
  5968. }
  5969. enum ath9k_int ath9k_hw_intrget(struct ath_hal *ah)
  5970. {
  5971. return AH5416(ah)->ah_maskReg;
  5972. }
  5973. enum ath9k_int ath9k_hw_set_interrupts(struct ath_hal *ah, enum ath9k_int ints)
  5974. {
  5975. struct ath_hal_5416 *ahp = AH5416(ah);
  5976. u32 omask = ahp->ah_maskReg;
  5977. u32 mask, mask2;
  5978. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5979. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: 0x%x => 0x%x\n", __func__,
  5980. omask, ints);
  5981. if (omask & ATH9K_INT_GLOBAL) {
  5982. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: disable IER\n",
  5983. __func__);
  5984. REG_WRITE(ah, AR_IER, AR_IER_DISABLE);
  5985. (void) REG_READ(ah, AR_IER);
  5986. if (!AR_SREV_9100(ah)) {
  5987. REG_WRITE(ah, AR_INTR_ASYNC_ENABLE, 0);
  5988. (void) REG_READ(ah, AR_INTR_ASYNC_ENABLE);
  5989. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
  5990. (void) REG_READ(ah, AR_INTR_SYNC_ENABLE);
  5991. }
  5992. }
  5993. mask = ints & ATH9K_INT_COMMON;
  5994. mask2 = 0;
  5995. if (ints & ATH9K_INT_TX) {
  5996. if (ahp->ah_txOkInterruptMask)
  5997. mask |= AR_IMR_TXOK;
  5998. if (ahp->ah_txDescInterruptMask)
  5999. mask |= AR_IMR_TXDESC;
  6000. if (ahp->ah_txErrInterruptMask)
  6001. mask |= AR_IMR_TXERR;
  6002. if (ahp->ah_txEolInterruptMask)
  6003. mask |= AR_IMR_TXEOL;
  6004. }
  6005. if (ints & ATH9K_INT_RX) {
  6006. mask |= AR_IMR_RXERR;
  6007. if (ahp->ah_intrMitigation)
  6008. mask |= AR_IMR_RXMINTR | AR_IMR_RXINTM;
  6009. else
  6010. mask |= AR_IMR_RXOK | AR_IMR_RXDESC;
  6011. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  6012. mask |= AR_IMR_GENTMR;
  6013. }
  6014. if (ints & (ATH9K_INT_BMISC)) {
  6015. mask |= AR_IMR_BCNMISC;
  6016. if (ints & ATH9K_INT_TIM)
  6017. mask2 |= AR_IMR_S2_TIM;
  6018. if (ints & ATH9K_INT_DTIM)
  6019. mask2 |= AR_IMR_S2_DTIM;
  6020. if (ints & ATH9K_INT_DTIMSYNC)
  6021. mask2 |= AR_IMR_S2_DTIMSYNC;
  6022. if (ints & ATH9K_INT_CABEND)
  6023. mask2 |= (AR_IMR_S2_CABEND);
  6024. }
  6025. if (ints & (ATH9K_INT_GTT | ATH9K_INT_CST)) {
  6026. mask |= AR_IMR_BCNMISC;
  6027. if (ints & ATH9K_INT_GTT)
  6028. mask2 |= AR_IMR_S2_GTT;
  6029. if (ints & ATH9K_INT_CST)
  6030. mask2 |= AR_IMR_S2_CST;
  6031. }
  6032. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: new IMR 0x%x\n", __func__,
  6033. mask);
  6034. REG_WRITE(ah, AR_IMR, mask);
  6035. mask = REG_READ(ah, AR_IMR_S2) & ~(AR_IMR_S2_TIM |
  6036. AR_IMR_S2_DTIM |
  6037. AR_IMR_S2_DTIMSYNC |
  6038. AR_IMR_S2_CABEND |
  6039. AR_IMR_S2_CABTO |
  6040. AR_IMR_S2_TSFOOR |
  6041. AR_IMR_S2_GTT | AR_IMR_S2_CST);
  6042. REG_WRITE(ah, AR_IMR_S2, mask | mask2);
  6043. ahp->ah_maskReg = ints;
  6044. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  6045. if (ints & ATH9K_INT_TIM_TIMER)
  6046. REG_SET_BIT(ah, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
  6047. else
  6048. REG_CLR_BIT(ah, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
  6049. }
  6050. if (ints & ATH9K_INT_GLOBAL) {
  6051. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: enable IER\n",
  6052. __func__);
  6053. REG_WRITE(ah, AR_IER, AR_IER_ENABLE);
  6054. if (!AR_SREV_9100(ah)) {
  6055. REG_WRITE(ah, AR_INTR_ASYNC_ENABLE,
  6056. AR_INTR_MAC_IRQ);
  6057. REG_WRITE(ah, AR_INTR_ASYNC_MASK, AR_INTR_MAC_IRQ);
  6058. REG_WRITE(ah, AR_INTR_SYNC_ENABLE,
  6059. AR_INTR_SYNC_DEFAULT);
  6060. REG_WRITE(ah, AR_INTR_SYNC_MASK,
  6061. AR_INTR_SYNC_DEFAULT);
  6062. }
  6063. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "AR_IMR 0x%x IER 0x%x\n",
  6064. REG_READ(ah, AR_IMR), REG_READ(ah, AR_IER));
  6065. }
  6066. return omask;
  6067. }
  6068. void
  6069. ath9k_hw_beaconinit(struct ath_hal *ah,
  6070. u32 next_beacon, u32 beacon_period)
  6071. {
  6072. struct ath_hal_5416 *ahp = AH5416(ah);
  6073. int flags = 0;
  6074. ahp->ah_beaconInterval = beacon_period;
  6075. switch (ah->ah_opmode) {
  6076. case ATH9K_M_STA:
  6077. case ATH9K_M_MONITOR:
  6078. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
  6079. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, 0xffff);
  6080. REG_WRITE(ah, AR_NEXT_SWBA, 0x7ffff);
  6081. flags |= AR_TBTT_TIMER_EN;
  6082. break;
  6083. case ATH9K_M_IBSS:
  6084. REG_SET_BIT(ah, AR_TXCFG,
  6085. AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
  6086. REG_WRITE(ah, AR_NEXT_NDP_TIMER,
  6087. TU_TO_USEC(next_beacon +
  6088. (ahp->ah_atimWindow ? ahp->
  6089. ah_atimWindow : 1)));
  6090. flags |= AR_NDP_TIMER_EN;
  6091. case ATH9K_M_HOSTAP:
  6092. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
  6093. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT,
  6094. TU_TO_USEC(next_beacon -
  6095. ah->ah_config.
  6096. dma_beacon_response_time));
  6097. REG_WRITE(ah, AR_NEXT_SWBA,
  6098. TU_TO_USEC(next_beacon -
  6099. ah->ah_config.
  6100. sw_beacon_response_time));
  6101. flags |=
  6102. AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
  6103. break;
  6104. }
  6105. REG_WRITE(ah, AR_BEACON_PERIOD, TU_TO_USEC(beacon_period));
  6106. REG_WRITE(ah, AR_DMA_BEACON_PERIOD, TU_TO_USEC(beacon_period));
  6107. REG_WRITE(ah, AR_SWBA_PERIOD, TU_TO_USEC(beacon_period));
  6108. REG_WRITE(ah, AR_NDP_PERIOD, TU_TO_USEC(beacon_period));
  6109. beacon_period &= ~ATH9K_BEACON_ENA;
  6110. if (beacon_period & ATH9K_BEACON_RESET_TSF) {
  6111. beacon_period &= ~ATH9K_BEACON_RESET_TSF;
  6112. ath9k_hw_reset_tsf(ah);
  6113. }
  6114. REG_SET_BIT(ah, AR_TIMER_MODE, flags);
  6115. }
  6116. void
  6117. ath9k_hw_set_sta_beacon_timers(struct ath_hal *ah,
  6118. const struct ath9k_beacon_state *bs)
  6119. {
  6120. u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
  6121. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6122. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
  6123. REG_WRITE(ah, AR_BEACON_PERIOD,
  6124. TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
  6125. REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
  6126. TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
  6127. REG_RMW_FIELD(ah, AR_RSSI_THR,
  6128. AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
  6129. beaconintval = bs->bs_intval & ATH9K_BEACON_PERIOD;
  6130. if (bs->bs_sleepduration > beaconintval)
  6131. beaconintval = bs->bs_sleepduration;
  6132. dtimperiod = bs->bs_dtimperiod;
  6133. if (bs->bs_sleepduration > dtimperiod)
  6134. dtimperiod = bs->bs_sleepduration;
  6135. if (beaconintval == dtimperiod)
  6136. nextTbtt = bs->bs_nextdtim;
  6137. else
  6138. nextTbtt = bs->bs_nexttbtt;
  6139. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: next DTIM %d\n", __func__,
  6140. bs->bs_nextdtim);
  6141. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: next beacon %d\n", __func__,
  6142. nextTbtt);
  6143. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: beacon period %d\n", __func__,
  6144. beaconintval);
  6145. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: DTIM period %d\n", __func__,
  6146. dtimperiod);
  6147. REG_WRITE(ah, AR_NEXT_DTIM,
  6148. TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
  6149. REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
  6150. REG_WRITE(ah, AR_SLEEP1,
  6151. SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
  6152. | AR_SLEEP1_ASSUME_DTIM);
  6153. if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
  6154. beacontimeout = (BEACON_TIMEOUT_VAL << 3);
  6155. else
  6156. beacontimeout = MIN_BEACON_TIMEOUT_VAL;
  6157. REG_WRITE(ah, AR_SLEEP2,
  6158. SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
  6159. REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
  6160. REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
  6161. REG_SET_BIT(ah, AR_TIMER_MODE,
  6162. AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
  6163. AR_DTIM_TIMER_EN);
  6164. }
  6165. bool ath9k_hw_keyisvalid(struct ath_hal *ah, u16 entry)
  6166. {
  6167. if (entry < ah->ah_caps.keycache_size) {
  6168. u32 val = REG_READ(ah, AR_KEYTABLE_MAC1(entry));
  6169. if (val & AR_KEYTABLE_VALID)
  6170. return true;
  6171. }
  6172. return false;
  6173. }
  6174. bool ath9k_hw_keyreset(struct ath_hal *ah, u16 entry)
  6175. {
  6176. u32 keyType;
  6177. if (entry >= ah->ah_caps.keycache_size) {
  6178. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6179. "%s: entry %u out of range\n", __func__, entry);
  6180. return false;
  6181. }
  6182. keyType = REG_READ(ah, AR_KEYTABLE_TYPE(entry));
  6183. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
  6184. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
  6185. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
  6186. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
  6187. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
  6188. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
  6189. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
  6190. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
  6191. if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
  6192. u16 micentry = entry + 64;
  6193. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
  6194. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  6195. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
  6196. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  6197. }
  6198. if (ah->ah_curchan == NULL)
  6199. return true;
  6200. return true;
  6201. }
  6202. bool
  6203. ath9k_hw_keysetmac(struct ath_hal *ah, u16 entry,
  6204. const u8 *mac)
  6205. {
  6206. u32 macHi, macLo;
  6207. if (entry >= ah->ah_caps.keycache_size) {
  6208. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6209. "%s: entry %u out of range\n", __func__, entry);
  6210. return false;
  6211. }
  6212. if (mac != NULL) {
  6213. macHi = (mac[5] << 8) | mac[4];
  6214. macLo = (mac[3] << 24) | (mac[2] << 16)
  6215. | (mac[1] << 8) | mac[0];
  6216. macLo >>= 1;
  6217. macLo |= (macHi & 1) << 31;
  6218. macHi >>= 1;
  6219. } else {
  6220. macLo = macHi = 0;
  6221. }
  6222. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
  6223. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | AR_KEYTABLE_VALID);
  6224. return true;
  6225. }
  6226. bool
  6227. ath9k_hw_set_keycache_entry(struct ath_hal *ah, u16 entry,
  6228. const struct ath9k_keyval *k,
  6229. const u8 *mac, int xorKey)
  6230. {
  6231. const struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6232. u32 key0, key1, key2, key3, key4;
  6233. u32 keyType;
  6234. u32 xorMask = xorKey ?
  6235. (ATH9K_KEY_XOR << 24 | ATH9K_KEY_XOR << 16 | ATH9K_KEY_XOR << 8
  6236. | ATH9K_KEY_XOR) : 0;
  6237. struct ath_hal_5416 *ahp = AH5416(ah);
  6238. if (entry >= pCap->keycache_size) {
  6239. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6240. "%s: entry %u out of range\n", __func__, entry);
  6241. return false;
  6242. }
  6243. switch (k->kv_type) {
  6244. case ATH9K_CIPHER_AES_OCB:
  6245. keyType = AR_KEYTABLE_TYPE_AES;
  6246. break;
  6247. case ATH9K_CIPHER_AES_CCM:
  6248. if (!(pCap->hw_caps & ATH9K_HW_CAP_CIPHER_AESCCM)) {
  6249. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6250. "%s: AES-CCM not supported by "
  6251. "mac rev 0x%x\n", __func__,
  6252. ah->ah_macRev);
  6253. return false;
  6254. }
  6255. keyType = AR_KEYTABLE_TYPE_CCM;
  6256. break;
  6257. case ATH9K_CIPHER_TKIP:
  6258. keyType = AR_KEYTABLE_TYPE_TKIP;
  6259. if (ATH9K_IS_MIC_ENABLED(ah)
  6260. && entry + 64 >= pCap->keycache_size) {
  6261. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6262. "%s: entry %u inappropriate for TKIP\n",
  6263. __func__, entry);
  6264. return false;
  6265. }
  6266. break;
  6267. case ATH9K_CIPHER_WEP:
  6268. if (k->kv_len < LEN_WEP40) {
  6269. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6270. "%s: WEP key length %u too small\n",
  6271. __func__, k->kv_len);
  6272. return false;
  6273. }
  6274. if (k->kv_len <= LEN_WEP40)
  6275. keyType = AR_KEYTABLE_TYPE_40;
  6276. else if (k->kv_len <= LEN_WEP104)
  6277. keyType = AR_KEYTABLE_TYPE_104;
  6278. else
  6279. keyType = AR_KEYTABLE_TYPE_128;
  6280. break;
  6281. case ATH9K_CIPHER_CLR:
  6282. keyType = AR_KEYTABLE_TYPE_CLR;
  6283. break;
  6284. default:
  6285. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6286. "%s: cipher %u not supported\n", __func__,
  6287. k->kv_type);
  6288. return false;
  6289. }
  6290. key0 = get_unaligned_le32(k->kv_val + 0) ^ xorMask;
  6291. key1 = (get_unaligned_le16(k->kv_val + 4) ^ xorMask) & 0xffff;
  6292. key2 = get_unaligned_le32(k->kv_val + 6) ^ xorMask;
  6293. key3 = (get_unaligned_le16(k->kv_val + 10) ^ xorMask) & 0xffff;
  6294. key4 = get_unaligned_le32(k->kv_val + 12) ^ xorMask;
  6295. if (k->kv_len <= LEN_WEP104)
  6296. key4 &= 0xff;
  6297. if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
  6298. u16 micentry = entry + 64;
  6299. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
  6300. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
  6301. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  6302. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  6303. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  6304. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  6305. (void) ath9k_hw_keysetmac(ah, entry, mac);
  6306. if (ahp->ah_miscMode & AR_PCU_MIC_NEW_LOC_ENA) {
  6307. u32 mic0, mic1, mic2, mic3, mic4;
  6308. mic0 = get_unaligned_le32(k->kv_mic + 0);
  6309. mic2 = get_unaligned_le32(k->kv_mic + 4);
  6310. mic1 = get_unaligned_le16(k->kv_txmic + 2) & 0xffff;
  6311. mic3 = get_unaligned_le16(k->kv_txmic + 0) & 0xffff;
  6312. mic4 = get_unaligned_le32(k->kv_txmic + 4);
  6313. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  6314. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
  6315. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  6316. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
  6317. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
  6318. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  6319. AR_KEYTABLE_TYPE_CLR);
  6320. } else {
  6321. u32 mic0, mic2;
  6322. mic0 = get_unaligned_le32(k->kv_mic + 0);
  6323. mic2 = get_unaligned_le32(k->kv_mic + 4);
  6324. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  6325. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  6326. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  6327. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  6328. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  6329. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  6330. AR_KEYTABLE_TYPE_CLR);
  6331. }
  6332. REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
  6333. REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
  6334. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  6335. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  6336. } else {
  6337. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  6338. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  6339. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  6340. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  6341. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  6342. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  6343. (void) ath9k_hw_keysetmac(ah, entry, mac);
  6344. }
  6345. if (ah->ah_curchan == NULL)
  6346. return true;
  6347. return true;
  6348. }
  6349. bool
  6350. ath9k_hw_updatetxtriglevel(struct ath_hal *ah, bool bIncTrigLevel)
  6351. {
  6352. struct ath_hal_5416 *ahp = AH5416(ah);
  6353. u32 txcfg, curLevel, newLevel;
  6354. enum ath9k_int omask;
  6355. if (ah->ah_txTrigLevel >= MAX_TX_FIFO_THRESHOLD)
  6356. return false;
  6357. omask = ath9k_hw_set_interrupts(ah,
  6358. ahp->ah_maskReg & ~ATH9K_INT_GLOBAL);
  6359. txcfg = REG_READ(ah, AR_TXCFG);
  6360. curLevel = MS(txcfg, AR_FTRIG);
  6361. newLevel = curLevel;
  6362. if (bIncTrigLevel) {
  6363. if (curLevel < MAX_TX_FIFO_THRESHOLD)
  6364. newLevel++;
  6365. } else if (curLevel > MIN_TX_FIFO_THRESHOLD)
  6366. newLevel--;
  6367. if (newLevel != curLevel)
  6368. REG_WRITE(ah, AR_TXCFG,
  6369. (txcfg & ~AR_FTRIG) | SM(newLevel, AR_FTRIG));
  6370. ath9k_hw_set_interrupts(ah, omask);
  6371. ah->ah_txTrigLevel = newLevel;
  6372. return newLevel != curLevel;
  6373. }
  6374. bool ath9k_hw_set_txq_props(struct ath_hal *ah, int q,
  6375. const struct ath9k_tx_queue_info *qinfo)
  6376. {
  6377. u32 cw;
  6378. struct ath_hal_5416 *ahp = AH5416(ah);
  6379. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6380. struct ath9k_tx_queue_info *qi;
  6381. if (q >= pCap->total_queues) {
  6382. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6383. __func__, q);
  6384. return false;
  6385. }
  6386. qi = &ahp->ah_txq[q];
  6387. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6388. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue\n",
  6389. __func__);
  6390. return false;
  6391. }
  6392. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: queue %p\n", __func__, qi);
  6393. qi->tqi_ver = qinfo->tqi_ver;
  6394. qi->tqi_subtype = qinfo->tqi_subtype;
  6395. qi->tqi_qflags = qinfo->tqi_qflags;
  6396. qi->tqi_priority = qinfo->tqi_priority;
  6397. if (qinfo->tqi_aifs != ATH9K_TXQ_USEDEFAULT)
  6398. qi->tqi_aifs = min(qinfo->tqi_aifs, 255U);
  6399. else
  6400. qi->tqi_aifs = INIT_AIFS;
  6401. if (qinfo->tqi_cwmin != ATH9K_TXQ_USEDEFAULT) {
  6402. cw = min(qinfo->tqi_cwmin, 1024U);
  6403. qi->tqi_cwmin = 1;
  6404. while (qi->tqi_cwmin < cw)
  6405. qi->tqi_cwmin = (qi->tqi_cwmin << 1) | 1;
  6406. } else
  6407. qi->tqi_cwmin = qinfo->tqi_cwmin;
  6408. if (qinfo->tqi_cwmax != ATH9K_TXQ_USEDEFAULT) {
  6409. cw = min(qinfo->tqi_cwmax, 1024U);
  6410. qi->tqi_cwmax = 1;
  6411. while (qi->tqi_cwmax < cw)
  6412. qi->tqi_cwmax = (qi->tqi_cwmax << 1) | 1;
  6413. } else
  6414. qi->tqi_cwmax = INIT_CWMAX;
  6415. if (qinfo->tqi_shretry != 0)
  6416. qi->tqi_shretry = min((u32) qinfo->tqi_shretry, 15U);
  6417. else
  6418. qi->tqi_shretry = INIT_SH_RETRY;
  6419. if (qinfo->tqi_lgretry != 0)
  6420. qi->tqi_lgretry = min((u32) qinfo->tqi_lgretry, 15U);
  6421. else
  6422. qi->tqi_lgretry = INIT_LG_RETRY;
  6423. qi->tqi_cbrPeriod = qinfo->tqi_cbrPeriod;
  6424. qi->tqi_cbrOverflowLimit = qinfo->tqi_cbrOverflowLimit;
  6425. qi->tqi_burstTime = qinfo->tqi_burstTime;
  6426. qi->tqi_readyTime = qinfo->tqi_readyTime;
  6427. switch (qinfo->tqi_subtype) {
  6428. case ATH9K_WME_UPSD:
  6429. if (qi->tqi_type == ATH9K_TX_QUEUE_DATA)
  6430. qi->tqi_intFlags = ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS;
  6431. break;
  6432. default:
  6433. break;
  6434. }
  6435. return true;
  6436. }
  6437. bool ath9k_hw_get_txq_props(struct ath_hal *ah, int q,
  6438. struct ath9k_tx_queue_info *qinfo)
  6439. {
  6440. struct ath_hal_5416 *ahp = AH5416(ah);
  6441. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6442. struct ath9k_tx_queue_info *qi;
  6443. if (q >= pCap->total_queues) {
  6444. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6445. __func__, q);
  6446. return false;
  6447. }
  6448. qi = &ahp->ah_txq[q];
  6449. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6450. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue\n",
  6451. __func__);
  6452. return false;
  6453. }
  6454. qinfo->tqi_qflags = qi->tqi_qflags;
  6455. qinfo->tqi_ver = qi->tqi_ver;
  6456. qinfo->tqi_subtype = qi->tqi_subtype;
  6457. qinfo->tqi_qflags = qi->tqi_qflags;
  6458. qinfo->tqi_priority = qi->tqi_priority;
  6459. qinfo->tqi_aifs = qi->tqi_aifs;
  6460. qinfo->tqi_cwmin = qi->tqi_cwmin;
  6461. qinfo->tqi_cwmax = qi->tqi_cwmax;
  6462. qinfo->tqi_shretry = qi->tqi_shretry;
  6463. qinfo->tqi_lgretry = qi->tqi_lgretry;
  6464. qinfo->tqi_cbrPeriod = qi->tqi_cbrPeriod;
  6465. qinfo->tqi_cbrOverflowLimit = qi->tqi_cbrOverflowLimit;
  6466. qinfo->tqi_burstTime = qi->tqi_burstTime;
  6467. qinfo->tqi_readyTime = qi->tqi_readyTime;
  6468. return true;
  6469. }
  6470. int
  6471. ath9k_hw_setuptxqueue(struct ath_hal *ah, enum ath9k_tx_queue type,
  6472. const struct ath9k_tx_queue_info *qinfo)
  6473. {
  6474. struct ath_hal_5416 *ahp = AH5416(ah);
  6475. struct ath9k_tx_queue_info *qi;
  6476. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6477. int q;
  6478. switch (type) {
  6479. case ATH9K_TX_QUEUE_BEACON:
  6480. q = pCap->total_queues - 1;
  6481. break;
  6482. case ATH9K_TX_QUEUE_CAB:
  6483. q = pCap->total_queues - 2;
  6484. break;
  6485. case ATH9K_TX_QUEUE_PSPOLL:
  6486. q = 1;
  6487. break;
  6488. case ATH9K_TX_QUEUE_UAPSD:
  6489. q = pCap->total_queues - 3;
  6490. break;
  6491. case ATH9K_TX_QUEUE_DATA:
  6492. for (q = 0; q < pCap->total_queues; q++)
  6493. if (ahp->ah_txq[q].tqi_type ==
  6494. ATH9K_TX_QUEUE_INACTIVE)
  6495. break;
  6496. if (q == pCap->total_queues) {
  6497. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  6498. "%s: no available tx queue\n", __func__);
  6499. return -1;
  6500. }
  6501. break;
  6502. default:
  6503. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: bad tx queue type %u\n",
  6504. __func__, type);
  6505. return -1;
  6506. }
  6507. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: queue %u\n", __func__, q);
  6508. qi = &ahp->ah_txq[q];
  6509. if (qi->tqi_type != ATH9K_TX_QUEUE_INACTIVE) {
  6510. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  6511. "%s: tx queue %u already active\n", __func__, q);
  6512. return -1;
  6513. }
  6514. memset(qi, 0, sizeof(struct ath9k_tx_queue_info));
  6515. qi->tqi_type = type;
  6516. if (qinfo == NULL) {
  6517. qi->tqi_qflags =
  6518. TXQ_FLAG_TXOKINT_ENABLE
  6519. | TXQ_FLAG_TXERRINT_ENABLE
  6520. | TXQ_FLAG_TXDESCINT_ENABLE | TXQ_FLAG_TXURNINT_ENABLE;
  6521. qi->tqi_aifs = INIT_AIFS;
  6522. qi->tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  6523. qi->tqi_cwmax = INIT_CWMAX;
  6524. qi->tqi_shretry = INIT_SH_RETRY;
  6525. qi->tqi_lgretry = INIT_LG_RETRY;
  6526. qi->tqi_physCompBuf = 0;
  6527. } else {
  6528. qi->tqi_physCompBuf = qinfo->tqi_physCompBuf;
  6529. (void) ath9k_hw_set_txq_props(ah, q, qinfo);
  6530. }
  6531. return q;
  6532. }
  6533. static void
  6534. ath9k_hw_set_txq_interrupts(struct ath_hal *ah,
  6535. struct ath9k_tx_queue_info *qi)
  6536. {
  6537. struct ath_hal_5416 *ahp = AH5416(ah);
  6538. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  6539. "%s: tx ok 0x%x err 0x%x desc 0x%x eol 0x%x urn 0x%x\n",
  6540. __func__, ahp->ah_txOkInterruptMask,
  6541. ahp->ah_txErrInterruptMask, ahp->ah_txDescInterruptMask,
  6542. ahp->ah_txEolInterruptMask, ahp->ah_txUrnInterruptMask);
  6543. REG_WRITE(ah, AR_IMR_S0,
  6544. SM(ahp->ah_txOkInterruptMask, AR_IMR_S0_QCU_TXOK)
  6545. | SM(ahp->ah_txDescInterruptMask, AR_IMR_S0_QCU_TXDESC));
  6546. REG_WRITE(ah, AR_IMR_S1,
  6547. SM(ahp->ah_txErrInterruptMask, AR_IMR_S1_QCU_TXERR)
  6548. | SM(ahp->ah_txEolInterruptMask, AR_IMR_S1_QCU_TXEOL));
  6549. REG_RMW_FIELD(ah, AR_IMR_S2,
  6550. AR_IMR_S2_QCU_TXURN, ahp->ah_txUrnInterruptMask);
  6551. }
  6552. bool ath9k_hw_releasetxqueue(struct ath_hal *ah, u32 q)
  6553. {
  6554. struct ath_hal_5416 *ahp = AH5416(ah);
  6555. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6556. struct ath9k_tx_queue_info *qi;
  6557. if (q >= pCap->total_queues) {
  6558. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6559. __func__, q);
  6560. return false;
  6561. }
  6562. qi = &ahp->ah_txq[q];
  6563. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6564. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue %u\n",
  6565. __func__, q);
  6566. return false;
  6567. }
  6568. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: release queue %u\n",
  6569. __func__, q);
  6570. qi->tqi_type = ATH9K_TX_QUEUE_INACTIVE;
  6571. ahp->ah_txOkInterruptMask &= ~(1 << q);
  6572. ahp->ah_txErrInterruptMask &= ~(1 << q);
  6573. ahp->ah_txDescInterruptMask &= ~(1 << q);
  6574. ahp->ah_txEolInterruptMask &= ~(1 << q);
  6575. ahp->ah_txUrnInterruptMask &= ~(1 << q);
  6576. ath9k_hw_set_txq_interrupts(ah, qi);
  6577. return true;
  6578. }
  6579. bool ath9k_hw_resettxqueue(struct ath_hal *ah, u32 q)
  6580. {
  6581. struct ath_hal_5416 *ahp = AH5416(ah);
  6582. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6583. struct ath9k_channel *chan = ah->ah_curchan;
  6584. struct ath9k_tx_queue_info *qi;
  6585. u32 cwMin, chanCwMin, value;
  6586. if (q >= pCap->total_queues) {
  6587. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6588. __func__, q);
  6589. return false;
  6590. }
  6591. qi = &ahp->ah_txq[q];
  6592. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6593. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue %u\n",
  6594. __func__, q);
  6595. return true;
  6596. }
  6597. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: reset queue %u\n", __func__, q);
  6598. if (qi->tqi_cwmin == ATH9K_TXQ_USEDEFAULT) {
  6599. if (chan && IS_CHAN_B(chan))
  6600. chanCwMin = INIT_CWMIN_11B;
  6601. else
  6602. chanCwMin = INIT_CWMIN;
  6603. for (cwMin = 1; cwMin < chanCwMin; cwMin = (cwMin << 1) | 1);
  6604. } else
  6605. cwMin = qi->tqi_cwmin;
  6606. REG_WRITE(ah, AR_DLCL_IFS(q), SM(cwMin, AR_D_LCL_IFS_CWMIN)
  6607. | SM(qi->tqi_cwmax, AR_D_LCL_IFS_CWMAX)
  6608. | SM(qi->tqi_aifs, AR_D_LCL_IFS_AIFS));
  6609. REG_WRITE(ah, AR_DRETRY_LIMIT(q),
  6610. SM(INIT_SSH_RETRY, AR_D_RETRY_LIMIT_STA_SH)
  6611. | SM(INIT_SLG_RETRY, AR_D_RETRY_LIMIT_STA_LG)
  6612. | SM(qi->tqi_shretry, AR_D_RETRY_LIMIT_FR_SH));
  6613. REG_WRITE(ah, AR_QMISC(q), AR_Q_MISC_DCU_EARLY_TERM_REQ);
  6614. REG_WRITE(ah, AR_DMISC(q),
  6615. AR_D_MISC_CW_BKOFF_EN | AR_D_MISC_FRAG_WAIT_EN | 0x2);
  6616. if (qi->tqi_cbrPeriod) {
  6617. REG_WRITE(ah, AR_QCBRCFG(q),
  6618. SM(qi->tqi_cbrPeriod, AR_Q_CBRCFG_INTERVAL)
  6619. | SM(qi->tqi_cbrOverflowLimit,
  6620. AR_Q_CBRCFG_OVF_THRESH));
  6621. REG_WRITE(ah, AR_QMISC(q),
  6622. REG_READ(ah,
  6623. AR_QMISC(q)) | AR_Q_MISC_FSP_CBR | (qi->
  6624. tqi_cbrOverflowLimit
  6625. ?
  6626. AR_Q_MISC_CBR_EXP_CNTR_LIMIT_EN
  6627. :
  6628. 0));
  6629. }
  6630. if (qi->tqi_readyTime && (qi->tqi_type != ATH9K_TX_QUEUE_CAB)) {
  6631. REG_WRITE(ah, AR_QRDYTIMECFG(q),
  6632. SM(qi->tqi_readyTime, AR_Q_RDYTIMECFG_DURATION) |
  6633. AR_Q_RDYTIMECFG_EN);
  6634. }
  6635. REG_WRITE(ah, AR_DCHNTIME(q),
  6636. SM(qi->tqi_burstTime, AR_D_CHNTIME_DUR) |
  6637. (qi->tqi_burstTime ? AR_D_CHNTIME_EN : 0));
  6638. if (qi->tqi_burstTime
  6639. && (qi->tqi_qflags & TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)) {
  6640. REG_WRITE(ah, AR_QMISC(q),
  6641. REG_READ(ah,
  6642. AR_QMISC(q)) |
  6643. AR_Q_MISC_RDYTIME_EXP_POLICY);
  6644. }
  6645. if (qi->tqi_qflags & TXQ_FLAG_BACKOFF_DISABLE) {
  6646. REG_WRITE(ah, AR_DMISC(q),
  6647. REG_READ(ah, AR_DMISC(q)) |
  6648. AR_D_MISC_POST_FR_BKOFF_DIS);
  6649. }
  6650. if (qi->tqi_qflags & TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE) {
  6651. REG_WRITE(ah, AR_DMISC(q),
  6652. REG_READ(ah, AR_DMISC(q)) |
  6653. AR_D_MISC_FRAG_BKOFF_EN);
  6654. }
  6655. switch (qi->tqi_type) {
  6656. case ATH9K_TX_QUEUE_BEACON:
  6657. REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
  6658. | AR_Q_MISC_FSP_DBA_GATED
  6659. | AR_Q_MISC_BEACON_USE
  6660. | AR_Q_MISC_CBR_INCR_DIS1);
  6661. REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
  6662. | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
  6663. AR_D_MISC_ARB_LOCKOUT_CNTRL_S)
  6664. | AR_D_MISC_BEACON_USE
  6665. | AR_D_MISC_POST_FR_BKOFF_DIS);
  6666. break;
  6667. case ATH9K_TX_QUEUE_CAB:
  6668. REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
  6669. | AR_Q_MISC_FSP_DBA_GATED
  6670. | AR_Q_MISC_CBR_INCR_DIS1
  6671. | AR_Q_MISC_CBR_INCR_DIS0);
  6672. value = (qi->tqi_readyTime
  6673. - (ah->ah_config.sw_beacon_response_time -
  6674. ah->ah_config.dma_beacon_response_time)
  6675. -
  6676. ah->ah_config.additional_swba_backoff) *
  6677. 1024;
  6678. REG_WRITE(ah, AR_QRDYTIMECFG(q),
  6679. value | AR_Q_RDYTIMECFG_EN);
  6680. REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
  6681. | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
  6682. AR_D_MISC_ARB_LOCKOUT_CNTRL_S));
  6683. break;
  6684. case ATH9K_TX_QUEUE_PSPOLL:
  6685. REG_WRITE(ah, AR_QMISC(q),
  6686. REG_READ(ah,
  6687. AR_QMISC(q)) | AR_Q_MISC_CBR_INCR_DIS1);
  6688. break;
  6689. case ATH9K_TX_QUEUE_UAPSD:
  6690. REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
  6691. | AR_D_MISC_POST_FR_BKOFF_DIS);
  6692. break;
  6693. default:
  6694. break;
  6695. }
  6696. if (qi->tqi_intFlags & ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS) {
  6697. REG_WRITE(ah, AR_DMISC(q),
  6698. REG_READ(ah, AR_DMISC(q)) |
  6699. SM(AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL,
  6700. AR_D_MISC_ARB_LOCKOUT_CNTRL) |
  6701. AR_D_MISC_POST_FR_BKOFF_DIS);
  6702. }
  6703. if (qi->tqi_qflags & TXQ_FLAG_TXOKINT_ENABLE)
  6704. ahp->ah_txOkInterruptMask |= 1 << q;
  6705. else
  6706. ahp->ah_txOkInterruptMask &= ~(1 << q);
  6707. if (qi->tqi_qflags & TXQ_FLAG_TXERRINT_ENABLE)
  6708. ahp->ah_txErrInterruptMask |= 1 << q;
  6709. else
  6710. ahp->ah_txErrInterruptMask &= ~(1 << q);
  6711. if (qi->tqi_qflags & TXQ_FLAG_TXDESCINT_ENABLE)
  6712. ahp->ah_txDescInterruptMask |= 1 << q;
  6713. else
  6714. ahp->ah_txDescInterruptMask &= ~(1 << q);
  6715. if (qi->tqi_qflags & TXQ_FLAG_TXEOLINT_ENABLE)
  6716. ahp->ah_txEolInterruptMask |= 1 << q;
  6717. else
  6718. ahp->ah_txEolInterruptMask &= ~(1 << q);
  6719. if (qi->tqi_qflags & TXQ_FLAG_TXURNINT_ENABLE)
  6720. ahp->ah_txUrnInterruptMask |= 1 << q;
  6721. else
  6722. ahp->ah_txUrnInterruptMask &= ~(1 << q);
  6723. ath9k_hw_set_txq_interrupts(ah, qi);
  6724. return true;
  6725. }
  6726. void ath9k_hw_gettxintrtxqs(struct ath_hal *ah, u32 *txqs)
  6727. {
  6728. struct ath_hal_5416 *ahp = AH5416(ah);
  6729. *txqs &= ahp->ah_intrTxqs;
  6730. ahp->ah_intrTxqs &= ~(*txqs);
  6731. }
  6732. bool
  6733. ath9k_hw_filltxdesc(struct ath_hal *ah, struct ath_desc *ds,
  6734. u32 segLen, bool firstSeg,
  6735. bool lastSeg, const struct ath_desc *ds0)
  6736. {
  6737. struct ar5416_desc *ads = AR5416DESC(ds);
  6738. if (firstSeg) {
  6739. ads->ds_ctl1 |= segLen | (lastSeg ? 0 : AR_TxMore);
  6740. } else if (lastSeg) {
  6741. ads->ds_ctl0 = 0;
  6742. ads->ds_ctl1 = segLen;
  6743. ads->ds_ctl2 = AR5416DESC_CONST(ds0)->ds_ctl2;
  6744. ads->ds_ctl3 = AR5416DESC_CONST(ds0)->ds_ctl3;
  6745. } else {
  6746. ads->ds_ctl0 = 0;
  6747. ads->ds_ctl1 = segLen | AR_TxMore;
  6748. ads->ds_ctl2 = 0;
  6749. ads->ds_ctl3 = 0;
  6750. }
  6751. ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
  6752. ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
  6753. ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
  6754. ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
  6755. ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
  6756. return true;
  6757. }
  6758. void ath9k_hw_cleartxdesc(struct ath_hal *ah, struct ath_desc *ds)
  6759. {
  6760. struct ar5416_desc *ads = AR5416DESC(ds);
  6761. ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
  6762. ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
  6763. ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
  6764. ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
  6765. ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
  6766. }
  6767. int
  6768. ath9k_hw_txprocdesc(struct ath_hal *ah, struct ath_desc *ds)
  6769. {
  6770. struct ar5416_desc *ads = AR5416DESC(ds);
  6771. if ((ads->ds_txstatus9 & AR_TxDone) == 0)
  6772. return -EINPROGRESS;
  6773. ds->ds_txstat.ts_seqnum = MS(ads->ds_txstatus9, AR_SeqNum);
  6774. ds->ds_txstat.ts_tstamp = ads->AR_SendTimestamp;
  6775. ds->ds_txstat.ts_status = 0;
  6776. ds->ds_txstat.ts_flags = 0;
  6777. if (ads->ds_txstatus1 & AR_ExcessiveRetries)
  6778. ds->ds_txstat.ts_status |= ATH9K_TXERR_XRETRY;
  6779. if (ads->ds_txstatus1 & AR_Filtered)
  6780. ds->ds_txstat.ts_status |= ATH9K_TXERR_FILT;
  6781. if (ads->ds_txstatus1 & AR_FIFOUnderrun)
  6782. ds->ds_txstat.ts_status |= ATH9K_TXERR_FIFO;
  6783. if (ads->ds_txstatus9 & AR_TxOpExceeded)
  6784. ds->ds_txstat.ts_status |= ATH9K_TXERR_XTXOP;
  6785. if (ads->ds_txstatus1 & AR_TxTimerExpired)
  6786. ds->ds_txstat.ts_status |= ATH9K_TXERR_TIMER_EXPIRED;
  6787. if (ads->ds_txstatus1 & AR_DescCfgErr)
  6788. ds->ds_txstat.ts_flags |= ATH9K_TX_DESC_CFG_ERR;
  6789. if (ads->ds_txstatus1 & AR_TxDataUnderrun) {
  6790. ds->ds_txstat.ts_flags |= ATH9K_TX_DATA_UNDERRUN;
  6791. ath9k_hw_updatetxtriglevel(ah, true);
  6792. }
  6793. if (ads->ds_txstatus1 & AR_TxDelimUnderrun) {
  6794. ds->ds_txstat.ts_flags |= ATH9K_TX_DELIM_UNDERRUN;
  6795. ath9k_hw_updatetxtriglevel(ah, true);
  6796. }
  6797. if (ads->ds_txstatus0 & AR_TxBaStatus) {
  6798. ds->ds_txstat.ts_flags |= ATH9K_TX_BA;
  6799. ds->ds_txstat.ba_low = ads->AR_BaBitmapLow;
  6800. ds->ds_txstat.ba_high = ads->AR_BaBitmapHigh;
  6801. }
  6802. ds->ds_txstat.ts_rateindex = MS(ads->ds_txstatus9, AR_FinalTxIdx);
  6803. switch (ds->ds_txstat.ts_rateindex) {
  6804. case 0:
  6805. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate0);
  6806. break;
  6807. case 1:
  6808. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate1);
  6809. break;
  6810. case 2:
  6811. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate2);
  6812. break;
  6813. case 3:
  6814. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate3);
  6815. break;
  6816. }
  6817. ds->ds_txstat.ts_rssi = MS(ads->ds_txstatus5, AR_TxRSSICombined);
  6818. ds->ds_txstat.ts_rssi_ctl0 = MS(ads->ds_txstatus0, AR_TxRSSIAnt00);
  6819. ds->ds_txstat.ts_rssi_ctl1 = MS(ads->ds_txstatus0, AR_TxRSSIAnt01);
  6820. ds->ds_txstat.ts_rssi_ctl2 = MS(ads->ds_txstatus0, AR_TxRSSIAnt02);
  6821. ds->ds_txstat.ts_rssi_ext0 = MS(ads->ds_txstatus5, AR_TxRSSIAnt10);
  6822. ds->ds_txstat.ts_rssi_ext1 = MS(ads->ds_txstatus5, AR_TxRSSIAnt11);
  6823. ds->ds_txstat.ts_rssi_ext2 = MS(ads->ds_txstatus5, AR_TxRSSIAnt12);
  6824. ds->ds_txstat.evm0 = ads->AR_TxEVM0;
  6825. ds->ds_txstat.evm1 = ads->AR_TxEVM1;
  6826. ds->ds_txstat.evm2 = ads->AR_TxEVM2;
  6827. ds->ds_txstat.ts_shortretry = MS(ads->ds_txstatus1, AR_RTSFailCnt);
  6828. ds->ds_txstat.ts_longretry = MS(ads->ds_txstatus1, AR_DataFailCnt);
  6829. ds->ds_txstat.ts_virtcol = MS(ads->ds_txstatus1, AR_VirtRetryCnt);
  6830. ds->ds_txstat.ts_antenna = 1;
  6831. return 0;
  6832. }
  6833. void
  6834. ath9k_hw_set11n_txdesc(struct ath_hal *ah, struct ath_desc *ds,
  6835. u32 pktLen, enum ath9k_pkt_type type, u32 txPower,
  6836. u32 keyIx, enum ath9k_key_type keyType, u32 flags)
  6837. {
  6838. struct ar5416_desc *ads = AR5416DESC(ds);
  6839. struct ath_hal_5416 *ahp = AH5416(ah);
  6840. txPower += ahp->ah_txPowerIndexOffset;
  6841. if (txPower > 63)
  6842. txPower = 63;
  6843. ads->ds_ctl0 = (pktLen & AR_FrameLen)
  6844. | (flags & ATH9K_TXDESC_VMF ? AR_VirtMoreFrag : 0)
  6845. | SM(txPower, AR_XmitPower)
  6846. | (flags & ATH9K_TXDESC_VEOL ? AR_VEOL : 0)
  6847. | (flags & ATH9K_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
  6848. | (flags & ATH9K_TXDESC_INTREQ ? AR_TxIntrReq : 0)
  6849. | (keyIx != ATH9K_TXKEYIX_INVALID ? AR_DestIdxValid : 0);
  6850. ads->ds_ctl1 =
  6851. (keyIx != ATH9K_TXKEYIX_INVALID ? SM(keyIx, AR_DestIdx) : 0)
  6852. | SM(type, AR_FrameType)
  6853. | (flags & ATH9K_TXDESC_NOACK ? AR_NoAck : 0)
  6854. | (flags & ATH9K_TXDESC_EXT_ONLY ? AR_ExtOnly : 0)
  6855. | (flags & ATH9K_TXDESC_EXT_AND_CTL ? AR_ExtAndCtl : 0);
  6856. ads->ds_ctl6 = SM(keyType, AR_EncrType);
  6857. if (AR_SREV_9285(ah)) {
  6858. ads->ds_ctl8 = 0;
  6859. ads->ds_ctl9 = 0;
  6860. ads->ds_ctl10 = 0;
  6861. ads->ds_ctl11 = 0;
  6862. }
  6863. }
  6864. void
  6865. ath9k_hw_set11n_ratescenario(struct ath_hal *ah, struct ath_desc *ds,
  6866. struct ath_desc *lastds,
  6867. u32 durUpdateEn, u32 rtsctsRate,
  6868. u32 rtsctsDuration,
  6869. struct ath9k_11n_rate_series series[],
  6870. u32 nseries, u32 flags)
  6871. {
  6872. struct ar5416_desc *ads = AR5416DESC(ds);
  6873. struct ar5416_desc *last_ads = AR5416DESC(lastds);
  6874. u32 ds_ctl0;
  6875. (void) nseries;
  6876. (void) rtsctsDuration;
  6877. if (flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA)) {
  6878. ds_ctl0 = ads->ds_ctl0;
  6879. if (flags & ATH9K_TXDESC_RTSENA) {
  6880. ds_ctl0 &= ~AR_CTSEnable;
  6881. ds_ctl0 |= AR_RTSEnable;
  6882. } else {
  6883. ds_ctl0 &= ~AR_RTSEnable;
  6884. ds_ctl0 |= AR_CTSEnable;
  6885. }
  6886. ads->ds_ctl0 = ds_ctl0;
  6887. } else {
  6888. ads->ds_ctl0 =
  6889. (ads->ds_ctl0 & ~(AR_RTSEnable | AR_CTSEnable));
  6890. }
  6891. ads->ds_ctl2 = set11nTries(series, 0)
  6892. | set11nTries(series, 1)
  6893. | set11nTries(series, 2)
  6894. | set11nTries(series, 3)
  6895. | (durUpdateEn ? AR_DurUpdateEna : 0)
  6896. | SM(0, AR_BurstDur);
  6897. ads->ds_ctl3 = set11nRate(series, 0)
  6898. | set11nRate(series, 1)
  6899. | set11nRate(series, 2)
  6900. | set11nRate(series, 3);
  6901. ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
  6902. | set11nPktDurRTSCTS(series, 1);
  6903. ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
  6904. | set11nPktDurRTSCTS(series, 3);
  6905. ads->ds_ctl7 = set11nRateFlags(series, 0)
  6906. | set11nRateFlags(series, 1)
  6907. | set11nRateFlags(series, 2)
  6908. | set11nRateFlags(series, 3)
  6909. | SM(rtsctsRate, AR_RTSCTSRate);
  6910. last_ads->ds_ctl2 = ads->ds_ctl2;
  6911. last_ads->ds_ctl3 = ads->ds_ctl3;
  6912. }
  6913. void
  6914. ath9k_hw_set11n_aggr_first(struct ath_hal *ah, struct ath_desc *ds,
  6915. u32 aggrLen)
  6916. {
  6917. struct ar5416_desc *ads = AR5416DESC(ds);
  6918. ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
  6919. ads->ds_ctl6 &= ~AR_AggrLen;
  6920. ads->ds_ctl6 |= SM(aggrLen, AR_AggrLen);
  6921. }
  6922. void
  6923. ath9k_hw_set11n_aggr_middle(struct ath_hal *ah, struct ath_desc *ds,
  6924. u32 numDelims)
  6925. {
  6926. struct ar5416_desc *ads = AR5416DESC(ds);
  6927. unsigned int ctl6;
  6928. ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
  6929. ctl6 = ads->ds_ctl6;
  6930. ctl6 &= ~AR_PadDelim;
  6931. ctl6 |= SM(numDelims, AR_PadDelim);
  6932. ads->ds_ctl6 = ctl6;
  6933. }
  6934. void ath9k_hw_set11n_aggr_last(struct ath_hal *ah, struct ath_desc *ds)
  6935. {
  6936. struct ar5416_desc *ads = AR5416DESC(ds);
  6937. ads->ds_ctl1 |= AR_IsAggr;
  6938. ads->ds_ctl1 &= ~AR_MoreAggr;
  6939. ads->ds_ctl6 &= ~AR_PadDelim;
  6940. }
  6941. void ath9k_hw_clr11n_aggr(struct ath_hal *ah, struct ath_desc *ds)
  6942. {
  6943. struct ar5416_desc *ads = AR5416DESC(ds);
  6944. ads->ds_ctl1 &= (~AR_IsAggr & ~AR_MoreAggr);
  6945. }
  6946. void
  6947. ath9k_hw_set11n_burstduration(struct ath_hal *ah, struct ath_desc *ds,
  6948. u32 burstDuration)
  6949. {
  6950. struct ar5416_desc *ads = AR5416DESC(ds);
  6951. ads->ds_ctl2 &= ~AR_BurstDur;
  6952. ads->ds_ctl2 |= SM(burstDuration, AR_BurstDur);
  6953. }
  6954. void
  6955. ath9k_hw_set11n_virtualmorefrag(struct ath_hal *ah, struct ath_desc *ds,
  6956. u32 vmf)
  6957. {
  6958. struct ar5416_desc *ads = AR5416DESC(ds);
  6959. if (vmf)
  6960. ads->ds_ctl0 |= AR_VirtMoreFrag;
  6961. else
  6962. ads->ds_ctl0 &= ~AR_VirtMoreFrag;
  6963. }
  6964. void ath9k_hw_putrxbuf(struct ath_hal *ah, u32 rxdp)
  6965. {
  6966. REG_WRITE(ah, AR_RXDP, rxdp);
  6967. }
  6968. void ath9k_hw_rxena(struct ath_hal *ah)
  6969. {
  6970. REG_WRITE(ah, AR_CR, AR_CR_RXE);
  6971. }
  6972. bool ath9k_hw_setrxabort(struct ath_hal *ah, bool set)
  6973. {
  6974. if (set) {
  6975. REG_SET_BIT(ah, AR_DIAG_SW,
  6976. (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  6977. if (!ath9k_hw_wait
  6978. (ah, AR_OBS_BUS_1, AR_OBS_BUS_1_RX_STATE, 0)) {
  6979. u32 reg;
  6980. REG_CLR_BIT(ah, AR_DIAG_SW,
  6981. (AR_DIAG_RX_DIS |
  6982. AR_DIAG_RX_ABORT));
  6983. reg = REG_READ(ah, AR_OBS_BUS_1);
  6984. DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
  6985. "%s: rx failed to go idle in 10 ms RXSM=0x%x\n",
  6986. __func__, reg);
  6987. return false;
  6988. }
  6989. } else {
  6990. REG_CLR_BIT(ah, AR_DIAG_SW,
  6991. (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  6992. }
  6993. return true;
  6994. }
  6995. void
  6996. ath9k_hw_setmcastfilter(struct ath_hal *ah, u32 filter0,
  6997. u32 filter1)
  6998. {
  6999. REG_WRITE(ah, AR_MCAST_FIL0, filter0);
  7000. REG_WRITE(ah, AR_MCAST_FIL1, filter1);
  7001. }
  7002. bool
  7003. ath9k_hw_setuprxdesc(struct ath_hal *ah, struct ath_desc *ds,
  7004. u32 size, u32 flags)
  7005. {
  7006. struct ar5416_desc *ads = AR5416DESC(ds);
  7007. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  7008. ads->ds_ctl1 = size & AR_BufLen;
  7009. if (flags & ATH9K_RXDESC_INTREQ)
  7010. ads->ds_ctl1 |= AR_RxIntrReq;
  7011. ads->ds_rxstatus8 &= ~AR_RxDone;
  7012. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  7013. memset(&(ads->u), 0, sizeof(ads->u));
  7014. return true;
  7015. }
  7016. int
  7017. ath9k_hw_rxprocdesc(struct ath_hal *ah, struct ath_desc *ds,
  7018. u32 pa, struct ath_desc *nds, u64 tsf)
  7019. {
  7020. struct ar5416_desc ads;
  7021. struct ar5416_desc *adsp = AR5416DESC(ds);
  7022. if ((adsp->ds_rxstatus8 & AR_RxDone) == 0)
  7023. return -EINPROGRESS;
  7024. ads.u.rx = adsp->u.rx;
  7025. ds->ds_rxstat.rs_status = 0;
  7026. ds->ds_rxstat.rs_flags = 0;
  7027. ds->ds_rxstat.rs_datalen = ads.ds_rxstatus1 & AR_DataLen;
  7028. ds->ds_rxstat.rs_tstamp = ads.AR_RcvTimestamp;
  7029. ds->ds_rxstat.rs_rssi = MS(ads.ds_rxstatus4, AR_RxRSSICombined);
  7030. ds->ds_rxstat.rs_rssi_ctl0 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt00);
  7031. ds->ds_rxstat.rs_rssi_ctl1 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt01);
  7032. ds->ds_rxstat.rs_rssi_ctl2 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt02);
  7033. ds->ds_rxstat.rs_rssi_ext0 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt10);
  7034. ds->ds_rxstat.rs_rssi_ext1 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt11);
  7035. ds->ds_rxstat.rs_rssi_ext2 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt12);
  7036. if (ads.ds_rxstatus8 & AR_RxKeyIdxValid)
  7037. ds->ds_rxstat.rs_keyix = MS(ads.ds_rxstatus8, AR_KeyIdx);
  7038. else
  7039. ds->ds_rxstat.rs_keyix = ATH9K_RXKEYIX_INVALID;
  7040. ds->ds_rxstat.rs_rate = RXSTATUS_RATE(ah, (&ads));
  7041. ds->ds_rxstat.rs_more = (ads.ds_rxstatus1 & AR_RxMore) ? 1 : 0;
  7042. ds->ds_rxstat.rs_isaggr = (ads.ds_rxstatus8 & AR_RxAggr) ? 1 : 0;
  7043. ds->ds_rxstat.rs_moreaggr =
  7044. (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
  7045. ds->ds_rxstat.rs_antenna = MS(ads.ds_rxstatus3, AR_RxAntenna);
  7046. ds->ds_rxstat.rs_flags =
  7047. (ads.ds_rxstatus3 & AR_GI) ? ATH9K_RX_GI : 0;
  7048. ds->ds_rxstat.rs_flags |=
  7049. (ads.ds_rxstatus3 & AR_2040) ? ATH9K_RX_2040 : 0;
  7050. if (ads.ds_rxstatus8 & AR_PreDelimCRCErr)
  7051. ds->ds_rxstat.rs_flags |= ATH9K_RX_DELIM_CRC_PRE;
  7052. if (ads.ds_rxstatus8 & AR_PostDelimCRCErr)
  7053. ds->ds_rxstat.rs_flags |= ATH9K_RX_DELIM_CRC_POST;
  7054. if (ads.ds_rxstatus8 & AR_DecryptBusyErr)
  7055. ds->ds_rxstat.rs_flags |= ATH9K_RX_DECRYPT_BUSY;
  7056. if ((ads.ds_rxstatus8 & AR_RxFrameOK) == 0) {
  7057. if (ads.ds_rxstatus8 & AR_CRCErr)
  7058. ds->ds_rxstat.rs_status |= ATH9K_RXERR_CRC;
  7059. else if (ads.ds_rxstatus8 & AR_PHYErr) {
  7060. u32 phyerr;
  7061. ds->ds_rxstat.rs_status |= ATH9K_RXERR_PHY;
  7062. phyerr = MS(ads.ds_rxstatus8, AR_PHYErrCode);
  7063. ds->ds_rxstat.rs_phyerr = phyerr;
  7064. } else if (ads.ds_rxstatus8 & AR_DecryptCRCErr)
  7065. ds->ds_rxstat.rs_status |= ATH9K_RXERR_DECRYPT;
  7066. else if (ads.ds_rxstatus8 & AR_MichaelErr)
  7067. ds->ds_rxstat.rs_status |= ATH9K_RXERR_MIC;
  7068. }
  7069. return 0;
  7070. }
  7071. static void ath9k_hw_setup_rate_table(struct ath_hal *ah,
  7072. struct ath9k_rate_table *rt)
  7073. {
  7074. int i;
  7075. if (rt->rateCodeToIndex[0] != 0)
  7076. return;
  7077. for (i = 0; i < 256; i++)
  7078. rt->rateCodeToIndex[i] = (u8) -1;
  7079. for (i = 0; i < rt->rateCount; i++) {
  7080. u8 code = rt->info[i].rateCode;
  7081. u8 cix = rt->info[i].controlRate;
  7082. rt->rateCodeToIndex[code] = i;
  7083. rt->rateCodeToIndex[code | rt->info[i].shortPreamble] = i;
  7084. rt->info[i].lpAckDuration =
  7085. ath9k_hw_computetxtime(ah, rt,
  7086. WLAN_CTRL_FRAME_SIZE,
  7087. cix,
  7088. false);
  7089. rt->info[i].spAckDuration =
  7090. ath9k_hw_computetxtime(ah, rt,
  7091. WLAN_CTRL_FRAME_SIZE,
  7092. cix,
  7093. true);
  7094. }
  7095. }
  7096. const struct ath9k_rate_table *ath9k_hw_getratetable(struct ath_hal *ah,
  7097. u32 mode)
  7098. {
  7099. struct ath9k_rate_table *rt;
  7100. switch (mode) {
  7101. case ATH9K_MODE_11A:
  7102. rt = &ar5416_11a_table;
  7103. break;
  7104. case ATH9K_MODE_11B:
  7105. rt = &ar5416_11b_table;
  7106. break;
  7107. case ATH9K_MODE_11G:
  7108. rt = &ar5416_11g_table;
  7109. break;
  7110. case ATH9K_MODE_11NG_HT20:
  7111. case ATH9K_MODE_11NG_HT40PLUS:
  7112. case ATH9K_MODE_11NG_HT40MINUS:
  7113. rt = &ar5416_11ng_table;
  7114. break;
  7115. case ATH9K_MODE_11NA_HT20:
  7116. case ATH9K_MODE_11NA_HT40PLUS:
  7117. case ATH9K_MODE_11NA_HT40MINUS:
  7118. rt = &ar5416_11na_table;
  7119. break;
  7120. default:
  7121. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL, "%s: invalid mode 0x%x\n",
  7122. __func__, mode);
  7123. return NULL;
  7124. }
  7125. ath9k_hw_setup_rate_table(ah, rt);
  7126. return rt;
  7127. }
  7128. static const char *ath9k_hw_devname(u16 devid)
  7129. {
  7130. switch (devid) {
  7131. case AR5416_DEVID_PCI:
  7132. case AR5416_DEVID_PCIE:
  7133. return "Atheros 5416";
  7134. case AR9160_DEVID_PCI:
  7135. return "Atheros 9160";
  7136. case AR9280_DEVID_PCI:
  7137. case AR9280_DEVID_PCIE:
  7138. return "Atheros 9280";
  7139. }
  7140. return NULL;
  7141. }
  7142. const char *ath9k_hw_probe(u16 vendorid, u16 devid)
  7143. {
  7144. return vendorid == ATHEROS_VENDOR_ID ?
  7145. ath9k_hw_devname(devid) : NULL;
  7146. }
  7147. struct ath_hal *ath9k_hw_attach(u16 devid,
  7148. struct ath_softc *sc,
  7149. void __iomem *mem,
  7150. int *error)
  7151. {
  7152. struct ath_hal *ah = NULL;
  7153. switch (devid) {
  7154. case AR5416_DEVID_PCI:
  7155. case AR5416_DEVID_PCIE:
  7156. case AR9160_DEVID_PCI:
  7157. case AR9280_DEVID_PCI:
  7158. case AR9280_DEVID_PCIE:
  7159. ah = ath9k_hw_do_attach(devid, sc, mem, error);
  7160. break;
  7161. default:
  7162. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  7163. "devid=0x%x not supported.\n", devid);
  7164. ah = NULL;
  7165. *error = -ENXIO;
  7166. break;
  7167. }
  7168. return ah;
  7169. }
  7170. u16
  7171. ath9k_hw_computetxtime(struct ath_hal *ah,
  7172. const struct ath9k_rate_table *rates,
  7173. u32 frameLen, u16 rateix,
  7174. bool shortPreamble)
  7175. {
  7176. u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
  7177. u32 kbps;
  7178. kbps = rates->info[rateix].rateKbps;
  7179. if (kbps == 0)
  7180. return 0;
  7181. switch (rates->info[rateix].phy) {
  7182. case PHY_CCK:
  7183. phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
  7184. if (shortPreamble && rates->info[rateix].shortPreamble)
  7185. phyTime >>= 1;
  7186. numBits = frameLen << 3;
  7187. txTime = CCK_SIFS_TIME + phyTime
  7188. + ((numBits * 1000) / kbps);
  7189. break;
  7190. case PHY_OFDM:
  7191. if (ah->ah_curchan && IS_CHAN_QUARTER_RATE(ah->ah_curchan)) {
  7192. bitsPerSymbol =
  7193. (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
  7194. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  7195. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  7196. txTime = OFDM_SIFS_TIME_QUARTER
  7197. + OFDM_PREAMBLE_TIME_QUARTER
  7198. + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
  7199. } else if (ah->ah_curchan &&
  7200. IS_CHAN_HALF_RATE(ah->ah_curchan)) {
  7201. bitsPerSymbol =
  7202. (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
  7203. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  7204. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  7205. txTime = OFDM_SIFS_TIME_HALF +
  7206. OFDM_PREAMBLE_TIME_HALF
  7207. + (numSymbols * OFDM_SYMBOL_TIME_HALF);
  7208. } else {
  7209. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
  7210. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  7211. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  7212. txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
  7213. + (numSymbols * OFDM_SYMBOL_TIME);
  7214. }
  7215. break;
  7216. default:
  7217. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  7218. "%s: unknown phy %u (rate ix %u)\n", __func__,
  7219. rates->info[rateix].phy, rateix);
  7220. txTime = 0;
  7221. break;
  7222. }
  7223. return txTime;
  7224. }
  7225. u32 ath9k_hw_mhz2ieee(struct ath_hal *ah, u32 freq, u32 flags)
  7226. {
  7227. if (flags & CHANNEL_2GHZ) {
  7228. if (freq == 2484)
  7229. return 14;
  7230. if (freq < 2484)
  7231. return (freq - 2407) / 5;
  7232. else
  7233. return 15 + ((freq - 2512) / 20);
  7234. } else if (flags & CHANNEL_5GHZ) {
  7235. if (ath9k_regd_is_public_safety_sku(ah) &&
  7236. IS_CHAN_IN_PUBLIC_SAFETY_BAND(freq)) {
  7237. return ((freq * 10) +
  7238. (((freq % 5) == 2) ? 5 : 0) - 49400) / 5;
  7239. } else if ((flags & CHANNEL_A) && (freq <= 5000)) {
  7240. return (freq - 4000) / 5;
  7241. } else {
  7242. return (freq - 5000) / 5;
  7243. }
  7244. } else {
  7245. if (freq == 2484)
  7246. return 14;
  7247. if (freq < 2484)
  7248. return (freq - 2407) / 5;
  7249. if (freq < 5000) {
  7250. if (ath9k_regd_is_public_safety_sku(ah)
  7251. && IS_CHAN_IN_PUBLIC_SAFETY_BAND(freq)) {
  7252. return ((freq * 10) +
  7253. (((freq % 5) ==
  7254. 2) ? 5 : 0) - 49400) / 5;
  7255. } else if (freq > 4900) {
  7256. return (freq - 4000) / 5;
  7257. } else {
  7258. return 15 + ((freq - 2512) / 20);
  7259. }
  7260. }
  7261. return (freq - 5000) / 5;
  7262. }
  7263. }
  7264. /* We can tune this as we go by monitoring really low values */
  7265. #define ATH9K_NF_TOO_LOW -60
  7266. /* AR5416 may return very high value (like -31 dBm), in those cases the nf
  7267. * is incorrect and we should use the static NF value. Later we can try to
  7268. * find out why they are reporting these values */
  7269. static bool ath9k_hw_nf_in_range(struct ath_hal *ah, s16 nf)
  7270. {
  7271. if (nf > ATH9K_NF_TOO_LOW) {
  7272. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  7273. "%s: noise floor value detected (%d) is "
  7274. "lower than what we think is a "
  7275. "reasonable value (%d)\n",
  7276. __func__, nf, ATH9K_NF_TOO_LOW);
  7277. return false;
  7278. }
  7279. return true;
  7280. }
  7281. s16
  7282. ath9k_hw_getchan_noise(struct ath_hal *ah, struct ath9k_channel *chan)
  7283. {
  7284. struct ath9k_channel *ichan;
  7285. s16 nf;
  7286. ichan = ath9k_regd_check_channel(ah, chan);
  7287. if (ichan == NULL) {
  7288. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  7289. "%s: invalid channel %u/0x%x; no mapping\n",
  7290. __func__, chan->channel, chan->channelFlags);
  7291. return ATH_DEFAULT_NOISE_FLOOR;
  7292. }
  7293. if (ichan->rawNoiseFloor == 0) {
  7294. enum wireless_mode mode = ath9k_hw_chan2wmode(ah, chan);
  7295. nf = NOISE_FLOOR[mode];
  7296. } else
  7297. nf = ichan->rawNoiseFloor;
  7298. if (!ath9k_hw_nf_in_range(ah, nf))
  7299. nf = ATH_DEFAULT_NOISE_FLOOR;
  7300. return nf;
  7301. }
  7302. bool ath9k_hw_set_tsfadjust(struct ath_hal *ah, u32 setting)
  7303. {
  7304. struct ath_hal_5416 *ahp = AH5416(ah);
  7305. if (setting)
  7306. ahp->ah_miscMode |= AR_PCU_TX_ADD_TSF;
  7307. else
  7308. ahp->ah_miscMode &= ~AR_PCU_TX_ADD_TSF;
  7309. return true;
  7310. }
  7311. bool ath9k_hw_phycounters(struct ath_hal *ah)
  7312. {
  7313. struct ath_hal_5416 *ahp = AH5416(ah);
  7314. return ahp->ah_hasHwPhyCounters ? true : false;
  7315. }
  7316. u32 ath9k_hw_gettxbuf(struct ath_hal *ah, u32 q)
  7317. {
  7318. return REG_READ(ah, AR_QTXDP(q));
  7319. }
  7320. bool ath9k_hw_puttxbuf(struct ath_hal *ah, u32 q,
  7321. u32 txdp)
  7322. {
  7323. REG_WRITE(ah, AR_QTXDP(q), txdp);
  7324. return true;
  7325. }
  7326. bool ath9k_hw_txstart(struct ath_hal *ah, u32 q)
  7327. {
  7328. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: queue %u\n", __func__, q);
  7329. REG_WRITE(ah, AR_Q_TXE, 1 << q);
  7330. return true;
  7331. }
  7332. u32 ath9k_hw_numtxpending(struct ath_hal *ah, u32 q)
  7333. {
  7334. u32 npend;
  7335. npend = REG_READ(ah, AR_QSTS(q)) & AR_Q_STS_PEND_FR_CNT;
  7336. if (npend == 0) {
  7337. if (REG_READ(ah, AR_Q_TXE) & (1 << q))
  7338. npend = 1;
  7339. }
  7340. return npend;
  7341. }
  7342. bool ath9k_hw_stoptxdma(struct ath_hal *ah, u32 q)
  7343. {
  7344. u32 wait;
  7345. REG_WRITE(ah, AR_Q_TXD, 1 << q);
  7346. for (wait = 1000; wait != 0; wait--) {
  7347. if (ath9k_hw_numtxpending(ah, q) == 0)
  7348. break;
  7349. udelay(100);
  7350. }
  7351. if (ath9k_hw_numtxpending(ah, q)) {
  7352. u32 tsfLow, j;
  7353. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  7354. "%s: Num of pending TX Frames %d on Q %d\n",
  7355. __func__, ath9k_hw_numtxpending(ah, q), q);
  7356. for (j = 0; j < 2; j++) {
  7357. tsfLow = REG_READ(ah, AR_TSF_L32);
  7358. REG_WRITE(ah, AR_QUIET2,
  7359. SM(10, AR_QUIET2_QUIET_DUR));
  7360. REG_WRITE(ah, AR_QUIET_PERIOD, 100);
  7361. REG_WRITE(ah, AR_NEXT_QUIET_TIMER, tsfLow >> 10);
  7362. REG_SET_BIT(ah, AR_TIMER_MODE,
  7363. AR_QUIET_TIMER_EN);
  7364. if ((REG_READ(ah, AR_TSF_L32) >> 10) ==
  7365. (tsfLow >> 10)) {
  7366. break;
  7367. }
  7368. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  7369. "%s: TSF have moved while trying to set "
  7370. "quiet time TSF: 0x%08x\n",
  7371. __func__, tsfLow);
  7372. }
  7373. REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
  7374. udelay(200);
  7375. REG_CLR_BIT(ah, AR_TIMER_MODE, AR_QUIET_TIMER_EN);
  7376. wait = 1000;
  7377. while (ath9k_hw_numtxpending(ah, q)) {
  7378. if ((--wait) == 0) {
  7379. DPRINTF(ah->ah_sc, ATH_DBG_XMIT,
  7380. "%s: Failed to stop Tx DMA in 100 "
  7381. "msec after killing last frame\n",
  7382. __func__);
  7383. break;
  7384. }
  7385. udelay(100);
  7386. }
  7387. REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
  7388. }
  7389. REG_WRITE(ah, AR_Q_TXD, 0);
  7390. return wait != 0;
  7391. }