hw.c 221 KB

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