hw.c 223 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575
  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 ATH9K_MODE_11A;
  215. if (IS_CHAN_G(chan))
  216. return ATH9K_MODE_11G;
  217. return ATH9K_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.dma_beacon_response_time = 2;
  300. ah->ah_config.sw_beacon_response_time = 10;
  301. ah->ah_config.additional_swba_backoff = 0;
  302. ah->ah_config.ack_6mb = 0x0;
  303. ah->ah_config.cwm_ignore_extcca = 0;
  304. ah->ah_config.pcie_powersave_enable = 0;
  305. ah->ah_config.pcie_l1skp_enable = 0;
  306. ah->ah_config.pcie_clock_req = 0;
  307. ah->ah_config.pcie_power_reset = 0x100;
  308. ah->ah_config.pcie_restore = 0;
  309. ah->ah_config.pcie_waen = 0;
  310. ah->ah_config.analog_shiftreg = 1;
  311. ah->ah_config.ht_enable = 1;
  312. ah->ah_config.ofdm_trig_low = 200;
  313. ah->ah_config.ofdm_trig_high = 500;
  314. ah->ah_config.cck_trig_high = 200;
  315. ah->ah_config.cck_trig_low = 100;
  316. ah->ah_config.enable_ani = 0;
  317. ah->ah_config.noise_immunity_level = 4;
  318. ah->ah_config.ofdm_weaksignal_det = 1;
  319. ah->ah_config.cck_weaksignal_thr = 0;
  320. ah->ah_config.spur_immunity_level = 2;
  321. ah->ah_config.firstep_level = 0;
  322. ah->ah_config.rssi_thr_high = 40;
  323. ah->ah_config.rssi_thr_low = 7;
  324. ah->ah_config.diversity_control = 0;
  325. ah->ah_config.antenna_switch_swap = 0;
  326. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  327. ah->ah_config.spurchans[i][0] = AR_NO_SPUR;
  328. ah->ah_config.spurchans[i][1] = AR_NO_SPUR;
  329. }
  330. ah->ah_config.intr_mitigation = 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.analog_shiftreg)
  401. udelay(100);
  402. return;
  403. }
  404. static u8 ath9k_hw_get_num_ant_config(struct ath_hal_5416 *ahp,
  405. enum ieee80211_band freq_band)
  406. {
  407. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  408. struct modal_eep_header *pModal =
  409. &(eep->modalHeader[IEEE80211_BAND_5GHZ == 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 ath9k_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 ATH9K_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 ATH9K_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 ATH9K_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 ATH9K_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.cwm_ignore_extcca)
  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.diversity_control;
  1061. ahp->ah_antennaSwitchSwap =
  1062. ah->ah_config.antenna_switch_swap;
  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.spurchans[i][is2GHz]);
  1196. switch (ah->ah_config.spurmode) {
  1197. case SPUR_DISABLE:
  1198. break;
  1199. case SPUR_ENABLE_IOCTL:
  1200. spur_val = ah->ah_config.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.enable_ani)
  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 == ATH9K_MODE_11G || mode == ATH9K_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 == ATH9K_MODE_11G || mode == ATH9K_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.ofdm_trig_high;
  2178. ahp->ah_curani->ofdmTrigLow =
  2179. ah->ah_config.ofdm_trig_low;
  2180. ahp->ah_curani->cckTrigHigh =
  2181. ah->ah_config.cck_trig_high;
  2182. ahp->ah_curani->cckTrigLow =
  2183. ah->ah_config.cck_trig_low;
  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.num_gpio_pins)
  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 ath9k_hw_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. eeval = ath9k_hw_get_eeprom(ahp, EEP_OP_MODE);
  2573. bitmap_zero(pCap->wireless_modes, ATH9K_MODE_MAX);
  2574. if (eeval & AR5416_OPFLAGS_11A) {
  2575. set_bit(ATH9K_MODE_11A, pCap->wireless_modes);
  2576. if (ah->ah_config.ht_enable) {
  2577. if (!(eeval & AR5416_OPFLAGS_N_5G_HT20))
  2578. set_bit(ATH9K_MODE_11NA_HT20,
  2579. pCap->wireless_modes);
  2580. if (!(eeval & AR5416_OPFLAGS_N_5G_HT40)) {
  2581. set_bit(ATH9K_MODE_11NA_HT40PLUS,
  2582. pCap->wireless_modes);
  2583. set_bit(ATH9K_MODE_11NA_HT40MINUS,
  2584. pCap->wireless_modes);
  2585. }
  2586. }
  2587. }
  2588. if (eeval & AR5416_OPFLAGS_11G) {
  2589. set_bit(ATH9K_MODE_11B, pCap->wireless_modes);
  2590. set_bit(ATH9K_MODE_11G, pCap->wireless_modes);
  2591. if (ah->ah_config.ht_enable) {
  2592. if (!(eeval & AR5416_OPFLAGS_N_2G_HT20))
  2593. set_bit(ATH9K_MODE_11NG_HT20,
  2594. pCap->wireless_modes);
  2595. if (!(eeval & AR5416_OPFLAGS_N_2G_HT40)) {
  2596. set_bit(ATH9K_MODE_11NG_HT40PLUS,
  2597. pCap->wireless_modes);
  2598. set_bit(ATH9K_MODE_11NG_HT40MINUS,
  2599. pCap->wireless_modes);
  2600. }
  2601. }
  2602. }
  2603. pCap->tx_chainmask = ath9k_hw_get_eeprom(ahp, EEP_TX_MASK);
  2604. if ((ah->ah_isPciExpress)
  2605. || (eeval & AR5416_OPFLAGS_11A)) {
  2606. pCap->rx_chainmask =
  2607. ath9k_hw_get_eeprom(ahp, EEP_RX_MASK);
  2608. } else {
  2609. pCap->rx_chainmask =
  2610. (ath9k_hw_gpio_get(ah, 0)) ? 0x5 : 0x7;
  2611. }
  2612. if (!(AR_SREV_9280(ah) && (ah->ah_macRev == 0)))
  2613. ahp->ah_miscMode |= AR_PCU_MIC_NEW_LOC_ENA;
  2614. pCap->low_2ghz_chan = 2312;
  2615. pCap->high_2ghz_chan = 2732;
  2616. pCap->low_5ghz_chan = 4920;
  2617. pCap->high_5ghz_chan = 6100;
  2618. pCap->hw_caps &= ~ATH9K_HW_CAP_CIPHER_CKIP;
  2619. pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_TKIP;
  2620. pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_AESCCM;
  2621. pCap->hw_caps &= ~ATH9K_HW_CAP_MIC_CKIP;
  2622. pCap->hw_caps |= ATH9K_HW_CAP_MIC_TKIP;
  2623. pCap->hw_caps |= ATH9K_HW_CAP_MIC_AESCCM;
  2624. pCap->hw_caps |= ATH9K_HW_CAP_CHAN_SPREAD;
  2625. if (ah->ah_config.ht_enable)
  2626. pCap->hw_caps |= ATH9K_HW_CAP_HT;
  2627. else
  2628. pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
  2629. pCap->hw_caps |= ATH9K_HW_CAP_GTT;
  2630. pCap->hw_caps |= ATH9K_HW_CAP_VEOL;
  2631. pCap->hw_caps |= ATH9K_HW_CAP_BSSIDMASK;
  2632. pCap->hw_caps &= ~ATH9K_HW_CAP_MCAST_KEYSEARCH;
  2633. if (capField & AR_EEPROM_EEPCAP_MAXQCU)
  2634. pCap->total_queues =
  2635. MS(capField, AR_EEPROM_EEPCAP_MAXQCU);
  2636. else
  2637. pCap->total_queues = ATH9K_NUM_TX_QUEUES;
  2638. if (capField & AR_EEPROM_EEPCAP_KC_ENTRIES)
  2639. pCap->keycache_size =
  2640. 1 << MS(capField, AR_EEPROM_EEPCAP_KC_ENTRIES);
  2641. else
  2642. pCap->keycache_size = AR_KEYTABLE_SIZE;
  2643. pCap->hw_caps |= ATH9K_HW_CAP_FASTCC;
  2644. pCap->num_mr_retries = 4;
  2645. pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD;
  2646. if (AR_SREV_9280_10_OR_LATER(ah))
  2647. pCap->num_gpio_pins = AR928X_NUM_GPIO;
  2648. else
  2649. pCap->num_gpio_pins = AR_NUM_GPIO;
  2650. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2651. pCap->hw_caps |= ATH9K_HW_CAP_WOW;
  2652. pCap->hw_caps |= ATH9K_HW_CAP_WOW_MATCHPATTERN_EXACT;
  2653. } else {
  2654. pCap->hw_caps &= ~ATH9K_HW_CAP_WOW;
  2655. pCap->hw_caps &= ~ATH9K_HW_CAP_WOW_MATCHPATTERN_EXACT;
  2656. }
  2657. if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
  2658. pCap->hw_caps |= ATH9K_HW_CAP_CST;
  2659. pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
  2660. } else {
  2661. pCap->rts_aggr_limit = (8 * 1024);
  2662. }
  2663. pCap->hw_caps |= ATH9K_HW_CAP_ENHANCEDPM;
  2664. ah->ah_rfsilent = ath9k_hw_get_eeprom(ahp, EEP_RF_SILENT);
  2665. if (ah->ah_rfsilent & EEP_RFSILENT_ENABLED) {
  2666. ahp->ah_gpioSelect =
  2667. MS(ah->ah_rfsilent, EEP_RFSILENT_GPIO_SEL);
  2668. ahp->ah_polarity =
  2669. MS(ah->ah_rfsilent, EEP_RFSILENT_POLARITY);
  2670. ath9k_hw_setcapability(ah, ATH9K_CAP_RFSILENT, 1, true,
  2671. NULL);
  2672. pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
  2673. }
  2674. if ((ah->ah_macVersion == AR_SREV_VERSION_5416_PCI) ||
  2675. (ah->ah_macVersion == AR_SREV_VERSION_5416_PCIE) ||
  2676. (ah->ah_macVersion == AR_SREV_VERSION_9160) ||
  2677. (ah->ah_macVersion == AR_SREV_VERSION_9100) ||
  2678. (ah->ah_macVersion == AR_SREV_VERSION_9280))
  2679. pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
  2680. else
  2681. pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
  2682. if (AR_SREV_9280(ah))
  2683. pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
  2684. else
  2685. pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
  2686. if (ah->ah_currentRDExt & (1 << REG_EXT_JAPAN_MIDBAND)) {
  2687. pCap->reg_cap =
  2688. AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
  2689. AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
  2690. AR_EEPROM_EEREGCAP_EN_KK_U2 |
  2691. AR_EEPROM_EEREGCAP_EN_KK_MIDBAND;
  2692. } else {
  2693. pCap->reg_cap =
  2694. AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
  2695. AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN;
  2696. }
  2697. pCap->reg_cap |= AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND;
  2698. pCap->num_antcfg_5ghz =
  2699. ath9k_hw_get_num_ant_config(ahp, IEEE80211_BAND_5GHZ);
  2700. pCap->num_antcfg_2ghz =
  2701. ath9k_hw_get_num_ant_config(ahp, IEEE80211_BAND_2GHZ);
  2702. return true;
  2703. }
  2704. static void ar5416DisablePciePhy(struct ath_hal *ah)
  2705. {
  2706. if (!AR_SREV_9100(ah))
  2707. return;
  2708. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  2709. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  2710. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
  2711. REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
  2712. REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
  2713. REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
  2714. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  2715. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  2716. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
  2717. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  2718. }
  2719. static void ath9k_set_power_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. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  2724. AR_RTC_FORCE_WAKE_EN);
  2725. if (!AR_SREV_9100(ah))
  2726. REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
  2727. REG_CLR_BIT(ah, (u16) (AR_RTC_RESET),
  2728. AR_RTC_RESET_EN);
  2729. }
  2730. }
  2731. static void ath9k_set_power_network_sleep(struct ath_hal *ah, int setChip)
  2732. {
  2733. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  2734. if (setChip) {
  2735. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  2736. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  2737. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  2738. AR_RTC_FORCE_WAKE_ON_INT);
  2739. } else {
  2740. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  2741. AR_RTC_FORCE_WAKE_EN);
  2742. }
  2743. }
  2744. }
  2745. static bool ath9k_hw_set_power_awake(struct ath_hal *ah,
  2746. int setChip)
  2747. {
  2748. u32 val;
  2749. int i;
  2750. if (setChip) {
  2751. if ((REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M) ==
  2752. AR_RTC_STATUS_SHUTDOWN) {
  2753. if (ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)
  2754. != true) {
  2755. return false;
  2756. }
  2757. }
  2758. if (AR_SREV_9100(ah))
  2759. REG_SET_BIT(ah, AR_RTC_RESET,
  2760. AR_RTC_RESET_EN);
  2761. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  2762. AR_RTC_FORCE_WAKE_EN);
  2763. udelay(50);
  2764. for (i = POWER_UP_TIME / 50; i > 0; i--) {
  2765. val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
  2766. if (val == AR_RTC_STATUS_ON)
  2767. break;
  2768. udelay(50);
  2769. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  2770. AR_RTC_FORCE_WAKE_EN);
  2771. }
  2772. if (i == 0) {
  2773. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  2774. "%s: Failed to wakeup in %uus\n",
  2775. __func__, POWER_UP_TIME / 20);
  2776. return false;
  2777. }
  2778. }
  2779. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  2780. return true;
  2781. }
  2782. bool ath9k_hw_setpower(struct ath_hal *ah,
  2783. enum ath9k_power_mode mode)
  2784. {
  2785. struct ath_hal_5416 *ahp = AH5416(ah);
  2786. static const char *modes[] = {
  2787. "AWAKE",
  2788. "FULL-SLEEP",
  2789. "NETWORK SLEEP",
  2790. "UNDEFINED"
  2791. };
  2792. int status = true, setChip = true;
  2793. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT, "%s: %s -> %s (%s)\n", __func__,
  2794. modes[ahp->ah_powerMode], modes[mode],
  2795. setChip ? "set chip " : "");
  2796. switch (mode) {
  2797. case ATH9K_PM_AWAKE:
  2798. status = ath9k_hw_set_power_awake(ah, setChip);
  2799. break;
  2800. case ATH9K_PM_FULL_SLEEP:
  2801. ath9k_set_power_sleep(ah, setChip);
  2802. ahp->ah_chipFullSleep = true;
  2803. break;
  2804. case ATH9K_PM_NETWORK_SLEEP:
  2805. ath9k_set_power_network_sleep(ah, setChip);
  2806. break;
  2807. default:
  2808. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  2809. "%s: unknown power mode %u\n", __func__, mode);
  2810. return false;
  2811. }
  2812. ahp->ah_powerMode = mode;
  2813. return status;
  2814. }
  2815. static struct ath_hal *ath9k_hw_do_attach(u16 devid,
  2816. struct ath_softc *sc,
  2817. void __iomem *mem,
  2818. int *status)
  2819. {
  2820. struct ath_hal_5416 *ahp;
  2821. struct ath_hal *ah;
  2822. int ecode;
  2823. #ifndef CONFIG_SLOW_ANT_DIV
  2824. u32 i;
  2825. u32 j;
  2826. #endif
  2827. ahp = ath9k_hw_newstate(devid, sc, mem, status);
  2828. if (ahp == NULL)
  2829. return NULL;
  2830. ah = &ahp->ah;
  2831. ath9k_hw_set_defaults(ah);
  2832. if (ah->ah_config.intr_mitigation != 0)
  2833. ahp->ah_intrMitigation = true;
  2834. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
  2835. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: couldn't reset chip\n",
  2836. __func__);
  2837. ecode = -EIO;
  2838. goto bad;
  2839. }
  2840. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
  2841. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: couldn't wakeup chip\n",
  2842. __func__);
  2843. ecode = -EIO;
  2844. goto bad;
  2845. }
  2846. if (ah->ah_config.serialize_regmode == SER_REG_MODE_AUTO) {
  2847. if (ah->ah_macVersion == AR_SREV_VERSION_5416_PCI) {
  2848. ah->ah_config.serialize_regmode =
  2849. SER_REG_MODE_ON;
  2850. } else {
  2851. ah->ah_config.serialize_regmode =
  2852. SER_REG_MODE_OFF;
  2853. }
  2854. }
  2855. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  2856. "%s: serialize_regmode is %d\n",
  2857. __func__, ah->ah_config.serialize_regmode);
  2858. if ((ah->ah_macVersion != AR_SREV_VERSION_5416_PCI) &&
  2859. (ah->ah_macVersion != AR_SREV_VERSION_5416_PCIE) &&
  2860. (ah->ah_macVersion != AR_SREV_VERSION_9160) &&
  2861. (!AR_SREV_9100(ah)) && (!AR_SREV_9280(ah))) {
  2862. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  2863. "%s: Mac Chip Rev 0x%02x.%x is not supported by "
  2864. "this driver\n", __func__,
  2865. ah->ah_macVersion, ah->ah_macRev);
  2866. ecode = -EOPNOTSUPP;
  2867. goto bad;
  2868. }
  2869. if (AR_SREV_9100(ah)) {
  2870. ahp->ah_iqCalData.calData = &iq_cal_multi_sample;
  2871. ahp->ah_suppCals = IQ_MISMATCH_CAL;
  2872. ah->ah_isPciExpress = false;
  2873. }
  2874. ah->ah_phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
  2875. if (AR_SREV_9160_10_OR_LATER(ah)) {
  2876. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2877. ahp->ah_iqCalData.calData = &iq_cal_single_sample;
  2878. ahp->ah_adcGainCalData.calData =
  2879. &adc_gain_cal_single_sample;
  2880. ahp->ah_adcDcCalData.calData =
  2881. &adc_dc_cal_single_sample;
  2882. ahp->ah_adcDcCalInitData.calData =
  2883. &adc_init_dc_cal;
  2884. } else {
  2885. ahp->ah_iqCalData.calData = &iq_cal_multi_sample;
  2886. ahp->ah_adcGainCalData.calData =
  2887. &adc_gain_cal_multi_sample;
  2888. ahp->ah_adcDcCalData.calData =
  2889. &adc_dc_cal_multi_sample;
  2890. ahp->ah_adcDcCalInitData.calData =
  2891. &adc_init_dc_cal;
  2892. }
  2893. ahp->ah_suppCals =
  2894. ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
  2895. }
  2896. if (AR_SREV_9160(ah)) {
  2897. ah->ah_config.enable_ani = 1;
  2898. ahp->ah_ani_function = (ATH9K_ANI_SPUR_IMMUNITY_LEVEL |
  2899. ATH9K_ANI_FIRSTEP_LEVEL);
  2900. } else {
  2901. ahp->ah_ani_function = ATH9K_ANI_ALL;
  2902. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2903. ahp->ah_ani_function &=
  2904. ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
  2905. }
  2906. }
  2907. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  2908. "%s: This Mac Chip Rev 0x%02x.%x is \n", __func__,
  2909. ah->ah_macVersion, ah->ah_macRev);
  2910. if (AR_SREV_9280_20_OR_LATER(ah)) {
  2911. INIT_INI_ARRAY(&ahp->ah_iniModes, ar9280Modes_9280_2,
  2912. ARRAY_SIZE(ar9280Modes_9280_2), 6);
  2913. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar9280Common_9280_2,
  2914. ARRAY_SIZE(ar9280Common_9280_2), 2);
  2915. if (ah->ah_config.pcie_clock_req) {
  2916. INIT_INI_ARRAY(&ahp->ah_iniPcieSerdes,
  2917. ar9280PciePhy_clkreq_off_L1_9280,
  2918. ARRAY_SIZE
  2919. (ar9280PciePhy_clkreq_off_L1_9280),
  2920. 2);
  2921. } else {
  2922. INIT_INI_ARRAY(&ahp->ah_iniPcieSerdes,
  2923. ar9280PciePhy_clkreq_always_on_L1_9280,
  2924. ARRAY_SIZE
  2925. (ar9280PciePhy_clkreq_always_on_L1_9280),
  2926. 2);
  2927. }
  2928. INIT_INI_ARRAY(&ahp->ah_iniModesAdditional,
  2929. ar9280Modes_fast_clock_9280_2,
  2930. ARRAY_SIZE(ar9280Modes_fast_clock_9280_2),
  2931. 3);
  2932. } else if (AR_SREV_9280_10_OR_LATER(ah)) {
  2933. INIT_INI_ARRAY(&ahp->ah_iniModes, ar9280Modes_9280,
  2934. ARRAY_SIZE(ar9280Modes_9280), 6);
  2935. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar9280Common_9280,
  2936. ARRAY_SIZE(ar9280Common_9280), 2);
  2937. } else if (AR_SREV_9160_10_OR_LATER(ah)) {
  2938. INIT_INI_ARRAY(&ahp->ah_iniModes, ar5416Modes_9160,
  2939. ARRAY_SIZE(ar5416Modes_9160), 6);
  2940. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar5416Common_9160,
  2941. ARRAY_SIZE(ar5416Common_9160), 2);
  2942. INIT_INI_ARRAY(&ahp->ah_iniBank0, ar5416Bank0_9160,
  2943. ARRAY_SIZE(ar5416Bank0_9160), 2);
  2944. INIT_INI_ARRAY(&ahp->ah_iniBB_RfGain, ar5416BB_RfGain_9160,
  2945. ARRAY_SIZE(ar5416BB_RfGain_9160), 3);
  2946. INIT_INI_ARRAY(&ahp->ah_iniBank1, ar5416Bank1_9160,
  2947. ARRAY_SIZE(ar5416Bank1_9160), 2);
  2948. INIT_INI_ARRAY(&ahp->ah_iniBank2, ar5416Bank2_9160,
  2949. ARRAY_SIZE(ar5416Bank2_9160), 2);
  2950. INIT_INI_ARRAY(&ahp->ah_iniBank3, ar5416Bank3_9160,
  2951. ARRAY_SIZE(ar5416Bank3_9160), 3);
  2952. INIT_INI_ARRAY(&ahp->ah_iniBank6, ar5416Bank6_9160,
  2953. ARRAY_SIZE(ar5416Bank6_9160), 3);
  2954. INIT_INI_ARRAY(&ahp->ah_iniBank6TPC, ar5416Bank6TPC_9160,
  2955. ARRAY_SIZE(ar5416Bank6TPC_9160), 3);
  2956. INIT_INI_ARRAY(&ahp->ah_iniBank7, ar5416Bank7_9160,
  2957. ARRAY_SIZE(ar5416Bank7_9160), 2);
  2958. if (AR_SREV_9160_11(ah)) {
  2959. INIT_INI_ARRAY(&ahp->ah_iniAddac,
  2960. ar5416Addac_91601_1,
  2961. ARRAY_SIZE(ar5416Addac_91601_1), 2);
  2962. } else {
  2963. INIT_INI_ARRAY(&ahp->ah_iniAddac, ar5416Addac_9160,
  2964. ARRAY_SIZE(ar5416Addac_9160), 2);
  2965. }
  2966. } else if (AR_SREV_9100_OR_LATER(ah)) {
  2967. INIT_INI_ARRAY(&ahp->ah_iniModes, ar5416Modes_9100,
  2968. ARRAY_SIZE(ar5416Modes_9100), 6);
  2969. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar5416Common_9100,
  2970. ARRAY_SIZE(ar5416Common_9100), 2);
  2971. INIT_INI_ARRAY(&ahp->ah_iniBank0, ar5416Bank0_9100,
  2972. ARRAY_SIZE(ar5416Bank0_9100), 2);
  2973. INIT_INI_ARRAY(&ahp->ah_iniBB_RfGain, ar5416BB_RfGain_9100,
  2974. ARRAY_SIZE(ar5416BB_RfGain_9100), 3);
  2975. INIT_INI_ARRAY(&ahp->ah_iniBank1, ar5416Bank1_9100,
  2976. ARRAY_SIZE(ar5416Bank1_9100), 2);
  2977. INIT_INI_ARRAY(&ahp->ah_iniBank2, ar5416Bank2_9100,
  2978. ARRAY_SIZE(ar5416Bank2_9100), 2);
  2979. INIT_INI_ARRAY(&ahp->ah_iniBank3, ar5416Bank3_9100,
  2980. ARRAY_SIZE(ar5416Bank3_9100), 3);
  2981. INIT_INI_ARRAY(&ahp->ah_iniBank6, ar5416Bank6_9100,
  2982. ARRAY_SIZE(ar5416Bank6_9100), 3);
  2983. INIT_INI_ARRAY(&ahp->ah_iniBank6TPC, ar5416Bank6TPC_9100,
  2984. ARRAY_SIZE(ar5416Bank6TPC_9100), 3);
  2985. INIT_INI_ARRAY(&ahp->ah_iniBank7, ar5416Bank7_9100,
  2986. ARRAY_SIZE(ar5416Bank7_9100), 2);
  2987. INIT_INI_ARRAY(&ahp->ah_iniAddac, ar5416Addac_9100,
  2988. ARRAY_SIZE(ar5416Addac_9100), 2);
  2989. } else {
  2990. INIT_INI_ARRAY(&ahp->ah_iniModes, ar5416Modes,
  2991. ARRAY_SIZE(ar5416Modes), 6);
  2992. INIT_INI_ARRAY(&ahp->ah_iniCommon, ar5416Common,
  2993. ARRAY_SIZE(ar5416Common), 2);
  2994. INIT_INI_ARRAY(&ahp->ah_iniBank0, ar5416Bank0,
  2995. ARRAY_SIZE(ar5416Bank0), 2);
  2996. INIT_INI_ARRAY(&ahp->ah_iniBB_RfGain, ar5416BB_RfGain,
  2997. ARRAY_SIZE(ar5416BB_RfGain), 3);
  2998. INIT_INI_ARRAY(&ahp->ah_iniBank1, ar5416Bank1,
  2999. ARRAY_SIZE(ar5416Bank1), 2);
  3000. INIT_INI_ARRAY(&ahp->ah_iniBank2, ar5416Bank2,
  3001. ARRAY_SIZE(ar5416Bank2), 2);
  3002. INIT_INI_ARRAY(&ahp->ah_iniBank3, ar5416Bank3,
  3003. ARRAY_SIZE(ar5416Bank3), 3);
  3004. INIT_INI_ARRAY(&ahp->ah_iniBank6, ar5416Bank6,
  3005. ARRAY_SIZE(ar5416Bank6), 3);
  3006. INIT_INI_ARRAY(&ahp->ah_iniBank6TPC, ar5416Bank6TPC,
  3007. ARRAY_SIZE(ar5416Bank6TPC), 3);
  3008. INIT_INI_ARRAY(&ahp->ah_iniBank7, ar5416Bank7,
  3009. ARRAY_SIZE(ar5416Bank7), 2);
  3010. INIT_INI_ARRAY(&ahp->ah_iniAddac, ar5416Addac,
  3011. ARRAY_SIZE(ar5416Addac), 2);
  3012. }
  3013. if (ah->ah_isPciExpress)
  3014. ath9k_hw_configpcipowersave(ah, 0);
  3015. else
  3016. ar5416DisablePciePhy(ah);
  3017. ecode = ath9k_hw_post_attach(ah);
  3018. if (ecode != 0)
  3019. goto bad;
  3020. #ifndef CONFIG_SLOW_ANT_DIV
  3021. if (ah->ah_devid == AR9280_DEVID_PCI) {
  3022. for (i = 0; i < ahp->ah_iniModes.ia_rows; i++) {
  3023. u32 reg = INI_RA(&ahp->ah_iniModes, i, 0);
  3024. for (j = 1; j < ahp->ah_iniModes.ia_columns; j++) {
  3025. u32 val = INI_RA(&ahp->ah_iniModes, i, j);
  3026. INI_RA(&ahp->ah_iniModes, i, j) =
  3027. ath9k_hw_ini_fixup(ah, &ahp->ah_eeprom,
  3028. reg, val);
  3029. }
  3030. }
  3031. }
  3032. #endif
  3033. if (!ath9k_hw_fill_cap_info(ah)) {
  3034. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  3035. "%s:failed ath9k_hw_fill_cap_info\n", __func__);
  3036. ecode = -EINVAL;
  3037. goto bad;
  3038. }
  3039. ecode = ath9k_hw_init_macaddr(ah);
  3040. if (ecode != 0) {
  3041. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  3042. "%s: failed initializing mac address\n",
  3043. __func__);
  3044. goto bad;
  3045. }
  3046. if (AR_SREV_9285(ah))
  3047. ah->ah_txTrigLevel = (AR_FTRIG_256B >> AR_FTRIG_S);
  3048. else
  3049. ah->ah_txTrigLevel = (AR_FTRIG_512B >> AR_FTRIG_S);
  3050. #ifndef ATH_NF_PER_CHAN
  3051. ath9k_init_nfcal_hist_buffer(ah);
  3052. #endif
  3053. return ah;
  3054. bad:
  3055. if (ahp)
  3056. ath9k_hw_detach((struct ath_hal *) ahp);
  3057. if (status)
  3058. *status = ecode;
  3059. return NULL;
  3060. }
  3061. void ath9k_hw_detach(struct ath_hal *ah)
  3062. {
  3063. if (!AR_SREV_9100(ah))
  3064. ath9k_hw_ani_detach(ah);
  3065. ath9k_hw_rfdetach(ah);
  3066. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  3067. kfree(ah);
  3068. }
  3069. bool ath9k_get_channel_edges(struct ath_hal *ah,
  3070. u16 flags, u16 *low,
  3071. u16 *high)
  3072. {
  3073. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  3074. if (flags & CHANNEL_5GHZ) {
  3075. *low = pCap->low_5ghz_chan;
  3076. *high = pCap->high_5ghz_chan;
  3077. return true;
  3078. }
  3079. if ((flags & CHANNEL_2GHZ)) {
  3080. *low = pCap->low_2ghz_chan;
  3081. *high = pCap->high_2ghz_chan;
  3082. return true;
  3083. }
  3084. return false;
  3085. }
  3086. static inline bool ath9k_hw_fill_vpd_table(u8 pwrMin,
  3087. u8 pwrMax,
  3088. u8 *pPwrList,
  3089. u8 *pVpdList,
  3090. u16
  3091. numIntercepts,
  3092. u8 *pRetVpdList)
  3093. {
  3094. u16 i, k;
  3095. u8 currPwr = pwrMin;
  3096. u16 idxL = 0, idxR = 0;
  3097. for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
  3098. ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
  3099. numIntercepts, &(idxL),
  3100. &(idxR));
  3101. if (idxR < 1)
  3102. idxR = 1;
  3103. if (idxL == numIntercepts - 1)
  3104. idxL = (u16) (numIntercepts - 2);
  3105. if (pPwrList[idxL] == pPwrList[idxR])
  3106. k = pVpdList[idxL];
  3107. else
  3108. k = (u16) (((currPwr -
  3109. pPwrList[idxL]) *
  3110. pVpdList[idxR] +
  3111. (pPwrList[idxR] -
  3112. currPwr) * pVpdList[idxL]) /
  3113. (pPwrList[idxR] -
  3114. pPwrList[idxL]));
  3115. pRetVpdList[i] = (u8) k;
  3116. currPwr += 2;
  3117. }
  3118. return true;
  3119. }
  3120. static inline void
  3121. ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hal *ah,
  3122. struct ath9k_channel *chan,
  3123. struct cal_data_per_freq *pRawDataSet,
  3124. u8 *bChans,
  3125. u16 availPiers,
  3126. u16 tPdGainOverlap,
  3127. int16_t *pMinCalPower,
  3128. u16 *pPdGainBoundaries,
  3129. u8 *pPDADCValues,
  3130. u16 numXpdGains)
  3131. {
  3132. int i, j, k;
  3133. int16_t ss;
  3134. u16 idxL = 0, idxR = 0, numPiers;
  3135. static u8 vpdTableL[AR5416_NUM_PD_GAINS]
  3136. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  3137. static u8 vpdTableR[AR5416_NUM_PD_GAINS]
  3138. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  3139. static u8 vpdTableI[AR5416_NUM_PD_GAINS]
  3140. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  3141. u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
  3142. u8 minPwrT4[AR5416_NUM_PD_GAINS];
  3143. u8 maxPwrT4[AR5416_NUM_PD_GAINS];
  3144. int16_t vpdStep;
  3145. int16_t tmpVal;
  3146. u16 sizeCurrVpdTable, maxIndex, tgtIndex;
  3147. bool match;
  3148. int16_t minDelta = 0;
  3149. struct chan_centers centers;
  3150. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3151. for (numPiers = 0; numPiers < availPiers; numPiers++) {
  3152. if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
  3153. break;
  3154. }
  3155. match = ath9k_hw_get_lower_upper_index((u8)
  3156. FREQ2FBIN(centers.
  3157. synth_center,
  3158. IS_CHAN_2GHZ
  3159. (chan)), bChans,
  3160. numPiers, &idxL, &idxR);
  3161. if (match) {
  3162. for (i = 0; i < numXpdGains; i++) {
  3163. minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
  3164. maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
  3165. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  3166. pRawDataSet[idxL].
  3167. pwrPdg[i],
  3168. pRawDataSet[idxL].
  3169. vpdPdg[i],
  3170. AR5416_PD_GAIN_ICEPTS,
  3171. vpdTableI[i]);
  3172. }
  3173. } else {
  3174. for (i = 0; i < numXpdGains; i++) {
  3175. pVpdL = pRawDataSet[idxL].vpdPdg[i];
  3176. pPwrL = pRawDataSet[idxL].pwrPdg[i];
  3177. pVpdR = pRawDataSet[idxR].vpdPdg[i];
  3178. pPwrR = pRawDataSet[idxR].pwrPdg[i];
  3179. minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
  3180. maxPwrT4[i] =
  3181. min(pPwrL[AR5416_PD_GAIN_ICEPTS - 1],
  3182. pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);
  3183. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  3184. pPwrL, pVpdL,
  3185. AR5416_PD_GAIN_ICEPTS,
  3186. vpdTableL[i]);
  3187. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  3188. pPwrR, pVpdR,
  3189. AR5416_PD_GAIN_ICEPTS,
  3190. vpdTableR[i]);
  3191. for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
  3192. vpdTableI[i][j] =
  3193. (u8) (ath9k_hw_interpolate
  3194. ((u16)
  3195. FREQ2FBIN(centers.
  3196. synth_center,
  3197. IS_CHAN_2GHZ
  3198. (chan)),
  3199. bChans[idxL],
  3200. bChans[idxR], vpdTableL[i]
  3201. [j], vpdTableR[i]
  3202. [j]));
  3203. }
  3204. }
  3205. }
  3206. *pMinCalPower = (int16_t) (minPwrT4[0] / 2);
  3207. k = 0;
  3208. for (i = 0; i < numXpdGains; i++) {
  3209. if (i == (numXpdGains - 1))
  3210. pPdGainBoundaries[i] =
  3211. (u16) (maxPwrT4[i] / 2);
  3212. else
  3213. pPdGainBoundaries[i] =
  3214. (u16) ((maxPwrT4[i] +
  3215. minPwrT4[i + 1]) / 4);
  3216. pPdGainBoundaries[i] =
  3217. min((u16) AR5416_MAX_RATE_POWER,
  3218. pPdGainBoundaries[i]);
  3219. if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah)) {
  3220. minDelta = pPdGainBoundaries[0] - 23;
  3221. pPdGainBoundaries[0] = 23;
  3222. } else {
  3223. minDelta = 0;
  3224. }
  3225. if (i == 0) {
  3226. if (AR_SREV_9280_10_OR_LATER(ah))
  3227. ss = (int16_t) (0 - (minPwrT4[i] / 2));
  3228. else
  3229. ss = 0;
  3230. } else {
  3231. ss = (int16_t) ((pPdGainBoundaries[i - 1] -
  3232. (minPwrT4[i] / 2)) -
  3233. tPdGainOverlap + 1 + minDelta);
  3234. }
  3235. vpdStep = (int16_t) (vpdTableI[i][1] - vpdTableI[i][0]);
  3236. vpdStep = (int16_t) ((vpdStep < 1) ? 1 : vpdStep);
  3237. while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  3238. tmpVal = (int16_t) (vpdTableI[i][0] + ss * vpdStep);
  3239. pPDADCValues[k++] =
  3240. (u8) ((tmpVal < 0) ? 0 : tmpVal);
  3241. ss++;
  3242. }
  3243. sizeCurrVpdTable =
  3244. (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
  3245. tgtIndex = (u8) (pPdGainBoundaries[i] + tPdGainOverlap -
  3246. (minPwrT4[i] / 2));
  3247. maxIndex = (tgtIndex <
  3248. sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
  3249. while ((ss < maxIndex)
  3250. && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  3251. pPDADCValues[k++] = vpdTableI[i][ss++];
  3252. }
  3253. vpdStep = (int16_t) (vpdTableI[i][sizeCurrVpdTable - 1] -
  3254. vpdTableI[i][sizeCurrVpdTable - 2]);
  3255. vpdStep = (int16_t) ((vpdStep < 1) ? 1 : vpdStep);
  3256. if (tgtIndex > maxIndex) {
  3257. while ((ss <= tgtIndex)
  3258. && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  3259. tmpVal = (int16_t) ((vpdTableI[i]
  3260. [sizeCurrVpdTable -
  3261. 1] + (ss - maxIndex +
  3262. 1) * vpdStep));
  3263. pPDADCValues[k++] = (u8) ((tmpVal >
  3264. 255) ? 255 : tmpVal);
  3265. ss++;
  3266. }
  3267. }
  3268. }
  3269. while (i < AR5416_PD_GAINS_IN_MASK) {
  3270. pPdGainBoundaries[i] = pPdGainBoundaries[i - 1];
  3271. i++;
  3272. }
  3273. while (k < AR5416_NUM_PDADC_VALUES) {
  3274. pPDADCValues[k] = pPDADCValues[k - 1];
  3275. k++;
  3276. }
  3277. return;
  3278. }
  3279. static inline bool
  3280. ath9k_hw_set_power_cal_table(struct ath_hal *ah,
  3281. struct ar5416_eeprom *pEepData,
  3282. struct ath9k_channel *chan,
  3283. int16_t *pTxPowerIndexOffset)
  3284. {
  3285. struct cal_data_per_freq *pRawDataset;
  3286. u8 *pCalBChans = NULL;
  3287. u16 pdGainOverlap_t2;
  3288. static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
  3289. u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
  3290. u16 numPiers, i, j;
  3291. int16_t tMinCalPower;
  3292. u16 numXpdGain, xpdMask;
  3293. u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
  3294. u32 reg32, regOffset, regChainOffset;
  3295. int16_t modalIdx;
  3296. struct ath_hal_5416 *ahp = AH5416(ah);
  3297. modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
  3298. xpdMask = pEepData->modalHeader[modalIdx].xpdGain;
  3299. if ((pEepData->baseEepHeader.
  3300. version & AR5416_EEP_VER_MINOR_MASK) >=
  3301. AR5416_EEP_MINOR_VER_2) {
  3302. pdGainOverlap_t2 =
  3303. pEepData->modalHeader[modalIdx].pdGainOverlap;
  3304. } else {
  3305. pdGainOverlap_t2 =
  3306. (u16) (MS
  3307. (REG_READ(ah, AR_PHY_TPCRG5),
  3308. AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
  3309. }
  3310. if (IS_CHAN_2GHZ(chan)) {
  3311. pCalBChans = pEepData->calFreqPier2G;
  3312. numPiers = AR5416_NUM_2G_CAL_PIERS;
  3313. } else {
  3314. pCalBChans = pEepData->calFreqPier5G;
  3315. numPiers = AR5416_NUM_5G_CAL_PIERS;
  3316. }
  3317. numXpdGain = 0;
  3318. for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
  3319. if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
  3320. if (numXpdGain >= AR5416_NUM_PD_GAINS)
  3321. break;
  3322. xpdGainValues[numXpdGain] =
  3323. (u16) (AR5416_PD_GAINS_IN_MASK - i);
  3324. numXpdGain++;
  3325. }
  3326. }
  3327. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
  3328. (numXpdGain - 1) & 0x3);
  3329. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
  3330. xpdGainValues[0]);
  3331. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
  3332. xpdGainValues[1]);
  3333. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
  3334. xpdGainValues[2]);
  3335. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  3336. if (AR_SREV_5416_V20_OR_LATER(ah) &&
  3337. (ahp->ah_rxchainmask == 5 || ahp->ah_txchainmask == 5)
  3338. && (i != 0)) {
  3339. regChainOffset = (i == 1) ? 0x2000 : 0x1000;
  3340. } else
  3341. regChainOffset = i * 0x1000;
  3342. if (pEepData->baseEepHeader.txMask & (1 << i)) {
  3343. if (IS_CHAN_2GHZ(chan))
  3344. pRawDataset = pEepData->calPierData2G[i];
  3345. else
  3346. pRawDataset = pEepData->calPierData5G[i];
  3347. ath9k_hw_get_gain_boundaries_pdadcs(ah, chan,
  3348. pRawDataset,
  3349. pCalBChans,
  3350. numPiers,
  3351. pdGainOverlap_t2,
  3352. &tMinCalPower,
  3353. gainBoundaries,
  3354. pdadcValues,
  3355. numXpdGain);
  3356. if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
  3357. REG_WRITE(ah,
  3358. AR_PHY_TPCRG5 + regChainOffset,
  3359. SM(pdGainOverlap_t2,
  3360. AR_PHY_TPCRG5_PD_GAIN_OVERLAP)
  3361. | SM(gainBoundaries[0],
  3362. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1)
  3363. | SM(gainBoundaries[1],
  3364. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2)
  3365. | SM(gainBoundaries[2],
  3366. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3)
  3367. | SM(gainBoundaries[3],
  3368. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
  3369. }
  3370. regOffset =
  3371. AR_PHY_BASE + (672 << 2) + regChainOffset;
  3372. for (j = 0; j < 32; j++) {
  3373. reg32 =
  3374. ((pdadcValues[4 * j + 0] & 0xFF) << 0)
  3375. | ((pdadcValues[4 * j + 1] & 0xFF) <<
  3376. 8) | ((pdadcValues[4 * j + 2] &
  3377. 0xFF) << 16) |
  3378. ((pdadcValues[4 * j + 3] & 0xFF) <<
  3379. 24);
  3380. REG_WRITE(ah, regOffset, reg32);
  3381. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  3382. "PDADC (%d,%4x): %4.4x %8.8x\n",
  3383. i, regChainOffset, regOffset,
  3384. reg32);
  3385. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  3386. "PDADC: Chain %d | PDADC %3d Value %3d | "
  3387. "PDADC %3d Value %3d | PDADC %3d Value %3d | "
  3388. "PDADC %3d Value %3d |\n",
  3389. i, 4 * j, pdadcValues[4 * j],
  3390. 4 * j + 1, pdadcValues[4 * j + 1],
  3391. 4 * j + 2, pdadcValues[4 * j + 2],
  3392. 4 * j + 3,
  3393. pdadcValues[4 * j + 3]);
  3394. regOffset += 4;
  3395. }
  3396. }
  3397. }
  3398. *pTxPowerIndexOffset = 0;
  3399. return true;
  3400. }
  3401. void ath9k_hw_configpcipowersave(struct ath_hal *ah, int restore)
  3402. {
  3403. struct ath_hal_5416 *ahp = AH5416(ah);
  3404. u8 i;
  3405. if (ah->ah_isPciExpress != true)
  3406. return;
  3407. if (ah->ah_config.pcie_powersave_enable == 2)
  3408. return;
  3409. if (restore)
  3410. return;
  3411. if (AR_SREV_9280_20_OR_LATER(ah)) {
  3412. for (i = 0; i < ahp->ah_iniPcieSerdes.ia_rows; i++) {
  3413. REG_WRITE(ah, INI_RA(&ahp->ah_iniPcieSerdes, i, 0),
  3414. INI_RA(&ahp->ah_iniPcieSerdes, i, 1));
  3415. }
  3416. udelay(1000);
  3417. } else if (AR_SREV_9280(ah)
  3418. && (ah->ah_macRev == AR_SREV_REVISION_9280_10)) {
  3419. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fd00);
  3420. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  3421. REG_WRITE(ah, AR_PCIE_SERDES, 0xa8000019);
  3422. REG_WRITE(ah, AR_PCIE_SERDES, 0x13160820);
  3423. REG_WRITE(ah, AR_PCIE_SERDES, 0xe5980560);
  3424. if (ah->ah_config.pcie_clock_req)
  3425. REG_WRITE(ah, AR_PCIE_SERDES, 0x401deffc);
  3426. else
  3427. REG_WRITE(ah, AR_PCIE_SERDES, 0x401deffd);
  3428. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  3429. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  3430. REG_WRITE(ah, AR_PCIE_SERDES, 0x00043007);
  3431. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  3432. udelay(1000);
  3433. } else {
  3434. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  3435. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  3436. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000039);
  3437. REG_WRITE(ah, AR_PCIE_SERDES, 0x53160824);
  3438. REG_WRITE(ah, AR_PCIE_SERDES, 0xe5980579);
  3439. REG_WRITE(ah, AR_PCIE_SERDES, 0x001defff);
  3440. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  3441. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  3442. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e3007);
  3443. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  3444. }
  3445. REG_SET_BIT(ah, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA);
  3446. if (ah->ah_config.pcie_waen) {
  3447. REG_WRITE(ah, AR_WA, ah->ah_config.pcie_waen);
  3448. } else {
  3449. if (AR_SREV_9280(ah))
  3450. REG_WRITE(ah, AR_WA, 0x0040073f);
  3451. else
  3452. REG_WRITE(ah, AR_WA, 0x0000073f);
  3453. }
  3454. }
  3455. static inline void
  3456. ath9k_hw_get_legacy_target_powers(struct ath_hal *ah,
  3457. struct ath9k_channel *chan,
  3458. struct cal_target_power_leg *powInfo,
  3459. u16 numChannels,
  3460. struct cal_target_power_leg *pNewPower,
  3461. u16 numRates,
  3462. bool isExtTarget)
  3463. {
  3464. u16 clo, chi;
  3465. int i;
  3466. int matchIndex = -1, lowIndex = -1;
  3467. u16 freq;
  3468. struct chan_centers centers;
  3469. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3470. freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;
  3471. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
  3472. IS_CHAN_2GHZ(chan))) {
  3473. matchIndex = 0;
  3474. } else {
  3475. for (i = 0; (i < numChannels)
  3476. && (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  3477. if (freq ==
  3478. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3479. IS_CHAN_2GHZ(chan))) {
  3480. matchIndex = i;
  3481. break;
  3482. } else if ((freq <
  3483. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3484. IS_CHAN_2GHZ(chan)))
  3485. && (freq >
  3486. ath9k_hw_fbin2freq(powInfo[i - 1].
  3487. bChannel,
  3488. IS_CHAN_2GHZ
  3489. (chan)))) {
  3490. lowIndex = i - 1;
  3491. break;
  3492. }
  3493. }
  3494. if ((matchIndex == -1) && (lowIndex == -1))
  3495. matchIndex = i - 1;
  3496. }
  3497. if (matchIndex != -1) {
  3498. *pNewPower = powInfo[matchIndex];
  3499. } else {
  3500. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  3501. IS_CHAN_2GHZ(chan));
  3502. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  3503. IS_CHAN_2GHZ(chan));
  3504. for (i = 0; i < numRates; i++) {
  3505. pNewPower->tPow2x[i] =
  3506. (u8) ath9k_hw_interpolate(freq, clo, chi,
  3507. powInfo
  3508. [lowIndex].
  3509. tPow2x[i],
  3510. powInfo
  3511. [lowIndex +
  3512. 1].tPow2x[i]);
  3513. }
  3514. }
  3515. }
  3516. static inline void
  3517. ath9k_hw_get_target_powers(struct ath_hal *ah,
  3518. struct ath9k_channel *chan,
  3519. struct cal_target_power_ht *powInfo,
  3520. u16 numChannels,
  3521. struct cal_target_power_ht *pNewPower,
  3522. u16 numRates,
  3523. bool isHt40Target)
  3524. {
  3525. u16 clo, chi;
  3526. int i;
  3527. int matchIndex = -1, lowIndex = -1;
  3528. u16 freq;
  3529. struct chan_centers centers;
  3530. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3531. freq = isHt40Target ? centers.synth_center : centers.ctl_center;
  3532. if (freq <=
  3533. ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
  3534. matchIndex = 0;
  3535. } else {
  3536. for (i = 0; (i < numChannels)
  3537. && (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  3538. if (freq ==
  3539. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3540. IS_CHAN_2GHZ(chan))) {
  3541. matchIndex = i;
  3542. break;
  3543. } else
  3544. if ((freq <
  3545. ath9k_hw_fbin2freq(powInfo[i].bChannel,
  3546. IS_CHAN_2GHZ(chan)))
  3547. && (freq >
  3548. ath9k_hw_fbin2freq(powInfo[i - 1].
  3549. bChannel,
  3550. IS_CHAN_2GHZ
  3551. (chan)))) {
  3552. lowIndex = i - 1;
  3553. break;
  3554. }
  3555. }
  3556. if ((matchIndex == -1) && (lowIndex == -1))
  3557. matchIndex = i - 1;
  3558. }
  3559. if (matchIndex != -1) {
  3560. *pNewPower = powInfo[matchIndex];
  3561. } else {
  3562. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  3563. IS_CHAN_2GHZ(chan));
  3564. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  3565. IS_CHAN_2GHZ(chan));
  3566. for (i = 0; i < numRates; i++) {
  3567. pNewPower->tPow2x[i] =
  3568. (u8) ath9k_hw_interpolate(freq, clo, chi,
  3569. powInfo
  3570. [lowIndex].
  3571. tPow2x[i],
  3572. powInfo
  3573. [lowIndex +
  3574. 1].tPow2x[i]);
  3575. }
  3576. }
  3577. }
  3578. static inline u16
  3579. ath9k_hw_get_max_edge_power(u16 freq,
  3580. struct cal_ctl_edges *pRdEdgesPower,
  3581. bool is2GHz)
  3582. {
  3583. u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  3584. int i;
  3585. for (i = 0; (i < AR5416_NUM_BAND_EDGES)
  3586. && (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  3587. if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
  3588. is2GHz)) {
  3589. twiceMaxEdgePower = pRdEdgesPower[i].tPower;
  3590. break;
  3591. } else if ((i > 0)
  3592. && (freq <
  3593. ath9k_hw_fbin2freq(pRdEdgesPower[i].
  3594. bChannel, is2GHz))) {
  3595. if (ath9k_hw_fbin2freq
  3596. (pRdEdgesPower[i - 1].bChannel, is2GHz) < freq
  3597. && pRdEdgesPower[i - 1].flag) {
  3598. twiceMaxEdgePower =
  3599. pRdEdgesPower[i - 1].tPower;
  3600. }
  3601. break;
  3602. }
  3603. }
  3604. return twiceMaxEdgePower;
  3605. }
  3606. static inline bool
  3607. ath9k_hw_set_power_per_rate_table(struct ath_hal *ah,
  3608. struct ar5416_eeprom *pEepData,
  3609. struct ath9k_channel *chan,
  3610. int16_t *ratesArray,
  3611. u16 cfgCtl,
  3612. u8 AntennaReduction,
  3613. u8 twiceMaxRegulatoryPower,
  3614. u8 powerLimit)
  3615. {
  3616. u8 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  3617. static const u16 tpScaleReductionTable[5] =
  3618. { 0, 3, 6, 9, AR5416_MAX_RATE_POWER };
  3619. int i;
  3620. int8_t twiceLargestAntenna;
  3621. struct cal_ctl_data *rep;
  3622. struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
  3623. 0, { 0, 0, 0, 0}
  3624. };
  3625. struct cal_target_power_leg targetPowerOfdmExt = {
  3626. 0, { 0, 0, 0, 0} }, targetPowerCckExt = {
  3627. 0, { 0, 0, 0, 0 }
  3628. };
  3629. struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
  3630. 0, {0, 0, 0, 0}
  3631. };
  3632. u8 scaledPower = 0, minCtlPower, maxRegAllowedPower;
  3633. u16 ctlModesFor11a[] =
  3634. { CTL_11A, CTL_5GHT20, CTL_11A_EXT, CTL_5GHT40 };
  3635. u16 ctlModesFor11g[] =
  3636. { CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT,
  3637. CTL_2GHT40
  3638. };
  3639. u16 numCtlModes, *pCtlMode, ctlMode, freq;
  3640. struct chan_centers centers;
  3641. int tx_chainmask;
  3642. u8 twiceMinEdgePower;
  3643. struct ath_hal_5416 *ahp = AH5416(ah);
  3644. tx_chainmask = ahp->ah_txchainmask;
  3645. ath9k_hw_get_channel_centers(ah, chan, &centers);
  3646. twiceLargestAntenna = max(
  3647. pEepData->modalHeader
  3648. [IS_CHAN_2GHZ(chan)].antennaGainCh[0],
  3649. pEepData->modalHeader
  3650. [IS_CHAN_2GHZ(chan)].antennaGainCh[1]);
  3651. twiceLargestAntenna = max((u8) twiceLargestAntenna,
  3652. pEepData->modalHeader
  3653. [IS_CHAN_2GHZ(chan)].antennaGainCh[2]);
  3654. twiceLargestAntenna =
  3655. (int8_t) min(AntennaReduction - twiceLargestAntenna, 0);
  3656. maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;
  3657. if (ah->ah_tpScale != ATH9K_TP_SCALE_MAX) {
  3658. maxRegAllowedPower -=
  3659. (tpScaleReductionTable[(ah->ah_tpScale)] * 2);
  3660. }
  3661. scaledPower = min(powerLimit, maxRegAllowedPower);
  3662. switch (ar5416_get_ntxchains(tx_chainmask)) {
  3663. case 1:
  3664. break;
  3665. case 2:
  3666. scaledPower -=
  3667. pEepData->modalHeader[IS_CHAN_2GHZ(chan)].
  3668. pwrDecreaseFor2Chain;
  3669. break;
  3670. case 3:
  3671. scaledPower -=
  3672. pEepData->modalHeader[IS_CHAN_2GHZ(chan)].
  3673. pwrDecreaseFor3Chain;
  3674. break;
  3675. }
  3676. scaledPower = max(0, (int32_t) scaledPower);
  3677. if (IS_CHAN_2GHZ(chan)) {
  3678. numCtlModes =
  3679. ARRAY_SIZE(ctlModesFor11g) -
  3680. SUB_NUM_CTL_MODES_AT_2G_40;
  3681. pCtlMode = ctlModesFor11g;
  3682. ath9k_hw_get_legacy_target_powers(ah, chan,
  3683. pEepData->
  3684. calTargetPowerCck,
  3685. AR5416_NUM_2G_CCK_TARGET_POWERS,
  3686. &targetPowerCck, 4,
  3687. false);
  3688. ath9k_hw_get_legacy_target_powers(ah, chan,
  3689. pEepData->
  3690. calTargetPower2G,
  3691. AR5416_NUM_2G_20_TARGET_POWERS,
  3692. &targetPowerOfdm, 4,
  3693. false);
  3694. ath9k_hw_get_target_powers(ah, chan,
  3695. pEepData->calTargetPower2GHT20,
  3696. AR5416_NUM_2G_20_TARGET_POWERS,
  3697. &targetPowerHt20, 8, false);
  3698. if (IS_CHAN_HT40(chan)) {
  3699. numCtlModes = ARRAY_SIZE(ctlModesFor11g);
  3700. ath9k_hw_get_target_powers(ah, chan,
  3701. pEepData->
  3702. calTargetPower2GHT40,
  3703. AR5416_NUM_2G_40_TARGET_POWERS,
  3704. &targetPowerHt40, 8,
  3705. true);
  3706. ath9k_hw_get_legacy_target_powers(ah, chan,
  3707. pEepData->
  3708. calTargetPowerCck,
  3709. AR5416_NUM_2G_CCK_TARGET_POWERS,
  3710. &targetPowerCckExt,
  3711. 4, true);
  3712. ath9k_hw_get_legacy_target_powers(ah, chan,
  3713. pEepData->
  3714. calTargetPower2G,
  3715. AR5416_NUM_2G_20_TARGET_POWERS,
  3716. &targetPowerOfdmExt,
  3717. 4, true);
  3718. }
  3719. } else {
  3720. numCtlModes =
  3721. ARRAY_SIZE(ctlModesFor11a) -
  3722. SUB_NUM_CTL_MODES_AT_5G_40;
  3723. pCtlMode = ctlModesFor11a;
  3724. ath9k_hw_get_legacy_target_powers(ah, chan,
  3725. pEepData->
  3726. calTargetPower5G,
  3727. AR5416_NUM_5G_20_TARGET_POWERS,
  3728. &targetPowerOfdm, 4,
  3729. false);
  3730. ath9k_hw_get_target_powers(ah, chan,
  3731. pEepData->calTargetPower5GHT20,
  3732. AR5416_NUM_5G_20_TARGET_POWERS,
  3733. &targetPowerHt20, 8, false);
  3734. if (IS_CHAN_HT40(chan)) {
  3735. numCtlModes = ARRAY_SIZE(ctlModesFor11a);
  3736. ath9k_hw_get_target_powers(ah, chan,
  3737. pEepData->
  3738. calTargetPower5GHT40,
  3739. AR5416_NUM_5G_40_TARGET_POWERS,
  3740. &targetPowerHt40, 8,
  3741. true);
  3742. ath9k_hw_get_legacy_target_powers(ah, chan,
  3743. pEepData->
  3744. calTargetPower5G,
  3745. AR5416_NUM_5G_20_TARGET_POWERS,
  3746. &targetPowerOfdmExt,
  3747. 4, true);
  3748. }
  3749. }
  3750. for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
  3751. bool isHt40CtlMode =
  3752. (pCtlMode[ctlMode] == CTL_5GHT40)
  3753. || (pCtlMode[ctlMode] == CTL_2GHT40);
  3754. if (isHt40CtlMode)
  3755. freq = centers.synth_center;
  3756. else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
  3757. freq = centers.ext_center;
  3758. else
  3759. freq = centers.ctl_center;
  3760. if (ar5416_get_eep_ver(ahp) == 14
  3761. && ar5416_get_eep_rev(ahp) <= 2)
  3762. twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  3763. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3764. "LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d, "
  3765. "EXT_ADDITIVE %d\n",
  3766. ctlMode, numCtlModes, isHt40CtlMode,
  3767. (pCtlMode[ctlMode] & EXT_ADDITIVE));
  3768. for (i = 0; (i < AR5416_NUM_CTLS) && pEepData->ctlIndex[i];
  3769. i++) {
  3770. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3771. " LOOP-Ctlidx %d: cfgCtl 0x%2.2x "
  3772. "pCtlMode 0x%2.2x ctlIndex 0x%2.2x "
  3773. "chan %d\n",
  3774. i, cfgCtl, pCtlMode[ctlMode],
  3775. pEepData->ctlIndex[i], chan->channel);
  3776. if ((((cfgCtl & ~CTL_MODE_M) |
  3777. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  3778. pEepData->ctlIndex[i])
  3779. ||
  3780. (((cfgCtl & ~CTL_MODE_M) |
  3781. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  3782. ((pEepData->
  3783. ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
  3784. rep = &(pEepData->ctlData[i]);
  3785. twiceMinEdgePower =
  3786. ath9k_hw_get_max_edge_power(freq,
  3787. rep->
  3788. ctlEdges
  3789. [ar5416_get_ntxchains
  3790. (tx_chainmask)
  3791. - 1],
  3792. IS_CHAN_2GHZ
  3793. (chan));
  3794. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3795. " MATCH-EE_IDX %d: ch %d is2 %d "
  3796. "2xMinEdge %d chainmask %d chains %d\n",
  3797. i, freq, IS_CHAN_2GHZ(chan),
  3798. twiceMinEdgePower, tx_chainmask,
  3799. ar5416_get_ntxchains
  3800. (tx_chainmask));
  3801. if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
  3802. twiceMaxEdgePower =
  3803. min(twiceMaxEdgePower,
  3804. twiceMinEdgePower);
  3805. } else {
  3806. twiceMaxEdgePower =
  3807. twiceMinEdgePower;
  3808. break;
  3809. }
  3810. }
  3811. }
  3812. minCtlPower = min(twiceMaxEdgePower, scaledPower);
  3813. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  3814. " SEL-Min ctlMode %d pCtlMode %d "
  3815. "2xMaxEdge %d sP %d minCtlPwr %d\n",
  3816. ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
  3817. scaledPower, minCtlPower);
  3818. switch (pCtlMode[ctlMode]) {
  3819. case CTL_11B:
  3820. for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x);
  3821. i++) {
  3822. targetPowerCck.tPow2x[i] =
  3823. min(targetPowerCck.tPow2x[i],
  3824. minCtlPower);
  3825. }
  3826. break;
  3827. case CTL_11A:
  3828. case CTL_11G:
  3829. for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x);
  3830. i++) {
  3831. targetPowerOfdm.tPow2x[i] =
  3832. min(targetPowerOfdm.tPow2x[i],
  3833. minCtlPower);
  3834. }
  3835. break;
  3836. case CTL_5GHT20:
  3837. case CTL_2GHT20:
  3838. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x);
  3839. i++) {
  3840. targetPowerHt20.tPow2x[i] =
  3841. min(targetPowerHt20.tPow2x[i],
  3842. minCtlPower);
  3843. }
  3844. break;
  3845. case CTL_11B_EXT:
  3846. targetPowerCckExt.tPow2x[0] =
  3847. min(targetPowerCckExt.tPow2x[0], minCtlPower);
  3848. break;
  3849. case CTL_11A_EXT:
  3850. case CTL_11G_EXT:
  3851. targetPowerOfdmExt.tPow2x[0] =
  3852. min(targetPowerOfdmExt.tPow2x[0], minCtlPower);
  3853. break;
  3854. case CTL_5GHT40:
  3855. case CTL_2GHT40:
  3856. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x);
  3857. i++) {
  3858. targetPowerHt40.tPow2x[i] =
  3859. min(targetPowerHt40.tPow2x[i],
  3860. minCtlPower);
  3861. }
  3862. break;
  3863. default:
  3864. break;
  3865. }
  3866. }
  3867. ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
  3868. ratesArray[rate18mb] = ratesArray[rate24mb] =
  3869. targetPowerOfdm.tPow2x[0];
  3870. ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
  3871. ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
  3872. ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
  3873. ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
  3874. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
  3875. ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
  3876. if (IS_CHAN_2GHZ(chan)) {
  3877. ratesArray[rate1l] = targetPowerCck.tPow2x[0];
  3878. ratesArray[rate2s] = ratesArray[rate2l] =
  3879. targetPowerCck.tPow2x[1];
  3880. ratesArray[rate5_5s] = ratesArray[rate5_5l] =
  3881. targetPowerCck.tPow2x[2];
  3882. ;
  3883. ratesArray[rate11s] = ratesArray[rate11l] =
  3884. targetPowerCck.tPow2x[3];
  3885. ;
  3886. }
  3887. if (IS_CHAN_HT40(chan)) {
  3888. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
  3889. ratesArray[rateHt40_0 + i] =
  3890. targetPowerHt40.tPow2x[i];
  3891. }
  3892. ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
  3893. ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
  3894. ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
  3895. if (IS_CHAN_2GHZ(chan)) {
  3896. ratesArray[rateExtCck] =
  3897. targetPowerCckExt.tPow2x[0];
  3898. }
  3899. }
  3900. return true;
  3901. }
  3902. static int
  3903. ath9k_hw_set_txpower(struct ath_hal *ah,
  3904. struct ar5416_eeprom *pEepData,
  3905. struct ath9k_channel *chan,
  3906. u16 cfgCtl,
  3907. u8 twiceAntennaReduction,
  3908. u8 twiceMaxRegulatoryPower,
  3909. u8 powerLimit)
  3910. {
  3911. struct modal_eep_header *pModal =
  3912. &(pEepData->modalHeader[IS_CHAN_2GHZ(chan)]);
  3913. int16_t ratesArray[Ar5416RateSize];
  3914. int16_t txPowerIndexOffset = 0;
  3915. u8 ht40PowerIncForPdadc = 2;
  3916. int i;
  3917. memset(ratesArray, 0, sizeof(ratesArray));
  3918. if ((pEepData->baseEepHeader.
  3919. version & AR5416_EEP_VER_MINOR_MASK) >=
  3920. AR5416_EEP_MINOR_VER_2) {
  3921. ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
  3922. }
  3923. if (!ath9k_hw_set_power_per_rate_table(ah, pEepData, chan,
  3924. &ratesArray[0], cfgCtl,
  3925. twiceAntennaReduction,
  3926. twiceMaxRegulatoryPower,
  3927. powerLimit)) {
  3928. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  3929. "ath9k_hw_set_txpower: unable to set "
  3930. "tx power per rate table\n");
  3931. return -EIO;
  3932. }
  3933. if (!ath9k_hw_set_power_cal_table
  3934. (ah, pEepData, chan, &txPowerIndexOffset)) {
  3935. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  3936. "ath9k_hw_set_txpower: unable to set power table\n");
  3937. return -EIO;
  3938. }
  3939. for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
  3940. ratesArray[i] =
  3941. (int16_t) (txPowerIndexOffset + ratesArray[i]);
  3942. if (ratesArray[i] > AR5416_MAX_RATE_POWER)
  3943. ratesArray[i] = AR5416_MAX_RATE_POWER;
  3944. }
  3945. if (AR_SREV_9280_10_OR_LATER(ah)) {
  3946. for (i = 0; i < Ar5416RateSize; i++)
  3947. ratesArray[i] -= AR5416_PWR_TABLE_OFFSET * 2;
  3948. }
  3949. REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
  3950. ATH9K_POW_SM(ratesArray[rate18mb], 24)
  3951. | ATH9K_POW_SM(ratesArray[rate12mb], 16)
  3952. | ATH9K_POW_SM(ratesArray[rate9mb], 8)
  3953. | ATH9K_POW_SM(ratesArray[rate6mb], 0)
  3954. );
  3955. REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
  3956. ATH9K_POW_SM(ratesArray[rate54mb], 24)
  3957. | ATH9K_POW_SM(ratesArray[rate48mb], 16)
  3958. | ATH9K_POW_SM(ratesArray[rate36mb], 8)
  3959. | ATH9K_POW_SM(ratesArray[rate24mb], 0)
  3960. );
  3961. if (IS_CHAN_2GHZ(chan)) {
  3962. REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
  3963. ATH9K_POW_SM(ratesArray[rate2s], 24)
  3964. | ATH9K_POW_SM(ratesArray[rate2l], 16)
  3965. | ATH9K_POW_SM(ratesArray[rateXr], 8)
  3966. | ATH9K_POW_SM(ratesArray[rate1l], 0)
  3967. );
  3968. REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
  3969. ATH9K_POW_SM(ratesArray[rate11s], 24)
  3970. | ATH9K_POW_SM(ratesArray[rate11l], 16)
  3971. | ATH9K_POW_SM(ratesArray[rate5_5s], 8)
  3972. | ATH9K_POW_SM(ratesArray[rate5_5l], 0)
  3973. );
  3974. }
  3975. REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
  3976. ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
  3977. | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
  3978. | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
  3979. | ATH9K_POW_SM(ratesArray[rateHt20_0], 0)
  3980. );
  3981. REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
  3982. ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
  3983. | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
  3984. | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
  3985. | ATH9K_POW_SM(ratesArray[rateHt20_4], 0)
  3986. );
  3987. if (IS_CHAN_HT40(chan)) {
  3988. REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
  3989. ATH9K_POW_SM(ratesArray[rateHt40_3] +
  3990. ht40PowerIncForPdadc, 24)
  3991. | ATH9K_POW_SM(ratesArray[rateHt40_2] +
  3992. ht40PowerIncForPdadc, 16)
  3993. | ATH9K_POW_SM(ratesArray[rateHt40_1] +
  3994. ht40PowerIncForPdadc, 8)
  3995. | ATH9K_POW_SM(ratesArray[rateHt40_0] +
  3996. ht40PowerIncForPdadc, 0)
  3997. );
  3998. REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
  3999. ATH9K_POW_SM(ratesArray[rateHt40_7] +
  4000. ht40PowerIncForPdadc, 24)
  4001. | ATH9K_POW_SM(ratesArray[rateHt40_6] +
  4002. ht40PowerIncForPdadc, 16)
  4003. | ATH9K_POW_SM(ratesArray[rateHt40_5] +
  4004. ht40PowerIncForPdadc, 8)
  4005. | ATH9K_POW_SM(ratesArray[rateHt40_4] +
  4006. ht40PowerIncForPdadc, 0)
  4007. );
  4008. REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
  4009. ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
  4010. | ATH9K_POW_SM(ratesArray[rateExtCck], 16)
  4011. | ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
  4012. | ATH9K_POW_SM(ratesArray[rateDupCck], 0)
  4013. );
  4014. }
  4015. REG_WRITE(ah, AR_PHY_POWER_TX_SUB,
  4016. ATH9K_POW_SM(pModal->pwrDecreaseFor3Chain, 6)
  4017. | ATH9K_POW_SM(pModal->pwrDecreaseFor2Chain, 0)
  4018. );
  4019. i = rate6mb;
  4020. if (IS_CHAN_HT40(chan))
  4021. i = rateHt40_0;
  4022. else if (IS_CHAN_HT20(chan))
  4023. i = rateHt20_0;
  4024. if (AR_SREV_9280_10_OR_LATER(ah))
  4025. ah->ah_maxPowerLevel =
  4026. ratesArray[i] + AR5416_PWR_TABLE_OFFSET * 2;
  4027. else
  4028. ah->ah_maxPowerLevel = ratesArray[i];
  4029. return 0;
  4030. }
  4031. static inline void ath9k_hw_get_delta_slope_vals(struct ath_hal *ah,
  4032. u32 coef_scaled,
  4033. u32 *coef_mantissa,
  4034. u32 *coef_exponent)
  4035. {
  4036. u32 coef_exp, coef_man;
  4037. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  4038. if ((coef_scaled >> coef_exp) & 0x1)
  4039. break;
  4040. coef_exp = 14 - (coef_exp - COEF_SCALE_S);
  4041. coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
  4042. *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
  4043. *coef_exponent = coef_exp - 16;
  4044. }
  4045. static void
  4046. ath9k_hw_set_delta_slope(struct ath_hal *ah,
  4047. struct ath9k_channel *chan)
  4048. {
  4049. u32 coef_scaled, ds_coef_exp, ds_coef_man;
  4050. u32 clockMhzScaled = 0x64000000;
  4051. struct chan_centers centers;
  4052. if (IS_CHAN_HALF_RATE(chan))
  4053. clockMhzScaled = clockMhzScaled >> 1;
  4054. else if (IS_CHAN_QUARTER_RATE(chan))
  4055. clockMhzScaled = clockMhzScaled >> 2;
  4056. ath9k_hw_get_channel_centers(ah, chan, &centers);
  4057. coef_scaled = clockMhzScaled / centers.synth_center;
  4058. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  4059. &ds_coef_exp);
  4060. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  4061. AR_PHY_TIMING3_DSC_MAN, ds_coef_man);
  4062. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  4063. AR_PHY_TIMING3_DSC_EXP, ds_coef_exp);
  4064. coef_scaled = (9 * coef_scaled) / 10;
  4065. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  4066. &ds_coef_exp);
  4067. REG_RMW_FIELD(ah, AR_PHY_HALFGI,
  4068. AR_PHY_HALFGI_DSC_MAN, ds_coef_man);
  4069. REG_RMW_FIELD(ah, AR_PHY_HALFGI,
  4070. AR_PHY_HALFGI_DSC_EXP, ds_coef_exp);
  4071. }
  4072. static void ath9k_hw_9280_spur_mitigate(struct ath_hal *ah,
  4073. struct ath9k_channel *chan)
  4074. {
  4075. int bb_spur = AR_NO_SPUR;
  4076. int freq;
  4077. int bin, cur_bin;
  4078. int bb_spur_off, spur_subchannel_sd;
  4079. int spur_freq_sd;
  4080. int spur_delta_phase;
  4081. int denominator;
  4082. int upper, lower, cur_vit_mask;
  4083. int tmp, newVal;
  4084. int i;
  4085. int pilot_mask_reg[4] = { AR_PHY_TIMING7, AR_PHY_TIMING8,
  4086. AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
  4087. };
  4088. int chan_mask_reg[4] = { AR_PHY_TIMING9, AR_PHY_TIMING10,
  4089. AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
  4090. };
  4091. int inc[4] = { 0, 100, 0, 0 };
  4092. struct chan_centers centers;
  4093. int8_t mask_m[123];
  4094. int8_t mask_p[123];
  4095. int8_t mask_amt;
  4096. int tmp_mask;
  4097. int cur_bb_spur;
  4098. bool is2GHz = IS_CHAN_2GHZ(chan);
  4099. memset(&mask_m, 0, sizeof(int8_t) * 123);
  4100. memset(&mask_p, 0, sizeof(int8_t) * 123);
  4101. ath9k_hw_get_channel_centers(ah, chan, &centers);
  4102. freq = centers.synth_center;
  4103. ah->ah_config.spurmode = SPUR_ENABLE_EEPROM;
  4104. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  4105. cur_bb_spur = ath9k_hw_eeprom_get_spur_chan(ah, i, is2GHz);
  4106. if (is2GHz)
  4107. cur_bb_spur = (cur_bb_spur / 10) + AR_BASE_FREQ_2GHZ;
  4108. else
  4109. cur_bb_spur = (cur_bb_spur / 10) + AR_BASE_FREQ_5GHZ;
  4110. if (AR_NO_SPUR == cur_bb_spur)
  4111. break;
  4112. cur_bb_spur = cur_bb_spur - freq;
  4113. if (IS_CHAN_HT40(chan)) {
  4114. if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT40) &&
  4115. (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT40)) {
  4116. bb_spur = cur_bb_spur;
  4117. break;
  4118. }
  4119. } else if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT20) &&
  4120. (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT20)) {
  4121. bb_spur = cur_bb_spur;
  4122. break;
  4123. }
  4124. }
  4125. if (AR_NO_SPUR == bb_spur) {
  4126. REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK,
  4127. AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
  4128. return;
  4129. } else {
  4130. REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK,
  4131. AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
  4132. }
  4133. bin = bb_spur * 320;
  4134. tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
  4135. newVal = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
  4136. AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
  4137. AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
  4138. AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
  4139. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), newVal);
  4140. newVal = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
  4141. AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
  4142. AR_PHY_SPUR_REG_MASK_RATE_SELECT |
  4143. AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
  4144. SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
  4145. REG_WRITE(ah, AR_PHY_SPUR_REG, newVal);
  4146. if (IS_CHAN_HT40(chan)) {
  4147. if (bb_spur < 0) {
  4148. spur_subchannel_sd = 1;
  4149. bb_spur_off = bb_spur + 10;
  4150. } else {
  4151. spur_subchannel_sd = 0;
  4152. bb_spur_off = bb_spur - 10;
  4153. }
  4154. } else {
  4155. spur_subchannel_sd = 0;
  4156. bb_spur_off = bb_spur;
  4157. }
  4158. if (IS_CHAN_HT40(chan))
  4159. spur_delta_phase =
  4160. ((bb_spur * 262144) /
  4161. 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  4162. else
  4163. spur_delta_phase =
  4164. ((bb_spur * 524288) /
  4165. 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  4166. denominator = IS_CHAN_2GHZ(chan) ? 44 : 40;
  4167. spur_freq_sd = ((bb_spur_off * 2048) / denominator) & 0x3ff;
  4168. newVal = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
  4169. SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
  4170. SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
  4171. REG_WRITE(ah, AR_PHY_TIMING11, newVal);
  4172. newVal = spur_subchannel_sd << AR_PHY_SFCORR_SPUR_SUBCHNL_SD_S;
  4173. REG_WRITE(ah, AR_PHY_SFCORR_EXT, newVal);
  4174. cur_bin = -6000;
  4175. upper = bin + 100;
  4176. lower = bin - 100;
  4177. for (i = 0; i < 4; i++) {
  4178. int pilot_mask = 0;
  4179. int chan_mask = 0;
  4180. int bp = 0;
  4181. for (bp = 0; bp < 30; bp++) {
  4182. if ((cur_bin > lower) && (cur_bin < upper)) {
  4183. pilot_mask = pilot_mask | 0x1 << bp;
  4184. chan_mask = chan_mask | 0x1 << bp;
  4185. }
  4186. cur_bin += 100;
  4187. }
  4188. cur_bin += inc[i];
  4189. REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
  4190. REG_WRITE(ah, chan_mask_reg[i], chan_mask);
  4191. }
  4192. cur_vit_mask = 6100;
  4193. upper = bin + 120;
  4194. lower = bin - 120;
  4195. for (i = 0; i < 123; i++) {
  4196. if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
  4197. /* workaround for gcc bug #37014 */
  4198. volatile int tmp = abs(cur_vit_mask - bin);
  4199. if (tmp < 75)
  4200. mask_amt = 1;
  4201. else
  4202. mask_amt = 0;
  4203. if (cur_vit_mask < 0)
  4204. mask_m[abs(cur_vit_mask / 100)] = mask_amt;
  4205. else
  4206. mask_p[cur_vit_mask / 100] = mask_amt;
  4207. }
  4208. cur_vit_mask -= 100;
  4209. }
  4210. tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
  4211. | (mask_m[48] << 26) | (mask_m[49] << 24)
  4212. | (mask_m[50] << 22) | (mask_m[51] << 20)
  4213. | (mask_m[52] << 18) | (mask_m[53] << 16)
  4214. | (mask_m[54] << 14) | (mask_m[55] << 12)
  4215. | (mask_m[56] << 10) | (mask_m[57] << 8)
  4216. | (mask_m[58] << 6) | (mask_m[59] << 4)
  4217. | (mask_m[60] << 2) | (mask_m[61] << 0);
  4218. REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
  4219. REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
  4220. tmp_mask = (mask_m[31] << 28)
  4221. | (mask_m[32] << 26) | (mask_m[33] << 24)
  4222. | (mask_m[34] << 22) | (mask_m[35] << 20)
  4223. | (mask_m[36] << 18) | (mask_m[37] << 16)
  4224. | (mask_m[48] << 14) | (mask_m[39] << 12)
  4225. | (mask_m[40] << 10) | (mask_m[41] << 8)
  4226. | (mask_m[42] << 6) | (mask_m[43] << 4)
  4227. | (mask_m[44] << 2) | (mask_m[45] << 0);
  4228. REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
  4229. REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
  4230. tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
  4231. | (mask_m[18] << 26) | (mask_m[18] << 24)
  4232. | (mask_m[20] << 22) | (mask_m[20] << 20)
  4233. | (mask_m[22] << 18) | (mask_m[22] << 16)
  4234. | (mask_m[24] << 14) | (mask_m[24] << 12)
  4235. | (mask_m[25] << 10) | (mask_m[26] << 8)
  4236. | (mask_m[27] << 6) | (mask_m[28] << 4)
  4237. | (mask_m[29] << 2) | (mask_m[30] << 0);
  4238. REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
  4239. REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
  4240. tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
  4241. | (mask_m[2] << 26) | (mask_m[3] << 24)
  4242. | (mask_m[4] << 22) | (mask_m[5] << 20)
  4243. | (mask_m[6] << 18) | (mask_m[7] << 16)
  4244. | (mask_m[8] << 14) | (mask_m[9] << 12)
  4245. | (mask_m[10] << 10) | (mask_m[11] << 8)
  4246. | (mask_m[12] << 6) | (mask_m[13] << 4)
  4247. | (mask_m[14] << 2) | (mask_m[15] << 0);
  4248. REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
  4249. REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
  4250. tmp_mask = (mask_p[15] << 28)
  4251. | (mask_p[14] << 26) | (mask_p[13] << 24)
  4252. | (mask_p[12] << 22) | (mask_p[11] << 20)
  4253. | (mask_p[10] << 18) | (mask_p[9] << 16)
  4254. | (mask_p[8] << 14) | (mask_p[7] << 12)
  4255. | (mask_p[6] << 10) | (mask_p[5] << 8)
  4256. | (mask_p[4] << 6) | (mask_p[3] << 4)
  4257. | (mask_p[2] << 2) | (mask_p[1] << 0);
  4258. REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
  4259. REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
  4260. tmp_mask = (mask_p[30] << 28)
  4261. | (mask_p[29] << 26) | (mask_p[28] << 24)
  4262. | (mask_p[27] << 22) | (mask_p[26] << 20)
  4263. | (mask_p[25] << 18) | (mask_p[24] << 16)
  4264. | (mask_p[23] << 14) | (mask_p[22] << 12)
  4265. | (mask_p[21] << 10) | (mask_p[20] << 8)
  4266. | (mask_p[19] << 6) | (mask_p[18] << 4)
  4267. | (mask_p[17] << 2) | (mask_p[16] << 0);
  4268. REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
  4269. REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
  4270. tmp_mask = (mask_p[45] << 28)
  4271. | (mask_p[44] << 26) | (mask_p[43] << 24)
  4272. | (mask_p[42] << 22) | (mask_p[41] << 20)
  4273. | (mask_p[40] << 18) | (mask_p[39] << 16)
  4274. | (mask_p[38] << 14) | (mask_p[37] << 12)
  4275. | (mask_p[36] << 10) | (mask_p[35] << 8)
  4276. | (mask_p[34] << 6) | (mask_p[33] << 4)
  4277. | (mask_p[32] << 2) | (mask_p[31] << 0);
  4278. REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
  4279. REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
  4280. tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
  4281. | (mask_p[59] << 26) | (mask_p[58] << 24)
  4282. | (mask_p[57] << 22) | (mask_p[56] << 20)
  4283. | (mask_p[55] << 18) | (mask_p[54] << 16)
  4284. | (mask_p[53] << 14) | (mask_p[52] << 12)
  4285. | (mask_p[51] << 10) | (mask_p[50] << 8)
  4286. | (mask_p[49] << 6) | (mask_p[48] << 4)
  4287. | (mask_p[47] << 2) | (mask_p[46] << 0);
  4288. REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
  4289. REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
  4290. }
  4291. static void ath9k_hw_spur_mitigate(struct ath_hal *ah,
  4292. struct ath9k_channel *chan)
  4293. {
  4294. int bb_spur = AR_NO_SPUR;
  4295. int bin, cur_bin;
  4296. int spur_freq_sd;
  4297. int spur_delta_phase;
  4298. int denominator;
  4299. int upper, lower, cur_vit_mask;
  4300. int tmp, new;
  4301. int i;
  4302. int pilot_mask_reg[4] = { AR_PHY_TIMING7, AR_PHY_TIMING8,
  4303. AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
  4304. };
  4305. int chan_mask_reg[4] = { AR_PHY_TIMING9, AR_PHY_TIMING10,
  4306. AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
  4307. };
  4308. int inc[4] = { 0, 100, 0, 0 };
  4309. int8_t mask_m[123];
  4310. int8_t mask_p[123];
  4311. int8_t mask_amt;
  4312. int tmp_mask;
  4313. int cur_bb_spur;
  4314. bool is2GHz = IS_CHAN_2GHZ(chan);
  4315. memset(&mask_m, 0, sizeof(int8_t) * 123);
  4316. memset(&mask_p, 0, sizeof(int8_t) * 123);
  4317. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  4318. cur_bb_spur = ath9k_hw_eeprom_get_spur_chan(ah, i, is2GHz);
  4319. if (AR_NO_SPUR == cur_bb_spur)
  4320. break;
  4321. cur_bb_spur = cur_bb_spur - (chan->channel * 10);
  4322. if ((cur_bb_spur > -95) && (cur_bb_spur < 95)) {
  4323. bb_spur = cur_bb_spur;
  4324. break;
  4325. }
  4326. }
  4327. if (AR_NO_SPUR == bb_spur)
  4328. return;
  4329. bin = bb_spur * 32;
  4330. tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
  4331. new = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
  4332. AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
  4333. AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
  4334. AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
  4335. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), new);
  4336. new = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
  4337. AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
  4338. AR_PHY_SPUR_REG_MASK_RATE_SELECT |
  4339. AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
  4340. SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
  4341. REG_WRITE(ah, AR_PHY_SPUR_REG, new);
  4342. spur_delta_phase = ((bb_spur * 524288) / 100) &
  4343. AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  4344. denominator = IS_CHAN_2GHZ(chan) ? 440 : 400;
  4345. spur_freq_sd = ((bb_spur * 2048) / denominator) & 0x3ff;
  4346. new = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
  4347. SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
  4348. SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
  4349. REG_WRITE(ah, AR_PHY_TIMING11, new);
  4350. cur_bin = -6000;
  4351. upper = bin + 100;
  4352. lower = bin - 100;
  4353. for (i = 0; i < 4; i++) {
  4354. int pilot_mask = 0;
  4355. int chan_mask = 0;
  4356. int bp = 0;
  4357. for (bp = 0; bp < 30; bp++) {
  4358. if ((cur_bin > lower) && (cur_bin < upper)) {
  4359. pilot_mask = pilot_mask | 0x1 << bp;
  4360. chan_mask = chan_mask | 0x1 << bp;
  4361. }
  4362. cur_bin += 100;
  4363. }
  4364. cur_bin += inc[i];
  4365. REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
  4366. REG_WRITE(ah, chan_mask_reg[i], chan_mask);
  4367. }
  4368. cur_vit_mask = 6100;
  4369. upper = bin + 120;
  4370. lower = bin - 120;
  4371. for (i = 0; i < 123; i++) {
  4372. if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
  4373. /* workaround for gcc bug #37014 */
  4374. volatile int tmp = abs(cur_vit_mask - bin);
  4375. if (tmp < 75)
  4376. mask_amt = 1;
  4377. else
  4378. mask_amt = 0;
  4379. if (cur_vit_mask < 0)
  4380. mask_m[abs(cur_vit_mask / 100)] = mask_amt;
  4381. else
  4382. mask_p[cur_vit_mask / 100] = mask_amt;
  4383. }
  4384. cur_vit_mask -= 100;
  4385. }
  4386. tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
  4387. | (mask_m[48] << 26) | (mask_m[49] << 24)
  4388. | (mask_m[50] << 22) | (mask_m[51] << 20)
  4389. | (mask_m[52] << 18) | (mask_m[53] << 16)
  4390. | (mask_m[54] << 14) | (mask_m[55] << 12)
  4391. | (mask_m[56] << 10) | (mask_m[57] << 8)
  4392. | (mask_m[58] << 6) | (mask_m[59] << 4)
  4393. | (mask_m[60] << 2) | (mask_m[61] << 0);
  4394. REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
  4395. REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
  4396. tmp_mask = (mask_m[31] << 28)
  4397. | (mask_m[32] << 26) | (mask_m[33] << 24)
  4398. | (mask_m[34] << 22) | (mask_m[35] << 20)
  4399. | (mask_m[36] << 18) | (mask_m[37] << 16)
  4400. | (mask_m[48] << 14) | (mask_m[39] << 12)
  4401. | (mask_m[40] << 10) | (mask_m[41] << 8)
  4402. | (mask_m[42] << 6) | (mask_m[43] << 4)
  4403. | (mask_m[44] << 2) | (mask_m[45] << 0);
  4404. REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
  4405. REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
  4406. tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
  4407. | (mask_m[18] << 26) | (mask_m[18] << 24)
  4408. | (mask_m[20] << 22) | (mask_m[20] << 20)
  4409. | (mask_m[22] << 18) | (mask_m[22] << 16)
  4410. | (mask_m[24] << 14) | (mask_m[24] << 12)
  4411. | (mask_m[25] << 10) | (mask_m[26] << 8)
  4412. | (mask_m[27] << 6) | (mask_m[28] << 4)
  4413. | (mask_m[29] << 2) | (mask_m[30] << 0);
  4414. REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
  4415. REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
  4416. tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
  4417. | (mask_m[2] << 26) | (mask_m[3] << 24)
  4418. | (mask_m[4] << 22) | (mask_m[5] << 20)
  4419. | (mask_m[6] << 18) | (mask_m[7] << 16)
  4420. | (mask_m[8] << 14) | (mask_m[9] << 12)
  4421. | (mask_m[10] << 10) | (mask_m[11] << 8)
  4422. | (mask_m[12] << 6) | (mask_m[13] << 4)
  4423. | (mask_m[14] << 2) | (mask_m[15] << 0);
  4424. REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
  4425. REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
  4426. tmp_mask = (mask_p[15] << 28)
  4427. | (mask_p[14] << 26) | (mask_p[13] << 24)
  4428. | (mask_p[12] << 22) | (mask_p[11] << 20)
  4429. | (mask_p[10] << 18) | (mask_p[9] << 16)
  4430. | (mask_p[8] << 14) | (mask_p[7] << 12)
  4431. | (mask_p[6] << 10) | (mask_p[5] << 8)
  4432. | (mask_p[4] << 6) | (mask_p[3] << 4)
  4433. | (mask_p[2] << 2) | (mask_p[1] << 0);
  4434. REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
  4435. REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
  4436. tmp_mask = (mask_p[30] << 28)
  4437. | (mask_p[29] << 26) | (mask_p[28] << 24)
  4438. | (mask_p[27] << 22) | (mask_p[26] << 20)
  4439. | (mask_p[25] << 18) | (mask_p[24] << 16)
  4440. | (mask_p[23] << 14) | (mask_p[22] << 12)
  4441. | (mask_p[21] << 10) | (mask_p[20] << 8)
  4442. | (mask_p[19] << 6) | (mask_p[18] << 4)
  4443. | (mask_p[17] << 2) | (mask_p[16] << 0);
  4444. REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
  4445. REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
  4446. tmp_mask = (mask_p[45] << 28)
  4447. | (mask_p[44] << 26) | (mask_p[43] << 24)
  4448. | (mask_p[42] << 22) | (mask_p[41] << 20)
  4449. | (mask_p[40] << 18) | (mask_p[39] << 16)
  4450. | (mask_p[38] << 14) | (mask_p[37] << 12)
  4451. | (mask_p[36] << 10) | (mask_p[35] << 8)
  4452. | (mask_p[34] << 6) | (mask_p[33] << 4)
  4453. | (mask_p[32] << 2) | (mask_p[31] << 0);
  4454. REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
  4455. REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
  4456. tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
  4457. | (mask_p[59] << 26) | (mask_p[58] << 24)
  4458. | (mask_p[57] << 22) | (mask_p[56] << 20)
  4459. | (mask_p[55] << 18) | (mask_p[54] << 16)
  4460. | (mask_p[53] << 14) | (mask_p[52] << 12)
  4461. | (mask_p[51] << 10) | (mask_p[50] << 8)
  4462. | (mask_p[49] << 6) | (mask_p[48] << 4)
  4463. | (mask_p[47] << 2) | (mask_p[46] << 0);
  4464. REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
  4465. REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
  4466. }
  4467. static inline void ath9k_hw_init_chain_masks(struct ath_hal *ah)
  4468. {
  4469. struct ath_hal_5416 *ahp = AH5416(ah);
  4470. int rx_chainmask, tx_chainmask;
  4471. rx_chainmask = ahp->ah_rxchainmask;
  4472. tx_chainmask = ahp->ah_txchainmask;
  4473. switch (rx_chainmask) {
  4474. case 0x5:
  4475. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  4476. AR_PHY_SWAP_ALT_CHAIN);
  4477. case 0x3:
  4478. if (((ah)->ah_macVersion <= AR_SREV_VERSION_9160)) {
  4479. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, 0x7);
  4480. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, 0x7);
  4481. break;
  4482. }
  4483. case 0x1:
  4484. case 0x2:
  4485. if (!AR_SREV_9280(ah))
  4486. break;
  4487. case 0x7:
  4488. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
  4489. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
  4490. break;
  4491. default:
  4492. break;
  4493. }
  4494. REG_WRITE(ah, AR_SELFGEN_MASK, tx_chainmask);
  4495. if (tx_chainmask == 0x5) {
  4496. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  4497. AR_PHY_SWAP_ALT_CHAIN);
  4498. }
  4499. if (AR_SREV_9100(ah))
  4500. REG_WRITE(ah, AR_PHY_ANALOG_SWAP,
  4501. REG_READ(ah, AR_PHY_ANALOG_SWAP) | 0x00000001);
  4502. }
  4503. static void ath9k_hw_set_addac(struct ath_hal *ah,
  4504. struct ath9k_channel *chan)
  4505. {
  4506. struct modal_eep_header *pModal;
  4507. struct ath_hal_5416 *ahp = AH5416(ah);
  4508. struct ar5416_eeprom *eep = &ahp->ah_eeprom;
  4509. u8 biaslevel;
  4510. if (ah->ah_macVersion != AR_SREV_VERSION_9160)
  4511. return;
  4512. if (ar5416_get_eep_rev(ahp) < AR5416_EEP_MINOR_VER_7)
  4513. return;
  4514. pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  4515. if (pModal->xpaBiasLvl != 0xff) {
  4516. biaslevel = pModal->xpaBiasLvl;
  4517. } else {
  4518. u16 resetFreqBin, freqBin, freqCount = 0;
  4519. struct chan_centers centers;
  4520. ath9k_hw_get_channel_centers(ah, chan, &centers);
  4521. resetFreqBin =
  4522. FREQ2FBIN(centers.synth_center, IS_CHAN_2GHZ(chan));
  4523. freqBin = pModal->xpaBiasLvlFreq[0] & 0xff;
  4524. biaslevel = (u8) (pModal->xpaBiasLvlFreq[0] >> 14);
  4525. freqCount++;
  4526. while (freqCount < 3) {
  4527. if (pModal->xpaBiasLvlFreq[freqCount] == 0x0)
  4528. break;
  4529. freqBin = pModal->xpaBiasLvlFreq[freqCount] & 0xff;
  4530. if (resetFreqBin >= freqBin) {
  4531. biaslevel =
  4532. (u8) (pModal->
  4533. xpaBiasLvlFreq[freqCount]
  4534. >> 14);
  4535. } else {
  4536. break;
  4537. }
  4538. freqCount++;
  4539. }
  4540. }
  4541. if (IS_CHAN_2GHZ(chan)) {
  4542. INI_RA(&ahp->ah_iniAddac, 7, 1) =
  4543. (INI_RA(&ahp->ah_iniAddac, 7, 1) & (~0x18)) | biaslevel
  4544. << 3;
  4545. } else {
  4546. INI_RA(&ahp->ah_iniAddac, 6, 1) =
  4547. (INI_RA(&ahp->ah_iniAddac, 6, 1) & (~0xc0)) | biaslevel
  4548. << 6;
  4549. }
  4550. }
  4551. static u32 ath9k_hw_mac_usec(struct ath_hal *ah, u32 clks)
  4552. {
  4553. if (ah->ah_curchan != NULL)
  4554. return clks /
  4555. CLOCK_RATE[ath9k_hw_chan2wmode(ah, ah->ah_curchan)];
  4556. else
  4557. return clks / CLOCK_RATE[ATH9K_MODE_11B];
  4558. }
  4559. static u32 ath9k_hw_mac_to_usec(struct ath_hal *ah, u32 clks)
  4560. {
  4561. struct ath9k_channel *chan = ah->ah_curchan;
  4562. if (chan && IS_CHAN_HT40(chan))
  4563. return ath9k_hw_mac_usec(ah, clks) / 2;
  4564. else
  4565. return ath9k_hw_mac_usec(ah, clks);
  4566. }
  4567. static u32 ath9k_hw_mac_clks(struct ath_hal *ah, u32 usecs)
  4568. {
  4569. if (ah->ah_curchan != NULL)
  4570. return usecs * CLOCK_RATE[ath9k_hw_chan2wmode(ah,
  4571. ah->ah_curchan)];
  4572. else
  4573. return usecs * CLOCK_RATE[ATH9K_MODE_11B];
  4574. }
  4575. static u32 ath9k_hw_mac_to_clks(struct ath_hal *ah, u32 usecs)
  4576. {
  4577. struct ath9k_channel *chan = ah->ah_curchan;
  4578. if (chan && IS_CHAN_HT40(chan))
  4579. return ath9k_hw_mac_clks(ah, usecs) * 2;
  4580. else
  4581. return ath9k_hw_mac_clks(ah, usecs);
  4582. }
  4583. static bool ath9k_hw_set_ack_timeout(struct ath_hal *ah, u32 us)
  4584. {
  4585. struct ath_hal_5416 *ahp = AH5416(ah);
  4586. if (us > ath9k_hw_mac_to_usec(ah, MS(0xffffffff, AR_TIME_OUT_ACK))) {
  4587. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: bad ack timeout %u\n",
  4588. __func__, us);
  4589. ahp->ah_acktimeout = (u32) -1;
  4590. return false;
  4591. } else {
  4592. REG_RMW_FIELD(ah, AR_TIME_OUT,
  4593. AR_TIME_OUT_ACK, ath9k_hw_mac_to_clks(ah, us));
  4594. ahp->ah_acktimeout = us;
  4595. return true;
  4596. }
  4597. }
  4598. static bool ath9k_hw_set_cts_timeout(struct ath_hal *ah, u32 us)
  4599. {
  4600. struct ath_hal_5416 *ahp = AH5416(ah);
  4601. if (us > ath9k_hw_mac_to_usec(ah, MS(0xffffffff, AR_TIME_OUT_CTS))) {
  4602. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: bad cts timeout %u\n",
  4603. __func__, us);
  4604. ahp->ah_ctstimeout = (u32) -1;
  4605. return false;
  4606. } else {
  4607. REG_RMW_FIELD(ah, AR_TIME_OUT,
  4608. AR_TIME_OUT_CTS, ath9k_hw_mac_to_clks(ah, us));
  4609. ahp->ah_ctstimeout = us;
  4610. return true;
  4611. }
  4612. }
  4613. static bool ath9k_hw_set_global_txtimeout(struct ath_hal *ah,
  4614. u32 tu)
  4615. {
  4616. struct ath_hal_5416 *ahp = AH5416(ah);
  4617. if (tu > 0xFFFF) {
  4618. DPRINTF(ah->ah_sc, ATH_DBG_XMIT,
  4619. "%s: bad global tx timeout %u\n", __func__, tu);
  4620. ahp->ah_globaltxtimeout = (u32) -1;
  4621. return false;
  4622. } else {
  4623. REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
  4624. ahp->ah_globaltxtimeout = tu;
  4625. return true;
  4626. }
  4627. }
  4628. bool ath9k_hw_setslottime(struct ath_hal *ah, u32 us)
  4629. {
  4630. struct ath_hal_5416 *ahp = AH5416(ah);
  4631. if (us < ATH9K_SLOT_TIME_9 || us > ath9k_hw_mac_to_usec(ah, 0xffff)) {
  4632. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: bad slot time %u\n",
  4633. __func__, us);
  4634. ahp->ah_slottime = (u32) -1;
  4635. return false;
  4636. } else {
  4637. REG_WRITE(ah, AR_D_GBL_IFS_SLOT, ath9k_hw_mac_to_clks(ah, us));
  4638. ahp->ah_slottime = us;
  4639. return true;
  4640. }
  4641. }
  4642. static inline void ath9k_hw_init_user_settings(struct ath_hal *ah)
  4643. {
  4644. struct ath_hal_5416 *ahp = AH5416(ah);
  4645. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "--AP %s ahp->ah_miscMode 0x%x\n",
  4646. __func__, ahp->ah_miscMode);
  4647. if (ahp->ah_miscMode != 0)
  4648. REG_WRITE(ah, AR_PCU_MISC,
  4649. REG_READ(ah, AR_PCU_MISC) | ahp->ah_miscMode);
  4650. if (ahp->ah_slottime != (u32) -1)
  4651. ath9k_hw_setslottime(ah, ahp->ah_slottime);
  4652. if (ahp->ah_acktimeout != (u32) -1)
  4653. ath9k_hw_set_ack_timeout(ah, ahp->ah_acktimeout);
  4654. if (ahp->ah_ctstimeout != (u32) -1)
  4655. ath9k_hw_set_cts_timeout(ah, ahp->ah_ctstimeout);
  4656. if (ahp->ah_globaltxtimeout != (u32) -1)
  4657. ath9k_hw_set_global_txtimeout(ah, ahp->ah_globaltxtimeout);
  4658. }
  4659. static inline int
  4660. ath9k_hw_process_ini(struct ath_hal *ah,
  4661. struct ath9k_channel *chan,
  4662. enum ath9k_ht_macmode macmode)
  4663. {
  4664. int i, regWrites = 0;
  4665. struct ath_hal_5416 *ahp = AH5416(ah);
  4666. u32 modesIndex, freqIndex;
  4667. int status;
  4668. switch (chan->chanmode) {
  4669. case CHANNEL_A:
  4670. case CHANNEL_A_HT20:
  4671. modesIndex = 1;
  4672. freqIndex = 1;
  4673. break;
  4674. case CHANNEL_A_HT40PLUS:
  4675. case CHANNEL_A_HT40MINUS:
  4676. modesIndex = 2;
  4677. freqIndex = 1;
  4678. break;
  4679. case CHANNEL_G:
  4680. case CHANNEL_G_HT20:
  4681. case CHANNEL_B:
  4682. modesIndex = 4;
  4683. freqIndex = 2;
  4684. break;
  4685. case CHANNEL_G_HT40PLUS:
  4686. case CHANNEL_G_HT40MINUS:
  4687. modesIndex = 3;
  4688. freqIndex = 2;
  4689. break;
  4690. default:
  4691. return -EINVAL;
  4692. }
  4693. REG_WRITE(ah, AR_PHY(0), 0x00000007);
  4694. REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
  4695. ath9k_hw_set_addac(ah, chan);
  4696. if (AR_SREV_5416_V22_OR_LATER(ah)) {
  4697. REG_WRITE_ARRAY(&ahp->ah_iniAddac, 1, regWrites);
  4698. } else {
  4699. struct ar5416IniArray temp;
  4700. u32 addacSize =
  4701. sizeof(u32) * ahp->ah_iniAddac.ia_rows *
  4702. ahp->ah_iniAddac.ia_columns;
  4703. memcpy(ahp->ah_addac5416_21,
  4704. ahp->ah_iniAddac.ia_array, addacSize);
  4705. (ahp->ah_addac5416_21)[31 *
  4706. ahp->ah_iniAddac.ia_columns + 1] = 0;
  4707. temp.ia_array = ahp->ah_addac5416_21;
  4708. temp.ia_columns = ahp->ah_iniAddac.ia_columns;
  4709. temp.ia_rows = ahp->ah_iniAddac.ia_rows;
  4710. REG_WRITE_ARRAY(&temp, 1, regWrites);
  4711. }
  4712. REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
  4713. for (i = 0; i < ahp->ah_iniModes.ia_rows; i++) {
  4714. u32 reg = INI_RA(&ahp->ah_iniModes, i, 0);
  4715. u32 val = INI_RA(&ahp->ah_iniModes, i, modesIndex);
  4716. #ifdef CONFIG_SLOW_ANT_DIV
  4717. if (ah->ah_devid == AR9280_DEVID_PCI)
  4718. val = ath9k_hw_ini_fixup(ah, &ahp->ah_eeprom, reg,
  4719. val);
  4720. #endif
  4721. REG_WRITE(ah, reg, val);
  4722. if (reg >= 0x7800 && reg < 0x78a0
  4723. && ah->ah_config.analog_shiftreg) {
  4724. udelay(100);
  4725. }
  4726. DO_DELAY(regWrites);
  4727. }
  4728. for (i = 0; i < ahp->ah_iniCommon.ia_rows; i++) {
  4729. u32 reg = INI_RA(&ahp->ah_iniCommon, i, 0);
  4730. u32 val = INI_RA(&ahp->ah_iniCommon, i, 1);
  4731. REG_WRITE(ah, reg, val);
  4732. if (reg >= 0x7800 && reg < 0x78a0
  4733. && ah->ah_config.analog_shiftreg) {
  4734. udelay(100);
  4735. }
  4736. DO_DELAY(regWrites);
  4737. }
  4738. ath9k_hw_write_regs(ah, modesIndex, freqIndex, regWrites);
  4739. if (AR_SREV_9280_20(ah) && IS_CHAN_A_5MHZ_SPACED(chan)) {
  4740. REG_WRITE_ARRAY(&ahp->ah_iniModesAdditional, modesIndex,
  4741. regWrites);
  4742. }
  4743. ath9k_hw_override_ini(ah, chan);
  4744. ath9k_hw_set_regs(ah, chan, macmode);
  4745. ath9k_hw_init_chain_masks(ah);
  4746. status = ath9k_hw_set_txpower(ah, &ahp->ah_eeprom, chan,
  4747. ath9k_regd_get_ctl(ah, chan),
  4748. ath9k_regd_get_antenna_allowed(ah,
  4749. chan),
  4750. chan->maxRegTxPower * 2,
  4751. min((u32) MAX_RATE_POWER,
  4752. (u32) ah->ah_powerLimit));
  4753. if (status != 0) {
  4754. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  4755. "%s: error init'ing transmit power\n", __func__);
  4756. return -EIO;
  4757. }
  4758. if (!ath9k_hw_set_rf_regs(ah, chan, freqIndex)) {
  4759. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  4760. "%s: ar5416SetRfRegs failed\n", __func__);
  4761. return -EIO;
  4762. }
  4763. return 0;
  4764. }
  4765. static inline void ath9k_hw_setup_calibration(struct ath_hal *ah,
  4766. struct hal_cal_list *currCal)
  4767. {
  4768. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(0),
  4769. AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
  4770. currCal->calData->calCountMax);
  4771. switch (currCal->calData->calType) {
  4772. case IQ_MISMATCH_CAL:
  4773. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  4774. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4775. "%s: starting IQ Mismatch Calibration\n",
  4776. __func__);
  4777. break;
  4778. case ADC_GAIN_CAL:
  4779. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
  4780. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4781. "%s: starting ADC Gain Calibration\n", __func__);
  4782. break;
  4783. case ADC_DC_CAL:
  4784. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
  4785. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4786. "%s: starting ADC DC Calibration\n", __func__);
  4787. break;
  4788. case ADC_DC_INIT_CAL:
  4789. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
  4790. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4791. "%s: starting Init ADC DC Calibration\n",
  4792. __func__);
  4793. break;
  4794. }
  4795. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  4796. AR_PHY_TIMING_CTRL4_DO_CAL);
  4797. }
  4798. static inline void ath9k_hw_reset_calibration(struct ath_hal *ah,
  4799. struct hal_cal_list *currCal)
  4800. {
  4801. struct ath_hal_5416 *ahp = AH5416(ah);
  4802. int i;
  4803. ath9k_hw_setup_calibration(ah, currCal);
  4804. currCal->calState = CAL_RUNNING;
  4805. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  4806. ahp->ah_Meas0.sign[i] = 0;
  4807. ahp->ah_Meas1.sign[i] = 0;
  4808. ahp->ah_Meas2.sign[i] = 0;
  4809. ahp->ah_Meas3.sign[i] = 0;
  4810. }
  4811. ahp->ah_CalSamples = 0;
  4812. }
  4813. static inline void
  4814. ath9k_hw_per_calibration(struct ath_hal *ah,
  4815. struct ath9k_channel *ichan,
  4816. u8 rxchainmask,
  4817. struct hal_cal_list *currCal,
  4818. bool *isCalDone)
  4819. {
  4820. struct ath_hal_5416 *ahp = AH5416(ah);
  4821. *isCalDone = false;
  4822. if (currCal->calState == CAL_RUNNING) {
  4823. if (!(REG_READ(ah,
  4824. AR_PHY_TIMING_CTRL4(0)) &
  4825. AR_PHY_TIMING_CTRL4_DO_CAL)) {
  4826. currCal->calData->calCollect(ah);
  4827. ahp->ah_CalSamples++;
  4828. if (ahp->ah_CalSamples >=
  4829. currCal->calData->calNumSamples) {
  4830. int i, numChains = 0;
  4831. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  4832. if (rxchainmask & (1 << i))
  4833. numChains++;
  4834. }
  4835. currCal->calData->calPostProc(ah,
  4836. numChains);
  4837. ichan->CalValid |=
  4838. currCal->calData->calType;
  4839. currCal->calState = CAL_DONE;
  4840. *isCalDone = true;
  4841. } else {
  4842. ath9k_hw_setup_calibration(ah, currCal);
  4843. }
  4844. }
  4845. } else if (!(ichan->CalValid & currCal->calData->calType)) {
  4846. ath9k_hw_reset_calibration(ah, currCal);
  4847. }
  4848. }
  4849. static inline bool ath9k_hw_run_init_cals(struct ath_hal *ah,
  4850. int init_cal_count)
  4851. {
  4852. struct ath_hal_5416 *ahp = AH5416(ah);
  4853. struct ath9k_channel ichan;
  4854. bool isCalDone;
  4855. struct hal_cal_list *currCal = ahp->ah_cal_list_curr;
  4856. const struct hal_percal_data *calData = currCal->calData;
  4857. int i;
  4858. if (currCal == NULL)
  4859. return false;
  4860. ichan.CalValid = 0;
  4861. for (i = 0; i < init_cal_count; i++) {
  4862. ath9k_hw_reset_calibration(ah, currCal);
  4863. if (!ath9k_hw_wait(ah, AR_PHY_TIMING_CTRL4(0),
  4864. AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
  4865. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4866. "%s: Cal %d failed to complete in 100ms.\n",
  4867. __func__, calData->calType);
  4868. ahp->ah_cal_list = ahp->ah_cal_list_last =
  4869. ahp->ah_cal_list_curr = NULL;
  4870. return false;
  4871. }
  4872. ath9k_hw_per_calibration(ah, &ichan, ahp->ah_rxchainmask,
  4873. currCal, &isCalDone);
  4874. if (!isCalDone) {
  4875. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  4876. "%s: Not able to run Init Cal %d.\n",
  4877. __func__, calData->calType);
  4878. }
  4879. if (currCal->calNext) {
  4880. currCal = currCal->calNext;
  4881. calData = currCal->calData;
  4882. }
  4883. }
  4884. ahp->ah_cal_list = ahp->ah_cal_list_last = ahp->ah_cal_list_curr = NULL;
  4885. return true;
  4886. }
  4887. static inline bool
  4888. ath9k_hw_channel_change(struct ath_hal *ah,
  4889. struct ath9k_channel *chan,
  4890. enum ath9k_ht_macmode macmode)
  4891. {
  4892. u32 synthDelay, qnum;
  4893. struct ath_hal_5416 *ahp = AH5416(ah);
  4894. for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
  4895. if (ath9k_hw_numtxpending(ah, qnum)) {
  4896. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  4897. "%s: Transmit frames pending on queue %d\n",
  4898. __func__, qnum);
  4899. return false;
  4900. }
  4901. }
  4902. REG_WRITE(ah, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
  4903. if (!ath9k_hw_wait(ah, AR_PHY_RFBUS_GRANT, AR_PHY_RFBUS_GRANT_EN,
  4904. AR_PHY_RFBUS_GRANT_EN)) {
  4905. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  4906. "%s: Could not kill baseband RX\n", __func__);
  4907. return false;
  4908. }
  4909. ath9k_hw_set_regs(ah, chan, macmode);
  4910. if (AR_SREV_9280_10_OR_LATER(ah)) {
  4911. if (!(ath9k_hw_ar9280_set_channel(ah, chan))) {
  4912. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  4913. "%s: failed to set channel\n", __func__);
  4914. return false;
  4915. }
  4916. } else {
  4917. if (!(ath9k_hw_set_channel(ah, chan))) {
  4918. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  4919. "%s: failed to set channel\n", __func__);
  4920. return false;
  4921. }
  4922. }
  4923. if (ath9k_hw_set_txpower(ah, &ahp->ah_eeprom, chan,
  4924. ath9k_regd_get_ctl(ah, chan),
  4925. ath9k_regd_get_antenna_allowed(ah, chan),
  4926. chan->maxRegTxPower * 2,
  4927. min((u32) MAX_RATE_POWER,
  4928. (u32) ah->ah_powerLimit)) != 0) {
  4929. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  4930. "%s: error init'ing transmit power\n", __func__);
  4931. return false;
  4932. }
  4933. synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  4934. if (IS_CHAN_CCK(chan))
  4935. synthDelay = (4 * synthDelay) / 22;
  4936. else
  4937. synthDelay /= 10;
  4938. udelay(synthDelay + BASE_ACTIVATE_DELAY);
  4939. REG_WRITE(ah, AR_PHY_RFBUS_REQ, 0);
  4940. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  4941. ath9k_hw_set_delta_slope(ah, chan);
  4942. if (AR_SREV_9280_10_OR_LATER(ah))
  4943. ath9k_hw_9280_spur_mitigate(ah, chan);
  4944. else
  4945. ath9k_hw_spur_mitigate(ah, chan);
  4946. if (!chan->oneTimeCalsDone)
  4947. chan->oneTimeCalsDone = true;
  4948. return true;
  4949. }
  4950. static bool ath9k_hw_chip_reset(struct ath_hal *ah,
  4951. struct ath9k_channel *chan)
  4952. {
  4953. struct ath_hal_5416 *ahp = AH5416(ah);
  4954. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  4955. return false;
  4956. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  4957. return false;
  4958. ahp->ah_chipFullSleep = false;
  4959. ath9k_hw_init_pll(ah, chan);
  4960. ath9k_hw_set_rfmode(ah, chan);
  4961. return true;
  4962. }
  4963. static inline void ath9k_hw_set_dma(struct ath_hal *ah)
  4964. {
  4965. u32 regval;
  4966. regval = REG_READ(ah, AR_AHB_MODE);
  4967. REG_WRITE(ah, AR_AHB_MODE, regval | AR_AHB_PREFETCH_RD_EN);
  4968. regval = REG_READ(ah, AR_TXCFG) & ~AR_TXCFG_DMASZ_MASK;
  4969. REG_WRITE(ah, AR_TXCFG, regval | AR_TXCFG_DMASZ_128B);
  4970. REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->ah_txTrigLevel);
  4971. regval = REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_DMASZ_MASK;
  4972. REG_WRITE(ah, AR_RXCFG, regval | AR_RXCFG_DMASZ_128B);
  4973. REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
  4974. if (AR_SREV_9285(ah)) {
  4975. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  4976. AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
  4977. } else {
  4978. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  4979. AR_PCU_TXBUF_CTRL_USABLE_SIZE);
  4980. }
  4981. }
  4982. bool ath9k_hw_stopdmarecv(struct ath_hal *ah)
  4983. {
  4984. REG_WRITE(ah, AR_CR, AR_CR_RXD);
  4985. if (!ath9k_hw_wait(ah, AR_CR, AR_CR_RXE, 0)) {
  4986. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  4987. "%s: dma failed to stop in 10ms\n"
  4988. "AR_CR=0x%08x\nAR_DIAG_SW=0x%08x\n",
  4989. __func__,
  4990. REG_READ(ah, AR_CR), REG_READ(ah, AR_DIAG_SW));
  4991. return false;
  4992. } else {
  4993. return true;
  4994. }
  4995. }
  4996. void ath9k_hw_startpcureceive(struct ath_hal *ah)
  4997. {
  4998. REG_CLR_BIT(ah, AR_DIAG_SW,
  4999. (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  5000. ath9k_enable_mib_counters(ah);
  5001. ath9k_ani_reset(ah);
  5002. }
  5003. void ath9k_hw_stoppcurecv(struct ath_hal *ah)
  5004. {
  5005. REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
  5006. ath9k_hw_disable_mib_counters(ah);
  5007. }
  5008. static bool ath9k_hw_iscal_supported(struct ath_hal *ah,
  5009. struct ath9k_channel *chan,
  5010. enum hal_cal_types calType)
  5011. {
  5012. struct ath_hal_5416 *ahp = AH5416(ah);
  5013. bool retval = false;
  5014. switch (calType & ahp->ah_suppCals) {
  5015. case IQ_MISMATCH_CAL:
  5016. if (!IS_CHAN_B(chan))
  5017. retval = true;
  5018. break;
  5019. case ADC_GAIN_CAL:
  5020. case ADC_DC_CAL:
  5021. if (!IS_CHAN_B(chan)
  5022. && !(IS_CHAN_2GHZ(chan) && IS_CHAN_HT20(chan)))
  5023. retval = true;
  5024. break;
  5025. }
  5026. return retval;
  5027. }
  5028. static inline bool ath9k_hw_init_cal(struct ath_hal *ah,
  5029. struct ath9k_channel *chan)
  5030. {
  5031. struct ath_hal_5416 *ahp = AH5416(ah);
  5032. struct ath9k_channel *ichan =
  5033. ath9k_regd_check_channel(ah, chan);
  5034. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  5035. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  5036. AR_PHY_AGC_CONTROL_CAL);
  5037. if (!ath9k_hw_wait
  5038. (ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
  5039. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5040. "%s: offset calibration failed to complete in 1ms; "
  5041. "noisy environment?\n", __func__);
  5042. return false;
  5043. }
  5044. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  5045. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  5046. AR_PHY_AGC_CONTROL_NF);
  5047. ahp->ah_cal_list = ahp->ah_cal_list_last = ahp->ah_cal_list_curr =
  5048. NULL;
  5049. if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah)) {
  5050. if (ath9k_hw_iscal_supported(ah, chan, ADC_GAIN_CAL)) {
  5051. INIT_CAL(&ahp->ah_adcGainCalData);
  5052. INSERT_CAL(ahp, &ahp->ah_adcGainCalData);
  5053. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5054. "%s: enabling ADC Gain Calibration.\n",
  5055. __func__);
  5056. }
  5057. if (ath9k_hw_iscal_supported(ah, chan, ADC_DC_CAL)) {
  5058. INIT_CAL(&ahp->ah_adcDcCalData);
  5059. INSERT_CAL(ahp, &ahp->ah_adcDcCalData);
  5060. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5061. "%s: enabling ADC DC Calibration.\n",
  5062. __func__);
  5063. }
  5064. if (ath9k_hw_iscal_supported(ah, chan, IQ_MISMATCH_CAL)) {
  5065. INIT_CAL(&ahp->ah_iqCalData);
  5066. INSERT_CAL(ahp, &ahp->ah_iqCalData);
  5067. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5068. "%s: enabling IQ Calibration.\n",
  5069. __func__);
  5070. }
  5071. ahp->ah_cal_list_curr = ahp->ah_cal_list;
  5072. if (ahp->ah_cal_list_curr)
  5073. ath9k_hw_reset_calibration(ah,
  5074. ahp->ah_cal_list_curr);
  5075. }
  5076. ichan->CalValid = 0;
  5077. return true;
  5078. }
  5079. bool ath9k_hw_reset(struct ath_hal *ah, enum ath9k_opmode opmode,
  5080. struct ath9k_channel *chan,
  5081. enum ath9k_ht_macmode macmode,
  5082. u8 txchainmask, u8 rxchainmask,
  5083. enum ath9k_ht_extprotspacing extprotspacing,
  5084. bool bChannelChange,
  5085. int *status)
  5086. {
  5087. #define FAIL(_code) do { ecode = _code; goto bad; } while (0)
  5088. u32 saveLedState;
  5089. struct ath_hal_5416 *ahp = AH5416(ah);
  5090. struct ath9k_channel *curchan = ah->ah_curchan;
  5091. u32 saveDefAntenna;
  5092. u32 macStaId1;
  5093. int ecode;
  5094. int i, rx_chainmask;
  5095. ahp->ah_extprotspacing = extprotspacing;
  5096. ahp->ah_txchainmask = txchainmask;
  5097. ahp->ah_rxchainmask = rxchainmask;
  5098. if (AR_SREV_9280(ah)) {
  5099. ahp->ah_txchainmask &= 0x3;
  5100. ahp->ah_rxchainmask &= 0x3;
  5101. }
  5102. if (ath9k_hw_check_chan(ah, chan) == NULL) {
  5103. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  5104. "%s: invalid channel %u/0x%x; no mapping\n",
  5105. __func__, chan->channel, chan->channelFlags);
  5106. FAIL(-EINVAL);
  5107. }
  5108. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  5109. return false;
  5110. if (curchan)
  5111. ath9k_hw_getnf(ah, curchan);
  5112. if (bChannelChange &&
  5113. (ahp->ah_chipFullSleep != true) &&
  5114. (ah->ah_curchan != NULL) &&
  5115. (chan->channel != ah->ah_curchan->channel) &&
  5116. ((chan->channelFlags & CHANNEL_ALL) ==
  5117. (ah->ah_curchan->channelFlags & CHANNEL_ALL)) &&
  5118. (!AR_SREV_9280(ah) || (!IS_CHAN_A_5MHZ_SPACED(chan) &&
  5119. !IS_CHAN_A_5MHZ_SPACED(ah->
  5120. ah_curchan)))) {
  5121. if (ath9k_hw_channel_change(ah, chan, macmode)) {
  5122. ath9k_hw_loadnf(ah, ah->ah_curchan);
  5123. ath9k_hw_start_nfcal(ah);
  5124. return true;
  5125. }
  5126. }
  5127. saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
  5128. if (saveDefAntenna == 0)
  5129. saveDefAntenna = 1;
  5130. macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
  5131. saveLedState = REG_READ(ah, AR_CFG_LED) &
  5132. (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
  5133. AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
  5134. ath9k_hw_mark_phy_inactive(ah);
  5135. if (!ath9k_hw_chip_reset(ah, chan)) {
  5136. DPRINTF(ah->ah_sc, ATH_DBG_RESET, "%s: chip reset failed\n",
  5137. __func__);
  5138. FAIL(-EIO);
  5139. }
  5140. if (AR_SREV_9280(ah)) {
  5141. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
  5142. AR_GPIO_JTAG_DISABLE);
  5143. if (test_bit(ATH9K_MODE_11A, ah->ah_caps.wireless_modes)) {
  5144. if (IS_CHAN_5GHZ(chan))
  5145. ath9k_hw_set_gpio(ah, 9, 0);
  5146. else
  5147. ath9k_hw_set_gpio(ah, 9, 1);
  5148. }
  5149. ath9k_hw_cfg_output(ah, 9, ATH9K_GPIO_OUTPUT_MUX_AS_OUTPUT);
  5150. }
  5151. ecode = ath9k_hw_process_ini(ah, chan, macmode);
  5152. if (ecode != 0)
  5153. goto bad;
  5154. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  5155. ath9k_hw_set_delta_slope(ah, chan);
  5156. if (AR_SREV_9280_10_OR_LATER(ah))
  5157. ath9k_hw_9280_spur_mitigate(ah, chan);
  5158. else
  5159. ath9k_hw_spur_mitigate(ah, chan);
  5160. if (!ath9k_hw_eeprom_set_board_values(ah, chan)) {
  5161. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  5162. "%s: error setting board options\n", __func__);
  5163. FAIL(-EIO);
  5164. }
  5165. ath9k_hw_decrease_chain_power(ah, chan);
  5166. REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(ahp->ah_macaddr));
  5167. REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(ahp->ah_macaddr + 4)
  5168. | macStaId1
  5169. | AR_STA_ID1_RTS_USE_DEF
  5170. | (ah->ah_config.
  5171. ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
  5172. | ahp->ah_staId1Defaults);
  5173. ath9k_hw_set_operating_mode(ah, opmode);
  5174. REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(ahp->ah_bssidmask));
  5175. REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(ahp->ah_bssidmask + 4));
  5176. REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
  5177. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(ahp->ah_bssid));
  5178. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(ahp->ah_bssid + 4) |
  5179. ((ahp->ah_assocId & 0x3fff) << AR_BSS_ID1_AID_S));
  5180. REG_WRITE(ah, AR_ISR, ~0);
  5181. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  5182. if (AR_SREV_9280_10_OR_LATER(ah)) {
  5183. if (!(ath9k_hw_ar9280_set_channel(ah, chan)))
  5184. FAIL(-EIO);
  5185. } else {
  5186. if (!(ath9k_hw_set_channel(ah, chan)))
  5187. FAIL(-EIO);
  5188. }
  5189. for (i = 0; i < AR_NUM_DCU; i++)
  5190. REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
  5191. ahp->ah_intrTxqs = 0;
  5192. for (i = 0; i < ah->ah_caps.total_queues; i++)
  5193. ath9k_hw_resettxqueue(ah, i);
  5194. ath9k_hw_init_interrupt_masks(ah, opmode);
  5195. ath9k_hw_init_qos(ah);
  5196. ath9k_hw_init_user_settings(ah);
  5197. ah->ah_opmode = opmode;
  5198. REG_WRITE(ah, AR_STA_ID1,
  5199. REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PRESERVE_SEQNUM);
  5200. ath9k_hw_set_dma(ah);
  5201. REG_WRITE(ah, AR_OBS, 8);
  5202. if (ahp->ah_intrMitigation) {
  5203. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
  5204. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
  5205. }
  5206. ath9k_hw_init_bb(ah, chan);
  5207. if (!ath9k_hw_init_cal(ah, chan))
  5208. FAIL(-ENODEV);
  5209. rx_chainmask = ahp->ah_rxchainmask;
  5210. if ((rx_chainmask == 0x5) || (rx_chainmask == 0x3)) {
  5211. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
  5212. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
  5213. }
  5214. REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
  5215. if (AR_SREV_9100(ah)) {
  5216. u32 mask;
  5217. mask = REG_READ(ah, AR_CFG);
  5218. if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
  5219. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  5220. "%s CFG Byte Swap Set 0x%x\n", __func__,
  5221. mask);
  5222. } else {
  5223. mask =
  5224. INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
  5225. REG_WRITE(ah, AR_CFG, mask);
  5226. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  5227. "%s Setting CFG 0x%x\n", __func__,
  5228. REG_READ(ah, AR_CFG));
  5229. }
  5230. } else {
  5231. #ifdef __BIG_ENDIAN
  5232. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  5233. #endif
  5234. }
  5235. return true;
  5236. bad:
  5237. if (status)
  5238. *status = ecode;
  5239. return false;
  5240. #undef FAIL
  5241. }
  5242. bool ath9k_hw_phy_disable(struct ath_hal *ah)
  5243. {
  5244. return ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM);
  5245. }
  5246. bool ath9k_hw_disable(struct ath_hal *ah)
  5247. {
  5248. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  5249. return false;
  5250. return ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD);
  5251. }
  5252. bool
  5253. ath9k_hw_calibrate(struct ath_hal *ah, struct ath9k_channel *chan,
  5254. u8 rxchainmask, bool longcal,
  5255. bool *isCalDone)
  5256. {
  5257. struct ath_hal_5416 *ahp = AH5416(ah);
  5258. struct hal_cal_list *currCal = ahp->ah_cal_list_curr;
  5259. struct ath9k_channel *ichan =
  5260. ath9k_regd_check_channel(ah, chan);
  5261. *isCalDone = true;
  5262. if (ichan == NULL) {
  5263. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL,
  5264. "%s: invalid channel %u/0x%x; no mapping\n",
  5265. __func__, chan->channel, chan->channelFlags);
  5266. return false;
  5267. }
  5268. if (currCal &&
  5269. (currCal->calState == CAL_RUNNING ||
  5270. currCal->calState == CAL_WAITING)) {
  5271. ath9k_hw_per_calibration(ah, ichan, rxchainmask, currCal,
  5272. isCalDone);
  5273. if (*isCalDone) {
  5274. ahp->ah_cal_list_curr = currCal = currCal->calNext;
  5275. if (currCal->calState == CAL_WAITING) {
  5276. *isCalDone = false;
  5277. ath9k_hw_reset_calibration(ah, currCal);
  5278. }
  5279. }
  5280. }
  5281. if (longcal) {
  5282. ath9k_hw_getnf(ah, ichan);
  5283. ath9k_hw_loadnf(ah, ah->ah_curchan);
  5284. ath9k_hw_start_nfcal(ah);
  5285. if ((ichan->channelFlags & CHANNEL_CW_INT) != 0) {
  5286. chan->channelFlags |= CHANNEL_CW_INT;
  5287. ichan->channelFlags &= ~CHANNEL_CW_INT;
  5288. }
  5289. }
  5290. return true;
  5291. }
  5292. static void ath9k_hw_iqcal_collect(struct ath_hal *ah)
  5293. {
  5294. struct ath_hal_5416 *ahp = AH5416(ah);
  5295. int i;
  5296. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  5297. ahp->ah_totalPowerMeasI[i] +=
  5298. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  5299. ahp->ah_totalPowerMeasQ[i] +=
  5300. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  5301. ahp->ah_totalIqCorrMeas[i] +=
  5302. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  5303. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5304. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  5305. ahp->ah_CalSamples, i, ahp->ah_totalPowerMeasI[i],
  5306. ahp->ah_totalPowerMeasQ[i],
  5307. ahp->ah_totalIqCorrMeas[i]);
  5308. }
  5309. }
  5310. static void ath9k_hw_adc_gaincal_collect(struct ath_hal *ah)
  5311. {
  5312. struct ath_hal_5416 *ahp = AH5416(ah);
  5313. int i;
  5314. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  5315. ahp->ah_totalAdcIOddPhase[i] +=
  5316. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  5317. ahp->ah_totalAdcIEvenPhase[i] +=
  5318. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  5319. ahp->ah_totalAdcQOddPhase[i] +=
  5320. REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  5321. ahp->ah_totalAdcQEvenPhase[i] +=
  5322. REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  5323. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5324. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  5325. "oddq=0x%08x; evenq=0x%08x;\n",
  5326. ahp->ah_CalSamples, i,
  5327. ahp->ah_totalAdcIOddPhase[i],
  5328. ahp->ah_totalAdcIEvenPhase[i],
  5329. ahp->ah_totalAdcQOddPhase[i],
  5330. ahp->ah_totalAdcQEvenPhase[i]);
  5331. }
  5332. }
  5333. static void ath9k_hw_adc_dccal_collect(struct ath_hal *ah)
  5334. {
  5335. struct ath_hal_5416 *ahp = AH5416(ah);
  5336. int i;
  5337. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  5338. ahp->ah_totalAdcDcOffsetIOddPhase[i] +=
  5339. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  5340. ahp->ah_totalAdcDcOffsetIEvenPhase[i] +=
  5341. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  5342. ahp->ah_totalAdcDcOffsetQOddPhase[i] +=
  5343. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  5344. ahp->ah_totalAdcDcOffsetQEvenPhase[i] +=
  5345. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  5346. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5347. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  5348. "oddq=0x%08x; evenq=0x%08x;\n",
  5349. ahp->ah_CalSamples, i,
  5350. ahp->ah_totalAdcDcOffsetIOddPhase[i],
  5351. ahp->ah_totalAdcDcOffsetIEvenPhase[i],
  5352. ahp->ah_totalAdcDcOffsetQOddPhase[i],
  5353. ahp->ah_totalAdcDcOffsetQEvenPhase[i]);
  5354. }
  5355. }
  5356. static void ath9k_hw_iqcalibrate(struct ath_hal *ah, u8 numChains)
  5357. {
  5358. struct ath_hal_5416 *ahp = AH5416(ah);
  5359. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  5360. u32 qCoffDenom, iCoffDenom;
  5361. int32_t qCoff, iCoff;
  5362. int iqCorrNeg, i;
  5363. for (i = 0; i < numChains; i++) {
  5364. powerMeasI = ahp->ah_totalPowerMeasI[i];
  5365. powerMeasQ = ahp->ah_totalPowerMeasQ[i];
  5366. iqCorrMeas = ahp->ah_totalIqCorrMeas[i];
  5367. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5368. "Starting IQ Cal and Correction for Chain %d\n",
  5369. i);
  5370. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5371. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  5372. i, ahp->ah_totalIqCorrMeas[i]);
  5373. iqCorrNeg = 0;
  5374. if (iqCorrMeas > 0x80000000) {
  5375. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  5376. iqCorrNeg = 1;
  5377. }
  5378. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5379. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  5380. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5381. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  5382. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  5383. iqCorrNeg);
  5384. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
  5385. qCoffDenom = powerMeasQ / 64;
  5386. if (powerMeasQ != 0) {
  5387. iCoff = iqCorrMeas / iCoffDenom;
  5388. qCoff = powerMeasI / qCoffDenom - 64;
  5389. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5390. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  5391. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5392. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  5393. iCoff = iCoff & 0x3f;
  5394. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5395. "New: Chn %d iCoff = 0x%08x\n", i, iCoff);
  5396. if (iqCorrNeg == 0x0)
  5397. iCoff = 0x40 - iCoff;
  5398. if (qCoff > 15)
  5399. qCoff = 15;
  5400. else if (qCoff <= -16)
  5401. qCoff = 16;
  5402. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5403. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  5404. i, iCoff, qCoff);
  5405. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  5406. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
  5407. iCoff);
  5408. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  5409. AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
  5410. qCoff);
  5411. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5412. "IQ Cal and Correction done for Chain %d\n",
  5413. i);
  5414. }
  5415. }
  5416. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  5417. AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
  5418. }
  5419. static void
  5420. ath9k_hw_adc_gaincal_calibrate(struct ath_hal *ah, u8 numChains)
  5421. {
  5422. struct ath_hal_5416 *ahp = AH5416(ah);
  5423. u32 iOddMeasOffset, iEvenMeasOffset, qOddMeasOffset,
  5424. qEvenMeasOffset;
  5425. u32 qGainMismatch, iGainMismatch, val, i;
  5426. for (i = 0; i < numChains; i++) {
  5427. iOddMeasOffset = ahp->ah_totalAdcIOddPhase[i];
  5428. iEvenMeasOffset = ahp->ah_totalAdcIEvenPhase[i];
  5429. qOddMeasOffset = ahp->ah_totalAdcQOddPhase[i];
  5430. qEvenMeasOffset = ahp->ah_totalAdcQEvenPhase[i];
  5431. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5432. "Starting ADC Gain Cal for Chain %d\n", i);
  5433. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5434. "Chn %d pwr_meas_odd_i = 0x%08x\n", i,
  5435. iOddMeasOffset);
  5436. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5437. "Chn %d pwr_meas_even_i = 0x%08x\n", i,
  5438. iEvenMeasOffset);
  5439. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5440. "Chn %d pwr_meas_odd_q = 0x%08x\n", i,
  5441. qOddMeasOffset);
  5442. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5443. "Chn %d pwr_meas_even_q = 0x%08x\n", i,
  5444. qEvenMeasOffset);
  5445. if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
  5446. iGainMismatch =
  5447. ((iEvenMeasOffset * 32) /
  5448. iOddMeasOffset) & 0x3f;
  5449. qGainMismatch =
  5450. ((qOddMeasOffset * 32) /
  5451. qEvenMeasOffset) & 0x3f;
  5452. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5453. "Chn %d gain_mismatch_i = 0x%08x\n", i,
  5454. iGainMismatch);
  5455. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5456. "Chn %d gain_mismatch_q = 0x%08x\n", i,
  5457. qGainMismatch);
  5458. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  5459. val &= 0xfffff000;
  5460. val |= (qGainMismatch) | (iGainMismatch << 6);
  5461. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  5462. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5463. "ADC Gain Cal done for Chain %d\n", i);
  5464. }
  5465. }
  5466. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  5467. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  5468. AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
  5469. }
  5470. static void
  5471. ath9k_hw_adc_dccal_calibrate(struct ath_hal *ah, u8 numChains)
  5472. {
  5473. struct ath_hal_5416 *ahp = AH5416(ah);
  5474. u32 iOddMeasOffset, iEvenMeasOffset, val, i;
  5475. int32_t qOddMeasOffset, qEvenMeasOffset, qDcMismatch, iDcMismatch;
  5476. const struct hal_percal_data *calData =
  5477. ahp->ah_cal_list_curr->calData;
  5478. u32 numSamples =
  5479. (1 << (calData->calCountMax + 5)) * calData->calNumSamples;
  5480. for (i = 0; i < numChains; i++) {
  5481. iOddMeasOffset = ahp->ah_totalAdcDcOffsetIOddPhase[i];
  5482. iEvenMeasOffset = ahp->ah_totalAdcDcOffsetIEvenPhase[i];
  5483. qOddMeasOffset = ahp->ah_totalAdcDcOffsetQOddPhase[i];
  5484. qEvenMeasOffset = ahp->ah_totalAdcDcOffsetQEvenPhase[i];
  5485. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5486. "Starting ADC DC Offset Cal for Chain %d\n", i);
  5487. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5488. "Chn %d pwr_meas_odd_i = %d\n", i,
  5489. iOddMeasOffset);
  5490. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5491. "Chn %d pwr_meas_even_i = %d\n", i,
  5492. iEvenMeasOffset);
  5493. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5494. "Chn %d pwr_meas_odd_q = %d\n", i,
  5495. qOddMeasOffset);
  5496. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5497. "Chn %d pwr_meas_even_q = %d\n", i,
  5498. qEvenMeasOffset);
  5499. iDcMismatch = (((iEvenMeasOffset - iOddMeasOffset) * 2) /
  5500. numSamples) & 0x1ff;
  5501. qDcMismatch = (((qOddMeasOffset - qEvenMeasOffset) * 2) /
  5502. numSamples) & 0x1ff;
  5503. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5504. "Chn %d dc_offset_mismatch_i = 0x%08x\n", i,
  5505. iDcMismatch);
  5506. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5507. "Chn %d dc_offset_mismatch_q = 0x%08x\n", i,
  5508. qDcMismatch);
  5509. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  5510. val &= 0xc0000fff;
  5511. val |= (qDcMismatch << 12) | (iDcMismatch << 21);
  5512. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  5513. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5514. "ADC DC Offset Cal done for Chain %d\n", i);
  5515. }
  5516. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  5517. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  5518. AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
  5519. }
  5520. bool ath9k_hw_set_txpowerlimit(struct ath_hal *ah, u32 limit)
  5521. {
  5522. struct ath_hal_5416 *ahp = AH5416(ah);
  5523. struct ath9k_channel *chan = ah->ah_curchan;
  5524. ah->ah_powerLimit = min(limit, (u32) MAX_RATE_POWER);
  5525. if (ath9k_hw_set_txpower(ah, &ahp->ah_eeprom, chan,
  5526. ath9k_regd_get_ctl(ah, chan),
  5527. ath9k_regd_get_antenna_allowed(ah,
  5528. chan),
  5529. chan->maxRegTxPower * 2,
  5530. min((u32) MAX_RATE_POWER,
  5531. (u32) ah->ah_powerLimit)) != 0)
  5532. return false;
  5533. return true;
  5534. }
  5535. void
  5536. ath9k_hw_get_channel_centers(struct ath_hal *ah,
  5537. struct ath9k_channel *chan,
  5538. struct chan_centers *centers)
  5539. {
  5540. int8_t extoff;
  5541. struct ath_hal_5416 *ahp = AH5416(ah);
  5542. if (!IS_CHAN_HT40(chan)) {
  5543. centers->ctl_center = centers->ext_center =
  5544. centers->synth_center = chan->channel;
  5545. return;
  5546. }
  5547. if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
  5548. (chan->chanmode == CHANNEL_G_HT40PLUS)) {
  5549. centers->synth_center =
  5550. chan->channel + HT40_CHANNEL_CENTER_SHIFT;
  5551. extoff = 1;
  5552. } else {
  5553. centers->synth_center =
  5554. chan->channel - HT40_CHANNEL_CENTER_SHIFT;
  5555. extoff = -1;
  5556. }
  5557. centers->ctl_center = centers->synth_center - (extoff *
  5558. HT40_CHANNEL_CENTER_SHIFT);
  5559. centers->ext_center = centers->synth_center + (extoff *
  5560. ((ahp->
  5561. ah_extprotspacing
  5562. ==
  5563. ATH9K_HT_EXTPROTSPACING_20)
  5564. ?
  5565. HT40_CHANNEL_CENTER_SHIFT
  5566. : 15));
  5567. }
  5568. void
  5569. ath9k_hw_reset_calvalid(struct ath_hal *ah, struct ath9k_channel *chan,
  5570. bool *isCalDone)
  5571. {
  5572. struct ath_hal_5416 *ahp = AH5416(ah);
  5573. struct ath9k_channel *ichan =
  5574. ath9k_regd_check_channel(ah, chan);
  5575. struct hal_cal_list *currCal = ahp->ah_cal_list_curr;
  5576. *isCalDone = true;
  5577. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  5578. return;
  5579. if (currCal == NULL)
  5580. return;
  5581. if (ichan == NULL) {
  5582. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5583. "%s: invalid channel %u/0x%x; no mapping\n",
  5584. __func__, chan->channel, chan->channelFlags);
  5585. return;
  5586. }
  5587. if (currCal->calState != CAL_DONE) {
  5588. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5589. "%s: Calibration state incorrect, %d\n",
  5590. __func__, currCal->calState);
  5591. return;
  5592. }
  5593. if (!ath9k_hw_iscal_supported(ah, chan, currCal->calData->calType))
  5594. return;
  5595. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  5596. "%s: Resetting Cal %d state for channel %u/0x%x\n",
  5597. __func__, currCal->calData->calType, chan->channel,
  5598. chan->channelFlags);
  5599. ichan->CalValid &= ~currCal->calData->calType;
  5600. currCal->calState = CAL_WAITING;
  5601. *isCalDone = false;
  5602. }
  5603. void ath9k_hw_getmac(struct ath_hal *ah, u8 *mac)
  5604. {
  5605. struct ath_hal_5416 *ahp = AH5416(ah);
  5606. memcpy(mac, ahp->ah_macaddr, ETH_ALEN);
  5607. }
  5608. bool ath9k_hw_setmac(struct ath_hal *ah, const u8 *mac)
  5609. {
  5610. struct ath_hal_5416 *ahp = AH5416(ah);
  5611. memcpy(ahp->ah_macaddr, mac, ETH_ALEN);
  5612. return true;
  5613. }
  5614. void ath9k_hw_getbssidmask(struct ath_hal *ah, u8 *mask)
  5615. {
  5616. struct ath_hal_5416 *ahp = AH5416(ah);
  5617. memcpy(mask, ahp->ah_bssidmask, ETH_ALEN);
  5618. }
  5619. bool
  5620. ath9k_hw_setbssidmask(struct ath_hal *ah, const u8 *mask)
  5621. {
  5622. struct ath_hal_5416 *ahp = AH5416(ah);
  5623. memcpy(ahp->ah_bssidmask, mask, ETH_ALEN);
  5624. REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(ahp->ah_bssidmask));
  5625. REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(ahp->ah_bssidmask + 4));
  5626. return true;
  5627. }
  5628. #ifdef CONFIG_ATH9K_RFKILL
  5629. static void ath9k_enable_rfkill(struct ath_hal *ah)
  5630. {
  5631. struct ath_hal_5416 *ahp = AH5416(ah);
  5632. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
  5633. AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
  5634. REG_CLR_BIT(ah, AR_GPIO_INPUT_MUX2,
  5635. AR_GPIO_INPUT_MUX2_RFSILENT);
  5636. ath9k_hw_cfg_gpio_input(ah, ahp->ah_gpioSelect);
  5637. REG_SET_BIT(ah, AR_PHY_TEST, RFSILENT_BB);
  5638. if (ahp->ah_gpioBit == ath9k_hw_gpio_get(ah, ahp->ah_gpioSelect)) {
  5639. ath9k_hw_set_gpio_intr(ah, ahp->ah_gpioSelect,
  5640. !ahp->ah_gpioBit);
  5641. } else {
  5642. ath9k_hw_set_gpio_intr(ah, ahp->ah_gpioSelect,
  5643. ahp->ah_gpioBit);
  5644. }
  5645. }
  5646. #endif
  5647. void
  5648. ath9k_hw_write_associd(struct ath_hal *ah, const u8 *bssid,
  5649. u16 assocId)
  5650. {
  5651. struct ath_hal_5416 *ahp = AH5416(ah);
  5652. memcpy(ahp->ah_bssid, bssid, ETH_ALEN);
  5653. ahp->ah_assocId = assocId;
  5654. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(ahp->ah_bssid));
  5655. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(ahp->ah_bssid + 4) |
  5656. ((assocId & 0x3fff) << AR_BSS_ID1_AID_S));
  5657. }
  5658. u64 ath9k_hw_gettsf64(struct ath_hal *ah)
  5659. {
  5660. u64 tsf;
  5661. tsf = REG_READ(ah, AR_TSF_U32);
  5662. tsf = (tsf << 32) | REG_READ(ah, AR_TSF_L32);
  5663. return tsf;
  5664. }
  5665. void ath9k_hw_reset_tsf(struct ath_hal *ah)
  5666. {
  5667. int count;
  5668. count = 0;
  5669. while (REG_READ(ah, AR_SLP32_MODE) & AR_SLP32_TSF_WRITE_STATUS) {
  5670. count++;
  5671. if (count > 10) {
  5672. DPRINTF(ah->ah_sc, ATH_DBG_RESET,
  5673. "%s: AR_SLP32_TSF_WRITE_STATUS limit exceeded\n",
  5674. __func__);
  5675. break;
  5676. }
  5677. udelay(10);
  5678. }
  5679. REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
  5680. }
  5681. u32 ath9k_hw_getdefantenna(struct ath_hal *ah)
  5682. {
  5683. return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
  5684. }
  5685. void ath9k_hw_setantenna(struct ath_hal *ah, u32 antenna)
  5686. {
  5687. REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
  5688. }
  5689. bool
  5690. ath9k_hw_setantennaswitch(struct ath_hal *ah,
  5691. enum ath9k_ant_setting settings,
  5692. struct ath9k_channel *chan,
  5693. u8 *tx_chainmask,
  5694. u8 *rx_chainmask,
  5695. u8 *antenna_cfgd)
  5696. {
  5697. struct ath_hal_5416 *ahp = AH5416(ah);
  5698. static u8 tx_chainmask_cfg, rx_chainmask_cfg;
  5699. if (AR_SREV_9280(ah)) {
  5700. if (!tx_chainmask_cfg) {
  5701. tx_chainmask_cfg = *tx_chainmask;
  5702. rx_chainmask_cfg = *rx_chainmask;
  5703. }
  5704. switch (settings) {
  5705. case ATH9K_ANT_FIXED_A:
  5706. *tx_chainmask = ATH9K_ANTENNA0_CHAINMASK;
  5707. *rx_chainmask = ATH9K_ANTENNA0_CHAINMASK;
  5708. *antenna_cfgd = true;
  5709. break;
  5710. case ATH9K_ANT_FIXED_B:
  5711. if (ah->ah_caps.tx_chainmask >
  5712. ATH9K_ANTENNA1_CHAINMASK) {
  5713. *tx_chainmask = ATH9K_ANTENNA1_CHAINMASK;
  5714. }
  5715. *rx_chainmask = ATH9K_ANTENNA1_CHAINMASK;
  5716. *antenna_cfgd = true;
  5717. break;
  5718. case ATH9K_ANT_VARIABLE:
  5719. *tx_chainmask = tx_chainmask_cfg;
  5720. *rx_chainmask = rx_chainmask_cfg;
  5721. *antenna_cfgd = true;
  5722. break;
  5723. default:
  5724. break;
  5725. }
  5726. } else {
  5727. ahp->ah_diversityControl = settings;
  5728. }
  5729. return true;
  5730. }
  5731. void ath9k_hw_setopmode(struct ath_hal *ah)
  5732. {
  5733. ath9k_hw_set_operating_mode(ah, ah->ah_opmode);
  5734. }
  5735. bool
  5736. ath9k_hw_getcapability(struct ath_hal *ah, enum ath9k_capability_type type,
  5737. u32 capability, u32 *result)
  5738. {
  5739. struct ath_hal_5416 *ahp = AH5416(ah);
  5740. const struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5741. switch (type) {
  5742. case ATH9K_CAP_CIPHER:
  5743. switch (capability) {
  5744. case ATH9K_CIPHER_AES_CCM:
  5745. case ATH9K_CIPHER_AES_OCB:
  5746. case ATH9K_CIPHER_TKIP:
  5747. case ATH9K_CIPHER_WEP:
  5748. case ATH9K_CIPHER_MIC:
  5749. case ATH9K_CIPHER_CLR:
  5750. return true;
  5751. default:
  5752. return false;
  5753. }
  5754. case ATH9K_CAP_TKIP_MIC:
  5755. switch (capability) {
  5756. case 0:
  5757. return true;
  5758. case 1:
  5759. return (ahp->ah_staId1Defaults &
  5760. AR_STA_ID1_CRPT_MIC_ENABLE) ? true :
  5761. false;
  5762. }
  5763. case ATH9K_CAP_TKIP_SPLIT:
  5764. return (ahp->ah_miscMode & AR_PCU_MIC_NEW_LOC_ENA) ?
  5765. false : true;
  5766. case ATH9K_CAP_WME_TKIPMIC:
  5767. return 0;
  5768. case ATH9K_CAP_PHYCOUNTERS:
  5769. return ahp->ah_hasHwPhyCounters ? 0 : -ENXIO;
  5770. case ATH9K_CAP_DIVERSITY:
  5771. return (REG_READ(ah, AR_PHY_CCK_DETECT) &
  5772. AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV) ?
  5773. true : false;
  5774. case ATH9K_CAP_PHYDIAG:
  5775. return true;
  5776. case ATH9K_CAP_MCAST_KEYSRCH:
  5777. switch (capability) {
  5778. case 0:
  5779. return true;
  5780. case 1:
  5781. if (REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_ADHOC) {
  5782. return false;
  5783. } else {
  5784. return (ahp->ah_staId1Defaults &
  5785. AR_STA_ID1_MCAST_KSRCH) ? true :
  5786. false;
  5787. }
  5788. }
  5789. return false;
  5790. case ATH9K_CAP_TSF_ADJUST:
  5791. return (ahp->ah_miscMode & AR_PCU_TX_ADD_TSF) ?
  5792. true : false;
  5793. case ATH9K_CAP_RFSILENT:
  5794. if (capability == 3)
  5795. return false;
  5796. case ATH9K_CAP_ANT_CFG_2GHZ:
  5797. *result = pCap->num_antcfg_2ghz;
  5798. return true;
  5799. case ATH9K_CAP_ANT_CFG_5GHZ:
  5800. *result = pCap->num_antcfg_5ghz;
  5801. return true;
  5802. case ATH9K_CAP_TXPOW:
  5803. switch (capability) {
  5804. case 0:
  5805. return 0;
  5806. case 1:
  5807. *result = ah->ah_powerLimit;
  5808. return 0;
  5809. case 2:
  5810. *result = ah->ah_maxPowerLevel;
  5811. return 0;
  5812. case 3:
  5813. *result = ah->ah_tpScale;
  5814. return 0;
  5815. }
  5816. return false;
  5817. default:
  5818. return false;
  5819. }
  5820. }
  5821. int
  5822. ath9k_hw_select_antconfig(struct ath_hal *ah, u32 cfg)
  5823. {
  5824. struct ath_hal_5416 *ahp = AH5416(ah);
  5825. struct ath9k_channel *chan = ah->ah_curchan;
  5826. const struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5827. u16 ant_config;
  5828. u32 halNumAntConfig;
  5829. halNumAntConfig =
  5830. IS_CHAN_2GHZ(chan) ? pCap->num_antcfg_2ghz : pCap->
  5831. num_antcfg_5ghz;
  5832. if (cfg < halNumAntConfig) {
  5833. if (!ath9k_hw_get_eeprom_antenna_cfg(ahp, chan,
  5834. cfg, &ant_config)) {
  5835. REG_WRITE(ah, AR_PHY_SWITCH_COM, ant_config);
  5836. return 0;
  5837. }
  5838. }
  5839. return -EINVAL;
  5840. }
  5841. bool ath9k_hw_intrpend(struct ath_hal *ah)
  5842. {
  5843. u32 host_isr;
  5844. if (AR_SREV_9100(ah))
  5845. return true;
  5846. host_isr = REG_READ(ah, AR_INTR_ASYNC_CAUSE);
  5847. if ((host_isr & AR_INTR_MAC_IRQ) && (host_isr != AR_INTR_SPURIOUS))
  5848. return true;
  5849. host_isr = REG_READ(ah, AR_INTR_SYNC_CAUSE);
  5850. if ((host_isr & AR_INTR_SYNC_DEFAULT)
  5851. && (host_isr != AR_INTR_SPURIOUS))
  5852. return true;
  5853. return false;
  5854. }
  5855. bool ath9k_hw_getisr(struct ath_hal *ah, enum ath9k_int *masked)
  5856. {
  5857. u32 isr = 0;
  5858. u32 mask2 = 0;
  5859. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5860. u32 sync_cause = 0;
  5861. bool fatal_int = false;
  5862. if (!AR_SREV_9100(ah)) {
  5863. if (REG_READ(ah, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) {
  5864. if ((REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M)
  5865. == AR_RTC_STATUS_ON) {
  5866. isr = REG_READ(ah, AR_ISR);
  5867. }
  5868. }
  5869. sync_cause =
  5870. REG_READ(ah,
  5871. AR_INTR_SYNC_CAUSE) & AR_INTR_SYNC_DEFAULT;
  5872. *masked = 0;
  5873. if (!isr && !sync_cause)
  5874. return false;
  5875. } else {
  5876. *masked = 0;
  5877. isr = REG_READ(ah, AR_ISR);
  5878. }
  5879. if (isr) {
  5880. struct ath_hal_5416 *ahp = AH5416(ah);
  5881. if (isr & AR_ISR_BCNMISC) {
  5882. u32 isr2;
  5883. isr2 = REG_READ(ah, AR_ISR_S2);
  5884. if (isr2 & AR_ISR_S2_TIM)
  5885. mask2 |= ATH9K_INT_TIM;
  5886. if (isr2 & AR_ISR_S2_DTIM)
  5887. mask2 |= ATH9K_INT_DTIM;
  5888. if (isr2 & AR_ISR_S2_DTIMSYNC)
  5889. mask2 |= ATH9K_INT_DTIMSYNC;
  5890. if (isr2 & (AR_ISR_S2_CABEND))
  5891. mask2 |= ATH9K_INT_CABEND;
  5892. if (isr2 & AR_ISR_S2_GTT)
  5893. mask2 |= ATH9K_INT_GTT;
  5894. if (isr2 & AR_ISR_S2_CST)
  5895. mask2 |= ATH9K_INT_CST;
  5896. }
  5897. isr = REG_READ(ah, AR_ISR_RAC);
  5898. if (isr == 0xffffffff) {
  5899. *masked = 0;
  5900. return false;
  5901. }
  5902. *masked = isr & ATH9K_INT_COMMON;
  5903. if (ahp->ah_intrMitigation) {
  5904. if (isr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
  5905. *masked |= ATH9K_INT_RX;
  5906. }
  5907. if (isr & (AR_ISR_RXOK | AR_ISR_RXERR))
  5908. *masked |= ATH9K_INT_RX;
  5909. if (isr &
  5910. (AR_ISR_TXOK | AR_ISR_TXDESC | AR_ISR_TXERR |
  5911. AR_ISR_TXEOL)) {
  5912. u32 s0_s, s1_s;
  5913. *masked |= ATH9K_INT_TX;
  5914. s0_s = REG_READ(ah, AR_ISR_S0_S);
  5915. ahp->ah_intrTxqs |= MS(s0_s, AR_ISR_S0_QCU_TXOK);
  5916. ahp->ah_intrTxqs |= MS(s0_s, AR_ISR_S0_QCU_TXDESC);
  5917. s1_s = REG_READ(ah, AR_ISR_S1_S);
  5918. ahp->ah_intrTxqs |= MS(s1_s, AR_ISR_S1_QCU_TXERR);
  5919. ahp->ah_intrTxqs |= MS(s1_s, AR_ISR_S1_QCU_TXEOL);
  5920. }
  5921. if (isr & AR_ISR_RXORN) {
  5922. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  5923. "%s: receive FIFO overrun interrupt\n",
  5924. __func__);
  5925. }
  5926. if (!AR_SREV_9100(ah)) {
  5927. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  5928. u32 isr5 = REG_READ(ah, AR_ISR_S5_S);
  5929. if (isr5 & AR_ISR_S5_TIM_TIMER)
  5930. *masked |= ATH9K_INT_TIM_TIMER;
  5931. }
  5932. }
  5933. *masked |= mask2;
  5934. }
  5935. if (AR_SREV_9100(ah))
  5936. return true;
  5937. if (sync_cause) {
  5938. fatal_int =
  5939. (sync_cause &
  5940. (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR))
  5941. ? true : false;
  5942. if (fatal_int) {
  5943. if (sync_cause & AR_INTR_SYNC_HOST1_FATAL) {
  5944. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  5945. "%s: received PCI FATAL interrupt\n",
  5946. __func__);
  5947. }
  5948. if (sync_cause & AR_INTR_SYNC_HOST1_PERR) {
  5949. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  5950. "%s: received PCI PERR interrupt\n",
  5951. __func__);
  5952. }
  5953. }
  5954. if (sync_cause & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
  5955. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  5956. "%s: AR_INTR_SYNC_RADM_CPL_TIMEOUT\n",
  5957. __func__);
  5958. REG_WRITE(ah, AR_RC, AR_RC_HOSTIF);
  5959. REG_WRITE(ah, AR_RC, 0);
  5960. *masked |= ATH9K_INT_FATAL;
  5961. }
  5962. if (sync_cause & AR_INTR_SYNC_LOCAL_TIMEOUT) {
  5963. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  5964. "%s: AR_INTR_SYNC_LOCAL_TIMEOUT\n",
  5965. __func__);
  5966. }
  5967. REG_WRITE(ah, AR_INTR_SYNC_CAUSE_CLR, sync_cause);
  5968. (void) REG_READ(ah, AR_INTR_SYNC_CAUSE_CLR);
  5969. }
  5970. return true;
  5971. }
  5972. enum ath9k_int ath9k_hw_intrget(struct ath_hal *ah)
  5973. {
  5974. return AH5416(ah)->ah_maskReg;
  5975. }
  5976. enum ath9k_int ath9k_hw_set_interrupts(struct ath_hal *ah, enum ath9k_int ints)
  5977. {
  5978. struct ath_hal_5416 *ahp = AH5416(ah);
  5979. u32 omask = ahp->ah_maskReg;
  5980. u32 mask, mask2;
  5981. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  5982. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: 0x%x => 0x%x\n", __func__,
  5983. omask, ints);
  5984. if (omask & ATH9K_INT_GLOBAL) {
  5985. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: disable IER\n",
  5986. __func__);
  5987. REG_WRITE(ah, AR_IER, AR_IER_DISABLE);
  5988. (void) REG_READ(ah, AR_IER);
  5989. if (!AR_SREV_9100(ah)) {
  5990. REG_WRITE(ah, AR_INTR_ASYNC_ENABLE, 0);
  5991. (void) REG_READ(ah, AR_INTR_ASYNC_ENABLE);
  5992. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
  5993. (void) REG_READ(ah, AR_INTR_SYNC_ENABLE);
  5994. }
  5995. }
  5996. mask = ints & ATH9K_INT_COMMON;
  5997. mask2 = 0;
  5998. if (ints & ATH9K_INT_TX) {
  5999. if (ahp->ah_txOkInterruptMask)
  6000. mask |= AR_IMR_TXOK;
  6001. if (ahp->ah_txDescInterruptMask)
  6002. mask |= AR_IMR_TXDESC;
  6003. if (ahp->ah_txErrInterruptMask)
  6004. mask |= AR_IMR_TXERR;
  6005. if (ahp->ah_txEolInterruptMask)
  6006. mask |= AR_IMR_TXEOL;
  6007. }
  6008. if (ints & ATH9K_INT_RX) {
  6009. mask |= AR_IMR_RXERR;
  6010. if (ahp->ah_intrMitigation)
  6011. mask |= AR_IMR_RXMINTR | AR_IMR_RXINTM;
  6012. else
  6013. mask |= AR_IMR_RXOK | AR_IMR_RXDESC;
  6014. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  6015. mask |= AR_IMR_GENTMR;
  6016. }
  6017. if (ints & (ATH9K_INT_BMISC)) {
  6018. mask |= AR_IMR_BCNMISC;
  6019. if (ints & ATH9K_INT_TIM)
  6020. mask2 |= AR_IMR_S2_TIM;
  6021. if (ints & ATH9K_INT_DTIM)
  6022. mask2 |= AR_IMR_S2_DTIM;
  6023. if (ints & ATH9K_INT_DTIMSYNC)
  6024. mask2 |= AR_IMR_S2_DTIMSYNC;
  6025. if (ints & ATH9K_INT_CABEND)
  6026. mask2 |= (AR_IMR_S2_CABEND);
  6027. }
  6028. if (ints & (ATH9K_INT_GTT | ATH9K_INT_CST)) {
  6029. mask |= AR_IMR_BCNMISC;
  6030. if (ints & ATH9K_INT_GTT)
  6031. mask2 |= AR_IMR_S2_GTT;
  6032. if (ints & ATH9K_INT_CST)
  6033. mask2 |= AR_IMR_S2_CST;
  6034. }
  6035. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: new IMR 0x%x\n", __func__,
  6036. mask);
  6037. REG_WRITE(ah, AR_IMR, mask);
  6038. mask = REG_READ(ah, AR_IMR_S2) & ~(AR_IMR_S2_TIM |
  6039. AR_IMR_S2_DTIM |
  6040. AR_IMR_S2_DTIMSYNC |
  6041. AR_IMR_S2_CABEND |
  6042. AR_IMR_S2_CABTO |
  6043. AR_IMR_S2_TSFOOR |
  6044. AR_IMR_S2_GTT | AR_IMR_S2_CST);
  6045. REG_WRITE(ah, AR_IMR_S2, mask | mask2);
  6046. ahp->ah_maskReg = ints;
  6047. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  6048. if (ints & ATH9K_INT_TIM_TIMER)
  6049. REG_SET_BIT(ah, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
  6050. else
  6051. REG_CLR_BIT(ah, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
  6052. }
  6053. if (ints & ATH9K_INT_GLOBAL) {
  6054. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "%s: enable IER\n",
  6055. __func__);
  6056. REG_WRITE(ah, AR_IER, AR_IER_ENABLE);
  6057. if (!AR_SREV_9100(ah)) {
  6058. REG_WRITE(ah, AR_INTR_ASYNC_ENABLE,
  6059. AR_INTR_MAC_IRQ);
  6060. REG_WRITE(ah, AR_INTR_ASYNC_MASK, AR_INTR_MAC_IRQ);
  6061. REG_WRITE(ah, AR_INTR_SYNC_ENABLE,
  6062. AR_INTR_SYNC_DEFAULT);
  6063. REG_WRITE(ah, AR_INTR_SYNC_MASK,
  6064. AR_INTR_SYNC_DEFAULT);
  6065. }
  6066. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT, "AR_IMR 0x%x IER 0x%x\n",
  6067. REG_READ(ah, AR_IMR), REG_READ(ah, AR_IER));
  6068. }
  6069. return omask;
  6070. }
  6071. void
  6072. ath9k_hw_beaconinit(struct ath_hal *ah,
  6073. u32 next_beacon, u32 beacon_period)
  6074. {
  6075. struct ath_hal_5416 *ahp = AH5416(ah);
  6076. int flags = 0;
  6077. ahp->ah_beaconInterval = beacon_period;
  6078. switch (ah->ah_opmode) {
  6079. case ATH9K_M_STA:
  6080. case ATH9K_M_MONITOR:
  6081. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
  6082. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, 0xffff);
  6083. REG_WRITE(ah, AR_NEXT_SWBA, 0x7ffff);
  6084. flags |= AR_TBTT_TIMER_EN;
  6085. break;
  6086. case ATH9K_M_IBSS:
  6087. REG_SET_BIT(ah, AR_TXCFG,
  6088. AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
  6089. REG_WRITE(ah, AR_NEXT_NDP_TIMER,
  6090. TU_TO_USEC(next_beacon +
  6091. (ahp->ah_atimWindow ? ahp->
  6092. ah_atimWindow : 1)));
  6093. flags |= AR_NDP_TIMER_EN;
  6094. case ATH9K_M_HOSTAP:
  6095. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
  6096. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT,
  6097. TU_TO_USEC(next_beacon -
  6098. ah->ah_config.
  6099. dma_beacon_response_time));
  6100. REG_WRITE(ah, AR_NEXT_SWBA,
  6101. TU_TO_USEC(next_beacon -
  6102. ah->ah_config.
  6103. sw_beacon_response_time));
  6104. flags |=
  6105. AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
  6106. break;
  6107. }
  6108. REG_WRITE(ah, AR_BEACON_PERIOD, TU_TO_USEC(beacon_period));
  6109. REG_WRITE(ah, AR_DMA_BEACON_PERIOD, TU_TO_USEC(beacon_period));
  6110. REG_WRITE(ah, AR_SWBA_PERIOD, TU_TO_USEC(beacon_period));
  6111. REG_WRITE(ah, AR_NDP_PERIOD, TU_TO_USEC(beacon_period));
  6112. beacon_period &= ~ATH9K_BEACON_ENA;
  6113. if (beacon_period & ATH9K_BEACON_RESET_TSF) {
  6114. beacon_period &= ~ATH9K_BEACON_RESET_TSF;
  6115. ath9k_hw_reset_tsf(ah);
  6116. }
  6117. REG_SET_BIT(ah, AR_TIMER_MODE, flags);
  6118. }
  6119. void
  6120. ath9k_hw_set_sta_beacon_timers(struct ath_hal *ah,
  6121. const struct ath9k_beacon_state *bs)
  6122. {
  6123. u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
  6124. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6125. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
  6126. REG_WRITE(ah, AR_BEACON_PERIOD,
  6127. TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
  6128. REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
  6129. TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
  6130. REG_RMW_FIELD(ah, AR_RSSI_THR,
  6131. AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
  6132. beaconintval = bs->bs_intval & ATH9K_BEACON_PERIOD;
  6133. if (bs->bs_sleepduration > beaconintval)
  6134. beaconintval = bs->bs_sleepduration;
  6135. dtimperiod = bs->bs_dtimperiod;
  6136. if (bs->bs_sleepduration > dtimperiod)
  6137. dtimperiod = bs->bs_sleepduration;
  6138. if (beaconintval == dtimperiod)
  6139. nextTbtt = bs->bs_nextdtim;
  6140. else
  6141. nextTbtt = bs->bs_nexttbtt;
  6142. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: next DTIM %d\n", __func__,
  6143. bs->bs_nextdtim);
  6144. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: next beacon %d\n", __func__,
  6145. nextTbtt);
  6146. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: beacon period %d\n", __func__,
  6147. beaconintval);
  6148. DPRINTF(ah->ah_sc, ATH_DBG_BEACON, "%s: DTIM period %d\n", __func__,
  6149. dtimperiod);
  6150. REG_WRITE(ah, AR_NEXT_DTIM,
  6151. TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
  6152. REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
  6153. REG_WRITE(ah, AR_SLEEP1,
  6154. SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
  6155. | AR_SLEEP1_ASSUME_DTIM);
  6156. if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
  6157. beacontimeout = (BEACON_TIMEOUT_VAL << 3);
  6158. else
  6159. beacontimeout = MIN_BEACON_TIMEOUT_VAL;
  6160. REG_WRITE(ah, AR_SLEEP2,
  6161. SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
  6162. REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
  6163. REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
  6164. REG_SET_BIT(ah, AR_TIMER_MODE,
  6165. AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
  6166. AR_DTIM_TIMER_EN);
  6167. }
  6168. bool ath9k_hw_keyisvalid(struct ath_hal *ah, u16 entry)
  6169. {
  6170. if (entry < ah->ah_caps.keycache_size) {
  6171. u32 val = REG_READ(ah, AR_KEYTABLE_MAC1(entry));
  6172. if (val & AR_KEYTABLE_VALID)
  6173. return true;
  6174. }
  6175. return false;
  6176. }
  6177. bool ath9k_hw_keyreset(struct ath_hal *ah, u16 entry)
  6178. {
  6179. u32 keyType;
  6180. if (entry >= ah->ah_caps.keycache_size) {
  6181. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6182. "%s: entry %u out of range\n", __func__, entry);
  6183. return false;
  6184. }
  6185. keyType = REG_READ(ah, AR_KEYTABLE_TYPE(entry));
  6186. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
  6187. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
  6188. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
  6189. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
  6190. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
  6191. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
  6192. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
  6193. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
  6194. if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
  6195. u16 micentry = entry + 64;
  6196. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
  6197. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  6198. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
  6199. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  6200. }
  6201. if (ah->ah_curchan == NULL)
  6202. return true;
  6203. return true;
  6204. }
  6205. bool
  6206. ath9k_hw_keysetmac(struct ath_hal *ah, u16 entry,
  6207. const u8 *mac)
  6208. {
  6209. u32 macHi, macLo;
  6210. if (entry >= ah->ah_caps.keycache_size) {
  6211. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6212. "%s: entry %u out of range\n", __func__, entry);
  6213. return false;
  6214. }
  6215. if (mac != NULL) {
  6216. macHi = (mac[5] << 8) | mac[4];
  6217. macLo = (mac[3] << 24) | (mac[2] << 16)
  6218. | (mac[1] << 8) | mac[0];
  6219. macLo >>= 1;
  6220. macLo |= (macHi & 1) << 31;
  6221. macHi >>= 1;
  6222. } else {
  6223. macLo = macHi = 0;
  6224. }
  6225. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
  6226. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | AR_KEYTABLE_VALID);
  6227. return true;
  6228. }
  6229. bool
  6230. ath9k_hw_set_keycache_entry(struct ath_hal *ah, u16 entry,
  6231. const struct ath9k_keyval *k,
  6232. const u8 *mac, int xorKey)
  6233. {
  6234. const struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6235. u32 key0, key1, key2, key3, key4;
  6236. u32 keyType;
  6237. u32 xorMask = xorKey ?
  6238. (ATH9K_KEY_XOR << 24 | ATH9K_KEY_XOR << 16 | ATH9K_KEY_XOR << 8
  6239. | ATH9K_KEY_XOR) : 0;
  6240. struct ath_hal_5416 *ahp = AH5416(ah);
  6241. if (entry >= pCap->keycache_size) {
  6242. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6243. "%s: entry %u out of range\n", __func__, entry);
  6244. return false;
  6245. }
  6246. switch (k->kv_type) {
  6247. case ATH9K_CIPHER_AES_OCB:
  6248. keyType = AR_KEYTABLE_TYPE_AES;
  6249. break;
  6250. case ATH9K_CIPHER_AES_CCM:
  6251. if (!(pCap->hw_caps & ATH9K_HW_CAP_CIPHER_AESCCM)) {
  6252. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6253. "%s: AES-CCM not supported by "
  6254. "mac rev 0x%x\n", __func__,
  6255. ah->ah_macRev);
  6256. return false;
  6257. }
  6258. keyType = AR_KEYTABLE_TYPE_CCM;
  6259. break;
  6260. case ATH9K_CIPHER_TKIP:
  6261. keyType = AR_KEYTABLE_TYPE_TKIP;
  6262. if (ATH9K_IS_MIC_ENABLED(ah)
  6263. && entry + 64 >= pCap->keycache_size) {
  6264. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6265. "%s: entry %u inappropriate for TKIP\n",
  6266. __func__, entry);
  6267. return false;
  6268. }
  6269. break;
  6270. case ATH9K_CIPHER_WEP:
  6271. if (k->kv_len < 40 / NBBY) {
  6272. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6273. "%s: WEP key length %u too small\n",
  6274. __func__, k->kv_len);
  6275. return false;
  6276. }
  6277. if (k->kv_len <= 40 / NBBY)
  6278. keyType = AR_KEYTABLE_TYPE_40;
  6279. else if (k->kv_len <= 104 / NBBY)
  6280. keyType = AR_KEYTABLE_TYPE_104;
  6281. else
  6282. keyType = AR_KEYTABLE_TYPE_128;
  6283. break;
  6284. case ATH9K_CIPHER_CLR:
  6285. keyType = AR_KEYTABLE_TYPE_CLR;
  6286. break;
  6287. default:
  6288. DPRINTF(ah->ah_sc, ATH_DBG_KEYCACHE,
  6289. "%s: cipher %u not supported\n", __func__,
  6290. k->kv_type);
  6291. return false;
  6292. }
  6293. key0 = get_unaligned_le32(k->kv_val + 0) ^ xorMask;
  6294. key1 = (get_unaligned_le16(k->kv_val + 4) ^ xorMask) & 0xffff;
  6295. key2 = get_unaligned_le32(k->kv_val + 6) ^ xorMask;
  6296. key3 = (get_unaligned_le16(k->kv_val + 10) ^ xorMask) & 0xffff;
  6297. key4 = get_unaligned_le32(k->kv_val + 12) ^ xorMask;
  6298. if (k->kv_len <= 104 / NBBY)
  6299. key4 &= 0xff;
  6300. if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
  6301. u16 micentry = entry + 64;
  6302. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
  6303. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
  6304. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  6305. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  6306. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  6307. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  6308. (void) ath9k_hw_keysetmac(ah, entry, mac);
  6309. if (ahp->ah_miscMode & AR_PCU_MIC_NEW_LOC_ENA) {
  6310. u32 mic0, mic1, mic2, mic3, mic4;
  6311. mic0 = get_unaligned_le32(k->kv_mic + 0);
  6312. mic2 = get_unaligned_le32(k->kv_mic + 4);
  6313. mic1 = get_unaligned_le16(k->kv_txmic + 2) & 0xffff;
  6314. mic3 = get_unaligned_le16(k->kv_txmic + 0) & 0xffff;
  6315. mic4 = get_unaligned_le32(k->kv_txmic + 4);
  6316. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  6317. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
  6318. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  6319. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
  6320. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
  6321. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  6322. AR_KEYTABLE_TYPE_CLR);
  6323. } else {
  6324. u32 mic0, mic2;
  6325. mic0 = get_unaligned_le32(k->kv_mic + 0);
  6326. mic2 = get_unaligned_le32(k->kv_mic + 4);
  6327. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  6328. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  6329. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  6330. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  6331. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  6332. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  6333. AR_KEYTABLE_TYPE_CLR);
  6334. }
  6335. REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
  6336. REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
  6337. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  6338. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  6339. } else {
  6340. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  6341. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  6342. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  6343. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  6344. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  6345. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  6346. (void) ath9k_hw_keysetmac(ah, entry, mac);
  6347. }
  6348. if (ah->ah_curchan == NULL)
  6349. return true;
  6350. return true;
  6351. }
  6352. bool
  6353. ath9k_hw_updatetxtriglevel(struct ath_hal *ah, bool bIncTrigLevel)
  6354. {
  6355. struct ath_hal_5416 *ahp = AH5416(ah);
  6356. u32 txcfg, curLevel, newLevel;
  6357. enum ath9k_int omask;
  6358. if (ah->ah_txTrigLevel >= MAX_TX_FIFO_THRESHOLD)
  6359. return false;
  6360. omask = ath9k_hw_set_interrupts(ah,
  6361. ahp->ah_maskReg & ~ATH9K_INT_GLOBAL);
  6362. txcfg = REG_READ(ah, AR_TXCFG);
  6363. curLevel = MS(txcfg, AR_FTRIG);
  6364. newLevel = curLevel;
  6365. if (bIncTrigLevel) {
  6366. if (curLevel < MAX_TX_FIFO_THRESHOLD)
  6367. newLevel++;
  6368. } else if (curLevel > MIN_TX_FIFO_THRESHOLD)
  6369. newLevel--;
  6370. if (newLevel != curLevel)
  6371. REG_WRITE(ah, AR_TXCFG,
  6372. (txcfg & ~AR_FTRIG) | SM(newLevel, AR_FTRIG));
  6373. ath9k_hw_set_interrupts(ah, omask);
  6374. ah->ah_txTrigLevel = newLevel;
  6375. return newLevel != curLevel;
  6376. }
  6377. bool ath9k_hw_set_txq_props(struct ath_hal *ah, int q,
  6378. const struct ath9k_tx_queue_info *qinfo)
  6379. {
  6380. u32 cw;
  6381. struct ath_hal_5416 *ahp = AH5416(ah);
  6382. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6383. struct ath9k_tx_queue_info *qi;
  6384. if (q >= pCap->total_queues) {
  6385. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6386. __func__, q);
  6387. return false;
  6388. }
  6389. qi = &ahp->ah_txq[q];
  6390. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6391. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue\n",
  6392. __func__);
  6393. return false;
  6394. }
  6395. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: queue %p\n", __func__, qi);
  6396. qi->tqi_ver = qinfo->tqi_ver;
  6397. qi->tqi_subtype = qinfo->tqi_subtype;
  6398. qi->tqi_qflags = qinfo->tqi_qflags;
  6399. qi->tqi_priority = qinfo->tqi_priority;
  6400. if (qinfo->tqi_aifs != ATH9K_TXQ_USEDEFAULT)
  6401. qi->tqi_aifs = min(qinfo->tqi_aifs, 255U);
  6402. else
  6403. qi->tqi_aifs = INIT_AIFS;
  6404. if (qinfo->tqi_cwmin != ATH9K_TXQ_USEDEFAULT) {
  6405. cw = min(qinfo->tqi_cwmin, 1024U);
  6406. qi->tqi_cwmin = 1;
  6407. while (qi->tqi_cwmin < cw)
  6408. qi->tqi_cwmin = (qi->tqi_cwmin << 1) | 1;
  6409. } else
  6410. qi->tqi_cwmin = qinfo->tqi_cwmin;
  6411. if (qinfo->tqi_cwmax != ATH9K_TXQ_USEDEFAULT) {
  6412. cw = min(qinfo->tqi_cwmax, 1024U);
  6413. qi->tqi_cwmax = 1;
  6414. while (qi->tqi_cwmax < cw)
  6415. qi->tqi_cwmax = (qi->tqi_cwmax << 1) | 1;
  6416. } else
  6417. qi->tqi_cwmax = INIT_CWMAX;
  6418. if (qinfo->tqi_shretry != 0)
  6419. qi->tqi_shretry = min((u32) qinfo->tqi_shretry, 15U);
  6420. else
  6421. qi->tqi_shretry = INIT_SH_RETRY;
  6422. if (qinfo->tqi_lgretry != 0)
  6423. qi->tqi_lgretry = min((u32) qinfo->tqi_lgretry, 15U);
  6424. else
  6425. qi->tqi_lgretry = INIT_LG_RETRY;
  6426. qi->tqi_cbrPeriod = qinfo->tqi_cbrPeriod;
  6427. qi->tqi_cbrOverflowLimit = qinfo->tqi_cbrOverflowLimit;
  6428. qi->tqi_burstTime = qinfo->tqi_burstTime;
  6429. qi->tqi_readyTime = qinfo->tqi_readyTime;
  6430. switch (qinfo->tqi_subtype) {
  6431. case ATH9K_WME_UPSD:
  6432. if (qi->tqi_type == ATH9K_TX_QUEUE_DATA)
  6433. qi->tqi_intFlags = ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS;
  6434. break;
  6435. default:
  6436. break;
  6437. }
  6438. return true;
  6439. }
  6440. bool ath9k_hw_get_txq_props(struct ath_hal *ah, int q,
  6441. struct ath9k_tx_queue_info *qinfo)
  6442. {
  6443. struct ath_hal_5416 *ahp = AH5416(ah);
  6444. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6445. struct ath9k_tx_queue_info *qi;
  6446. if (q >= pCap->total_queues) {
  6447. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6448. __func__, q);
  6449. return false;
  6450. }
  6451. qi = &ahp->ah_txq[q];
  6452. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6453. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue\n",
  6454. __func__);
  6455. return false;
  6456. }
  6457. qinfo->tqi_qflags = qi->tqi_qflags;
  6458. qinfo->tqi_ver = qi->tqi_ver;
  6459. qinfo->tqi_subtype = qi->tqi_subtype;
  6460. qinfo->tqi_qflags = qi->tqi_qflags;
  6461. qinfo->tqi_priority = qi->tqi_priority;
  6462. qinfo->tqi_aifs = qi->tqi_aifs;
  6463. qinfo->tqi_cwmin = qi->tqi_cwmin;
  6464. qinfo->tqi_cwmax = qi->tqi_cwmax;
  6465. qinfo->tqi_shretry = qi->tqi_shretry;
  6466. qinfo->tqi_lgretry = qi->tqi_lgretry;
  6467. qinfo->tqi_cbrPeriod = qi->tqi_cbrPeriod;
  6468. qinfo->tqi_cbrOverflowLimit = qi->tqi_cbrOverflowLimit;
  6469. qinfo->tqi_burstTime = qi->tqi_burstTime;
  6470. qinfo->tqi_readyTime = qi->tqi_readyTime;
  6471. return true;
  6472. }
  6473. int
  6474. ath9k_hw_setuptxqueue(struct ath_hal *ah, enum ath9k_tx_queue type,
  6475. const struct ath9k_tx_queue_info *qinfo)
  6476. {
  6477. struct ath_hal_5416 *ahp = AH5416(ah);
  6478. struct ath9k_tx_queue_info *qi;
  6479. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6480. int q;
  6481. switch (type) {
  6482. case ATH9K_TX_QUEUE_BEACON:
  6483. q = pCap->total_queues - 1;
  6484. break;
  6485. case ATH9K_TX_QUEUE_CAB:
  6486. q = pCap->total_queues - 2;
  6487. break;
  6488. case ATH9K_TX_QUEUE_PSPOLL:
  6489. q = 1;
  6490. break;
  6491. case ATH9K_TX_QUEUE_UAPSD:
  6492. q = pCap->total_queues - 3;
  6493. break;
  6494. case ATH9K_TX_QUEUE_DATA:
  6495. for (q = 0; q < pCap->total_queues; q++)
  6496. if (ahp->ah_txq[q].tqi_type ==
  6497. ATH9K_TX_QUEUE_INACTIVE)
  6498. break;
  6499. if (q == pCap->total_queues) {
  6500. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  6501. "%s: no available tx queue\n", __func__);
  6502. return -1;
  6503. }
  6504. break;
  6505. default:
  6506. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: bad tx queue type %u\n",
  6507. __func__, type);
  6508. return -1;
  6509. }
  6510. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: queue %u\n", __func__, q);
  6511. qi = &ahp->ah_txq[q];
  6512. if (qi->tqi_type != ATH9K_TX_QUEUE_INACTIVE) {
  6513. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  6514. "%s: tx queue %u already active\n", __func__, q);
  6515. return -1;
  6516. }
  6517. memset(qi, 0, sizeof(struct ath9k_tx_queue_info));
  6518. qi->tqi_type = type;
  6519. if (qinfo == NULL) {
  6520. qi->tqi_qflags =
  6521. TXQ_FLAG_TXOKINT_ENABLE
  6522. | TXQ_FLAG_TXERRINT_ENABLE
  6523. | TXQ_FLAG_TXDESCINT_ENABLE | TXQ_FLAG_TXURNINT_ENABLE;
  6524. qi->tqi_aifs = INIT_AIFS;
  6525. qi->tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  6526. qi->tqi_cwmax = INIT_CWMAX;
  6527. qi->tqi_shretry = INIT_SH_RETRY;
  6528. qi->tqi_lgretry = INIT_LG_RETRY;
  6529. qi->tqi_physCompBuf = 0;
  6530. } else {
  6531. qi->tqi_physCompBuf = qinfo->tqi_physCompBuf;
  6532. (void) ath9k_hw_set_txq_props(ah, q, qinfo);
  6533. }
  6534. return q;
  6535. }
  6536. static void
  6537. ath9k_hw_set_txq_interrupts(struct ath_hal *ah,
  6538. struct ath9k_tx_queue_info *qi)
  6539. {
  6540. struct ath_hal_5416 *ahp = AH5416(ah);
  6541. DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
  6542. "%s: tx ok 0x%x err 0x%x desc 0x%x eol 0x%x urn 0x%x\n",
  6543. __func__, ahp->ah_txOkInterruptMask,
  6544. ahp->ah_txErrInterruptMask, ahp->ah_txDescInterruptMask,
  6545. ahp->ah_txEolInterruptMask, ahp->ah_txUrnInterruptMask);
  6546. REG_WRITE(ah, AR_IMR_S0,
  6547. SM(ahp->ah_txOkInterruptMask, AR_IMR_S0_QCU_TXOK)
  6548. | SM(ahp->ah_txDescInterruptMask, AR_IMR_S0_QCU_TXDESC));
  6549. REG_WRITE(ah, AR_IMR_S1,
  6550. SM(ahp->ah_txErrInterruptMask, AR_IMR_S1_QCU_TXERR)
  6551. | SM(ahp->ah_txEolInterruptMask, AR_IMR_S1_QCU_TXEOL));
  6552. REG_RMW_FIELD(ah, AR_IMR_S2,
  6553. AR_IMR_S2_QCU_TXURN, ahp->ah_txUrnInterruptMask);
  6554. }
  6555. bool ath9k_hw_releasetxqueue(struct ath_hal *ah, u32 q)
  6556. {
  6557. struct ath_hal_5416 *ahp = AH5416(ah);
  6558. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6559. struct ath9k_tx_queue_info *qi;
  6560. if (q >= pCap->total_queues) {
  6561. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6562. __func__, q);
  6563. return false;
  6564. }
  6565. qi = &ahp->ah_txq[q];
  6566. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6567. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue %u\n",
  6568. __func__, q);
  6569. return false;
  6570. }
  6571. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: release queue %u\n",
  6572. __func__, q);
  6573. qi->tqi_type = ATH9K_TX_QUEUE_INACTIVE;
  6574. ahp->ah_txOkInterruptMask &= ~(1 << q);
  6575. ahp->ah_txErrInterruptMask &= ~(1 << q);
  6576. ahp->ah_txDescInterruptMask &= ~(1 << q);
  6577. ahp->ah_txEolInterruptMask &= ~(1 << q);
  6578. ahp->ah_txUrnInterruptMask &= ~(1 << q);
  6579. ath9k_hw_set_txq_interrupts(ah, qi);
  6580. return true;
  6581. }
  6582. bool ath9k_hw_resettxqueue(struct ath_hal *ah, u32 q)
  6583. {
  6584. struct ath_hal_5416 *ahp = AH5416(ah);
  6585. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  6586. struct ath9k_channel *chan = ah->ah_curchan;
  6587. struct ath9k_tx_queue_info *qi;
  6588. u32 cwMin, chanCwMin, value;
  6589. if (q >= pCap->total_queues) {
  6590. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: invalid queue num %u\n",
  6591. __func__, q);
  6592. return false;
  6593. }
  6594. qi = &ahp->ah_txq[q];
  6595. if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
  6596. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: inactive queue %u\n",
  6597. __func__, q);
  6598. return true;
  6599. }
  6600. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: reset queue %u\n", __func__, q);
  6601. if (qi->tqi_cwmin == ATH9K_TXQ_USEDEFAULT) {
  6602. if (chan && IS_CHAN_B(chan))
  6603. chanCwMin = INIT_CWMIN_11B;
  6604. else
  6605. chanCwMin = INIT_CWMIN;
  6606. for (cwMin = 1; cwMin < chanCwMin; cwMin = (cwMin << 1) | 1);
  6607. } else
  6608. cwMin = qi->tqi_cwmin;
  6609. REG_WRITE(ah, AR_DLCL_IFS(q), SM(cwMin, AR_D_LCL_IFS_CWMIN)
  6610. | SM(qi->tqi_cwmax, AR_D_LCL_IFS_CWMAX)
  6611. | SM(qi->tqi_aifs, AR_D_LCL_IFS_AIFS));
  6612. REG_WRITE(ah, AR_DRETRY_LIMIT(q),
  6613. SM(INIT_SSH_RETRY, AR_D_RETRY_LIMIT_STA_SH)
  6614. | SM(INIT_SLG_RETRY, AR_D_RETRY_LIMIT_STA_LG)
  6615. | SM(qi->tqi_shretry, AR_D_RETRY_LIMIT_FR_SH)
  6616. );
  6617. REG_WRITE(ah, AR_QMISC(q), AR_Q_MISC_DCU_EARLY_TERM_REQ);
  6618. REG_WRITE(ah, AR_DMISC(q),
  6619. AR_D_MISC_CW_BKOFF_EN | AR_D_MISC_FRAG_WAIT_EN | 0x2);
  6620. if (qi->tqi_cbrPeriod) {
  6621. REG_WRITE(ah, AR_QCBRCFG(q),
  6622. SM(qi->tqi_cbrPeriod, AR_Q_CBRCFG_INTERVAL)
  6623. | SM(qi->tqi_cbrOverflowLimit,
  6624. AR_Q_CBRCFG_OVF_THRESH));
  6625. REG_WRITE(ah, AR_QMISC(q),
  6626. REG_READ(ah,
  6627. AR_QMISC(q)) | AR_Q_MISC_FSP_CBR | (qi->
  6628. tqi_cbrOverflowLimit
  6629. ?
  6630. AR_Q_MISC_CBR_EXP_CNTR_LIMIT_EN
  6631. :
  6632. 0));
  6633. }
  6634. if (qi->tqi_readyTime && (qi->tqi_type != ATH9K_TX_QUEUE_CAB)) {
  6635. REG_WRITE(ah, AR_QRDYTIMECFG(q),
  6636. SM(qi->tqi_readyTime, AR_Q_RDYTIMECFG_DURATION) |
  6637. AR_Q_RDYTIMECFG_EN);
  6638. }
  6639. REG_WRITE(ah, AR_DCHNTIME(q),
  6640. SM(qi->tqi_burstTime, AR_D_CHNTIME_DUR) |
  6641. (qi->tqi_burstTime ? AR_D_CHNTIME_EN : 0));
  6642. if (qi->tqi_burstTime
  6643. && (qi->tqi_qflags & TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)) {
  6644. REG_WRITE(ah, AR_QMISC(q),
  6645. REG_READ(ah,
  6646. AR_QMISC(q)) |
  6647. AR_Q_MISC_RDYTIME_EXP_POLICY);
  6648. }
  6649. if (qi->tqi_qflags & TXQ_FLAG_BACKOFF_DISABLE) {
  6650. REG_WRITE(ah, AR_DMISC(q),
  6651. REG_READ(ah, AR_DMISC(q)) |
  6652. AR_D_MISC_POST_FR_BKOFF_DIS);
  6653. }
  6654. if (qi->tqi_qflags & TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE) {
  6655. REG_WRITE(ah, AR_DMISC(q),
  6656. REG_READ(ah, AR_DMISC(q)) |
  6657. AR_D_MISC_FRAG_BKOFF_EN);
  6658. }
  6659. switch (qi->tqi_type) {
  6660. case ATH9K_TX_QUEUE_BEACON:
  6661. REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
  6662. | AR_Q_MISC_FSP_DBA_GATED
  6663. | AR_Q_MISC_BEACON_USE
  6664. | AR_Q_MISC_CBR_INCR_DIS1);
  6665. REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
  6666. | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
  6667. AR_D_MISC_ARB_LOCKOUT_CNTRL_S)
  6668. | AR_D_MISC_BEACON_USE
  6669. | AR_D_MISC_POST_FR_BKOFF_DIS);
  6670. break;
  6671. case ATH9K_TX_QUEUE_CAB:
  6672. REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
  6673. | AR_Q_MISC_FSP_DBA_GATED
  6674. | AR_Q_MISC_CBR_INCR_DIS1
  6675. | AR_Q_MISC_CBR_INCR_DIS0);
  6676. value = (qi->tqi_readyTime
  6677. - (ah->ah_config.sw_beacon_response_time -
  6678. ah->ah_config.dma_beacon_response_time)
  6679. -
  6680. ah->ah_config.additional_swba_backoff) *
  6681. 1024;
  6682. REG_WRITE(ah, AR_QRDYTIMECFG(q),
  6683. value | AR_Q_RDYTIMECFG_EN);
  6684. REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
  6685. | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
  6686. AR_D_MISC_ARB_LOCKOUT_CNTRL_S));
  6687. break;
  6688. case ATH9K_TX_QUEUE_PSPOLL:
  6689. REG_WRITE(ah, AR_QMISC(q),
  6690. REG_READ(ah,
  6691. AR_QMISC(q)) | AR_Q_MISC_CBR_INCR_DIS1);
  6692. break;
  6693. case ATH9K_TX_QUEUE_UAPSD:
  6694. REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
  6695. | AR_D_MISC_POST_FR_BKOFF_DIS);
  6696. break;
  6697. default:
  6698. break;
  6699. }
  6700. if (qi->tqi_intFlags & ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS) {
  6701. REG_WRITE(ah, AR_DMISC(q),
  6702. REG_READ(ah, AR_DMISC(q)) |
  6703. SM(AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL,
  6704. AR_D_MISC_ARB_LOCKOUT_CNTRL) |
  6705. AR_D_MISC_POST_FR_BKOFF_DIS);
  6706. }
  6707. if (qi->tqi_qflags & TXQ_FLAG_TXOKINT_ENABLE)
  6708. ahp->ah_txOkInterruptMask |= 1 << q;
  6709. else
  6710. ahp->ah_txOkInterruptMask &= ~(1 << q);
  6711. if (qi->tqi_qflags & TXQ_FLAG_TXERRINT_ENABLE)
  6712. ahp->ah_txErrInterruptMask |= 1 << q;
  6713. else
  6714. ahp->ah_txErrInterruptMask &= ~(1 << q);
  6715. if (qi->tqi_qflags & TXQ_FLAG_TXDESCINT_ENABLE)
  6716. ahp->ah_txDescInterruptMask |= 1 << q;
  6717. else
  6718. ahp->ah_txDescInterruptMask &= ~(1 << q);
  6719. if (qi->tqi_qflags & TXQ_FLAG_TXEOLINT_ENABLE)
  6720. ahp->ah_txEolInterruptMask |= 1 << q;
  6721. else
  6722. ahp->ah_txEolInterruptMask &= ~(1 << q);
  6723. if (qi->tqi_qflags & TXQ_FLAG_TXURNINT_ENABLE)
  6724. ahp->ah_txUrnInterruptMask |= 1 << q;
  6725. else
  6726. ahp->ah_txUrnInterruptMask &= ~(1 << q);
  6727. ath9k_hw_set_txq_interrupts(ah, qi);
  6728. return true;
  6729. }
  6730. void ath9k_hw_gettxintrtxqs(struct ath_hal *ah, u32 *txqs)
  6731. {
  6732. struct ath_hal_5416 *ahp = AH5416(ah);
  6733. *txqs &= ahp->ah_intrTxqs;
  6734. ahp->ah_intrTxqs &= ~(*txqs);
  6735. }
  6736. bool
  6737. ath9k_hw_filltxdesc(struct ath_hal *ah, struct ath_desc *ds,
  6738. u32 segLen, bool firstSeg,
  6739. bool lastSeg, const struct ath_desc *ds0)
  6740. {
  6741. struct ar5416_desc *ads = AR5416DESC(ds);
  6742. if (firstSeg) {
  6743. ads->ds_ctl1 |= segLen | (lastSeg ? 0 : AR_TxMore);
  6744. } else if (lastSeg) {
  6745. ads->ds_ctl0 = 0;
  6746. ads->ds_ctl1 = segLen;
  6747. ads->ds_ctl2 = AR5416DESC_CONST(ds0)->ds_ctl2;
  6748. ads->ds_ctl3 = AR5416DESC_CONST(ds0)->ds_ctl3;
  6749. } else {
  6750. ads->ds_ctl0 = 0;
  6751. ads->ds_ctl1 = segLen | AR_TxMore;
  6752. ads->ds_ctl2 = 0;
  6753. ads->ds_ctl3 = 0;
  6754. }
  6755. ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
  6756. ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
  6757. ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
  6758. ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
  6759. ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
  6760. return true;
  6761. }
  6762. void ath9k_hw_cleartxdesc(struct ath_hal *ah, struct ath_desc *ds)
  6763. {
  6764. struct ar5416_desc *ads = AR5416DESC(ds);
  6765. ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
  6766. ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
  6767. ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
  6768. ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
  6769. ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
  6770. }
  6771. int
  6772. ath9k_hw_txprocdesc(struct ath_hal *ah, struct ath_desc *ds)
  6773. {
  6774. struct ar5416_desc *ads = AR5416DESC(ds);
  6775. if ((ads->ds_txstatus9 & AR_TxDone) == 0)
  6776. return -EINPROGRESS;
  6777. ds->ds_txstat.ts_seqnum = MS(ads->ds_txstatus9, AR_SeqNum);
  6778. ds->ds_txstat.ts_tstamp = ads->AR_SendTimestamp;
  6779. ds->ds_txstat.ts_status = 0;
  6780. ds->ds_txstat.ts_flags = 0;
  6781. if (ads->ds_txstatus1 & AR_ExcessiveRetries)
  6782. ds->ds_txstat.ts_status |= ATH9K_TXERR_XRETRY;
  6783. if (ads->ds_txstatus1 & AR_Filtered)
  6784. ds->ds_txstat.ts_status |= ATH9K_TXERR_FILT;
  6785. if (ads->ds_txstatus1 & AR_FIFOUnderrun)
  6786. ds->ds_txstat.ts_status |= ATH9K_TXERR_FIFO;
  6787. if (ads->ds_txstatus9 & AR_TxOpExceeded)
  6788. ds->ds_txstat.ts_status |= ATH9K_TXERR_XTXOP;
  6789. if (ads->ds_txstatus1 & AR_TxTimerExpired)
  6790. ds->ds_txstat.ts_status |= ATH9K_TXERR_TIMER_EXPIRED;
  6791. if (ads->ds_txstatus1 & AR_DescCfgErr)
  6792. ds->ds_txstat.ts_flags |= ATH9K_TX_DESC_CFG_ERR;
  6793. if (ads->ds_txstatus1 & AR_TxDataUnderrun) {
  6794. ds->ds_txstat.ts_flags |= ATH9K_TX_DATA_UNDERRUN;
  6795. ath9k_hw_updatetxtriglevel(ah, true);
  6796. }
  6797. if (ads->ds_txstatus1 & AR_TxDelimUnderrun) {
  6798. ds->ds_txstat.ts_flags |= ATH9K_TX_DELIM_UNDERRUN;
  6799. ath9k_hw_updatetxtriglevel(ah, true);
  6800. }
  6801. if (ads->ds_txstatus0 & AR_TxBaStatus) {
  6802. ds->ds_txstat.ts_flags |= ATH9K_TX_BA;
  6803. ds->ds_txstat.ba_low = ads->AR_BaBitmapLow;
  6804. ds->ds_txstat.ba_high = ads->AR_BaBitmapHigh;
  6805. }
  6806. ds->ds_txstat.ts_rateindex = MS(ads->ds_txstatus9, AR_FinalTxIdx);
  6807. switch (ds->ds_txstat.ts_rateindex) {
  6808. case 0:
  6809. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate0);
  6810. break;
  6811. case 1:
  6812. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate1);
  6813. break;
  6814. case 2:
  6815. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate2);
  6816. break;
  6817. case 3:
  6818. ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate3);
  6819. break;
  6820. }
  6821. ds->ds_txstat.ts_rssi = MS(ads->ds_txstatus5, AR_TxRSSICombined);
  6822. ds->ds_txstat.ts_rssi_ctl0 = MS(ads->ds_txstatus0, AR_TxRSSIAnt00);
  6823. ds->ds_txstat.ts_rssi_ctl1 = MS(ads->ds_txstatus0, AR_TxRSSIAnt01);
  6824. ds->ds_txstat.ts_rssi_ctl2 = MS(ads->ds_txstatus0, AR_TxRSSIAnt02);
  6825. ds->ds_txstat.ts_rssi_ext0 = MS(ads->ds_txstatus5, AR_TxRSSIAnt10);
  6826. ds->ds_txstat.ts_rssi_ext1 = MS(ads->ds_txstatus5, AR_TxRSSIAnt11);
  6827. ds->ds_txstat.ts_rssi_ext2 = MS(ads->ds_txstatus5, AR_TxRSSIAnt12);
  6828. ds->ds_txstat.evm0 = ads->AR_TxEVM0;
  6829. ds->ds_txstat.evm1 = ads->AR_TxEVM1;
  6830. ds->ds_txstat.evm2 = ads->AR_TxEVM2;
  6831. ds->ds_txstat.ts_shortretry = MS(ads->ds_txstatus1, AR_RTSFailCnt);
  6832. ds->ds_txstat.ts_longretry = MS(ads->ds_txstatus1, AR_DataFailCnt);
  6833. ds->ds_txstat.ts_virtcol = MS(ads->ds_txstatus1, AR_VirtRetryCnt);
  6834. ds->ds_txstat.ts_antenna = 1;
  6835. return 0;
  6836. }
  6837. void
  6838. ath9k_hw_set11n_txdesc(struct ath_hal *ah, struct ath_desc *ds,
  6839. u32 pktLen, enum ath9k_pkt_type type, u32 txPower,
  6840. u32 keyIx, enum ath9k_key_type keyType, u32 flags)
  6841. {
  6842. struct ar5416_desc *ads = AR5416DESC(ds);
  6843. struct ath_hal_5416 *ahp = AH5416(ah);
  6844. txPower += ahp->ah_txPowerIndexOffset;
  6845. if (txPower > 63)
  6846. txPower = 63;
  6847. ads->ds_ctl0 = (pktLen & AR_FrameLen)
  6848. | (flags & ATH9K_TXDESC_VMF ? AR_VirtMoreFrag : 0)
  6849. | SM(txPower, AR_XmitPower)
  6850. | (flags & ATH9K_TXDESC_VEOL ? AR_VEOL : 0)
  6851. | (flags & ATH9K_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
  6852. | (flags & ATH9K_TXDESC_INTREQ ? AR_TxIntrReq : 0)
  6853. | (keyIx != ATH9K_TXKEYIX_INVALID ? AR_DestIdxValid : 0);
  6854. ads->ds_ctl1 =
  6855. (keyIx != ATH9K_TXKEYIX_INVALID ? SM(keyIx, AR_DestIdx) : 0)
  6856. | SM(type, AR_FrameType)
  6857. | (flags & ATH9K_TXDESC_NOACK ? AR_NoAck : 0)
  6858. | (flags & ATH9K_TXDESC_EXT_ONLY ? AR_ExtOnly : 0)
  6859. | (flags & ATH9K_TXDESC_EXT_AND_CTL ? AR_ExtAndCtl : 0);
  6860. ads->ds_ctl6 = SM(keyType, AR_EncrType);
  6861. if (AR_SREV_9285(ah)) {
  6862. ads->ds_ctl8 = 0;
  6863. ads->ds_ctl9 = 0;
  6864. ads->ds_ctl10 = 0;
  6865. ads->ds_ctl11 = 0;
  6866. }
  6867. }
  6868. void
  6869. ath9k_hw_set11n_ratescenario(struct ath_hal *ah, struct ath_desc *ds,
  6870. struct ath_desc *lastds,
  6871. u32 durUpdateEn, u32 rtsctsRate,
  6872. u32 rtsctsDuration,
  6873. struct ath9k_11n_rate_series series[],
  6874. u32 nseries, u32 flags)
  6875. {
  6876. struct ar5416_desc *ads = AR5416DESC(ds);
  6877. struct ar5416_desc *last_ads = AR5416DESC(lastds);
  6878. u32 ds_ctl0;
  6879. (void) nseries;
  6880. (void) rtsctsDuration;
  6881. if (flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA)) {
  6882. ds_ctl0 = ads->ds_ctl0;
  6883. if (flags & ATH9K_TXDESC_RTSENA) {
  6884. ds_ctl0 &= ~AR_CTSEnable;
  6885. ds_ctl0 |= AR_RTSEnable;
  6886. } else {
  6887. ds_ctl0 &= ~AR_RTSEnable;
  6888. ds_ctl0 |= AR_CTSEnable;
  6889. }
  6890. ads->ds_ctl0 = ds_ctl0;
  6891. } else {
  6892. ads->ds_ctl0 =
  6893. (ads->ds_ctl0 & ~(AR_RTSEnable | AR_CTSEnable));
  6894. }
  6895. ads->ds_ctl2 = set11nTries(series, 0)
  6896. | set11nTries(series, 1)
  6897. | set11nTries(series, 2)
  6898. | set11nTries(series, 3)
  6899. | (durUpdateEn ? AR_DurUpdateEna : 0)
  6900. | SM(0, AR_BurstDur);
  6901. ads->ds_ctl3 = set11nRate(series, 0)
  6902. | set11nRate(series, 1)
  6903. | set11nRate(series, 2)
  6904. | set11nRate(series, 3);
  6905. ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
  6906. | set11nPktDurRTSCTS(series, 1);
  6907. ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
  6908. | set11nPktDurRTSCTS(series, 3);
  6909. ads->ds_ctl7 = set11nRateFlags(series, 0)
  6910. | set11nRateFlags(series, 1)
  6911. | set11nRateFlags(series, 2)
  6912. | set11nRateFlags(series, 3)
  6913. | SM(rtsctsRate, AR_RTSCTSRate);
  6914. last_ads->ds_ctl2 = ads->ds_ctl2;
  6915. last_ads->ds_ctl3 = ads->ds_ctl3;
  6916. }
  6917. void
  6918. ath9k_hw_set11n_aggr_first(struct ath_hal *ah, struct ath_desc *ds,
  6919. u32 aggrLen)
  6920. {
  6921. struct ar5416_desc *ads = AR5416DESC(ds);
  6922. ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
  6923. ads->ds_ctl6 &= ~AR_AggrLen;
  6924. ads->ds_ctl6 |= SM(aggrLen, AR_AggrLen);
  6925. }
  6926. void
  6927. ath9k_hw_set11n_aggr_middle(struct ath_hal *ah, struct ath_desc *ds,
  6928. u32 numDelims)
  6929. {
  6930. struct ar5416_desc *ads = AR5416DESC(ds);
  6931. unsigned int ctl6;
  6932. ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
  6933. ctl6 = ads->ds_ctl6;
  6934. ctl6 &= ~AR_PadDelim;
  6935. ctl6 |= SM(numDelims, AR_PadDelim);
  6936. ads->ds_ctl6 = ctl6;
  6937. }
  6938. void ath9k_hw_set11n_aggr_last(struct ath_hal *ah, struct ath_desc *ds)
  6939. {
  6940. struct ar5416_desc *ads = AR5416DESC(ds);
  6941. ads->ds_ctl1 |= AR_IsAggr;
  6942. ads->ds_ctl1 &= ~AR_MoreAggr;
  6943. ads->ds_ctl6 &= ~AR_PadDelim;
  6944. }
  6945. void ath9k_hw_clr11n_aggr(struct ath_hal *ah, struct ath_desc *ds)
  6946. {
  6947. struct ar5416_desc *ads = AR5416DESC(ds);
  6948. ads->ds_ctl1 &= (~AR_IsAggr & ~AR_MoreAggr);
  6949. }
  6950. void
  6951. ath9k_hw_set11n_burstduration(struct ath_hal *ah, struct ath_desc *ds,
  6952. u32 burstDuration)
  6953. {
  6954. struct ar5416_desc *ads = AR5416DESC(ds);
  6955. ads->ds_ctl2 &= ~AR_BurstDur;
  6956. ads->ds_ctl2 |= SM(burstDuration, AR_BurstDur);
  6957. }
  6958. void
  6959. ath9k_hw_set11n_virtualmorefrag(struct ath_hal *ah, struct ath_desc *ds,
  6960. u32 vmf)
  6961. {
  6962. struct ar5416_desc *ads = AR5416DESC(ds);
  6963. if (vmf)
  6964. ads->ds_ctl0 |= AR_VirtMoreFrag;
  6965. else
  6966. ads->ds_ctl0 &= ~AR_VirtMoreFrag;
  6967. }
  6968. void ath9k_hw_putrxbuf(struct ath_hal *ah, u32 rxdp)
  6969. {
  6970. REG_WRITE(ah, AR_RXDP, rxdp);
  6971. }
  6972. void ath9k_hw_rxena(struct ath_hal *ah)
  6973. {
  6974. REG_WRITE(ah, AR_CR, AR_CR_RXE);
  6975. }
  6976. bool ath9k_hw_setrxabort(struct ath_hal *ah, bool set)
  6977. {
  6978. if (set) {
  6979. REG_SET_BIT(ah, AR_DIAG_SW,
  6980. (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  6981. if (!ath9k_hw_wait
  6982. (ah, AR_OBS_BUS_1, AR_OBS_BUS_1_RX_STATE, 0)) {
  6983. u32 reg;
  6984. REG_CLR_BIT(ah, AR_DIAG_SW,
  6985. (AR_DIAG_RX_DIS |
  6986. AR_DIAG_RX_ABORT));
  6987. reg = REG_READ(ah, AR_OBS_BUS_1);
  6988. DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
  6989. "%s: rx failed to go idle in 10 ms RXSM=0x%x\n",
  6990. __func__, reg);
  6991. return false;
  6992. }
  6993. } else {
  6994. REG_CLR_BIT(ah, AR_DIAG_SW,
  6995. (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  6996. }
  6997. return true;
  6998. }
  6999. void
  7000. ath9k_hw_setmcastfilter(struct ath_hal *ah, u32 filter0,
  7001. u32 filter1)
  7002. {
  7003. REG_WRITE(ah, AR_MCAST_FIL0, filter0);
  7004. REG_WRITE(ah, AR_MCAST_FIL1, filter1);
  7005. }
  7006. bool
  7007. ath9k_hw_setuprxdesc(struct ath_hal *ah, struct ath_desc *ds,
  7008. u32 size, u32 flags)
  7009. {
  7010. struct ar5416_desc *ads = AR5416DESC(ds);
  7011. struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
  7012. ads->ds_ctl1 = size & AR_BufLen;
  7013. if (flags & ATH9K_RXDESC_INTREQ)
  7014. ads->ds_ctl1 |= AR_RxIntrReq;
  7015. ads->ds_rxstatus8 &= ~AR_RxDone;
  7016. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  7017. memset(&(ads->u), 0, sizeof(ads->u));
  7018. return true;
  7019. }
  7020. int
  7021. ath9k_hw_rxprocdesc(struct ath_hal *ah, struct ath_desc *ds,
  7022. u32 pa, struct ath_desc *nds, u64 tsf)
  7023. {
  7024. struct ar5416_desc ads;
  7025. struct ar5416_desc *adsp = AR5416DESC(ds);
  7026. if ((adsp->ds_rxstatus8 & AR_RxDone) == 0)
  7027. return -EINPROGRESS;
  7028. ads.u.rx = adsp->u.rx;
  7029. ds->ds_rxstat.rs_status = 0;
  7030. ds->ds_rxstat.rs_flags = 0;
  7031. ds->ds_rxstat.rs_datalen = ads.ds_rxstatus1 & AR_DataLen;
  7032. ds->ds_rxstat.rs_tstamp = ads.AR_RcvTimestamp;
  7033. ds->ds_rxstat.rs_rssi = MS(ads.ds_rxstatus4, AR_RxRSSICombined);
  7034. ds->ds_rxstat.rs_rssi_ctl0 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt00);
  7035. ds->ds_rxstat.rs_rssi_ctl1 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt01);
  7036. ds->ds_rxstat.rs_rssi_ctl2 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt02);
  7037. ds->ds_rxstat.rs_rssi_ext0 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt10);
  7038. ds->ds_rxstat.rs_rssi_ext1 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt11);
  7039. ds->ds_rxstat.rs_rssi_ext2 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt12);
  7040. if (ads.ds_rxstatus8 & AR_RxKeyIdxValid)
  7041. ds->ds_rxstat.rs_keyix = MS(ads.ds_rxstatus8, AR_KeyIdx);
  7042. else
  7043. ds->ds_rxstat.rs_keyix = ATH9K_RXKEYIX_INVALID;
  7044. ds->ds_rxstat.rs_rate = RXSTATUS_RATE(ah, (&ads));
  7045. ds->ds_rxstat.rs_more = (ads.ds_rxstatus1 & AR_RxMore) ? 1 : 0;
  7046. ds->ds_rxstat.rs_isaggr = (ads.ds_rxstatus8 & AR_RxAggr) ? 1 : 0;
  7047. ds->ds_rxstat.rs_moreaggr =
  7048. (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
  7049. ds->ds_rxstat.rs_antenna = MS(ads.ds_rxstatus3, AR_RxAntenna);
  7050. ds->ds_rxstat.rs_flags =
  7051. (ads.ds_rxstatus3 & AR_GI) ? ATH9K_RX_GI : 0;
  7052. ds->ds_rxstat.rs_flags |=
  7053. (ads.ds_rxstatus3 & AR_2040) ? ATH9K_RX_2040 : 0;
  7054. if (ads.ds_rxstatus8 & AR_PreDelimCRCErr)
  7055. ds->ds_rxstat.rs_flags |= ATH9K_RX_DELIM_CRC_PRE;
  7056. if (ads.ds_rxstatus8 & AR_PostDelimCRCErr)
  7057. ds->ds_rxstat.rs_flags |= ATH9K_RX_DELIM_CRC_POST;
  7058. if (ads.ds_rxstatus8 & AR_DecryptBusyErr)
  7059. ds->ds_rxstat.rs_flags |= ATH9K_RX_DECRYPT_BUSY;
  7060. if ((ads.ds_rxstatus8 & AR_RxFrameOK) == 0) {
  7061. if (ads.ds_rxstatus8 & AR_CRCErr)
  7062. ds->ds_rxstat.rs_status |= ATH9K_RXERR_CRC;
  7063. else if (ads.ds_rxstatus8 & AR_PHYErr) {
  7064. u32 phyerr;
  7065. ds->ds_rxstat.rs_status |= ATH9K_RXERR_PHY;
  7066. phyerr = MS(ads.ds_rxstatus8, AR_PHYErrCode);
  7067. ds->ds_rxstat.rs_phyerr = phyerr;
  7068. } else if (ads.ds_rxstatus8 & AR_DecryptCRCErr)
  7069. ds->ds_rxstat.rs_status |= ATH9K_RXERR_DECRYPT;
  7070. else if (ads.ds_rxstatus8 & AR_MichaelErr)
  7071. ds->ds_rxstat.rs_status |= ATH9K_RXERR_MIC;
  7072. }
  7073. return 0;
  7074. }
  7075. static void ath9k_hw_setup_rate_table(struct ath_hal *ah,
  7076. struct ath9k_rate_table *rt)
  7077. {
  7078. int i;
  7079. if (rt->rateCodeToIndex[0] != 0)
  7080. return;
  7081. for (i = 0; i < 256; i++)
  7082. rt->rateCodeToIndex[i] = (u8) -1;
  7083. for (i = 0; i < rt->rateCount; i++) {
  7084. u8 code = rt->info[i].rateCode;
  7085. u8 cix = rt->info[i].controlRate;
  7086. rt->rateCodeToIndex[code] = i;
  7087. rt->rateCodeToIndex[code | rt->info[i].shortPreamble] = i;
  7088. rt->info[i].lpAckDuration =
  7089. ath9k_hw_computetxtime(ah, rt,
  7090. WLAN_CTRL_FRAME_SIZE,
  7091. cix,
  7092. false);
  7093. rt->info[i].spAckDuration =
  7094. ath9k_hw_computetxtime(ah, rt,
  7095. WLAN_CTRL_FRAME_SIZE,
  7096. cix,
  7097. true);
  7098. }
  7099. }
  7100. const struct ath9k_rate_table *ath9k_hw_getratetable(struct ath_hal *ah,
  7101. u32 mode)
  7102. {
  7103. struct ath9k_rate_table *rt;
  7104. switch (mode) {
  7105. case ATH9K_MODE_11A:
  7106. rt = &ar5416_11a_table;
  7107. break;
  7108. case ATH9K_MODE_11B:
  7109. rt = &ar5416_11b_table;
  7110. break;
  7111. case ATH9K_MODE_11G:
  7112. rt = &ar5416_11g_table;
  7113. break;
  7114. case ATH9K_MODE_11NG_HT20:
  7115. case ATH9K_MODE_11NG_HT40PLUS:
  7116. case ATH9K_MODE_11NG_HT40MINUS:
  7117. rt = &ar5416_11ng_table;
  7118. break;
  7119. case ATH9K_MODE_11NA_HT20:
  7120. case ATH9K_MODE_11NA_HT40PLUS:
  7121. case ATH9K_MODE_11NA_HT40MINUS:
  7122. rt = &ar5416_11na_table;
  7123. break;
  7124. default:
  7125. DPRINTF(ah->ah_sc, ATH_DBG_CHANNEL, "%s: invalid mode 0x%x\n",
  7126. __func__, mode);
  7127. return NULL;
  7128. }
  7129. ath9k_hw_setup_rate_table(ah, rt);
  7130. return rt;
  7131. }
  7132. static const char *ath9k_hw_devname(u16 devid)
  7133. {
  7134. switch (devid) {
  7135. case AR5416_DEVID_PCI:
  7136. case AR5416_DEVID_PCIE:
  7137. return "Atheros 5416";
  7138. case AR9160_DEVID_PCI:
  7139. return "Atheros 9160";
  7140. case AR9280_DEVID_PCI:
  7141. case AR9280_DEVID_PCIE:
  7142. return "Atheros 9280";
  7143. }
  7144. return NULL;
  7145. }
  7146. const char *ath9k_hw_probe(u16 vendorid, u16 devid)
  7147. {
  7148. return vendorid == ATHEROS_VENDOR_ID ?
  7149. ath9k_hw_devname(devid) : NULL;
  7150. }
  7151. struct ath_hal *ath9k_hw_attach(u16 devid,
  7152. struct ath_softc *sc,
  7153. void __iomem *mem,
  7154. int *error)
  7155. {
  7156. struct ath_hal *ah = NULL;
  7157. switch (devid) {
  7158. case AR5416_DEVID_PCI:
  7159. case AR5416_DEVID_PCIE:
  7160. case AR9160_DEVID_PCI:
  7161. case AR9280_DEVID_PCI:
  7162. case AR9280_DEVID_PCIE:
  7163. ah = ath9k_hw_do_attach(devid, sc, mem, error);
  7164. break;
  7165. default:
  7166. DPRINTF(ah->ah_sc, ATH_DBG_ANY,
  7167. "devid=0x%x not supported.\n", devid);
  7168. ah = NULL;
  7169. *error = -ENXIO;
  7170. break;
  7171. }
  7172. if (ah != NULL) {
  7173. ah->ah_devid = ah->ah_devid;
  7174. ah->ah_subvendorid = ah->ah_subvendorid;
  7175. ah->ah_macVersion = ah->ah_macVersion;
  7176. ah->ah_macRev = ah->ah_macRev;
  7177. ah->ah_phyRev = ah->ah_phyRev;
  7178. ah->ah_analog5GhzRev = ah->ah_analog5GhzRev;
  7179. ah->ah_analog2GhzRev = ah->ah_analog2GhzRev;
  7180. }
  7181. return ah;
  7182. }
  7183. u16
  7184. ath9k_hw_computetxtime(struct ath_hal *ah,
  7185. const struct ath9k_rate_table *rates,
  7186. u32 frameLen, u16 rateix,
  7187. bool shortPreamble)
  7188. {
  7189. u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
  7190. u32 kbps;
  7191. kbps = rates->info[rateix].rateKbps;
  7192. if (kbps == 0)
  7193. return 0;
  7194. switch (rates->info[rateix].phy) {
  7195. case PHY_CCK:
  7196. phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
  7197. if (shortPreamble && rates->info[rateix].shortPreamble)
  7198. phyTime >>= 1;
  7199. numBits = frameLen << 3;
  7200. txTime = CCK_SIFS_TIME + phyTime
  7201. + ((numBits * 1000) / kbps);
  7202. break;
  7203. case PHY_OFDM:
  7204. if (ah->ah_curchan && IS_CHAN_QUARTER_RATE(ah->ah_curchan)) {
  7205. bitsPerSymbol =
  7206. (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
  7207. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  7208. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  7209. txTime = OFDM_SIFS_TIME_QUARTER
  7210. + OFDM_PREAMBLE_TIME_QUARTER
  7211. + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
  7212. } else if (ah->ah_curchan &&
  7213. IS_CHAN_HALF_RATE(ah->ah_curchan)) {
  7214. bitsPerSymbol =
  7215. (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
  7216. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  7217. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  7218. txTime = OFDM_SIFS_TIME_HALF +
  7219. OFDM_PREAMBLE_TIME_HALF
  7220. + (numSymbols * OFDM_SYMBOL_TIME_HALF);
  7221. } else {
  7222. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
  7223. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  7224. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  7225. txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
  7226. + (numSymbols * OFDM_SYMBOL_TIME);
  7227. }
  7228. break;
  7229. default:
  7230. DPRINTF(ah->ah_sc, ATH_DBG_PHY_IO,
  7231. "%s: unknown phy %u (rate ix %u)\n", __func__,
  7232. rates->info[rateix].phy, rateix);
  7233. txTime = 0;
  7234. break;
  7235. }
  7236. return txTime;
  7237. }
  7238. u32 ath9k_hw_mhz2ieee(struct ath_hal *ah, u32 freq, u32 flags)
  7239. {
  7240. if (flags & CHANNEL_2GHZ) {
  7241. if (freq == 2484)
  7242. return 14;
  7243. if (freq < 2484)
  7244. return (freq - 2407) / 5;
  7245. else
  7246. return 15 + ((freq - 2512) / 20);
  7247. } else if (flags & CHANNEL_5GHZ) {
  7248. if (ath9k_regd_is_public_safety_sku(ah) &&
  7249. IS_CHAN_IN_PUBLIC_SAFETY_BAND(freq)) {
  7250. return ((freq * 10) +
  7251. (((freq % 5) == 2) ? 5 : 0) - 49400) / 5;
  7252. } else if ((flags & CHANNEL_A) && (freq <= 5000)) {
  7253. return (freq - 4000) / 5;
  7254. } else {
  7255. return (freq - 5000) / 5;
  7256. }
  7257. } else {
  7258. if (freq == 2484)
  7259. return 14;
  7260. if (freq < 2484)
  7261. return (freq - 2407) / 5;
  7262. if (freq < 5000) {
  7263. if (ath9k_regd_is_public_safety_sku(ah)
  7264. && IS_CHAN_IN_PUBLIC_SAFETY_BAND(freq)) {
  7265. return ((freq * 10) +
  7266. (((freq % 5) ==
  7267. 2) ? 5 : 0) - 49400) / 5;
  7268. } else if (freq > 4900) {
  7269. return (freq - 4000) / 5;
  7270. } else {
  7271. return 15 + ((freq - 2512) / 20);
  7272. }
  7273. }
  7274. return (freq - 5000) / 5;
  7275. }
  7276. }
  7277. int16_t
  7278. ath9k_hw_getchan_noise(struct ath_hal *ah, struct ath9k_channel *chan)
  7279. {
  7280. struct ath9k_channel *ichan;
  7281. ichan = ath9k_regd_check_channel(ah, chan);
  7282. if (ichan == NULL) {
  7283. DPRINTF(ah->ah_sc, ATH_DBG_NF_CAL,
  7284. "%s: invalid channel %u/0x%x; no mapping\n",
  7285. __func__, chan->channel, chan->channelFlags);
  7286. return 0;
  7287. }
  7288. if (ichan->rawNoiseFloor == 0) {
  7289. enum wireless_mode mode = ath9k_hw_chan2wmode(ah, chan);
  7290. return NOISE_FLOOR[mode];
  7291. } else
  7292. return ichan->rawNoiseFloor;
  7293. }
  7294. bool ath9k_hw_set_tsfadjust(struct ath_hal *ah, u32 setting)
  7295. {
  7296. struct ath_hal_5416 *ahp = AH5416(ah);
  7297. if (setting)
  7298. ahp->ah_miscMode |= AR_PCU_TX_ADD_TSF;
  7299. else
  7300. ahp->ah_miscMode &= ~AR_PCU_TX_ADD_TSF;
  7301. return true;
  7302. }
  7303. bool ath9k_hw_phycounters(struct ath_hal *ah)
  7304. {
  7305. struct ath_hal_5416 *ahp = AH5416(ah);
  7306. return ahp->ah_hasHwPhyCounters ? true : false;
  7307. }
  7308. u32 ath9k_hw_gettxbuf(struct ath_hal *ah, u32 q)
  7309. {
  7310. return REG_READ(ah, AR_QTXDP(q));
  7311. }
  7312. bool ath9k_hw_puttxbuf(struct ath_hal *ah, u32 q,
  7313. u32 txdp)
  7314. {
  7315. REG_WRITE(ah, AR_QTXDP(q), txdp);
  7316. return true;
  7317. }
  7318. bool ath9k_hw_txstart(struct ath_hal *ah, u32 q)
  7319. {
  7320. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "%s: queue %u\n", __func__, q);
  7321. REG_WRITE(ah, AR_Q_TXE, 1 << q);
  7322. return true;
  7323. }
  7324. u32 ath9k_hw_numtxpending(struct ath_hal *ah, u32 q)
  7325. {
  7326. u32 npend;
  7327. npend = REG_READ(ah, AR_QSTS(q)) & AR_Q_STS_PEND_FR_CNT;
  7328. if (npend == 0) {
  7329. if (REG_READ(ah, AR_Q_TXE) & (1 << q))
  7330. npend = 1;
  7331. }
  7332. return npend;
  7333. }
  7334. bool ath9k_hw_stoptxdma(struct ath_hal *ah, u32 q)
  7335. {
  7336. u32 wait;
  7337. REG_WRITE(ah, AR_Q_TXD, 1 << q);
  7338. for (wait = 1000; wait != 0; wait--) {
  7339. if (ath9k_hw_numtxpending(ah, q) == 0)
  7340. break;
  7341. udelay(100);
  7342. }
  7343. if (ath9k_hw_numtxpending(ah, q)) {
  7344. u32 tsfLow, j;
  7345. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  7346. "%s: Num of pending TX Frames %d on Q %d\n",
  7347. __func__, ath9k_hw_numtxpending(ah, q), q);
  7348. for (j = 0; j < 2; j++) {
  7349. tsfLow = REG_READ(ah, AR_TSF_L32);
  7350. REG_WRITE(ah, AR_QUIET2,
  7351. SM(10, AR_QUIET2_QUIET_DUR));
  7352. REG_WRITE(ah, AR_QUIET_PERIOD, 100);
  7353. REG_WRITE(ah, AR_NEXT_QUIET_TIMER, tsfLow >> 10);
  7354. REG_SET_BIT(ah, AR_TIMER_MODE,
  7355. AR_QUIET_TIMER_EN);
  7356. if ((REG_READ(ah, AR_TSF_L32) >> 10) ==
  7357. (tsfLow >> 10)) {
  7358. break;
  7359. }
  7360. DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
  7361. "%s: TSF have moved while trying to set "
  7362. "quiet time TSF: 0x%08x\n",
  7363. __func__, tsfLow);
  7364. }
  7365. REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
  7366. udelay(200);
  7367. REG_CLR_BIT(ah, AR_TIMER_MODE, AR_QUIET_TIMER_EN);
  7368. wait = 1000;
  7369. while (ath9k_hw_numtxpending(ah, q)) {
  7370. if ((--wait) == 0) {
  7371. DPRINTF(ah->ah_sc, ATH_DBG_XMIT,
  7372. "%s: Failed to stop Tx DMA in 100 "
  7373. "msec after killing last frame\n",
  7374. __func__);
  7375. break;
  7376. }
  7377. udelay(100);
  7378. }
  7379. REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
  7380. }
  7381. REG_WRITE(ah, AR_Q_TXD, 0);
  7382. return wait != 0;
  7383. }