hw.c 223 KB

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