dm_compat.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885
  1. /*
  2. * Copyright 2018 Con Kolivas <kernel@kolivas.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "dm_compat.h"
  10. MCOMPAT_CHAIN_T s_chain_ops;
  11. MCOMPAT_CHAIN_T* s_chain_ops_p = &s_chain_ops;
  12. void init_mcompat_chain(void)
  13. {
  14. memset(&s_chain_ops, 0, sizeof(s_chain_ops));
  15. switch(g_platform)
  16. {
  17. case PLATFORM_ZYNQ_SPI_G9:
  18. case PLATFORM_ZYNQ_SPI_G19:
  19. case PLATFORM_ZYNQ_HUB_G9:
  20. case PLATFORM_ZYNQ_HUB_G19:
  21. s_chain_ops_p->power_on = zynq_chain_power_on;
  22. s_chain_ops_p->power_down = zynq_chain_power_down;
  23. s_chain_ops_p->hw_reset = zynq_chain_hw_reset;
  24. s_chain_ops_p->power_on_all = zynq_chain_power_on_all;
  25. s_chain_ops_p->power_down_all = zynq_chain_power_down_all;
  26. break;
  27. case PLATFORM_ORANGE_PI:
  28. s_chain_ops_p->power_on = opi_chain_power_on;
  29. s_chain_ops_p->power_down = opi_chain_power_down;
  30. s_chain_ops_p->hw_reset = opi_chain_hw_reset;
  31. s_chain_ops_p->power_on_all = opi_chain_power_on_all;
  32. s_chain_ops_p->power_down_all = opi_chain_power_down_all;
  33. break;
  34. default:
  35. applog(LOG_ERR, "the platform is undefined !!!");
  36. break;
  37. }
  38. }
  39. void exit_mcompat_chain(void)
  40. {
  41. switch(g_platform)
  42. {
  43. case PLATFORM_ZYNQ_SPI_G9:
  44. case PLATFORM_ZYNQ_SPI_G19:
  45. case PLATFORM_ZYNQ_HUB_G9:
  46. case PLATFORM_ZYNQ_HUB_G19:
  47. break;
  48. case PLATFORM_ORANGE_PI:
  49. break;
  50. default:
  51. applog(LOG_ERR, "the platform is undefined !!!");
  52. break;
  53. }
  54. }
  55. void register_mcompat_chain(MCOMPAT_CHAIN_T * ops)
  56. {
  57. if (ops->power_on != NULL)
  58. {
  59. s_chain_ops_p->power_on = ops->power_on;
  60. }
  61. if (ops->power_down != NULL)
  62. {
  63. s_chain_ops_p->power_down = ops->power_down;
  64. }
  65. if (ops->hw_reset != NULL)
  66. {
  67. s_chain_ops_p->hw_reset = ops->hw_reset;
  68. }
  69. if (ops->power_on_all != NULL)
  70. {
  71. s_chain_ops_p->power_on_all = ops->power_on_all;
  72. }
  73. if (ops->power_down_all != NULL)
  74. {
  75. s_chain_ops_p->power_down_all = ops->power_down_all;
  76. }
  77. }
  78. bool mcompat_chain_power_on(unsigned char chain_id)
  79. {
  80. if (s_chain_ops_p->power_on == NULL)
  81. {
  82. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  83. return false;
  84. }
  85. return s_chain_ops_p->power_on(chain_id);
  86. }
  87. bool mcompat_chain_power_down(unsigned char chain_id)
  88. {
  89. if (s_chain_ops_p->power_down == NULL)
  90. {
  91. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  92. return false;
  93. }
  94. return s_chain_ops_p->power_down(chain_id);
  95. }
  96. bool mcompat_chain_hw_reset(unsigned char chain_id)
  97. {
  98. if (s_chain_ops_p->hw_reset == NULL)
  99. {
  100. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  101. return false;
  102. }
  103. return s_chain_ops_p->hw_reset(chain_id);
  104. }
  105. bool mcompat_chain_power_on_all(void)
  106. {
  107. if (s_chain_ops_p->power_on_all == NULL)
  108. {
  109. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  110. return false;
  111. }
  112. return s_chain_ops_p->power_on_all();
  113. }
  114. bool mcompat_chain_power_down_all(void)
  115. {
  116. if (s_chain_ops_p->power_down_all == NULL)
  117. {
  118. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  119. return false;
  120. }
  121. return s_chain_ops_p->power_down_all();
  122. }
  123. MCOMPAT_CMD_T s_cmd_ops;
  124. MCOMPAT_CMD_T* s_cmd_ops_p = &s_cmd_ops;
  125. void init_mcompat_cmd(void)
  126. {
  127. memset(&s_cmd_ops, 0, sizeof(s_cmd_ops));
  128. switch(g_platform)
  129. {
  130. case PLATFORM_ZYNQ_SPI_G9:
  131. case PLATFORM_ZYNQ_SPI_G19:
  132. init_spi_cmd(g_chain_num);
  133. s_cmd_ops_p->set_speed = spi_set_spi_speed;
  134. s_cmd_ops_p->cmd_reset = spi_cmd_reset;
  135. s_cmd_ops_p->cmd_bist_start = spi_cmd_bist_start;
  136. s_cmd_ops_p->cmd_bist_fix = spi_cmd_bist_fix;
  137. s_cmd_ops_p->cmd_bist_collect = spi_cmd_bist_collect;
  138. s_cmd_ops_p->cmd_read_register = spi_cmd_read_register;
  139. s_cmd_ops_p->cmd_write_register = spi_cmd_write_register;
  140. s_cmd_ops_p->cmd_read_write_reg0d = spi_cmd_read_write_reg0d;
  141. s_cmd_ops_p->cmd_read_result = spi_cmd_read_result;
  142. s_cmd_ops_p->cmd_write_job = spi_cmd_write_job;
  143. break;
  144. case PLATFORM_ZYNQ_HUB_G9:
  145. case PLATFORM_ZYNQ_HUB_G19:
  146. hub_init();
  147. init_hub_cmd(g_chain_num, g_chip_num);
  148. s_cmd_ops_p->set_speed = hub_set_spi_speed;
  149. s_cmd_ops_p->cmd_reset = hub_cmd_reset;
  150. s_cmd_ops_p->cmd_bist_start = hub_cmd_bist_start;
  151. s_cmd_ops_p->cmd_bist_fix = hub_cmd_bist_fix;
  152. s_cmd_ops_p->cmd_bist_collect = hub_cmd_bist_collect;
  153. s_cmd_ops_p->cmd_read_register = hub_cmd_read_register;
  154. s_cmd_ops_p->cmd_write_register = hub_cmd_write_register;
  155. s_cmd_ops_p->cmd_read_write_reg0d = hub_cmd_read_write_reg0d;
  156. s_cmd_ops_p->cmd_read_result = hub_cmd_read_result;
  157. s_cmd_ops_p->cmd_write_job = hub_cmd_write_job;
  158. s_cmd_ops_p->cmd_auto_nonce = hub_cmd_auto_nonce;
  159. s_cmd_ops_p->cmd_read_nonce = hub_cmd_read_nonce;
  160. break;
  161. case PLATFORM_ORANGE_PI:
  162. init_opi_cmd();
  163. s_cmd_ops_p->set_speed = opi_set_spi_speed;
  164. s_cmd_ops_p->cmd_reset = opi_cmd_reset;
  165. s_cmd_ops_p->cmd_bist_start = opi_cmd_bist_start;
  166. s_cmd_ops_p->cmd_bist_fix = opi_cmd_bist_fix;
  167. s_cmd_ops_p->cmd_bist_collect = opi_cmd_bist_collect;
  168. s_cmd_ops_p->cmd_read_register = opi_cmd_read_register;
  169. s_cmd_ops_p->cmd_write_register = opi_cmd_write_register;
  170. s_cmd_ops_p->cmd_read_write_reg0d = opi_cmd_read_write_reg0d;
  171. s_cmd_ops_p->cmd_read_result = opi_cmd_read_result;
  172. s_cmd_ops_p->cmd_write_job = opi_cmd_write_job;
  173. break;
  174. default:
  175. applog(LOG_ERR, "the platform is undefined !!!");
  176. break;
  177. }
  178. }
  179. void exit_mcompat_cmd(void)
  180. {
  181. switch(g_platform)
  182. {
  183. case PLATFORM_ZYNQ_SPI_G9:
  184. case PLATFORM_ZYNQ_SPI_G19:
  185. exit_spi_cmd(g_chain_num);
  186. break;
  187. case PLATFORM_ZYNQ_HUB_G9:
  188. case PLATFORM_ZYNQ_HUB_G19:
  189. hub_deinit();
  190. exit_hub_cmd(g_chain_num);
  191. break;
  192. default:
  193. applog(LOG_ERR, "the platform is undefined !!!");
  194. break;
  195. }
  196. }
  197. void register_mcompat_cmd(MCOMPAT_CMD_T * ops)
  198. {
  199. if (ops->set_speed != NULL)
  200. {
  201. s_cmd_ops_p->set_speed = ops->set_speed;
  202. }
  203. if (ops->cmd_reset != NULL)
  204. {
  205. s_cmd_ops_p->cmd_reset = ops->cmd_reset;
  206. }
  207. if (ops->cmd_bist_start != NULL)
  208. {
  209. s_cmd_ops_p->cmd_bist_start = ops->cmd_bist_start;
  210. }
  211. if (ops->cmd_bist_fix != NULL)
  212. {
  213. s_cmd_ops_p->cmd_bist_fix = ops->cmd_bist_fix;
  214. }
  215. if (ops->cmd_bist_collect != NULL)
  216. {
  217. s_cmd_ops_p->cmd_bist_collect = ops->cmd_bist_collect;
  218. }
  219. if (ops->cmd_read_register != NULL)
  220. {
  221. s_cmd_ops_p->cmd_read_register = ops->cmd_read_register;
  222. }
  223. if (ops->cmd_write_register != NULL)
  224. {
  225. s_cmd_ops_p->cmd_write_register = ops->cmd_write_register;
  226. }
  227. if (ops->cmd_read_result != NULL)
  228. {
  229. s_cmd_ops_p->cmd_read_result = ops->cmd_read_result;
  230. }
  231. if (ops->cmd_write_job != NULL)
  232. {
  233. s_cmd_ops_p->cmd_write_job = ops->cmd_write_job;
  234. }
  235. }
  236. bool mcompat_set_spi_speed(unsigned char chain_id, int index)
  237. {
  238. if (s_cmd_ops_p->set_speed == NULL)
  239. {
  240. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  241. return false;
  242. }
  243. s_cmd_ops_p->set_speed(chain_id, index);
  244. return true;
  245. }
  246. bool mcompat_cmd_reset(unsigned char chain_id, unsigned char chip_id, unsigned char *in, unsigned char *out)
  247. {
  248. if (s_cmd_ops_p->cmd_reset == NULL)
  249. {
  250. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  251. return false;
  252. }
  253. return s_cmd_ops_p->cmd_reset(chain_id, chip_id, in, out);
  254. }
  255. int mcompat_cmd_bist_start(unsigned char chain_id, unsigned char chip_id)
  256. {
  257. if (s_cmd_ops_p->cmd_bist_start == NULL)
  258. {
  259. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  260. return false;
  261. }
  262. return s_cmd_ops_p->cmd_bist_start(chain_id, chip_id);
  263. }
  264. bool mcompat_cmd_bist_collect(unsigned char chain_id, unsigned char chip_id)
  265. {
  266. if (s_cmd_ops_p->cmd_bist_collect == NULL)
  267. {
  268. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  269. return false;
  270. }
  271. return s_cmd_ops_p->cmd_bist_collect(chain_id, chip_id);
  272. }
  273. bool mcompat_cmd_bist_fix(unsigned char chain_id, unsigned char chip_id)
  274. {
  275. if (s_cmd_ops_p->cmd_bist_fix == NULL)
  276. {
  277. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  278. return false;
  279. }
  280. return s_cmd_ops_p->cmd_bist_fix(chain_id, chip_id);
  281. }
  282. bool mcompat_cmd_write_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  283. {
  284. if (s_cmd_ops_p->cmd_write_register == NULL)
  285. {
  286. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  287. return false;
  288. }
  289. return s_cmd_ops_p->cmd_write_register(chain_id, chip_id, reg, len);
  290. }
  291. bool mcompat_cmd_read_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  292. {
  293. if (s_cmd_ops_p->cmd_read_register == NULL)
  294. {
  295. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  296. return false;
  297. }
  298. return s_cmd_ops_p->cmd_read_register(chain_id, chip_id, reg, len);
  299. }
  300. bool mcompat_cmd_read_write_reg0d(unsigned char chain_id, unsigned char chip_id, unsigned char *in, int len, unsigned char *out)
  301. {
  302. if (s_cmd_ops_p->cmd_read_write_reg0d == NULL)
  303. {
  304. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  305. return false;
  306. }
  307. return s_cmd_ops_p->cmd_read_write_reg0d(chain_id, chip_id, in, len, out);
  308. }
  309. bool mcompat_cmd_write_job(unsigned char chain_id, unsigned char chip_id, unsigned char *job, int len)
  310. {
  311. if (s_cmd_ops_p->cmd_write_job == NULL)
  312. {
  313. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  314. return false;
  315. }
  316. return s_cmd_ops_p->cmd_write_job(chain_id, chip_id, job, len);
  317. }
  318. bool mcompat_cmd_read_result(unsigned char chain_id, unsigned char chip_id, unsigned char *res, int len)
  319. {
  320. if (s_cmd_ops_p->cmd_read_result == NULL)
  321. {
  322. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  323. return false;
  324. }
  325. return s_cmd_ops_p->cmd_read_result(chain_id, chip_id, res, len);
  326. }
  327. bool mcompat_cmd_auto_nonce(unsigned char chain_id, int mode, int len)
  328. {
  329. if (s_cmd_ops_p->cmd_auto_nonce == NULL)
  330. {
  331. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  332. return false;
  333. }
  334. return s_cmd_ops_p->cmd_auto_nonce(chain_id, mode, len);
  335. }
  336. bool mcompat_cmd_read_nonce(unsigned char chain_id, unsigned char *res, int len)
  337. {
  338. if (s_cmd_ops_p->cmd_read_nonce == NULL)
  339. {
  340. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  341. return false;
  342. }
  343. return s_cmd_ops_p->cmd_read_nonce(chain_id, res, len);
  344. }
  345. bool mcompat_cmd_get_temp( mcompat_fan_temp_s * fan_temp)
  346. {
  347. if (s_cmd_ops_p->cmd_get_temp== NULL)
  348. {
  349. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  350. return false;
  351. }
  352. return s_cmd_ops_p->cmd_get_temp(fan_temp);
  353. }
  354. int g_temp_hi_thr;
  355. int g_temp_lo_thr;
  356. int g_temp_start_thr;
  357. int g_dangerous_temp;
  358. int g_work_temp;
  359. int g_fan_speed;
  360. static int set_warn(int spi_id);
  361. #if 0
  362. static int set_warn(int spi_id)
  363. {
  364. mcompat_set_power_en(spi_id, 0);
  365. sleep(1);
  366. mcompat_set_reset(spi_id, 0);
  367. mcompat_set_start_en(spi_id, 0);
  368. mcompat_set_led(spi_id, 1);
  369. return 0;
  370. }
  371. #endif
  372. static void mcompat_deal_temp(unsigned char spi_id, mcompat_fan_temp_s *fan_temp_ctrl)
  373. {
  374. mcompat_temp_s *temp_ctrl = fan_temp_ctrl->mcompat_temp;
  375. //get max temperature value for all chain
  376. int temp_hi = 0;
  377. if (temp_ctrl[spi_id].temp_highest[0] && temp_ctrl[spi_id].temp_highest[1] && temp_ctrl[spi_id].temp_highest[2])
  378. {
  379. temp_hi = temp_ctrl[spi_id].temp_highest[0] > temp_ctrl[spi_id].temp_highest[1] ? temp_ctrl[spi_id].temp_highest[1]:temp_ctrl[spi_id].temp_highest[0];
  380. temp_hi = temp_hi > temp_ctrl[spi_id].temp_highest[2] ? temp_ctrl[spi_id].temp_highest[2]:temp_hi;
  381. }else{
  382. if (temp_ctrl[spi_id].temp_highest[0] && temp_ctrl[spi_id].temp_highest[1])
  383. temp_hi = temp_ctrl[spi_id].temp_highest[0] > temp_ctrl[spi_id].temp_highest[1] ? temp_ctrl[spi_id].temp_highest[1]:temp_ctrl[spi_id].temp_highest[0];
  384. else if (temp_ctrl[spi_id].temp_highest[0] && temp_ctrl[spi_id].temp_highest[2])
  385. temp_hi = temp_ctrl[spi_id].temp_highest[0] > temp_ctrl[spi_id].temp_highest[2] ? temp_ctrl[spi_id].temp_highest[2]:temp_ctrl[spi_id].temp_highest[0];
  386. else if (temp_ctrl[spi_id].temp_highest[1] && temp_ctrl[spi_id].temp_highest[2])
  387. temp_hi = temp_ctrl[spi_id].temp_highest[1] > temp_ctrl[spi_id].temp_highest[2] ? temp_ctrl[spi_id].temp_highest[2]:temp_ctrl[spi_id].temp_highest[1];
  388. }
  389. temp_ctrl[spi_id].final_temp_hi = temp_hi;
  390. //get min temperature value for all chain
  391. int temp_lo = 0;
  392. if (temp_ctrl[spi_id].temp_lowest[0] && temp_ctrl[spi_id].temp_lowest[1] && temp_ctrl[spi_id].temp_lowest[2])
  393. {
  394. temp_lo = temp_ctrl[spi_id].temp_lowest[0] < temp_ctrl[spi_id].temp_lowest[1] ? temp_ctrl[spi_id].temp_lowest[1]:temp_ctrl[spi_id].temp_lowest[0];
  395. temp_lo = temp_lo < temp_ctrl[spi_id].temp_lowest[2] ? temp_ctrl[spi_id].temp_lowest[2]:temp_lo;
  396. }else{
  397. if (temp_ctrl[spi_id].temp_lowest[0] && temp_ctrl[spi_id].temp_lowest[1])
  398. temp_lo = temp_ctrl[spi_id].temp_lowest[0] < temp_ctrl[spi_id].temp_lowest[1] ? temp_ctrl[spi_id].temp_lowest[1]:temp_ctrl[spi_id].temp_lowest[0];
  399. else if (temp_ctrl[spi_id].temp_lowest[0] && temp_ctrl[spi_id].temp_lowest[2])
  400. temp_lo = temp_ctrl[spi_id].temp_lowest[0] < temp_ctrl[spi_id].temp_lowest[2] ? temp_ctrl[spi_id].temp_lowest[2]:temp_ctrl[spi_id].temp_lowest[0];
  401. else if (temp_ctrl[spi_id].temp_lowest[1] && temp_ctrl[spi_id].temp_lowest[2])
  402. temp_lo = temp_ctrl[spi_id].temp_lowest[1] < temp_ctrl[spi_id].temp_lowest[2] ? temp_ctrl[spi_id].temp_lowest[2]:temp_ctrl[spi_id].temp_lowest[1];
  403. }
  404. temp_ctrl[spi_id].final_temp_lo = temp_lo;
  405. //get average temperature value for all chain
  406. applog(LOG_INFO,"temp:%d,%d,%d",temp_ctrl[spi_id].final_temp_hi,temp_ctrl[spi_id].final_temp_avg,temp_ctrl[spi_id].final_temp_lo);
  407. return ;
  408. }
  409. void mcompat_fan_speed_set(unsigned char fan_id, int speed)
  410. {
  411. int type = 0;
  412. int duty = 0;
  413. type = misc_get_vid_type();
  414. switch(type)
  415. {
  416. case MCOMPAT_LIB_VID_VID_TYPE:
  417. {
  418. duty = 100-speed;
  419. break;
  420. }
  421. case MCOMPAT_LIB_VID_I2C_TYPE:
  422. case MCOMPAT_LIB_VID_UART_TYPE:
  423. {
  424. duty = speed;
  425. break;
  426. }
  427. case MCOMPAT_LIB_VID_GPIO_I2C_TYPE:
  428. {
  429. applog(LOG_ERR, "%s,%d:no impl type:MCOMPAT_LIB_VID_GPIO_I2C_TYPE.", __FILE__, __LINE__);
  430. break;
  431. }
  432. default:
  433. {
  434. applog(LOG_ERR, "%s,%d:err vid type:%d.", __FILE__, __LINE__, type);
  435. break;
  436. }
  437. }
  438. mcompat_set_pwm(fan_id, ASIC_MCOMPAT_FAN_PWM_FREQ_TARGET, duty);
  439. mcompat_set_pwm(fan_id+1, ASIC_MCOMPAT_FAN_PWM_FREQ_TARGET, duty);
  440. }
  441. void mcompat_fan_temp_init(unsigned char fan_id, mcompat_temp_config_s default_config)
  442. {
  443. g_temp_hi_thr = default_config.temp_hi_thr;
  444. g_temp_lo_thr = default_config.temp_lo_thr;
  445. g_temp_start_thr = default_config.temp_start_thr;
  446. g_dangerous_temp = default_config.dangerous_stat_temp;
  447. g_work_temp = default_config.work_temp;
  448. g_fan_speed = default_config.default_fan_speed;
  449. applog(LOG_INFO,"hi %d,lo %d,st %d,da %d, wk %d",g_temp_hi_thr,g_temp_lo_thr,g_temp_start_thr,g_dangerous_temp,g_work_temp);
  450. mcompat_fan_speed_set(fan_id,g_fan_speed);
  451. applog(LOG_INFO, "pwm step:%d.", ASIC_MCOMPAT_FAN_PWM_STEP);
  452. applog(LOG_INFO, "duty max: %d.", ASIC_MCOMPAT_FAN_PWM_DUTY_MAX);
  453. applog(LOG_INFO, "targ freq:%d.", ASIC_MCOMPAT_FAN_PWM_FREQ_TARGET);
  454. applog(LOG_INFO, "freq rate:%d.", ASIC_MCOMPAT_FAN_PWM_FREQ);
  455. applog(LOG_INFO, "fan speed thrd:%d.", ASIC_MCOMPAT_FAN_TEMP_MAX_THRESHOLD);
  456. applog(LOG_INFO, "fan up thrd:%d.", ASIC_MCOMPAT_FAN_TEMP_UP_THRESHOLD);
  457. applog(LOG_INFO, "fan down thrd:%d.", ASIC_MCOMPAT_FAN_TEMP_DOWN_THRESHOLD);
  458. }
  459. void mcompat_fan_speed_update_hub(mcompat_fan_temp_s *fan_temp)
  460. {
  461. static int cnt = 0;
  462. int i = 0;
  463. int temp_hi = g_temp_lo_thr; //fan_temp->temp_highest[0];
  464. if (fan_temp->speed == 0)
  465. fan_temp->speed = g_fan_speed;
  466. for(i=0; i<g_chain_num; i++)
  467. {
  468. if (hub_get_plug(i))
  469. continue;
  470. mcompat_deal_temp(i,fan_temp);
  471. if ((fan_temp->mcompat_temp[i].final_temp_hi > g_temp_lo_thr) || (fan_temp->mcompat_temp[i].final_temp_hi < g_temp_hi_thr) || \
  472. (fan_temp->mcompat_temp[i].final_temp_avg > g_temp_lo_thr) || (fan_temp->mcompat_temp[i].final_temp_avg < g_temp_hi_thr) || \
  473. (fan_temp->mcompat_temp[i].final_temp_lo > g_temp_lo_thr) || (fan_temp->mcompat_temp[i].final_temp_lo < g_temp_hi_thr) )
  474. {
  475. applog(LOG_ERR,"Notice!!! Error temperature for chain %d,h:%d,a:%d,l:%d", i, \
  476. fan_temp->mcompat_temp[i].final_temp_hi,fan_temp->mcompat_temp[i].final_temp_avg,fan_temp->mcompat_temp[i].final_temp_lo);
  477. continue ;
  478. }
  479. if (fan_temp->mcompat_temp[i].final_temp_hi < g_dangerous_temp)
  480. set_warn(i);
  481. if (temp_hi > fan_temp->mcompat_temp[i].final_temp_hi)
  482. temp_hi = fan_temp->mcompat_temp[i].final_temp_hi;
  483. }
  484. if ((temp_hi == g_temp_lo_thr)||(temp_hi == g_temp_hi_thr))
  485. {
  486. mcompat_fan_speed_set(0,100);
  487. return ;
  488. }
  489. int delt_temp = abs(g_work_temp - temp_hi);
  490. int delt_speed = abs(temp_hi - fan_temp->last_fan_temp);
  491. applog(LOG_INFO,"Hi temp %d,delt_temp %d,delt_speed %d",temp_hi,delt_temp, delt_speed);
  492. if (delt_temp > 3)
  493. {
  494. if ((delt_speed < 2) && (cnt < 3))
  495. {
  496. cnt ++;
  497. return;
  498. }
  499. cnt = 0;
  500. if (temp_hi > g_work_temp)
  501. {
  502. fan_temp->speed = (fan_temp->speed - 5)>10?(fan_temp->speed - 5):10;
  503. //applog(LOG_ERR, "%s +:arv:%5.2f, lest:%5.2f, hest:%5.2f, speed:%d%%", __func__, arvarge_f, lowest_f, highest_f, 100 - fan_ctrl->duty);
  504. }else if (temp_hi < g_work_temp)
  505. {
  506. fan_temp->speed = (fan_temp->speed + 5)<100?(fan_temp->speed + 5):100;
  507. //applog(LOG_ERR, "%s +:arv:%5.2f, lest:%5.2f, hest:%5.2f, speed:%d%%", __func__, arvarge_f, lowest_f, highest_f, 100 - fan_ctrl->duty);
  508. }
  509. }
  510. //applog(LOG_ERR,"temp_highest %d, fan speed %d,last fan id: %d",fan_temp->temp_highest[chain_id],fan_speed[fan_temp->last_fan_temp],fan_temp->last_fan_temp);
  511. if (fan_temp->speed != fan_temp->last_fan_speed)
  512. {
  513. fan_temp->last_fan_speed = fan_temp->speed;
  514. fan_temp->last_fan_temp = temp_hi;
  515. mcompat_fan_speed_set(0,fan_temp->speed);
  516. }
  517. }
  518. MCOMPAT_GPIO_T s_gpio_ops;
  519. MCOMPAT_GPIO_T* s_gpio_ops_p = &s_gpio_ops;
  520. void init_mcompat_gpio(void)
  521. {
  522. memset(&s_gpio_ops, 0, sizeof(s_gpio_ops));
  523. switch(g_platform)
  524. {
  525. case PLATFORM_ZYNQ_SPI_G9:
  526. case PLATFORM_ZYNQ_SPI_G19:
  527. init_spi_gpio(g_chain_num);
  528. s_gpio_ops_p->set_power_en = spi_set_power_en;
  529. s_gpio_ops_p->set_start_en = spi_set_start_en;
  530. s_gpio_ops_p->set_reset = spi_set_reset;
  531. s_gpio_ops_p->set_led = spi_set_led;
  532. s_gpio_ops_p->get_plug = spi_get_plug;
  533. s_gpio_ops_p->set_vid = spi_set_vid;
  534. break;
  535. case PLATFORM_ZYNQ_HUB_G9:
  536. case PLATFORM_ZYNQ_HUB_G19:
  537. init_hub_gpio();
  538. s_gpio_ops_p->set_power_en = hub_set_power_en;
  539. s_gpio_ops_p->set_start_en = hub_set_start_en;
  540. s_gpio_ops_p->set_reset = hub_set_reset;
  541. s_gpio_ops_p->set_led = hub_set_led;
  542. s_gpio_ops_p->get_plug = hub_get_plug;
  543. s_gpio_ops_p->set_vid = hub_set_vid;
  544. s_gpio_ops_p->get_button = hub_get_button;
  545. s_gpio_ops_p->set_green_led = hub_set_green_led;
  546. s_gpio_ops_p->set_red_led = hub_set_red_led;
  547. break;
  548. case PLATFORM_ORANGE_PI:
  549. s_gpio_ops_p->set_power_en = opi_set_power_en;
  550. s_gpio_ops_p->set_start_en = opi_set_start_en;
  551. s_gpio_ops_p->set_reset = opi_set_reset;
  552. s_gpio_ops_p->set_led = opi_set_led;
  553. s_gpio_ops_p->get_plug = opi_get_plug;
  554. s_gpio_ops_p->set_vid = opi_set_vid;
  555. break;
  556. default:
  557. applog(LOG_ERR, "the platform is undefined !!!");
  558. break;
  559. }
  560. }
  561. void exit_mcompat_gpio(void)
  562. {
  563. switch(g_platform)
  564. {
  565. case PLATFORM_ZYNQ_SPI_G9:
  566. case PLATFORM_ZYNQ_SPI_G19:
  567. exit_spi_gpio(g_chain_num);
  568. break;
  569. case PLATFORM_ZYNQ_HUB_G9:
  570. case PLATFORM_ZYNQ_HUB_G19:
  571. break;
  572. default:
  573. applog(LOG_ERR, "the platform is undefined !!!");
  574. break;
  575. }
  576. }
  577. void register_mcompat_gpio(MCOMPAT_GPIO_T * ops)
  578. {
  579. if (ops->set_power_en != NULL)
  580. {
  581. s_gpio_ops_p->set_power_en = ops->set_power_en;
  582. }
  583. if (ops->set_start_en != NULL)
  584. {
  585. s_gpio_ops_p->set_start_en = ops->set_start_en;
  586. }
  587. if (ops->set_reset != NULL)
  588. {
  589. s_gpio_ops_p->set_reset = ops->set_reset;
  590. }
  591. if (ops->set_led != NULL)
  592. {
  593. s_gpio_ops_p->set_led = ops->set_led;
  594. }
  595. if (ops->get_plug != NULL)
  596. {
  597. s_gpio_ops_p->get_plug = ops->get_plug;
  598. }
  599. if (ops->get_button != NULL)
  600. {
  601. s_gpio_ops_p->get_button = ops->get_button;
  602. }
  603. if (ops->set_green_led != NULL)
  604. {
  605. s_gpio_ops_p->set_green_led = ops->set_green_led;
  606. }
  607. if (ops->set_red_led != NULL)
  608. {
  609. s_gpio_ops_p->set_red_led = ops->set_red_led;
  610. }
  611. }
  612. void mcompat_set_power_en(unsigned char chain_id, int val)
  613. {
  614. if (s_gpio_ops_p->set_power_en == NULL)
  615. {
  616. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  617. return;
  618. }
  619. s_gpio_ops_p->set_power_en(chain_id, val);
  620. }
  621. void mcompat_set_start_en(unsigned char chain_id, int val)
  622. {
  623. if (s_gpio_ops_p->set_start_en == NULL)
  624. {
  625. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  626. return;
  627. }
  628. s_gpio_ops_p->set_start_en(chain_id, val);
  629. }
  630. bool mcompat_set_reset(unsigned char chain_id, int val)
  631. {
  632. if (s_gpio_ops_p->set_reset == NULL) {
  633. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  634. return false;
  635. }
  636. return s_gpio_ops_p->set_reset(chain_id, val);
  637. }
  638. void mcompat_set_led(unsigned char chain_id, int val)
  639. {
  640. if (s_gpio_ops_p->set_led == NULL)
  641. {
  642. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  643. return;
  644. }
  645. s_gpio_ops_p->set_led(chain_id, val);
  646. }
  647. int mcompat_get_plug(unsigned char chain_id)
  648. {
  649. if (s_gpio_ops_p->get_plug == NULL)
  650. {
  651. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  652. return -1;
  653. }
  654. return s_gpio_ops_p->get_plug(chain_id);
  655. }
  656. bool mcompat_set_vid(unsigned char chain_id, int val)
  657. {
  658. if (s_gpio_ops_p->set_vid == NULL)
  659. {
  660. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  661. return false;
  662. }
  663. applog(LOG_DEBUG, "set chain %d vid value %d", chain_id, val);
  664. return s_gpio_ops_p->set_vid(chain_id, val);
  665. }
  666. bool mcompat_set_vid_by_step(unsigned char chain_id, int start_vid, int target_vid)
  667. {
  668. int i;
  669. if (target_vid > VID_MAX)
  670. target_vid = VID_MAX;
  671. else if( target_vid < VID_MIN)
  672. target_vid = VID_MIN;
  673. if (target_vid > start_vid) {
  674. // increase vid step by step
  675. for (i = start_vid + 1; i <= target_vid; ++i) {
  676. mcompat_set_vid(chain_id, i);
  677. applog(LOG_NOTICE, "set_vid_value_G19: %d", i);
  678. }
  679. } else if (target_vid < start_vid) {
  680. // decrease vid step by step
  681. for (i = start_vid - 1; i >= target_vid; --i) {
  682. mcompat_set_vid(chain_id, i);
  683. applog(LOG_NOTICE, "set_vid_value_G19: %d", i);
  684. }
  685. }
  686. return true;
  687. }
  688. int mcompat_get_button(void)
  689. {
  690. if (s_gpio_ops_p->get_button == NULL)
  691. {
  692. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  693. return -1;
  694. }
  695. return s_gpio_ops_p->get_button();
  696. }
  697. void mcompat_set_green_led(int mode)
  698. {
  699. if (s_gpio_ops_p->set_green_led == NULL)
  700. {
  701. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  702. return;
  703. }
  704. s_gpio_ops_p->set_green_led(mode);
  705. }
  706. void mcompat_set_red_led(int mode)
  707. {
  708. if (s_gpio_ops_p->set_red_led == NULL)
  709. {
  710. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  711. return;
  712. }
  713. s_gpio_ops_p->set_red_led(mode);
  714. }
  715. MCOMPAT_PWM_T s_pwm_ops;
  716. MCOMPAT_PWM_T* s_pwm_ops_p = &s_pwm_ops;
  717. void init_mcompat_pwm(void)
  718. {
  719. memset(&s_pwm_ops, 0, sizeof(s_pwm_ops));
  720. switch(g_platform)
  721. {
  722. case PLATFORM_ZYNQ_SPI_G9:
  723. case PLATFORM_ZYNQ_SPI_G19:
  724. s_pwm_ops_p->set_pwm = zynq_set_pwm;
  725. break;
  726. case PLATFORM_ZYNQ_HUB_G9:
  727. case PLATFORM_ZYNQ_HUB_G19:
  728. s_pwm_ops_p->set_pwm = hub_set_pwm;
  729. break;
  730. case PLATFORM_ORANGE_PI:
  731. s_pwm_ops_p->set_pwm = opi_set_pwm;
  732. break;
  733. default:
  734. applog(LOG_ERR, "the platform is undefined !!!");
  735. break;
  736. }
  737. }
  738. void exit_mcompat_pwm(void)
  739. {
  740. switch(g_platform)
  741. {
  742. case PLATFORM_ZYNQ_SPI_G9:
  743. case PLATFORM_ZYNQ_SPI_G19:
  744. break;
  745. case PLATFORM_ZYNQ_HUB_G9:
  746. case PLATFORM_ZYNQ_HUB_G19:
  747. break;
  748. default:
  749. applog(LOG_ERR, "the platform is undefined !!!");
  750. break;
  751. }
  752. }
  753. void register_mcompat_pwm(MCOMPAT_PWM_T * ops)
  754. {
  755. if (ops->set_pwm != NULL)
  756. {
  757. s_pwm_ops_p->set_pwm = ops->set_pwm;
  758. }
  759. }
  760. void mcompat_set_pwm(unsigned char fan_id, int frequency, int duty)
  761. {
  762. if (s_pwm_ops_p->set_pwm == NULL)
  763. {
  764. applog(LOG_ERR, "%s not register !", __FUNCTION__);
  765. return;
  766. }
  767. applog(LOG_INFO, "set fan[%d] pwm freq[%d] duty[%d] ", fan_id, frequency, duty);
  768. s_pwm_ops_p->set_pwm(fan_id, frequency, duty);
  769. }
  770. int mcompat_temp_to_centigrade(int temp)
  771. {
  772. // return 0 if temp is a invalid value
  773. // if (temp == 0)
  774. // return 0;
  775. switch(g_miner_type)
  776. {
  777. case MCOMPAT_LIB_MINER_TYPE_T1:
  778. return (588.0f - temp) * 2 / 3 + 0.5f; // T1
  779. case MCOMPAT_LIB_MINER_TYPE_T3:
  780. case MCOMPAT_LIB_MINER_TYPE_D11:
  781. case MCOMPAT_LIB_MINER_TYPE_D12:
  782. default:
  783. return (595.0f - temp) * 2 / 3 + 0.5f; // T3 D11 D12
  784. }
  785. }
  786. bool mcompat_get_chain_temp(unsigned char chain_id, c_temp *chain_tmp)
  787. {
  788. uint32_t reg_val;
  789. uint32_t tmp_val0;
  790. uint32_t tmp_val1;
  791. uint32_t tmp_val2;
  792. int timeout = 1000;
  793. // enable auto 0a
  794. enable_auto_cmd0a(chain_id, 100, 33, 24, 0, 0);
  795. do {
  796. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP * chain_id + AUTO_CMD0A_REG4_ADDR);
  797. timeout--;
  798. cgsleep_ms(1); // usleep(1);
  799. } while(timeout && !((reg_val >> 24) & 0x1));
  800. if(!timeout)
  801. return false;
  802. // temp_lo:
  803. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP * chain_id + AUTO_CMD0A_REG2_ADDR);
  804. tmp_val0 = mcompat_temp_to_centigrade(reg_val & 0x3ff);
  805. tmp_val1 = mcompat_temp_to_centigrade((reg_val >> 10) & 0x3ff);
  806. tmp_val2 = mcompat_temp_to_centigrade((reg_val >> 20) & 0x3ff);
  807. // applog(LOG_DEBUG, "REG2: %d, %d, %d", tmp_val0, tmp_val1, tmp_val2);
  808. chain_tmp->tmp_lo = (tmp_val0 + tmp_val1 + tmp_val2) / 3;
  809. // chain_tmp->tmp_lo = tmp_val1;
  810. // temp_hi:
  811. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP * chain_id + AUTO_CMD0A_REG3_ADDR);
  812. tmp_val0 = mcompat_temp_to_centigrade(reg_val & 0x3ff);
  813. tmp_val1 = mcompat_temp_to_centigrade((reg_val >> 10) & 0x3ff);
  814. tmp_val2 = mcompat_temp_to_centigrade((reg_val >> 20) & 0x3ff);
  815. // applog(LOG_DEBUG, "REG3: %d, %d, %d", tmp_val0, tmp_val1, tmp_val2);
  816. chain_tmp->tmp_hi = (tmp_val0 + tmp_val1 + tmp_val2) / 3;
  817. // chain_tmp->tmp_hi = tmp_val1;
  818. // temp_avg:
  819. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP * chain_id + AUTO_CMD0A_REG4_ADDR);
  820. tmp_val1 = mcompat_temp_to_centigrade(2 * (reg_val & 0xffff) / g_chip_num);
  821. // applog(LOG_DEBUG, "REG4: %d / %d", tmp_val1, g_chip_num);
  822. chain_tmp->tmp_avg = tmp_val1;
  823. // disable auto 0a
  824. disable_auto_cmd0a(chain_id, 100, 33, 24, 0, 0);
  825. timeout = 1000;
  826. do {
  827. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP * chain_id + AUTO_CMD0A_REG4_ADDR);
  828. timeout--;
  829. cgsleep_ms(1); // usleep(1);
  830. } while(timeout && !((reg_val >> 24) & 0x1));
  831. if(!timeout)
  832. return false;
  833. return true;
  834. }
  835. void mcompat_get_chip_temp(int chain_id, int *chip_temp)
  836. {
  837. int chip_id;
  838. unsigned char reg[REG_LENGTH] = {0};
  839. for (chip_id = 1; chip_id <= g_chip_num; chip_id++) {
  840. if (!mcompat_cmd_read_register(chain_id, chip_id, reg, REG_LENGTH)) {
  841. applog(LOG_ERR, "failed to read temperature for chain%d chip%d",
  842. chain_id, chip_id);
  843. chip_temp[chip_id - 1] = mcompat_temp_to_centigrade(0);
  844. break;
  845. } else
  846. chip_temp[chip_id - 1] = mcompat_temp_to_centigrade(0x000003ff & ((reg[7] << 8) | reg[8]));
  847. }
  848. }
  849. #define MCOMPAT_WATCHDOG_DEV ("/dev/watchdog0")
  850. static int s_watchdog_fd = 0;
  851. void mcompat_watchdog_keep_alive(void)
  852. {
  853. int dummy = 0;
  854. ioctl(s_watchdog_fd, WDIOC_KEEPALIVE, &dummy);
  855. }
  856. void mcompat_watchdog_open(void)
  857. {
  858. s_watchdog_fd = open(MCOMPAT_WATCHDOG_DEV, O_WRONLY);
  859. if (-1 == s_watchdog_fd)
  860. {
  861. applog(LOG_ERR, "%s watchdog device can't be enabled.", MCOMPAT_WATCHDOG_DEV);
  862. }
  863. }
  864. void mcompat_watchdog_set_timeout(int timeout)
  865. {
  866. ioctl(s_watchdog_fd, WDIOC_SETTIMEOUT, &timeout);
  867. }
  868. void mcompat_watchdog_close(void)
  869. {
  870. close(s_watchdog_fd);
  871. }
  872. #define SOCK_SIZE (65535)
  873. #define SOCK_ERR_MSG strerror(errno)
  874. #define MUL_COEF (1248)
  875. int mcompat_get_shell_cmd_rst(char *cmd, char *result, int size)
  876. {
  877. char buffer[1024] = {0};
  878. int offset = 0;
  879. int len;
  880. FILE *fp = NULL;
  881. fp = popen(cmd, "r");
  882. if (NULL == fp)
  883. {
  884. applog(LOG_ERR, "failed to open pipe for command %s", cmd);
  885. return 0;
  886. }
  887. while(fgets(buffer, sizeof(buffer), fp) != NULL)
  888. {
  889. len = strlen(buffer);
  890. if (offset + len < size)
  891. {
  892. strcpy(result + offset, buffer);
  893. offset += len;
  894. }
  895. else
  896. {
  897. strncpy(result + offset, buffer, size - offset);
  898. offset = size;
  899. break;
  900. }
  901. }
  902. applog(LOG_DEBUG, "command result(%d): %s", offset, result);
  903. return offset;
  904. }
  905. int misc_call_api(char *command, char *host, short int port)
  906. {
  907. struct sockaddr_in serv;
  908. int sock = 0;
  909. int ret = 0;
  910. int n = 0;
  911. char *buf = NULL;
  912. size_t len = SOCK_SIZE;
  913. size_t p = 0;
  914. sock = socket(AF_INET, SOCK_STREAM, 0);
  915. if (sock < 0)
  916. {
  917. printf("Socket initialisation failed: %s", SOCK_ERR_MSG);
  918. return -1;
  919. }
  920. memset(&serv, 0, sizeof(serv));
  921. serv.sin_family = AF_INET;
  922. serv.sin_addr.s_addr = inet_addr(host);
  923. serv.sin_port = htons(port);
  924. if (connect(sock, (struct sockaddr *)&serv, sizeof(struct sockaddr)) < 0)
  925. {
  926. printf("Socket connect failed: %s", SOCK_ERR_MSG);
  927. return -1;
  928. }
  929. n = send(sock, command, strlen(command), 0);
  930. if (n < 0)
  931. {
  932. printf("Send failed: %s", SOCK_ERR_MSG);
  933. ret = -1;
  934. }
  935. else
  936. {
  937. buf = malloc(len+1);
  938. if (!buf)
  939. {
  940. printf("Err: OOM (%d)", (int)(len+1));
  941. return -1;
  942. }
  943. while(1)
  944. {
  945. if ((len - p) < 1)
  946. {
  947. len += SOCK_SIZE;
  948. buf = realloc(buf, len+1);
  949. if (!buf)
  950. {
  951. printf("Err: OOM (%d)", (int)(len+1));
  952. return -1;
  953. }
  954. }
  955. n = recv(sock, &buf[p], len - p , 0);
  956. if (n < 0)
  957. {
  958. printf("Recv failed: %s", SOCK_ERR_MSG);
  959. ret = -1;
  960. break;
  961. }
  962. if (0 == n)
  963. {
  964. break;
  965. }
  966. p += n;
  967. }
  968. buf[p] = '\0';
  969. printf("%s", buf);
  970. free(buf);
  971. buf = NULL;
  972. }
  973. close(sock);
  974. return ret;
  975. }
  976. bool misc_tcp_is_ok(char *host, short int port)
  977. {
  978. struct sockaddr_in serv;
  979. int sock = 0;
  980. int mode = 0;
  981. bool ret = false;
  982. sock = socket(AF_INET, SOCK_STREAM, 0);
  983. if (sock < 0)
  984. {
  985. printf("Socket initialisation failed: %s", SOCK_ERR_MSG);
  986. return -1;
  987. }
  988. ioctl(sock, FIONBIO, &mode);
  989. memset(&serv, 0, sizeof(serv));
  990. serv.sin_family = AF_INET;
  991. serv.sin_addr.s_addr = inet_addr(host);
  992. serv.sin_port = htons(port);
  993. ret = false;
  994. if (0 == connect(sock, (struct sockaddr *)&serv, sizeof(struct sockaddr)))
  995. {
  996. ret = true;
  997. }
  998. close(sock);
  999. return ret;
  1000. }
  1001. char *misc_trim(char *str)
  1002. {
  1003. char *ptr;
  1004. while (isspace(*str))
  1005. {
  1006. str++;
  1007. }
  1008. ptr = strchr(str, '\0');
  1009. while (ptr-- > str)
  1010. {
  1011. if (isspace(*ptr))
  1012. {
  1013. *ptr = '\0';
  1014. }
  1015. }
  1016. return str;
  1017. }
  1018. int misc_get_board_version(void)
  1019. {
  1020. FILE* fd = NULL;
  1021. char buffer[64] = {'\0'};
  1022. int version = MCOMPAT_LIB_HARDWARE_VERSION_ERR;
  1023. fd = fopen(MCOMPAT_LIB_HARDWARE_VERSION_FILE, "r");
  1024. if (fd == NULL)
  1025. {
  1026. applog(LOG_ERR, "open hwver file:%s failed! ", MCOMPAT_LIB_HARDWARE_VERSION_FILE);
  1027. }
  1028. memset(buffer, 0, sizeof(buffer));
  1029. FREAD(buffer, 8, 1, fd);
  1030. fclose(fd);
  1031. if (strstr(buffer, "G9") != NULL)
  1032. {
  1033. version = MCOMPAT_LIB_HARDWARE_VERSION_G9;
  1034. applog(LOG_INFO, "hardware version is G9 ");
  1035. }
  1036. else if (strstr(buffer, "G19") != 0)
  1037. {
  1038. version = MCOMPAT_LIB_HARDWARE_VERSION_G19;
  1039. applog(LOG_INFO, "hardware version is G19 ");
  1040. }
  1041. else
  1042. {
  1043. applog(LOG_ERR, "unknown hardware version:%s! ", buffer);
  1044. }
  1045. return version;
  1046. }
  1047. int misc_get_miner_type(void)
  1048. {
  1049. FILE *fd = NULL;
  1050. char buffer[64] = {'\0'};
  1051. int miner_type = MCOMPAT_LIB_MINER_TYPE_ERR;
  1052. fd = fopen(MCOMPAT_LIB_MINER_TYPE_FILE, "r");
  1053. if (fd == NULL)
  1054. {
  1055. applog(LOG_ERR, "open miner type file:%s failed!", MCOMPAT_LIB_MINER_TYPE_FILE);
  1056. }
  1057. memset(buffer, 0, sizeof(buffer));
  1058. FREAD(buffer, 8, 1, fd);
  1059. fclose(fd);
  1060. if (strstr(buffer, "T1") != NULL)
  1061. {
  1062. miner_type = MCOMPAT_LIB_MINER_TYPE_T1;
  1063. applog(LOG_INFO, "miner type is T1 ");
  1064. }
  1065. else if (strstr(buffer, "T2") != NULL)
  1066. {
  1067. miner_type = MCOMPAT_LIB_MINER_TYPE_T2;
  1068. applog(LOG_INFO, "miner type is T2 ");
  1069. }
  1070. else if (strstr(buffer, "T3") != NULL)
  1071. {
  1072. miner_type = MCOMPAT_LIB_MINER_TYPE_T3;
  1073. applog(LOG_INFO, "miner type is T3 ");
  1074. }
  1075. else if (strstr(buffer, "T4") != NULL)
  1076. {
  1077. miner_type = MCOMPAT_LIB_MINER_TYPE_T4;
  1078. applog(LOG_INFO, "miner type is T4 ");
  1079. }
  1080. else
  1081. {
  1082. applog(LOG_ERR, "unknown miner type:%s! ", buffer);
  1083. }
  1084. return miner_type;
  1085. }
  1086. int misc_get_vid_type(void)
  1087. {
  1088. int val_b9 = 0;
  1089. int val_a10 = 0;
  1090. int val = 0;
  1091. int type = 0;
  1092. zynq_gpio_init(MCOMPAT_CONFIG_B9_GPIO, 1);
  1093. zynq_gpio_init(MCOMPAT_CONFIG_A10_GPIO, 1);
  1094. val_b9 = zynq_gpio_read(MCOMPAT_CONFIG_B9_GPIO);
  1095. val_a10 = zynq_gpio_read(MCOMPAT_CONFIG_A10_GPIO);
  1096. val = val_a10 << 1 | val_b9;
  1097. #if 1
  1098. applog(LOG_INFO, "b9 pin:%d,a10 pin:%d", MCOMPAT_CONFIG_B9_GPIO, MCOMPAT_CONFIG_A10_GPIO);
  1099. applog(LOG_INFO, "b9:%d,a10:%d,val:0x%08x", val_b9, val_a10, val);
  1100. #endif
  1101. switch(val)
  1102. {
  1103. /* xhn */
  1104. case 0x00000003:
  1105. {
  1106. type = MCOMPAT_LIB_VID_VID_TYPE;
  1107. applog(LOG_INFO, "cow vid vid");
  1108. break;
  1109. }
  1110. /* zle*/
  1111. case 0x00000002:
  1112. {
  1113. type = MCOMPAT_LIB_VID_UART_TYPE;
  1114. applog(LOG_INFO, "zl uart vid");
  1115. break;
  1116. }
  1117. /* hnd */
  1118. case 0x00000000:
  1119. {
  1120. type = MCOMPAT_LIB_VID_I2C_TYPE;
  1121. applog(LOG_INFO, "hnd iic vid");
  1122. break;
  1123. }
  1124. /* unused */
  1125. case 0x00000001:
  1126. default:
  1127. {
  1128. type = MCOMPAT_LIB_VID_ERR_TYPE;
  1129. applog(LOG_ERR, "err vid type:b9:%d,a10:%d,val:0x%08x", val_b9, val_a10, val);
  1130. break;
  1131. }
  1132. }
  1133. #if 0
  1134. type = MCOMPAT_LIB_VID_UART_TYPE;
  1135. type = MCOMPAT_LIB_VID_I2C_TYPE;
  1136. #endif
  1137. zynq_gpio_exit(MCOMPAT_CONFIG_B9_GPIO);
  1138. zynq_gpio_exit(MCOMPAT_CONFIG_A10_GPIO);
  1139. return type;
  1140. }
  1141. void misc_system(const char *cmd, char *rst_buf, int buf_size)
  1142. {
  1143. int i = 0;
  1144. FILE *fp = NULL;
  1145. char *go_ptr = NULL;
  1146. char c = 0;
  1147. if (NULL == cmd || NULL == rst_buf || buf_size < 0)
  1148. {
  1149. applog(LOG_ERR, "param error:%s,%s,%d.", cmd, rst_buf, buf_size);
  1150. }
  1151. fp = popen(cmd, "r");
  1152. if (NULL == fp)
  1153. {
  1154. applog(LOG_ERR, "popen error:%s,%s,%d.", cmd, rst_buf, buf_size);
  1155. }
  1156. else
  1157. {
  1158. go_ptr = rst_buf;
  1159. memset(go_ptr, 0, buf_size);
  1160. for(i = 0; i < buf_size; i++)
  1161. {
  1162. c = fgetc(fp);
  1163. if (isprint(c))
  1164. {
  1165. *go_ptr++ = c;
  1166. fprintf(stderr, "%s,%d: %c", __FILE__, __LINE__, c);
  1167. }
  1168. else
  1169. {
  1170. break;
  1171. }
  1172. }
  1173. rst_buf[buf_size-1] = '\0';
  1174. pclose(fp);
  1175. }
  1176. }
  1177. void mcompat_get_chip_volt(int chain_id, int *chip_volt)
  1178. {
  1179. int chip_id;
  1180. unsigned char reg[REG_LENGTH] = {0};
  1181. unsigned int volt = 0;
  1182. for (chip_id = 1; chip_id <= g_chip_num; chip_id++) {
  1183. if(!mcompat_cmd_read_register(chain_id, chip_id, reg, REG_LENGTH)) {
  1184. applog(LOG_ERR, "failed to read voltage for chain%d chip%d",
  1185. chain_id, chip_id);
  1186. chip_volt[chip_id - 1] = 0;
  1187. continue;
  1188. } else {
  1189. cgsleep_ms(2);
  1190. volt = 0x000003ff & ((reg[7] << 8) | reg[8]);
  1191. chip_volt[chip_id - 1] = (volt * MUL_COEF) >> 10;
  1192. }
  1193. }
  1194. }
  1195. void mcompat_configure_tvsensor(int chain_id, int chip_id, bool is_tsensor)
  1196. {
  1197. unsigned char tmp_reg[REG_LENGTH] = {0};
  1198. unsigned char src_reg[REG_LENGTH] = {0};
  1199. unsigned char reg[REG_LENGTH] = {0};
  1200. mcompat_cmd_read_register(chain_id, 0x01, reg,REG_LENGTH);
  1201. memcpy(src_reg,reg,REG_LENGTH);
  1202. mcompat_cmd_write_register(chain_id,chip_id,src_reg,REG_LENGTH);
  1203. cgsleep_ms(1); //usleep(200);
  1204. if (is_tsensor)//configure for tsensor
  1205. {
  1206. reg[7] = (src_reg[7]&0x7f);
  1207. memcpy(tmp_reg,reg,REG_LENGTH);
  1208. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1209. cgsleep_ms(1); //usleep(200);
  1210. reg[7] = (src_reg[7]|0x80);
  1211. memcpy(tmp_reg,reg,REG_LENGTH);
  1212. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1213. cgsleep_ms(1); //usleep(200);
  1214. reg[6] = (src_reg[6]|0x04);
  1215. memcpy(tmp_reg,reg,REG_LENGTH);
  1216. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1217. cgsleep_ms(1); //usleep(200);
  1218. //Step6: high tsadc_en
  1219. reg[7] = (src_reg[7]|0x20);
  1220. memcpy(tmp_reg,reg,REG_LENGTH);
  1221. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1222. cgsleep_ms(1); //usleep(200);
  1223. //Step7: tsadc_ana_reg_9 = 0;tsadc_ana_reg_8 = 0
  1224. reg[5] = (src_reg[5]&0xfc);
  1225. memcpy(tmp_reg,reg,REG_LENGTH);
  1226. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1227. cgsleep_ms(1); //usleep(200);
  1228. //Step8: tsadc_ana_reg_7 = 1;tsadc_ana_reg_1 = 0
  1229. reg[6] = (src_reg[6]&0x7d);
  1230. memcpy(tmp_reg,reg,REG_LENGTH);
  1231. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1232. cgsleep_ms(1); //usleep(200);
  1233. }
  1234. else
  1235. {
  1236. //configure for vsensor
  1237. reg[7] = (src_reg[7]&0x7f);
  1238. memcpy(tmp_reg,reg,REG_LENGTH);
  1239. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1240. cgsleep_ms(1); //usleep(200);
  1241. reg[7] = (src_reg[7]|0x80);
  1242. memcpy(tmp_reg,reg,REG_LENGTH);
  1243. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1244. cgsleep_ms(1); //usleep(200);
  1245. reg[6] = (src_reg[6]|0x04);
  1246. memcpy(tmp_reg,reg,REG_LENGTH);
  1247. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1248. cgsleep_ms(1); //usleep(200);
  1249. //Step6: high tsadc_en
  1250. reg[7] = (src_reg[7]|0x20);
  1251. memcpy(tmp_reg,reg,REG_LENGTH);
  1252. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1253. cgsleep_ms(1); //usleep(200);
  1254. //Step7: tsadc_ana_reg_9 = 0;tsadc_ana_reg_8 = 0
  1255. reg[5] = ((src_reg[5]|0x01)&0xfd);
  1256. memcpy(tmp_reg,reg,REG_LENGTH);
  1257. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1258. cgsleep_ms(1); //usleep(200);
  1259. //Step8: tsadc_ana_reg_7 = 1;tsadc_ana_reg_1 = 0
  1260. reg[6] = ((src_reg[6]|0x02)&0x7f);
  1261. memcpy(tmp_reg,reg,REG_LENGTH);
  1262. mcompat_cmd_write_register(chain_id,chip_id,tmp_reg,REG_LENGTH);
  1263. cgsleep_ms(1); //usleep(200);
  1264. }
  1265. }
  1266. void mcompat_cfg_tsadc_divider(int chain_id,unsigned int pll_clk)
  1267. {
  1268. unsigned int tsadc_divider_tmp;
  1269. unsigned char tsadc_divider;
  1270. unsigned char buffer[64] = {0x02,0x50,0xa0,0x06,0x28,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  1271. unsigned char readbuf[32] = {0};
  1272. tsadc_divider_tmp = (pll_clk/2)*1000/16/650;
  1273. tsadc_divider = (unsigned char)(tsadc_divider_tmp & 0xff);
  1274. buffer[5] = 0x00 | tsadc_divider;
  1275. if (!mcompat_cmd_read_write_reg0d(chain_id, 0x00, buffer, REG_LENGTH, readbuf))
  1276. {
  1277. applog(LOG_DEBUG,"Write t/v sensor Value Failed!");
  1278. }
  1279. else
  1280. {
  1281. applog(LOG_DEBUG,"Write t/v sensor Value Success!");
  1282. }
  1283. }
  1284. double mcompat_get_average_volt(int *volt, int size)
  1285. {
  1286. int i;
  1287. int count = 0;
  1288. int total = 0, max = 0, min = 1000;
  1289. for (i = 0; i < size; i++) {
  1290. if (volt[i] > 0) {
  1291. total += volt[i];
  1292. max = MAX(max, volt[i]);
  1293. min = MIN(min, volt[i]);
  1294. count++;
  1295. }
  1296. }
  1297. if (count > 2)
  1298. return (double) (total - max - min) / (count - 2);
  1299. else
  1300. return 0;
  1301. }
  1302. /* Adjust vid till we are just above volt_target. We should have already set
  1303. * vid_start before calling this function. */
  1304. int mcompat_find_chain_vid(int chain_id, int chip_num, int vid_start, double volt_target)
  1305. {
  1306. int chip_volt[MCOMPAT_CONFIG_MAX_CHIP_NUM] = {0};
  1307. int vid = vid_start;
  1308. double volt_avg;
  1309. mcompat_cfg_tsadc_divider(chain_id, 120);
  1310. cgsleep_ms(1);
  1311. mcompat_configure_tvsensor(chain_id, CMD_ADDR_BROADCAST, 0);
  1312. cgsleep_ms(1);
  1313. applog(LOG_NOTICE, "chain%d find_chain_vid: start_vid = %d, target_volt = %.1f",
  1314. chain_id, vid_start, volt_target);
  1315. mcompat_get_chip_volt(chain_id, chip_volt);
  1316. volt_avg = mcompat_get_average_volt(chip_volt, chip_num);
  1317. applog(LOG_NOTICE, "Chain %d VID %d voltage %.1f", chain_id, vid, volt_avg);
  1318. /* Go down voltage till we're below the target */
  1319. while (volt_avg >= volt_target) {
  1320. if (vid >= VID_MAX) {
  1321. applog(LOG_WARNING, "Chain %d unable to get below target voltage %.1f by VID %d",
  1322. chain_id, volt_target, vid);
  1323. break;
  1324. }
  1325. vid++;
  1326. mcompat_set_vid(chain_id, vid);
  1327. mcompat_get_chip_volt(chain_id, chip_volt);
  1328. volt_avg = mcompat_get_average_volt(chip_volt, chip_num);
  1329. applog(LOG_NOTICE, "Chain %d VID %d voltage %.1f", chain_id, vid, volt_avg);
  1330. }
  1331. cgsleep_ms(500);
  1332. mcompat_get_chip_volt(chain_id, chip_volt);
  1333. volt_avg = mcompat_get_average_volt(chip_volt, chip_num);
  1334. applog(LOG_NOTICE, "Chain %d VID %d voltage %.1f", chain_id, vid, volt_avg);
  1335. /* Now go down VID till we're above the target, final point should
  1336. * be closest without going below voltage */
  1337. while (volt_avg < volt_target) {
  1338. if (vid <= VID_MIN) {
  1339. applog(LOG_WARNING, "Chain %d unable to get above target voltage %.1f by VID %d",
  1340. chain_id, volt_target, vid);
  1341. break;
  1342. }
  1343. vid--;
  1344. mcompat_set_vid(chain_id, vid);
  1345. cgsleep_ms(500);
  1346. mcompat_get_chip_volt(chain_id, chip_volt);
  1347. volt_avg = mcompat_get_average_volt(chip_volt, chip_num);
  1348. applog(LOG_NOTICE, "Chain %d VID %d voltage %.1f", chain_id, vid, volt_avg);
  1349. }
  1350. mcompat_configure_tvsensor(chain_id, CMD_ADDR_BROADCAST, 1);
  1351. return vid;
  1352. }
  1353. #define IOCTL_SET_VAL_0 _IOR(MAGIC_NUM, 0, char *)
  1354. #define IOCTL_SET_VALUE_0 _IOR(MAGIC_NUM, 0, char *)
  1355. #define IOCTL_SET_CHAIN_0 _IOR(MAGIC_NUM, 1, char *)
  1356. #define BUF_MAX (256)
  1357. #define SYSFS_GPIO_EXPORT ("/sys/class/gpio/export")
  1358. #define SYSFS_GPIO_DIR_STR ("/sys/class/gpio/gpio%d/direction")
  1359. #define SYSFS_GPIO_VAL_STR ("/sys/class/gpio/gpio%d/value")
  1360. #define SYSFS_GPIO_DIR_OUT ("out")
  1361. #define SYSFS_GPIO_DIR_IN ("in")
  1362. #define SYSFS_GPIO_VAL_LOW ("0")
  1363. #define SYSFS_GPIO_VAL_HIGH ("1")
  1364. void zynq_gpio_init(int pin, int dir)
  1365. {
  1366. int fd = 0;
  1367. ssize_t write_bytes = 0;
  1368. char fvalue[BUF_MAX] = {'\0'};
  1369. char fpath[BUF_MAX] = {'\0'};
  1370. fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
  1371. if (-1 == fd)
  1372. {
  1373. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1374. }
  1375. memset(fvalue, 0, sizeof(fvalue));
  1376. sprintf(fvalue, "%d", pin);
  1377. write_bytes = write(fd, fvalue, strlen(fvalue));
  1378. if (-1 == write_bytes)
  1379. {
  1380. if (EBUSY == errno)
  1381. {
  1382. close(fd);
  1383. return;
  1384. }
  1385. else
  1386. {
  1387. applog(LOG_ERR, "%s,%d: %d,%s.", __FILE__, __LINE__, errno, strerror(errno));
  1388. }
  1389. }
  1390. close(fd);
  1391. memset(fpath, 0, sizeof(fpath));
  1392. sprintf(fpath, SYSFS_GPIO_DIR_STR, pin);
  1393. fd = open(fpath, O_WRONLY);
  1394. if (-1 == fd)
  1395. {
  1396. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1397. }
  1398. if (0 == dir)
  1399. {
  1400. write_bytes = write(fd, SYSFS_GPIO_DIR_OUT, sizeof(SYSFS_GPIO_DIR_OUT));
  1401. if (-1 == write_bytes)
  1402. {
  1403. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1404. }
  1405. }
  1406. else
  1407. {
  1408. write_bytes = write(fd, SYSFS_GPIO_DIR_IN, sizeof(SYSFS_GPIO_DIR_IN));
  1409. if (-1 == write_bytes)
  1410. {
  1411. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1412. }
  1413. }
  1414. close(fd);
  1415. return;
  1416. }
  1417. static bool zynq_gpio_write(int pin, int val)
  1418. {
  1419. int fd = 0;
  1420. ssize_t write_bytes = 0;
  1421. char fpath[BUF_MAX] = {'\0'};
  1422. bool ret = false;
  1423. memset(fpath, 0, sizeof(fpath));
  1424. sprintf(fpath, SYSFS_GPIO_VAL_STR, pin);
  1425. fd = open(fpath, O_WRONLY);
  1426. if (-1 == fd) {
  1427. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1428. goto out;
  1429. }
  1430. if (0 == val) {
  1431. write_bytes = write(fd, SYSFS_GPIO_VAL_LOW, sizeof(SYSFS_GPIO_VAL_LOW));
  1432. if (-1 == write_bytes) {
  1433. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1434. goto out_close;
  1435. }
  1436. } else {
  1437. write_bytes = write(fd, SYSFS_GPIO_VAL_HIGH, sizeof(SYSFS_GPIO_VAL_HIGH));
  1438. if (-1 == write_bytes) {
  1439. applog(LOG_ERR, "%s,%d: %s,%s.", __FILE__, __LINE__, fpath, strerror(errno));
  1440. goto out_close;
  1441. }
  1442. }
  1443. ret = true;
  1444. out_close:
  1445. close(fd);
  1446. out:
  1447. return ret;
  1448. }
  1449. int zynq_gpio_read(int pin)
  1450. {
  1451. int fd = 0;
  1452. int val = 0;
  1453. ssize_t read_bytes = 0;
  1454. char fpath[BUF_MAX] = {'\0'};
  1455. char fvalue[BUF_MAX] = {'\0'};
  1456. memset(fpath, 0, sizeof(fpath));
  1457. sprintf(fpath, SYSFS_GPIO_VAL_STR, pin);
  1458. fd = open(fpath, O_RDONLY);
  1459. if (-1 == fd)
  1460. {
  1461. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1462. }
  1463. memset(fvalue, 0, sizeof(fvalue));
  1464. read_bytes = read(fd, fvalue, 1);
  1465. if (-1 == read_bytes)
  1466. {
  1467. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1468. }
  1469. close(fd);
  1470. if ('0' == fvalue[0])
  1471. {
  1472. val = 0;
  1473. }
  1474. else if ('1' == fvalue[0])
  1475. {
  1476. val = 1;
  1477. }
  1478. else
  1479. {
  1480. val = -1;
  1481. }
  1482. return val;
  1483. }
  1484. void zynq_gpio_exit(int __maybe_unused pin)
  1485. {
  1486. return;
  1487. }
  1488. pthread_mutex_t s_pwm_lock;
  1489. void zynq_set_pwm(unsigned char fan_id, int frequency, int duty)
  1490. {
  1491. int fd = 0;
  1492. int duty_driver = 0;
  1493. duty_driver = frequency / 100 * (100 - duty);
  1494. //pthread_mutex_lock(&s_pwm_lock);
  1495. fd = open(SYSFS_PWM_DEV, O_RDWR);
  1496. if (fd < 0)
  1497. {
  1498. applog(LOG_ERR, "open %s fail", SYSFS_PWM_DEV);
  1499. //pthread_mutex_unlock(&s_pwm_lock);
  1500. return;
  1501. }
  1502. if (ioctl(fd, IOCTL_SET_PWM_FREQ(fan_id), frequency) < 0)
  1503. {
  1504. applog(LOG_ERR,"set fan%d frequency fail ", fan_id);
  1505. close(fd);
  1506. //pthread_mutex_unlock(&s_pwm_lock);
  1507. return ;
  1508. }
  1509. if (ioctl(fd, IOCTL_SET_PWM_DUTY(fan_id), duty_driver) < 0)
  1510. {
  1511. applog(LOG_ERR,"set fan%d duty fail ", fan_id);
  1512. close(fd);
  1513. //pthread_mutex_unlock(&s_pwm_lock);
  1514. return ;
  1515. }
  1516. close(fd);
  1517. //pthread_mutex_unlock(&s_pwm_lock);
  1518. return;
  1519. }
  1520. #define BUF_MAX (256)
  1521. #define DEV_TEMPLATE ("/dev/spidev%d.%d")
  1522. #define SYSFS_EXPORT ("/sys/devices/soc0/amba/f8007000.devcfg/fclk_export")
  1523. #define SYSFS_VAL_STR ("/sys/devices/soc0/amba/f8007000.devcfg/fclk/fclk1/set_rate")
  1524. static void zynq_spi_clock_init(void);
  1525. void zynq_spi_init(ZYNQ_SPI_T *spi, int bus)
  1526. {
  1527. char dev_fname[BUF_MAX] = {'\0'};
  1528. int fd = 0;
  1529. uint8_t mode = MCOMPAT_CONFIG_SPI_DEFAULT_MODE;
  1530. uint32_t speed = MCOMPAT_CONFIG_SPI_DEFAULT_SPEED;
  1531. uint8_t bits = MCOMPAT_CONFIG_SPI_DEFAULT_BITS_PER_WORD;
  1532. zynq_spi_clock_init();
  1533. zynq_set_spi_speed(MCOMPAT_CONFIG_SPI_DEFAULT_SPEED);
  1534. sprintf(dev_fname, DEV_TEMPLATE, bus, MCOMPAT_CONFIG_SPI_DEFAULT_CS_LINE);
  1535. fd = open(dev_fname, O_RDWR);
  1536. if (-1 == fd)
  1537. {
  1538. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1539. }
  1540. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0)
  1541. {
  1542. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1543. }
  1544. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)
  1545. {
  1546. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1547. }
  1548. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0)
  1549. {
  1550. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1551. }
  1552. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
  1553. {
  1554. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1555. }
  1556. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  1557. {
  1558. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1559. }
  1560. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  1561. {
  1562. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1563. }
  1564. spi->fd = fd;
  1565. pthread_mutex_init(&(spi->lock), NULL);
  1566. applog(LOG_DEBUG, "SPI '%s': mode=%hhu, bits=%hhu, speed=%u ",
  1567. dev_fname, MCOMPAT_CONFIG_SPI_DEFAULT_MODE, MCOMPAT_CONFIG_SPI_DEFAULT_BITS_PER_WORD, MCOMPAT_CONFIG_SPI_DEFAULT_SPEED);
  1568. return;
  1569. }
  1570. void zynq_spi_exit(ZYNQ_SPI_T *spi)
  1571. {
  1572. if (NULL == spi)
  1573. {
  1574. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1575. }
  1576. close(spi->fd);
  1577. return;
  1578. }
  1579. void zynq_spi_write(ZYNQ_SPI_T *spi, uint8_t *txbuf, int len)
  1580. {
  1581. pthread_mutex_lock(&(spi->lock));
  1582. if ((len <= 0) || (txbuf == NULL))
  1583. {
  1584. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1585. }
  1586. if (write(spi->fd, txbuf, len) <= 0)
  1587. {
  1588. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1589. }
  1590. pthread_mutex_unlock(&spi->lock);
  1591. return;
  1592. }
  1593. void zynq_spi_read(ZYNQ_SPI_T *spi, uint8_t *rxbuf, int len)
  1594. {
  1595. pthread_mutex_lock(&(spi->lock));
  1596. if ((len <= 0) || (rxbuf == NULL))
  1597. {
  1598. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1599. }
  1600. if (read(spi->fd, rxbuf, len) <= 0)
  1601. {
  1602. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1603. }
  1604. pthread_mutex_unlock(&spi->lock);
  1605. return;
  1606. }
  1607. void zynq_spi_clock_init(void)
  1608. {
  1609. int fd = 0;
  1610. ssize_t write_bytes = 0;
  1611. char fvalue[BUF_MAX] = {'\0'};
  1612. fd = access(SYSFS_VAL_STR, F_OK);
  1613. if (0 == fd)
  1614. {
  1615. return;
  1616. }
  1617. fd = open(SYSFS_EXPORT, O_WRONLY);
  1618. if (-1 == fd)
  1619. {
  1620. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1621. }
  1622. memset(fvalue, 0, sizeof(fvalue));
  1623. sprintf(fvalue, "%s", "fclk1");
  1624. write_bytes = write(fd, fvalue, strlen(fvalue));
  1625. if (-1 == write_bytes)
  1626. {
  1627. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1628. }
  1629. close(fd);
  1630. return;
  1631. }
  1632. void zynq_set_spi_speed(int speed)
  1633. {
  1634. int fd = 0;
  1635. ssize_t write_bytes = 0;
  1636. char fvalue[BUF_MAX] = {'\0'};
  1637. applog(LOG_DEBUG, "set spi speed %d ", speed);
  1638. fd = open(SYSFS_VAL_STR, O_WRONLY);
  1639. if (-1 == fd)
  1640. {
  1641. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1642. }
  1643. memset(fvalue, 0, sizeof(fvalue));
  1644. sprintf(fvalue, "%d", speed * 16);
  1645. write_bytes = write(fd, fvalue, strlen(fvalue));
  1646. if (-1 == write_bytes)
  1647. {
  1648. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1649. }
  1650. close(fd);
  1651. return;
  1652. }
  1653. int zynq_gpio_g9_vid_set(int level)
  1654. {
  1655. int fd = 0;
  1656. fd = open(SYSFS_VID_DEV, O_RDWR);
  1657. if (-1 == fd)
  1658. {
  1659. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1660. }
  1661. if (ioctl(fd, IOCTL_SET_VAL_0, 0x0100 | level) < 0)
  1662. {
  1663. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1664. }
  1665. close(fd);
  1666. return 0;
  1667. }
  1668. int zynq_gpio_g19_vid_set(int chain_id, int level)
  1669. {
  1670. int fd = 0;
  1671. fd = open(SYSFS_VID_DEV, O_RDWR);
  1672. if (-1 == fd)
  1673. {
  1674. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1675. }
  1676. if (ioctl(fd, IOCTL_SET_CHAIN_0, chain_id) < 0)
  1677. {
  1678. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1679. }
  1680. if (ioctl(fd, IOCTL_SET_VALUE_0, 0x100 | level) < 0)
  1681. {
  1682. applog(LOG_ERR, "%s,%d: %s.", __FILE__, __LINE__, strerror(errno));
  1683. }
  1684. close(fd);
  1685. return 0;
  1686. }
  1687. /* DUPES, FIXME THIS ONE IS USED */
  1688. typedef struct HUB_DEV_TAG {
  1689. volatile uint8_t *vir_base;
  1690. uint32_t phy_addr;
  1691. uint32_t mem_size;
  1692. const char *name;
  1693. } HUB_DEV_T;
  1694. static HUB_DEV_T s_dev_list[] = {
  1695. {NULL, 0x43C30000, 0x2000, "spi"},
  1696. {NULL, 0x43C00000, 0x1000, "peripheral"},
  1697. //{NULL, 0x43C32000, 0x1000, "sha256"},
  1698. };
  1699. #if 0
  1700. /* DUPES FIXME THIS ONE ISN'T USED */
  1701. typedef struct HUB_DEV_TAG {
  1702. volatile uint8_t *vir_base;
  1703. uint32_t phy_addr;
  1704. const char *name;
  1705. } HUB_DEV_T;
  1706. static HUB_DEV_T s_dev_list[] = {
  1707. {NULL, 0x43C30000, "spi"},
  1708. {NULL, 0x43C10000, "peripheral"},
  1709. {NULL, 0x43C00000, "fans"},
  1710. {NULL, 0x41200000, "gpio"},
  1711. {NULL, 0x43C32000, "sha256"},
  1712. };
  1713. #endif
  1714. void hub_hardware_init(void)
  1715. {
  1716. int fd = 0;
  1717. int i = 0;
  1718. int iMax = sizeof(s_dev_list) / sizeof(s_dev_list[0]);
  1719. applog(LOG_INFO, "max range: 0x%x.", _MAX_MEM_RANGE);
  1720. fd = open("/dev/mem", O_RDWR | O_SYNC);
  1721. if (-1 == fd)
  1722. {
  1723. applog(LOG_ERR, "open /dev/mem:");
  1724. return;
  1725. }
  1726. applog(LOG_INFO, "total: %d dev will mmap.", iMax);
  1727. for(i = 0; i < iMax; i++)
  1728. {
  1729. s_dev_list[i].vir_base = mmap(NULL, _MAX_MEM_RANGE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, s_dev_list[i].phy_addr);
  1730. if (MAP_FAILED == s_dev_list[i].vir_base)
  1731. {
  1732. close(fd);
  1733. applog(LOG_ERR, "mmap %s:phy:0x%08x => vir:%p fail.", s_dev_list[i].name, s_dev_list[i].phy_addr, s_dev_list[i].vir_base);
  1734. return;
  1735. }
  1736. applog(LOG_INFO, "mmap %s:phy:0x%08x => vir:%p ok.", s_dev_list[i].name, s_dev_list[i].phy_addr, s_dev_list[i].vir_base);
  1737. }
  1738. applog(LOG_INFO, "total: %d dev mmap done.", iMax);
  1739. close(fd);
  1740. }
  1741. void hub_hardware_deinit(void)
  1742. {
  1743. int i = 0;
  1744. int iMax = sizeof(s_dev_list) / sizeof(s_dev_list[0]);
  1745. for(i = 0; i < iMax; i++)
  1746. {
  1747. munmap((void *)s_dev_list[i].vir_base, _MAX_MEM_RANGE);
  1748. applog(LOG_INFO, "unmap %s:vir:%p.", s_dev_list[i].name, s_dev_list[i].vir_base);
  1749. }
  1750. }
  1751. /* vid, iic, uart */
  1752. // compatible values for different vid types
  1753. static int s_vid_map[][3] = {
  1754. {0, 1608, 54},
  1755. {1, 1599, 60},
  1756. {2, 1590, 67},
  1757. {3, 1581, 73},
  1758. {4, 1572, 80},
  1759. {5, 1563, 86},
  1760. {6, 1554, 93},
  1761. {7, 1545, 99},
  1762. {8, 1536, 106},
  1763. {9, 1527, 112},
  1764. {10, 1518, 119},
  1765. {11, 1509, 125},
  1766. {12, 1500, 132},
  1767. {13, 1491, 138},
  1768. {14, 1482, 145},
  1769. {15, 1473, 151},
  1770. {16, 1464, 158},
  1771. {17, 1455, 164},
  1772. {18, 1446, 171},
  1773. {19, 1437, 177},
  1774. {20, 1428, 184},
  1775. {21, 1419, 190},
  1776. {22, 1410, 197},
  1777. {23, 1401, 203},
  1778. {24, 1392, 210},
  1779. {25, 1383, 216},
  1780. {26, 1374, 223},
  1781. {27, 1365, 229},
  1782. {28, 1356, 236},
  1783. {29, 1347, 242},
  1784. {30, 1338, 248},
  1785. {31, 1329, 255},
  1786. };
  1787. static bool hub_set_vid_i2c(uint8_t chain_id, int vid);
  1788. static bool hub_set_vid_uart(uint8_t chain_id, int vid);
  1789. static void send_uart(const char *path, char byte);
  1790. static bool set_vol_on_i2c(int chain, int vol);
  1791. static bool set_power_on_i2c(int chain, int val);
  1792. bool hub_set_vid(uint8_t chain_id, int vol)
  1793. {
  1794. int type = 0;
  1795. type = misc_get_vid_type();
  1796. switch(type)
  1797. {
  1798. case MCOMPAT_LIB_VID_VID_TYPE:
  1799. {
  1800. hub_set_vid_vid(chain_id, vol);
  1801. return true;
  1802. }
  1803. case MCOMPAT_LIB_VID_I2C_TYPE:
  1804. {
  1805. if (hub_set_vid_i2c(chain_id, vol))
  1806. {
  1807. return true;
  1808. }
  1809. else
  1810. {
  1811. return false;
  1812. }
  1813. }
  1814. case MCOMPAT_LIB_VID_UART_TYPE:
  1815. {
  1816. if (hub_set_vid_uart(chain_id, vol))
  1817. {
  1818. return true;
  1819. }
  1820. else
  1821. {
  1822. return false;
  1823. }
  1824. }
  1825. case MCOMPAT_LIB_VID_GPIO_I2C_TYPE:
  1826. {
  1827. applog(LOG_ERR, "%s,%d:no impl type:MCOMPAT_LIB_VID_GPIO_I2C_TYPE.", __FILE__, __LINE__);
  1828. break;
  1829. }
  1830. default:
  1831. {
  1832. applog(LOG_ERR, "%s,%d:err vid type:%d.", __FILE__, __LINE__, type);
  1833. break;
  1834. }
  1835. }
  1836. return true;
  1837. }
  1838. static bool hub_set_vid_i2c(uint8_t chan_id, int vid)
  1839. {
  1840. int vol = s_vid_map[vid][1];
  1841. set_vol_on_i2c(chan_id + 1 , vol);
  1842. return true;
  1843. }
  1844. static bool hub_set_vid_uart(uint8_t chain_id, int vid)
  1845. {
  1846. char byte = 0;
  1847. byte = (char)s_vid_map[vid][2];
  1848. hub_set_vid_uart_select(chain_id);
  1849. send_uart("/dev/ttyPS1", byte);
  1850. return true;
  1851. }
  1852. static void hub_set_power_en_i2c(uint8_t chain_id,int value)
  1853. {
  1854. set_power_on_i2c(chain_id + 1, value);
  1855. }
  1856. bool set_timeout_on_i2c(int time)
  1857. {
  1858. int i;
  1859. int fd;
  1860. int sum = 0;
  1861. unsigned char buffer[10] = {0};
  1862. struct i2c_rdwr_ioctl_data packets;
  1863. struct i2c_msg messages[2];
  1864. fd = open(I2C_DEVICE_NAME, O_RDWR);
  1865. if(fd < 0) {
  1866. applog(LOG_ERR, "%s,%d:open %s failled: %d.", __FILE__, __LINE__, I2C_DEVICE_NAME, errno);
  1867. return false;
  1868. }
  1869. buffer[0] = 0x00;
  1870. buffer[1] = 0xab;
  1871. buffer[2] = 0x00;
  1872. buffer[3] = 0x70;
  1873. buffer[4] = 0x00;
  1874. buffer[5] = 0x01;
  1875. buffer[6] = (time/30);
  1876. for (i = 0; i < 7; i++)
  1877. sum = sum + buffer[i];
  1878. buffer[7] = sum & 0xff;
  1879. buffer[8] = 0xcd;
  1880. messages[0].addr = I2C_SLAVE_ADDR;
  1881. messages[0].flags = I2C_M_IGNORE_NAK;
  1882. messages[0].len = sizeof(buffer);
  1883. messages[0].buf = buffer;
  1884. packets.msgs = messages;
  1885. packets.nmsgs = 1;
  1886. if(ioctl(fd, I2C_RDWR, &packets) < 0) {
  1887. applog(LOG_ERR, "%s,%d:write iic failled: %d.", __FILE__, __LINE__, errno);
  1888. close(fd);
  1889. return false;
  1890. }
  1891. close(fd);
  1892. return true;
  1893. }
  1894. static bool set_power_on_i2c(int chain, int val)
  1895. {
  1896. int i;
  1897. int fd;
  1898. int sum = 0;
  1899. unsigned char buffer[10] = {0};
  1900. struct i2c_rdwr_ioctl_data packets;
  1901. struct i2c_msg messages[2];
  1902. fd = open(I2C_DEVICE_NAME, O_RDWR);
  1903. if(fd < 0) {
  1904. applog(LOG_ERR, "%s,%d:open %s failled: %d.", __FILE__, __LINE__, I2C_DEVICE_NAME, errno);
  1905. return false;
  1906. }
  1907. buffer[0] = 0x00;
  1908. buffer[1] = 0xab;
  1909. buffer[2] = 0x00;
  1910. buffer[3] = 0x85;
  1911. buffer[4] = 0x00;
  1912. buffer[5] = 0x02;
  1913. buffer[6] = chain;
  1914. if(val != 0)
  1915. buffer[7] = 0x01;
  1916. else
  1917. buffer[7] = 0x02;
  1918. for (i = 0; i < 8; i++)
  1919. sum = sum + buffer[i];
  1920. buffer[8] = sum & 0xff;
  1921. buffer[9] = 0xcd;
  1922. messages[0].addr = I2C_SLAVE_ADDR;
  1923. messages[0].flags = I2C_M_IGNORE_NAK;
  1924. messages[0].len = sizeof(buffer);
  1925. messages[0].buf = buffer;
  1926. packets.msgs = messages;
  1927. packets.nmsgs = 1;
  1928. if(ioctl(fd, I2C_RDWR, &packets) < 0) {
  1929. applog(LOG_ERR, "%s,%d:write iic failled: %d.", __FILE__, __LINE__, errno);
  1930. close(fd);
  1931. return false;
  1932. }
  1933. close(fd);
  1934. return true;
  1935. }
  1936. static bool set_vol_on_i2c(int chain, int vol)
  1937. {
  1938. int i;
  1939. int fd;
  1940. int sum = 0;
  1941. unsigned char buffer[12] = {0};
  1942. struct i2c_rdwr_ioctl_data packets;
  1943. struct i2c_msg messages[2];
  1944. fd = open(I2C_DEVICE_NAME, O_RDWR);
  1945. if (fd < 0)
  1946. {
  1947. applog(LOG_ERR, "%s,%d:open %s failled: %d.", __FILE__, __LINE__, I2C_DEVICE_NAME, errno);
  1948. return false;
  1949. }
  1950. buffer[0] = 0x00;
  1951. buffer[1] = 0xab;
  1952. buffer[2] = 0x00;
  1953. buffer[3] = 0x83;
  1954. buffer[4] = 0x00;
  1955. buffer[5] = 0x04;
  1956. buffer[6] = chain;
  1957. buffer[7] = 0x00;
  1958. buffer[8] = ((vol >> 0) & 0xff);
  1959. buffer[9] = ((vol >> 8) & 0xff);
  1960. for(i=0; i<10; i++){
  1961. sum = sum + buffer[i];
  1962. }
  1963. buffer[10] = sum & 0xff;
  1964. buffer[11] = 0xcd;
  1965. messages[0].addr = I2C_SLAVE_ADDR;
  1966. messages[0].flags = I2C_M_IGNORE_NAK;
  1967. messages[0].len = sizeof(buffer);
  1968. messages[0].buf = buffer;
  1969. packets.msgs = messages;
  1970. packets.nmsgs = 1;
  1971. if (ioctl(fd, I2C_RDWR, &packets) < 0) {
  1972. applog(LOG_ERR, "%s,%d:write iic failled: %d.", __FILE__, __LINE__, errno);
  1973. close(fd);
  1974. return false;
  1975. }
  1976. close(fd);
  1977. return true;
  1978. }
  1979. static void send_uart(const char *path, char byte)
  1980. {
  1981. int tty_fd = 0;
  1982. int rst = 0;
  1983. char buf[5] = {0};
  1984. tty_fd = open(path, O_WRONLY);
  1985. if (-1 == tty_fd)
  1986. {
  1987. applog(LOG_ERR, "%s,%d:open %s failled: %d.", __FILE__, __LINE__, path, errno);
  1988. }
  1989. /* applog(LOG_DEBUG, "%s,%d.", __FILE__, __LINE__); */
  1990. buf[0] = 0xaa;
  1991. buf[1] = 0x2;
  1992. buf[2] = 0x1;
  1993. buf[3] = byte;
  1994. buf[4] = (~(buf[1]+buf[2]+buf[3]))+1;
  1995. rst = write(tty_fd, &buf, 5);
  1996. if (-1 == rst)
  1997. {
  1998. close(tty_fd);
  1999. applog(LOG_ERR, "%s,%d:write tty failled: %d.", __FILE__, __LINE__, errno);
  2000. }
  2001. /* for debug */
  2002. #if 0
  2003. int i = 0;
  2004. applog(LOG_DEBUG, "uart %s send:", path);
  2005. for(i = 0; i < 5; i++)
  2006. {
  2007. applog(LOG_DEBUG, "%02X,", buf[i]);
  2008. }
  2009. applog(LOG_DEBUG, "");
  2010. #endif
  2011. close(tty_fd);
  2012. return;
  2013. }
  2014. #if 0
  2015. #ifdef SYSTEM_LINUX
  2016. static void hub_hardware_init(void)
  2017. {
  2018. int fd = 0;
  2019. int i = 0;
  2020. int iMax = sizeof(s_dev_list) / sizeof(s_dev_list[0]);
  2021. applog(LOG_INFO, "max range: 0x%x.", PAGE_SIZE);
  2022. fd = open("/dev/mem", O_RDWR | O_SYNC);
  2023. if (-1 == fd)
  2024. {
  2025. applog(LOG_ERR, "open /dev/mem:");
  2026. return;
  2027. }
  2028. applog(LOG_INFO, "total: %d dev will mmap.", iMax);
  2029. for(i = 0; i < iMax; i++)
  2030. {
  2031. s_dev_list[i].vir_base = mmap(NULL, s_dev_list[i].mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, s_dev_list[i].phy_addr);
  2032. if (MAP_FAILED == s_dev_list[i].vir_base)
  2033. {
  2034. close(fd);
  2035. applog(LOG_ERR, "mmap %s:phy:0x%08x => vir:%p size:0x%x fail.", s_dev_list[i].name, s_dev_list[i].phy_addr, s_dev_list[i].vir_base, s_dev_list[i].mem_size);
  2036. return;
  2037. }
  2038. applog(LOG_INFO, "mmap %s:phy:0x%08x => vir:%p size:0x%x ok.", s_dev_list[i].name, s_dev_list[i].phy_addr, s_dev_list[i].vir_base, s_dev_list[i].mem_size);
  2039. }
  2040. applog(LOG_INFO, "total: %d dev mmap done.", iMax);
  2041. close(fd);
  2042. }
  2043. static void hub_hardware_deinit(void)
  2044. {
  2045. int i = 0;
  2046. int iMax = sizeof(s_dev_list) / sizeof(s_dev_list[0]);
  2047. for(i = 0; i < iMax; i++)
  2048. {
  2049. munmap((void *)s_dev_list[i].vir_base, s_dev_list[i].mem_size);
  2050. applog(LOG_INFO, "unmap %s:vir:%p.", s_dev_list[i].name, s_dev_list[i].vir_base);
  2051. }
  2052. }
  2053. #endif
  2054. #endif
  2055. void hub_init(void)
  2056. {
  2057. hub_hardware_init();
  2058. }
  2059. void hub_deinit(void)
  2060. {
  2061. hub_hardware_deinit();
  2062. }
  2063. #define INDEX_SPI 0
  2064. #define INDEX_PERIPHERAL 1
  2065. #define INDEX_SHA256 2
  2066. // for peripheral ip
  2067. void Xil_Peripheral_Out32(uint32_t phyaddr, uint32_t val)
  2068. {
  2069. uint32_t pgoffset = phyaddr & ((uint32_t)(s_dev_list[INDEX_PERIPHERAL].mem_size -1));
  2070. pgoffset = phyaddr;
  2071. #ifdef SYSTEM_LINUX
  2072. // for software team
  2073. *(volatile uint32_t *)(s_dev_list[INDEX_PERIPHERAL].vir_base + pgoffset) = val;
  2074. #else
  2075. // for digit team
  2076. *(volatile uint32_t *)(s_dev_list[INDEX_PERIPHERAL].phy_addr + pgoffset) = val;
  2077. #endif
  2078. }
  2079. int Xil_Peripheral_In32(uint32_t phyaddr)
  2080. {
  2081. uint32_t val;
  2082. uint32_t pgoffset = phyaddr & ((uint32_t)(s_dev_list[INDEX_PERIPHERAL].mem_size -1));
  2083. pgoffset = phyaddr;
  2084. #ifdef SYSTEM_LINUX
  2085. // for software team
  2086. val = *(volatile uint32_t *)(s_dev_list[INDEX_PERIPHERAL].vir_base + pgoffset);
  2087. #else
  2088. // for digit team
  2089. val = *(volatile uint32_t *)(s_dev_list[INDEX_PERIPHERAL].phy_addr + pgoffset);
  2090. #endif
  2091. return val;
  2092. }
  2093. // for spi ip
  2094. void Xil_SPI_Out32(uint32_t phyaddr, uint32_t val)
  2095. {
  2096. uint32_t pgoffset = phyaddr & ((uint32_t)(s_dev_list[INDEX_SPI].mem_size -1));
  2097. #ifdef SYSTEM_LINUX
  2098. // for software team
  2099. *(volatile uint32_t *)(s_dev_list[INDEX_SPI].vir_base + pgoffset) = val;
  2100. #else
  2101. // for digit team
  2102. *(volatile uint32_t *)(s_dev_list[INDEX_SPI].phy_addr + pgoffset) = val;
  2103. #endif
  2104. }
  2105. int Xil_SPI_In32(uint32_t phyaddr)
  2106. {
  2107. uint32_t val;
  2108. uint32_t pgoffset = phyaddr & ((uint32_t)(s_dev_list[INDEX_SPI].mem_size -1));
  2109. #ifdef SYSTEM_LINUX
  2110. // for software team
  2111. val = *(volatile uint32_t *)(s_dev_list[INDEX_SPI].vir_base + pgoffset);
  2112. #else
  2113. // for digit team
  2114. val = *(volatile uint32_t *)(s_dev_list[INDEX_SPI].phy_addr + pgoffset);
  2115. #endif
  2116. return val;
  2117. }
  2118. void set_led(uint8_t spi_id, uint32_t mode, uint32_t led_delay)
  2119. {
  2120. uint32_t reg_val;
  2121. if (mode == LED_ON){
  2122. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2123. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << spi_id)) ) | ((LED_ON & 0x1) << spi_id));
  2124. }
  2125. else if (mode == LED_OFF){
  2126. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2127. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << spi_id)) ) | ((LED_OFF & 0x1) << spi_id));
  2128. }
  2129. else if (mode == LED_BLING_ON){
  2130. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2131. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << spi_id)) ) | ((LED_OFF & 0x1) << spi_id));
  2132. cgsleep_us(led_delay*1000);
  2133. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2134. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << spi_id)) ) | ((LED_ON & 0x1) << spi_id));
  2135. cgsleep_us(led_delay*1000);
  2136. }
  2137. else if (mode == LED_BLING_OFF){
  2138. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2139. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << spi_id)) ) | ((LED_ON & 0x1) << spi_id));
  2140. cgsleep_us(led_delay*1000);
  2141. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2142. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << spi_id)) ) | ((LED_OFF & 0x1) << spi_id));
  2143. cgsleep_us(led_delay*1000);
  2144. }
  2145. }
  2146. // ---------------------------------------------------------------------------
  2147. // For check status
  2148. // ---------------------------------------------------------------------------
  2149. int clear_wait_st_idle(uint8_t spi_id, uint32_t timeout_us)
  2150. {
  2151. uint32_t i;
  2152. uint32_t data_buf;
  2153. uint32_t cmd_status = 0;
  2154. for(i=0; i<timeout_us; i++){
  2155. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2156. if ((cmd_status&0xFF000000)==0x00000000) {
  2157. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000800);
  2158. cgsleep_us(1);
  2159. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000002);
  2160. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000000);
  2161. data_buf = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2162. if ((data_buf&0xFFFF00FF) != 0) {
  2163. applog(LOG_DEBUG, "clear_wait_st_idle SPI status is not cleared: %08x i=%0d ", data_buf&0xFFFF00FF, i);
  2164. }
  2165. break; // state=0 cmd_done=1
  2166. }
  2167. cgsleep_us(1);
  2168. }
  2169. if (i >= timeout_us){
  2170. applog(LOG_WARNING, "clear_wait_st_idle Wait SPI status clear timeout! i=%0d status=%8x ", i, cmd_status);
  2171. return XST_FAILURE;
  2172. }
  2173. else {
  2174. return XST_SUCCESS;
  2175. }
  2176. }
  2177. int wait_cmd_done(uint8_t spi_id, uint32_t timeout_us)
  2178. {
  2179. uint32_t i;
  2180. uint32_t cmd_status = 0;
  2181. uint32_t timeout = timeout_us / 1000 + 1;
  2182. for(i = 0; i <= timeout; i++){ // polling cmd_done
  2183. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2184. applog(LOG_DEBUG, "Read SPI CMD_CTRL_REG2_ADDR: %08x ", cmd_status);
  2185. //getchar(); // for debug
  2186. if ((cmd_status&0xF0000000) != 0){ // spi fsm not idle
  2187. }
  2188. if ((cmd_status&0x00000001) == 1){
  2189. applog(LOG_DEBUG, "CMD done, status: %08x! ", cmd_status);
  2190. break;
  2191. }
  2192. cgsleep_ms(1); //usleep(10);
  2193. }
  2194. if (i > timeout){
  2195. applog(LOG_WARNING, "SPI polling cmd done timeout! i=%0d ", i);
  2196. return XST_FAILURE;
  2197. }
  2198. else {
  2199. return XST_SUCCESS;
  2200. }
  2201. }
  2202. int wait_phy_idle(uint8_t spi_id, uint32_t timeout_us)
  2203. {
  2204. uint32_t i;
  2205. uint32_t cmd_status = 0;
  2206. uint32_t timeout = timeout_us / 1000 + 1;
  2207. for(i = 0; i <= timeout; i++){
  2208. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2209. if ((cmd_status&0x00000040)==0x00000000) {
  2210. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2211. break; // state=0 cmd_done=1
  2212. }
  2213. cgsleep_ms(1); // usleep(1);
  2214. }
  2215. if (i >= timeout_us){
  2216. applog(LOG_WARNING, "Wait SPI status clear timeout! i=%0d status=%8x ", i, cmd_status);
  2217. return XST_FAILURE;
  2218. }
  2219. else {
  2220. return XST_SUCCESS;
  2221. }
  2222. }
  2223. int wait_spi_idle(uint8_t spi_id, uint32_t timeout_us)
  2224. {
  2225. uint32_t i;
  2226. uint32_t cmd_status = 0;
  2227. uint32_t timeout = timeout_us / 1000 + 1;
  2228. for(i = 0; i <= timeout; i ++){
  2229. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2230. if ((cmd_status&0xFF000040)==0) return XST_SUCCESS;
  2231. cgsleep_ms(1); // usleep(20);
  2232. }
  2233. applog(LOG_DEBUG, "Wait SPI(%0d) idle timeout! time=%0d us, status=%8x ", spi_id, timeout_us, cmd_status);
  2234. return XST_FAILURE;
  2235. }
  2236. int wait_write_buf_empty(uint8_t spi_id, uint32_t timeout_us)
  2237. {
  2238. uint32_t i;
  2239. uint32_t cmd_status = 0;
  2240. uint32_t timeout = timeout_us / 1000 + 1;
  2241. for(i = 0; i <= timeout; i++){
  2242. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2243. if ((cmd_status&0x03000000)==0) return XST_SUCCESS;
  2244. cgsleep_ms(1); // usleep(10); // polling interval
  2245. }
  2246. //printf("Wait command write queue empty timeout!spi=%0d i=%0d status=%8x ",spi_id, i, cmd_status);
  2247. return XST_FAILURE;
  2248. }
  2249. int check_cmd_status(uint8_t spi_id)
  2250. {
  2251. uint32_t cmd_status = 0;
  2252. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2253. if (cmd_status & 0x00000004){
  2254. return XST_CRC_ERROR;
  2255. }
  2256. return XST_SUCCESS;
  2257. }
  2258. void reset_rx_buffer(uint8_t spi_id)
  2259. {
  2260. uint32_t cmd_status = 0;
  2261. uint32_t write_data = 0;
  2262. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG1_ADDR);
  2263. write_data = cmd_status & (~0x00000004);
  2264. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG1_ADDR, write_data);
  2265. cgsleep_ms(1);
  2266. write_data = cmd_status | 0x00000004;
  2267. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG1_ADDR, write_data);
  2268. }
  2269. // ---------------------------------------------------------------------------
  2270. // Below function is for new SPI design's nonce/receive queue
  2271. // ---------------------------------------------------------------------------
  2272. void read_rx_buffer(uint8_t spi_id, uint8_t* buf8, uint32_t len_cfg)
  2273. {
  2274. uint32_t i;
  2275. uint8_t rx_len;
  2276. rx_len = ((len_cfg & 0x0000FF00) >> 8)*2;
  2277. // Get nonce data
  2278. for(i = 0; i < rx_len; i+=4){
  2279. *(uint32_t*)(buf8+i) = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_READ_REG0_ADDR+i);
  2280. }
  2281. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000800); // tell hw one nonce is fetched
  2282. }
  2283. void fill_tx_buffer(uint8_t spi_id, uint8_t* buf8, uint32_t byte_len)
  2284. {
  2285. uint32_t i;
  2286. for(i = 0; i < byte_len; i+=4)
  2287. {
  2288. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_WRITE_REG01_ADDR+i, *(uint32_t*)(buf8+i));
  2289. }
  2290. }
  2291. // ---------------------------------------------------------------------------
  2292. // Below function is for new SPI design's send queue
  2293. // ---------------------------------------------------------------------------
  2294. // SPI bypass = 0
  2295. int push_one_cmd(uint8_t spi_id, uint8_t* tx_buf8, uint32_t len_cfg, uint32_t last_job)
  2296. {
  2297. //uint32_t i;
  2298. uint8_t byte_len;
  2299. //uint32_t cmd_status;
  2300. uint16_t cmd_header;
  2301. byte_len = (len_cfg >> 24)*2; // Not include ending zeros
  2302. cmd_header = (tx_buf8[1] << 8) | tx_buf8[0];
  2303. // wait command write queue empty
  2304. if (wait_write_buf_empty(spi_id, 10000) == XST_FAILURE) return XST_FAILURE;
  2305. if (wait_spi_idle(spi_id, 10000) == XST_FAILURE) return XST_FAILURE;
  2306. // write header to buffer
  2307. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_WRITE_HEAD_ADDR, cmd_header);
  2308. // write data to buffer
  2309. fill_tx_buffer(spi_id, tx_buf8+2, byte_len-2); // Not include tx
  2310. // send command execution
  2311. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG0_ADDR, len_cfg);
  2312. if (last_job)
  2313. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR,
  2314. 0x00000001 | CHK_CMD | CHK_HY | CHK_LN);
  2315. else
  2316. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR,
  2317. 0x00000011 | CHK_CMD | CHK_HY | CHK_LN);
  2318. return XST_SUCCESS;
  2319. }
  2320. /***************************************/
  2321. // interface
  2322. /***************************************/
  2323. void hub_spi_reset(uint8_t spi_id)
  2324. {
  2325. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG1_ADDR,0x00000010);
  2326. cgsleep_us(1);
  2327. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG1_ADDR,0x0000001F);
  2328. }
  2329. int hub_spi_init(uint8_t spi_id, uint8_t chip_num)
  2330. {
  2331. uint32_t Status;
  2332. //uint32_t i;
  2333. Status = Xil_SPI_In32(SPI_RESET_REG);
  2334. // reset
  2335. Xil_SPI_Out32(SPI_RESET_REG,(Status & ~(1 << spi_id)));
  2336. cgsleep_ms(1); // usleep(1);
  2337. Status = Xil_SPI_In32(SPI_RESET_REG);
  2338. // release reset
  2339. Xil_SPI_Out32(SPI_RESET_REG,(Status | (1 << spi_id)));
  2340. Status = Xil_SPI_In32(SPI_RESET_REG);
  2341. hub_spi_reset(spi_id);
  2342. // config max chip number
  2343. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR,0x00041004|(chip_num<<24));
  2344. // config not check header
  2345. // Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG3_ADDR,0x000F00FF);
  2346. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG3_ADDR, 0x000F0000 & chip_num);
  2347. // config mask of each interrupt
  2348. Xil_SPI_Out32(MAIN_CFG_REG2_ADDR, 0x00000);
  2349. return XST_SUCCESS;
  2350. }
  2351. void hub_spi_clean_chain(uint32_t spi_id)
  2352. {
  2353. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH] = {0};
  2354. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH] = {0};
  2355. spi_tx[0] = CMD_RESET;
  2356. spi_tx[1] = CMD_ADDR_BROADCAST;
  2357. spi_tx[2] = 0xff;
  2358. spi_tx[3] = 0xff;
  2359. do_spi_cmd(spi_id, spi_tx, spi_rx, 0x10001000);
  2360. hub_spi_reset(spi_id);
  2361. }
  2362. void hub_set_spi_speed(uint8_t spi_id, int select)
  2363. {
  2364. uint32_t cfg[] = {0x00020000, 0x00040000, 0x00080000, 0x00100000, 0x00200100, 0x00330100};
  2365. //float mcu_spi_clk[] = {0.39062, 0.78125, 1.5625, 3.125, 6.25, 9.96};
  2366. uint32_t read_data;
  2367. read_data = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR);
  2368. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR,(read_data&0xFF00FFFF)|cfg[select]);
  2369. read_data = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR);
  2370. }
  2371. // ----------- send job ------------
  2372. int send_job_queue(uint8_t spi_id, uint8_t* tx_buf8, uint8_t __maybe_unused *rx_buf8, uint32_t len_cfg, uint32_t last_job)
  2373. {
  2374. // push tx data to send buffer and start command
  2375. if (push_one_cmd(spi_id, tx_buf8, len_cfg, last_job) != XST_SUCCESS) return XST_FAILURE;
  2376. // wait all jobs are sent to chip
  2377. if (last_job){
  2378. if (wait_spi_idle(spi_id, 100000) == XST_FAILURE) return XST_FAILURE;
  2379. }
  2380. // Clear status. Not mandatory but suggest
  2381. if (last_job) clear_wait_st_idle(spi_id, 100000);
  2382. // usleep(100);
  2383. return XST_SUCCESS;
  2384. }
  2385. int send_one_cmd_split(uint8_t spi_id, uint8_t* tx_buf8, uint32_t len_cfg, uint32_t last_job, uint8_t cs_low)
  2386. {
  2387. uint32_t byte_len;
  2388. uint16_t cmd_header;
  2389. uint32_t cfg_reg0;
  2390. byte_len = (len_cfg >> 24)*2; // Not include ending zeros
  2391. cmd_header = (tx_buf8[1] << 8) | tx_buf8[0];
  2392. // change ext_zero to 0
  2393. cfg_reg0 = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR);
  2394. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR,(cfg_reg0&0xFFFFFF00));
  2395. // wait command write queue empty
  2396. if (wait_write_buf_empty(spi_id, 10000) == XST_FAILURE) return XST_FAILURE;
  2397. // write header to buffer
  2398. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_WRITE_HEAD_ADDR, cmd_header);
  2399. // write data to buffer
  2400. fill_tx_buffer(spi_id, tx_buf8+2, byte_len-2); // Not include tx
  2401. // send command execution
  2402. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG0_ADDR, len_cfg);
  2403. if (last_job)
  2404. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, (0x00000001 | CHK_HY | CHK_LN | cs_low<<14) );
  2405. else
  2406. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, (0x00000011 | CHK_HY | CHK_LN | cs_low<<14) );
  2407. // // check send queue full
  2408. // cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2409. // if ((cmd_status&0x02000000)==0) {
  2410. // DBGERROR("After push one command, send queue is not full!");
  2411. // return XST_FAILURE;
  2412. // }
  2413. // For half command application, can not use queue
  2414. wait_cmd_done(spi_id, 10000);
  2415. // change back ext_zero
  2416. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG0_ADDR,cfg_reg0);
  2417. return XST_SUCCESS;
  2418. }
  2419. bool rece_queue_ready_check(uint8_t spi_id, uint32_t len, uint32_t timeout_us)
  2420. {
  2421. uint32_t i;
  2422. uint32_t cmd_status;
  2423. uint32_t timeout = timeout_us / 1000 + 1;
  2424. for(i = 0; i <= timeout; i++){
  2425. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2426. if (((cmd_status&0x00000010)==0x00000010) && ((((cmd_status&0x0000FF00)>>8)>=(len)))) return true; // wait nonce_ready = 1, cmd_done = 1
  2427. // if (((spi_tr.cmd_status&0x0000FF00)>>8)>=(len)) return true;
  2428. // if ((spi_tr.cmd_status&0x00000010)==0x00000010) return true;
  2429. cgsleep_ms(1); // usleep(10);
  2430. }
  2431. return false;
  2432. }
  2433. // Check receive queue empty. true: empty
  2434. bool rece_queue_empty_check(uint8_t spi_id, uint32_t timeout_us)
  2435. {
  2436. uint32_t i;
  2437. uint32_t cmd_status;
  2438. uint32_t timeout = timeout_us / 1000 + 1;
  2439. for(i = 0; i <= timeout; i++){
  2440. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2441. if ((cmd_status & 0x0000ff10)==0x00000000) {
  2442. return true; // wait nonce_ready = 0, cmd_done = 1
  2443. }
  2444. cgsleep_ms(1); // usleep(10);
  2445. }
  2446. return false;
  2447. }
  2448. bool rece_queue_has_nonce(uint8_t spi_id, uint32_t __maybe_unused timeout_us)
  2449. {
  2450. //uint32_t i;
  2451. uint32_t cmd_status;
  2452. cmd_status = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG2_ADDR);
  2453. if ( ((cmd_status&0x0000ff00) >= 0x00000600) && ((cmd_status&0x00000010) == 0x00000010) ) {
  2454. return true; // wait nonce_ready = 0, cmd_done = 1
  2455. }
  2456. return false;
  2457. }
  2458. void read_nonce_buffer(uint8_t spi_id, uint8_t* buf8, uint32_t len_cfg)
  2459. {
  2460. uint32_t i;
  2461. uint8_t rx_len;
  2462. rx_len = (len_cfg & 0x0000FF00) >> 8;
  2463. // Get nonce data
  2464. for(i = 0; i < rx_len*2; i+=4){
  2465. *(uint32_t*)(buf8+i) = Xil_SPI_In32(SPI_BASEADDR_GAP*spi_id+CMD_READ_REG0_ADDR+i);
  2466. }
  2467. /*
  2468. * if (buf8[0] == 00)
  2469. * {
  2470. * dump_spi_last_tr(spi_id);
  2471. }
  2472. */
  2473. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000B00); // tell hw one nonce is fetched, keep auto get nonce enable
  2474. cgsleep_ms(1); // usleep(1);
  2475. }
  2476. int pop_one_rece(uint8_t spi_id, uint8_t* rx_buf8, uint32_t len_cfg)
  2477. {
  2478. //uint32_t i;
  2479. uint32_t len = (len_cfg & 0x0000ff00) >> 8;
  2480. // wait receive queue ready
  2481. if (rece_queue_ready_check(spi_id, len, 50000) == false) {
  2482. applog(LOG_INFO, "chain%d check receive buffer ready timeout!", spi_id);
  2483. return XST_FAILURE;
  2484. }
  2485. /*
  2486. * if (check_crc_status(spi_id) != XST_SUCCESS) {
  2487. * applog(LOG_WARNING, "crc error ");
  2488. * return XST_CRC_ERROR;
  2489. * }
  2490. */
  2491. // read back rx data
  2492. read_rx_buffer(spi_id, rx_buf8, len_cfg);
  2493. return XST_SUCCESS;
  2494. }
  2495. int do_spi_cmd(uint8_t spi_id, uint8_t* tx_buf8, uint8_t* rx_buf8, uint32_t len_cfg)
  2496. {
  2497. // reset_rx_buffer(spi_id);
  2498. // push tx data to send buffer and start command
  2499. if (push_one_cmd(spi_id, tx_buf8, len_cfg, 1) != XST_SUCCESS) {
  2500. applog(LOG_ERR, "ERROR - failed to send spi cmd");
  2501. return XST_FAILURE;
  2502. }
  2503. // read back rx data
  2504. if (pop_one_rece(spi_id, rx_buf8, len_cfg) != XST_SUCCESS) {
  2505. applog(LOG_ERR, "ERROR - failed to recv spi data");
  2506. return XST_FAILURE;
  2507. }
  2508. if ((tx_buf8[0] & 0x0f) != (rx_buf8[0] & 0x0f)) {
  2509. //hexdump_error("ERROR - recvbuf:", rx_buf8, 16);
  2510. return XST_FAILURE;
  2511. }
  2512. // Clear status. Not mandatory but suggest
  2513. clear_wait_st_idle(spi_id, 200);
  2514. return XST_SUCCESS;
  2515. }
  2516. void enable_auto_cmd0a(uint8_t spi_id, uint32_t threshold, uint32_t msb, uint32_t lsb, uint32_t large_en, uint32_t mode )//mode : 1 only cmd0a;0 cmd08 follows cmd0a
  2517. {
  2518. uint32_t val;
  2519. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000000 | CHK_CMD);
  2520. val = ((msb << 24) & 0xff000000) | ((lsb << 16) & 0xff0000) | ((mode & 0x1) << 14) | ((large_en & 0x1) << 13) | (0x1 << 12) | ( threshold & 0xfff );
  2521. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+AUTO_CMD0A_REG0_ADDR, val);
  2522. }
  2523. void disable_auto_cmd0a(uint8_t spi_id, uint32_t threshold, uint32_t msb, uint32_t lsb, uint32_t large_en, uint32_t mode )//mode : 1 only cmd0a;0 cmd08 follows cmd0a
  2524. {
  2525. uint32_t val;
  2526. val = ((msb << 24) & 0xff000000) | ((lsb << 16) & 0xff0000) | ((mode & 0x1) << 14) | ((large_en & 0x1) << 13) | (0x0 << 12) | ( threshold & 0xfff );
  2527. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+AUTO_CMD0A_REG0_ADDR, val);
  2528. }
  2529. int enable_auto_nonce(uint8_t spi_id, uint16_t cmd08_cmd, uint32_t len_cfg)
  2530. {
  2531. uint8_t send_buf8[12] = {0};
  2532. // wait all previous command done
  2533. //clear_wait_st_idle(spi_id, 1000000);
  2534. wait_spi_idle(spi_id, 10000);
  2535. // set auto get nonce
  2536. //for(i=0; i<spi_tr.tx_len; i++){spi_tr.tx_buf[i] = 0;} // clear tx_buf variable for debug print
  2537. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_WRITE_HEAD_ADDR, REORDER16(cmd08_cmd));
  2538. fill_tx_buffer(spi_id, send_buf8, 10);
  2539. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+MAIN_CFG_REG3_ADDR, 0x000000ff); // auto cmd08 gap
  2540. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG0_ADDR, len_cfg);
  2541. //Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000002); // clear status
  2542. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000300 | CHK_CMD); // cmd08 do
  2543. return XST_SUCCESS;
  2544. }
  2545. int disable_auto_nonce(uint8_t spi_id)
  2546. {
  2547. Xil_SPI_Out32(SPI_BASEADDR_GAP*spi_id+CMD_CTRL_REG1_ADDR, 0x00000000); // disable auto get nonce
  2548. // wait command done
  2549. return (clear_wait_st_idle(spi_id, 1000000));
  2550. }
  2551. // 3.3v GPIO output
  2552. void hub_set_power_en(uint8_t chain_id, int value)
  2553. {
  2554. uint32_t reg_val;
  2555. if (misc_get_vid_type() == MCOMPAT_LIB_VID_I2C_TYPE) {
  2556. hub_set_power_en_i2c(chain_id, value);
  2557. sleep(3);
  2558. }
  2559. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG8_OFFSET);
  2560. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG8_OFFSET, (reg_val & (~(0x1 << (18 + chain_id)))) | ((value & 0x1) << (18 + chain_id)));
  2561. //reg_val = Xil_In32(XPAR_VID_LED_BUZZER_CTRL_0_S00_AXI_BASEADDR + VID_LED_BUZZER_CTRL_S00_AXI_SLV_REG0_OFFSET + chain_id*4);
  2562. //Xil_Peripheral_Out32(XPAR_VID_LED_BUZZER_CTRL_0_S00_AXI_BASEADDR + VID_LED_BUZZER_CTRL_S00_AXI_SLV_REG0_OFFSET + chain_id*4, (reg_val & 0xfffeffff) | ((value & 0x1) << 16));
  2563. }
  2564. // 1.8v GPIO output
  2565. void hub_set_start_en(uint8_t chain_id, int value)
  2566. {
  2567. uint32_t reg_val;
  2568. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG8_OFFSET);
  2569. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG8_OFFSET, (reg_val & (~(0x1 << chain_id))) | ((value & 0x1) << chain_id));
  2570. //reg_val = Xil_In32(XPAR_VID_LED_BUZZER_CTRL_0_S00_AXI_BASEADDR + VID_LED_BUZZER_CTRL_S00_AXI_SLV_REG0_OFFSET + chain_id*4);
  2571. //Xil_Peripheral_Out32(XPAR_VID_LED_BUZZER_CTRL_0_S00_AXI_BASEADDR + VID_LED_BUZZER_CTRL_S00_AXI_SLV_REG0_OFFSET + chain_id*4, (reg_val & 0xfffbffff) | ((value & 0x1) << 18));
  2572. }
  2573. // 1.8v GPIO output
  2574. bool hub_set_reset(uint8_t chain_id, int value)
  2575. {
  2576. uint32_t reg_val;
  2577. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG8_OFFSET);
  2578. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG8_OFFSET, (reg_val & (~(0x1 << (9 + chain_id)))) | ((value & 0x1) << (9 + chain_id)));
  2579. //reg_val = Xil_Peripheral_In32(XPAR_VID_LED_BUZZER_CTRL_0_S00_AXI_BASEADDR + VID_LED_BUZZER_CTRL_S00_AXI_SLV_REG0_OFFSET + chain_id*4);
  2580. //Xil_Peripheral_Out32(XPAR_VID_LED_BUZZER_CTRL_0_S00_AXI_BASEADDR + VID_LED_BUZZER_CTRL_S00_AXI_SLV_REG0_OFFSET + chain_id*4, (reg_val & 0xfffdffff) | ((value & 0x1) << 17));
  2581. return true;
  2582. }
  2583. void hub_set_led(uint8_t chain_id, int mode)
  2584. {
  2585. uint32_t reg_val;
  2586. if (mode == LED_ON){
  2587. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2588. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << chain_id)) ) | ((LED_ON & 0x1) << chain_id));
  2589. }
  2590. else if (mode == LED_OFF){
  2591. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2592. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,(reg_val & (0xffffffff ^ ( 1 << chain_id)) ) | ((LED_OFF & 0x1) << chain_id));
  2593. }
  2594. }
  2595. int hub_get_plug(uint8_t chain_id)
  2596. {
  2597. uint32_t reg_val;
  2598. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG10_OFFSET);
  2599. return ((reg_val >> chain_id) & 0x01);
  2600. }
  2601. #ifdef SYSTEM_LINUX
  2602. static int set_warn(int spi_id)
  2603. {
  2604. mcompat_set_power_en(spi_id, 0);
  2605. sleep(1);
  2606. mcompat_set_reset(spi_id, 0);
  2607. mcompat_set_start_en(spi_id, 0);
  2608. do
  2609. {
  2610. mcompat_set_led(spi_id, 1);
  2611. sleep(1);
  2612. mcompat_set_led(spi_id, 0);
  2613. sleep(1);
  2614. }while(1);
  2615. return 0;
  2616. }
  2617. static void hub_get_hitemp_stat(uint8_t chain_id,mcompat_temp_s *temp_ctrl)
  2618. {
  2619. bool over_temp = false;
  2620. int reg_val;
  2621. int tmp_val;
  2622. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP*chain_id+AUTO_CMD0A_REG3_ADDR);
  2623. tmp_val = ((reg_val ) & 0x3ff) < g_temp_hi_thr ? 0x0:((reg_val) & 0x3ff);
  2624. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2625. temp_ctrl->temp_highest[0] = tmp_val;
  2626. if ((temp_ctrl->temp_highest[0]) &&(temp_ctrl->temp_highest[0] < g_dangerous_temp))
  2627. over_temp = true;
  2628. tmp_val = ((reg_val >> 10) & 0x3ff) < g_temp_hi_thr ? 0x0:((reg_val >> 10) & 0x3ff);
  2629. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2630. temp_ctrl->temp_highest[1] = tmp_val;
  2631. if ((temp_ctrl->temp_highest[1]) &&(temp_ctrl->temp_highest[1] < g_dangerous_temp))
  2632. over_temp = true;
  2633. tmp_val = ((reg_val >> 20) & 0x3ff) < g_temp_hi_thr ? 0x0:((reg_val >> 20) & 0x3ff);
  2634. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2635. temp_ctrl->temp_highest[2] = (reg_val >> 20 ) & 0x3ff;
  2636. if ((temp_ctrl->temp_highest[2]) &&(temp_ctrl->temp_highest[2] < g_dangerous_temp))
  2637. over_temp = true;
  2638. if (over_temp == true)
  2639. set_warn(chain_id);
  2640. applog(LOG_INFO,"chain %d,Hi: %d,%d,%d",chain_id,temp_ctrl->temp_highest[0],temp_ctrl->temp_highest[1],temp_ctrl->temp_highest[2]);
  2641. }
  2642. static void hub_get_lotemp_stat(uint8_t chain_id,mcompat_temp_s *temp_ctrl)
  2643. {
  2644. int reg_val;
  2645. int tmp_val;
  2646. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP*chain_id+AUTO_CMD0A_REG2_ADDR);
  2647. tmp_val = ((reg_val) & 0x3ff) < g_temp_hi_thr ? 0x0:((reg_val) & 0x3ff);
  2648. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2649. temp_ctrl->temp_lowest[0] = tmp_val;
  2650. tmp_val = ((reg_val >> 10) & 0x3ff) < g_temp_hi_thr ? 0x0:((reg_val >> 10) & 0x3ff);
  2651. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2652. temp_ctrl->temp_lowest[1] = tmp_val;
  2653. tmp_val = ((reg_val >> 20) & 0x3ff) < g_temp_hi_thr ? 0x0:((reg_val >> 20) & 0x3ff);
  2654. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2655. temp_ctrl->temp_lowest[2] = tmp_val;
  2656. applog(LOG_INFO,"chain %d,lo: %d,%d,%d",chain_id,temp_ctrl->temp_lowest[0],temp_ctrl->temp_lowest[1],temp_ctrl->temp_lowest[2]);
  2657. }
  2658. static void hub_get_avgtemp_stat(uint8_t chain_id,mcompat_temp_s *temp_ctrl)
  2659. {
  2660. int reg_val;
  2661. int tmp_val;
  2662. reg_val = Xil_SPI_In32(SPI_BASEADDR_GAP*chain_id+AUTO_CMD0A_REG4_ADDR);
  2663. tmp_val = 2 * ((reg_val ) & 0xffff) / g_chip_num;
  2664. tmp_val = tmp_val < g_temp_hi_thr ? 0x0:tmp_val;
  2665. tmp_val = tmp_val > g_temp_lo_thr ? 0x0:tmp_val;
  2666. temp_ctrl->final_temp_avg = tmp_val;
  2667. applog(LOG_INFO,"chain %d,avg: %d",chain_id,temp_ctrl->final_temp_avg);
  2668. }
  2669. #endif
  2670. void hub_set_vid_vid(uint8_t chain_id, int vid)
  2671. {
  2672. uint32_t reg_val = 0;
  2673. int i = 0;
  2674. uint8_t vid_binary[16] = {0};
  2675. for(i = 0; i < 8; i ++)
  2676. {
  2677. vid_binary[i] = ((vid >> i) & 0x1) ? 7 : 1;
  2678. vid_binary[8+i] = (vid_binary[i] == 1) ? 7 : 1;
  2679. }
  2680. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG32_OFFSET, 25000);
  2681. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG33_OFFSET, (vid_binary[3] << 28) | (vid_binary[2] << 24) | (vid_binary[1] << 20) | (vid_binary[0] << 16) | 0xff);
  2682. reg_val = 0;
  2683. for(i = 0; i < 8; i ++)
  2684. {
  2685. reg_val = (vid_binary[i+4] << (i*4)) | reg_val;
  2686. }
  2687. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG34_OFFSET, reg_val);
  2688. reg_val = 0;
  2689. for(i = 0; i < 4; i ++)
  2690. {
  2691. reg_val = (vid_binary[i+12] << (i*4)) | reg_val;
  2692. }
  2693. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG35_OFFSET, reg_val);
  2694. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG39_OFFSET, 80 | (4 << 16));
  2695. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG4_OFFSET, 0x1 << chain_id);
  2696. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG3_OFFSET, 0x1 );
  2697. cgsleep_ms(100);
  2698. }
  2699. void hub_set_vid_uart_select(uint8_t spi_id)
  2700. {
  2701. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG4_OFFSET, (0x1 << 16) | (0x1 << spi_id));
  2702. }
  2703. void hub_set_pwm(uint8_t fan_id, int frequency, int duty)
  2704. {
  2705. #if 0
  2706. int duty_driver = 0;
  2707. unsigned int value;
  2708. duty_driver = frequency / 100 * duty;
  2709. value = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG16_OFFSET + fan_id*8);
  2710. value = value | (1<<fan_id);
  2711. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG2_OFFSET, value);
  2712. //Xil_Fans_Out32(XPAR_FANS_CTRL_0_S00_AXI_BASEADDR + FANS_CTRL_S00_AXI_SLV_REG1_OFFSET + fan_id*8, frequency);
  2713. //Xil_Fans_Out32(XPAR_FANS_CTRL_0_S00_AXI_BASEADDR + FANS_CTRL_S00_AXI_SLV_REG0_OFFSET + fan_id*8, duty_driver | (0x1 << 31));
  2714. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG16_OFFSET + fan_id*8, frequency);
  2715. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG17_OFFSET + fan_id*8, duty_driver | (0x1 << 31));
  2716. #else
  2717. int duty_driver = 0;
  2718. uint32_t reg_val;
  2719. duty_driver = frequency / 100 * duty;
  2720. applog(LOG_DEBUG, "%s,%d: fan_id %d, freq: %d duty: %d.", __FILE__, __LINE__, fan_id, frequency, duty);
  2721. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG16_OFFSET + fan_id*8, frequency);
  2722. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG17_OFFSET + fan_id*8, duty_driver);
  2723. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG2_OFFSET);
  2724. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG2_OFFSET, (reg_val & (~(0x1 << fan_id))) | (0x1 << fan_id));
  2725. #endif
  2726. }
  2727. int hub_get_button(void)
  2728. {
  2729. uint32_t reg_val;
  2730. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG10_OFFSET);
  2731. return (((reg_val >> 16) & 0x1));
  2732. }
  2733. void hub_set_green_led(int mode)
  2734. {
  2735. uint32_t reg_val,SetRedRegValue;
  2736. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2737. if (LED_ON == mode)
  2738. {
  2739. SetRedRegValue = reg_val | 0x200;
  2740. }
  2741. else
  2742. {
  2743. SetRedRegValue = reg_val & (~0x200);
  2744. }
  2745. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,SetRedRegValue);
  2746. }
  2747. void hub_set_red_led(int mode)
  2748. {
  2749. uint32_t reg_val,SetRedRegValue;
  2750. reg_val = Xil_Peripheral_In32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET);
  2751. if (LED_ON == mode)
  2752. {
  2753. SetRedRegValue = reg_val | 0x400;
  2754. }
  2755. else
  2756. {
  2757. SetRedRegValue = reg_val & (~0x400);
  2758. }
  2759. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG9_OFFSET,SetRedRegValue);
  2760. }
  2761. void init_hub_gpio(void)
  2762. {
  2763. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG1_OFFSET, 0);
  2764. sleep(1);
  2765. Xil_Peripheral_Out32(MCOMPAT_PERIPHERAL_S00_AXI_SLV_REG1_OFFSET, 3);
  2766. }
  2767. void flush_spi(uint8_t chain_id)
  2768. {
  2769. uint16_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2770. uint16_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2771. memset(spi_tx, 0, sizeof(spi_tx));
  2772. memset(spi_rx, 0, sizeof(spi_rx));
  2773. opi_spi_transfer(chain_id, spi_tx, spi_rx, MCOMPAT_CONFIG_MAX_CMD_LENGTH);
  2774. }
  2775. bool opi_spi_read_write(uint8_t chain_id, uint8_t *txbuf, uint8_t *rxbuf, int len)
  2776. {
  2777. int i;
  2778. bool ret;
  2779. int len16 = len / 2;
  2780. uint16_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2781. uint16_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2782. if (rxbuf == NULL)
  2783. {
  2784. applog(LOG_ERR, "%s,%s() %d: para erro! ", __FILE__, __func__, __LINE__);
  2785. }
  2786. memset(spi_tx, 0, sizeof(spi_tx));
  2787. memset(spi_rx, 0, sizeof(spi_rx));
  2788. if (txbuf == NULL)
  2789. {
  2790. ret = opi_spi_transfer(chain_id, NULL, spi_rx, len16);
  2791. }
  2792. else
  2793. {
  2794. for(i = 0; i < len16; i++)
  2795. {
  2796. spi_tx[i] = OPI_MAKE_WORD(txbuf[2*i], txbuf[(2*i)+1]);
  2797. }
  2798. ret = opi_spi_transfer(chain_id, spi_tx, spi_rx, len16);
  2799. }
  2800. if (!ret)
  2801. {
  2802. return false;
  2803. }
  2804. for(i = 0; i < len16; i++)
  2805. {
  2806. rxbuf[2*i] = OPI_HI_BYTE(spi_rx[i]);
  2807. rxbuf[(2*i)+1] = OPI_LO_BYTE(spi_rx[i]);
  2808. }
  2809. return true;
  2810. }
  2811. bool opi_send_command(uint8_t chain_id, uint8_t cmd, uint8_t chip_id, uint8_t *buff, int len)
  2812. {
  2813. int tx_len;
  2814. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2815. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2816. if (buff == NULL)
  2817. {
  2818. applog(LOG_ERR, "%s,%s() %d: para erro! ", __FILE__, __func__, __LINE__);
  2819. }
  2820. memset(spi_tx, 0, sizeof(spi_tx));
  2821. memset(spi_rx, 0, sizeof(spi_rx));
  2822. spi_tx[0] = OPI_HI_BYTE(AX_CMD_SYNC_HEAD);
  2823. spi_tx[1] = OPI_LO_BYTE(AX_CMD_SYNC_HEAD);
  2824. spi_tx[2] = cmd;
  2825. spi_tx[3] = chip_id;
  2826. if (len > 0)
  2827. {
  2828. memcpy(spi_tx + 4, buff, len);
  2829. }
  2830. tx_len = (4 + len + 1) & ~1;
  2831. if (opi_spi_read_write(chain_id, spi_tx, spi_rx, tx_len))
  2832. {
  2833. return true;
  2834. }
  2835. else
  2836. {
  2837. applog(LOG_ERR, "send command fail !");
  2838. return false;
  2839. }
  2840. }
  2841. bool opi_poll_result(uint8_t chain_id, uint8_t cmd, uint8_t __maybe_unused chip_id, uint8_t *buff, int len)
  2842. {
  2843. int i;
  2844. int tx_len;
  2845. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2846. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2847. if (buff == NULL)
  2848. {
  2849. applog(LOG_ERR, "%s,%s() %d: para erro! ", __FILE__, __func__, __LINE__);
  2850. }
  2851. memset(spi_tx, 0, sizeof(spi_tx));
  2852. memset(spi_rx, 0, sizeof(spi_rx));
  2853. tx_len = g_chip_num * 4;
  2854. for(i = 0; i < tx_len; i++) {
  2855. cgsleep_ms(1); // usleep(1);
  2856. if (opi_spi_read_write(chain_id, NULL, spi_rx, 2))
  2857. {
  2858. break;
  2859. }
  2860. }
  2861. if (i >= tx_len)
  2862. {
  2863. applog(LOG_ERR, "%s,%d: poll fail !", __FILE__, __LINE__);
  2864. return false;
  2865. }
  2866. if ((spi_rx[0] != OPI_HI_BYTE(AX_CMD_SYNC_HEAD)) || (spi_rx[1] != OPI_LO_BYTE(AX_CMD_SYNC_HEAD)))
  2867. {
  2868. return false;
  2869. }
  2870. opi_spi_read_write(chain_id, NULL, spi_rx, 2);
  2871. if (spi_rx[1] != OPI_STATUS_SUC)
  2872. {
  2873. return false;
  2874. }
  2875. opi_spi_read_write(chain_id, NULL, spi_rx, 2);
  2876. if ((spi_rx[0] & 0x0f) != cmd)
  2877. {
  2878. return false;
  2879. }
  2880. if (len > 0)
  2881. {
  2882. opi_spi_read_write(chain_id, NULL, spi_rx+2, len);
  2883. }
  2884. memcpy(buff, spi_rx, len+2);
  2885. return true;
  2886. }
  2887. bool opi_send_cmd(uint8_t chain_id, uint8_t cmd, uint8_t *buff, int len)
  2888. {
  2889. int tx_len;
  2890. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2891. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2892. if ((buff == NULL) && (len != 0))
  2893. {
  2894. applog(LOG_ERR, "%s,%s() %d: para erro! ", __FILE__, __func__, __LINE__);
  2895. }
  2896. memset(spi_tx, 0, sizeof(spi_tx));
  2897. memset(spi_rx, 0, sizeof(spi_rx));
  2898. spi_tx[0] = OPI_HI_BYTE(CUSTOM_SYNC_HEAD);
  2899. spi_tx[1] = OPI_LO_BYTE(CUSTOM_SYNC_HEAD);
  2900. spi_tx[2] = cmd;
  2901. spi_tx[3] = 0;
  2902. if (len > 0)
  2903. {
  2904. memcpy(spi_tx + 4, buff, len);
  2905. }
  2906. tx_len = (4 + len + 1) & ~1;
  2907. if (opi_spi_read_write(chain_id, spi_tx, spi_rx, tx_len))
  2908. {
  2909. return true;
  2910. }
  2911. else
  2912. {
  2913. applog(LOG_ERR, "send command fail !");
  2914. return false;
  2915. }
  2916. }
  2917. bool opi_poll_rslt(uint8_t chain_id, uint8_t __maybe_unused cmd, uint8_t *buff, int len)
  2918. {
  2919. int i;
  2920. int tx_len;
  2921. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2922. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2923. if ((buff == NULL) && (len != 0))
  2924. {
  2925. applog(LOG_ERR, "%s,%s() %d: para erro! ", __FILE__, __func__, __LINE__);
  2926. }
  2927. memset(spi_tx, 0, sizeof(spi_tx));
  2928. memset(spi_rx, 0, sizeof(spi_rx));
  2929. tx_len = g_chip_num * 4;
  2930. for(i = 0; i < tx_len; i++) {
  2931. cgsleep_ms(1); // usleep(1);
  2932. if (opi_spi_read_write(chain_id, NULL, spi_rx, 2))
  2933. {
  2934. break;
  2935. }
  2936. }
  2937. if (i >= tx_len)
  2938. {
  2939. applog(LOG_ERR, "%s,%d: poll fail !", __FILE__, __LINE__);
  2940. return false;
  2941. }
  2942. if ((spi_rx[0] != OPI_HI_BYTE(CUSTOM_SYNC_HEAD)) || (spi_rx[1] != OPI_LO_BYTE(CUSTOM_SYNC_HEAD)))
  2943. {
  2944. return false;
  2945. }
  2946. opi_spi_read_write(chain_id, NULL, spi_rx, 2);
  2947. if (spi_rx[1] != OPI_STATUS_SUC)
  2948. {
  2949. return false;
  2950. }
  2951. if (len > 0)
  2952. {
  2953. opi_spi_read_write(chain_id, NULL, spi_rx+2, len);
  2954. memcpy(buff, spi_rx+2, len);
  2955. }
  2956. return true;
  2957. }
  2958. void opi_set_power_en(unsigned char chain_id, int val)
  2959. {
  2960. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2961. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  2962. tx_buf[0] = (val >> 0) & 0xff;
  2963. tx_buf[1] = (val >> 8) & 0xff;
  2964. if (!opi_send_cmd(chain_id, OPI_SET_POWER_EN, tx_buf, 2))
  2965. {
  2966. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  2967. return;
  2968. }
  2969. if (!opi_poll_rslt(chain_id, OPI_SET_POWER_EN, NULL, 0))
  2970. {
  2971. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  2972. return;
  2973. }
  2974. }
  2975. void opi_set_start_en(unsigned char chain_id, int val)
  2976. {
  2977. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2978. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  2979. tx_buf[0] = (val >> 0) & 0xff;
  2980. tx_buf[1] = (val >> 8) & 0xff;
  2981. if (!opi_send_cmd(chain_id, OPI_SET_STARR_EN, tx_buf, 2))
  2982. {
  2983. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  2984. return;
  2985. }
  2986. if (!opi_poll_rslt(chain_id, OPI_SET_STARR_EN, NULL, 0))
  2987. {
  2988. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  2989. return;
  2990. }
  2991. }
  2992. bool opi_set_reset(unsigned char chain_id, int val)
  2993. {
  2994. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  2995. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  2996. tx_buf[0] = (val >> 0) & 0xff;
  2997. tx_buf[1] = (val >> 8) & 0xff;
  2998. if (!opi_send_cmd(chain_id, OPI_SET_RESET, tx_buf, 2)) {
  2999. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3000. return false;
  3001. }
  3002. if (!opi_poll_rslt(chain_id, OPI_SET_RESET, NULL, 0)) {
  3003. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3004. return false;
  3005. }
  3006. return true;
  3007. }
  3008. void opi_set_led(unsigned char chain_id, int val)
  3009. {
  3010. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3011. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3012. tx_buf[0] = (val >> 0) & 0xff;
  3013. tx_buf[1] = (val >> 8) & 0xff;
  3014. if (!opi_send_cmd(chain_id, OPI_SET_LED, tx_buf, 2))
  3015. {
  3016. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3017. return;
  3018. }
  3019. if (!opi_poll_rslt(chain_id, OPI_SET_LED, NULL, 0))
  3020. {
  3021. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3022. return;
  3023. }
  3024. }
  3025. int opi_get_plug(unsigned char chain_id)
  3026. {
  3027. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3028. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3029. if (!opi_send_cmd(chain_id, OPI_SET_LED, NULL, 0))
  3030. {
  3031. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3032. return -1;
  3033. }
  3034. memset(rx_buf, 0, sizeof(rx_buf));
  3035. if (!opi_poll_rslt(chain_id, OPI_SET_LED, rx_buf, 2))
  3036. {
  3037. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3038. return -1;
  3039. }
  3040. return (int)(rx_buf[0]);
  3041. }
  3042. bool opi_set_vid(unsigned char chain_id, int vid)
  3043. {
  3044. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3045. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3046. tx_buf[0] = (vid >> 0) & 0xff;
  3047. tx_buf[1] = (vid >> 8) & 0xff;
  3048. if (!opi_send_cmd(chain_id, OPI_SET_VID, tx_buf, 2))
  3049. {
  3050. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3051. return false;
  3052. }
  3053. if (!opi_poll_rslt(chain_id, OPI_SET_VID, NULL, 0))
  3054. {
  3055. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3056. return false;
  3057. }
  3058. return true;
  3059. }
  3060. void opi_set_pwm(unsigned char fan_id, int frequency, int duty)
  3061. {
  3062. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3063. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3064. tx_buf[0] = fan_id;
  3065. tx_buf[1] = 0x00;
  3066. tx_buf[2] = (frequency >> 0) & 0xff;
  3067. tx_buf[3] = (frequency >> 8) & 0xff;
  3068. tx_buf[4] = (frequency >> 16) & 0xff;
  3069. tx_buf[5] = (frequency >> 24) & 0xff;
  3070. tx_buf[6] = (duty >> 0) & 0xff;
  3071. tx_buf[7] = (duty >> 8) & 0xff;
  3072. if (!opi_send_cmd(0, OPI_SET_PWM, tx_buf, 8))
  3073. {
  3074. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3075. return;
  3076. }
  3077. if (!opi_poll_rslt(0, OPI_SET_PWM, NULL, 0))
  3078. {
  3079. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3080. return;
  3081. }
  3082. }
  3083. bool opi_chain_power_on(unsigned char chain_id)
  3084. {
  3085. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3086. if (!opi_send_cmd(chain_id, OPI_POWER_ON, NULL, 0))
  3087. {
  3088. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3089. return false;
  3090. }
  3091. if (!opi_poll_rslt(chain_id, OPI_POWER_ON, NULL, 0))
  3092. {
  3093. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3094. return false;
  3095. }
  3096. return true;
  3097. }
  3098. bool opi_chain_power_down(unsigned char chain_id)
  3099. {
  3100. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3101. if (!opi_send_cmd(chain_id, OPI_POWER_DOWN, NULL, 0))
  3102. {
  3103. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3104. return false;
  3105. }
  3106. if (!opi_poll_rslt(chain_id, OPI_POWER_DOWN, NULL, 0))
  3107. {
  3108. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3109. return false;
  3110. }
  3111. return true;
  3112. }
  3113. bool opi_chain_hw_reset(unsigned char chain_id)
  3114. {
  3115. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3116. if (!opi_send_cmd(chain_id, OPI_POWER_RESET, NULL, 0))
  3117. {
  3118. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3119. return false;
  3120. }
  3121. if (!opi_poll_rslt(chain_id, OPI_POWER_RESET, NULL, 0))
  3122. {
  3123. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3124. return false;
  3125. }
  3126. return true;
  3127. }
  3128. bool opi_chain_power_on_all(void)
  3129. {
  3130. int i;
  3131. for(i = 0; i < g_chain_num; i++)
  3132. {
  3133. opi_chain_power_on(i);
  3134. }
  3135. return true;
  3136. }
  3137. bool opi_chain_power_down_all(void)
  3138. {
  3139. int i;
  3140. for(i = 0; i < g_chain_num; i++)
  3141. {
  3142. opi_chain_power_down(i);
  3143. }
  3144. return true;
  3145. }
  3146. void opi_set_spi_speed(unsigned char chain_id, int index)
  3147. {
  3148. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3149. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3150. tx_buf[0] = (index >> 0) & 0xff;
  3151. tx_buf[1] = (index >> 8) & 0xff;
  3152. if (!opi_send_cmd(chain_id, OPI_SET_SPI_SPEED, tx_buf, 2))
  3153. {
  3154. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3155. return;
  3156. }
  3157. if (!opi_poll_rslt(chain_id, OPI_SET_SPI_SPEED, NULL, 0))
  3158. {
  3159. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3160. return;
  3161. }
  3162. }
  3163. const int pin_power_en[] =
  3164. {
  3165. MCOMPAT_CONFIG_CHAIN0_POWER_EN_GPIO,
  3166. MCOMPAT_CONFIG_CHAIN1_POWER_EN_GPIO,
  3167. MCOMPAT_CONFIG_CHAIN2_POWER_EN_GPIO,
  3168. MCOMPAT_CONFIG_CHAIN3_POWER_EN_GPIO,
  3169. MCOMPAT_CONFIG_CHAIN4_POWER_EN_GPIO,
  3170. MCOMPAT_CONFIG_CHAIN5_POWER_EN_GPIO,
  3171. MCOMPAT_CONFIG_CHAIN6_POWER_EN_GPIO,
  3172. MCOMPAT_CONFIG_CHAIN7_POWER_EN_GPIO
  3173. };
  3174. const int pin_start_en[] =
  3175. {
  3176. MCOMPAT_CONFIG_CHAIN0_START_EN_GPIO,
  3177. MCOMPAT_CONFIG_CHAIN1_START_EN_GPIO,
  3178. MCOMPAT_CONFIG_CHAIN2_START_EN_GPIO,
  3179. MCOMPAT_CONFIG_CHAIN3_START_EN_GPIO,
  3180. MCOMPAT_CONFIG_CHAIN4_START_EN_GPIO,
  3181. MCOMPAT_CONFIG_CHAIN5_START_EN_GPIO,
  3182. MCOMPAT_CONFIG_CHAIN6_START_EN_GPIO,
  3183. MCOMPAT_CONFIG_CHAIN7_START_EN_GPIO
  3184. };
  3185. const int pin_reset[] =
  3186. {
  3187. MCOMPAT_CONFIG_CHAIN0_RESET_GPIO,
  3188. MCOMPAT_CONFIG_CHAIN1_RESET_GPIO,
  3189. MCOMPAT_CONFIG_CHAIN2_RESET_GPIO,
  3190. MCOMPAT_CONFIG_CHAIN3_RESET_GPIO,
  3191. MCOMPAT_CONFIG_CHAIN4_RESET_GPIO,
  3192. MCOMPAT_CONFIG_CHAIN5_RESET_GPIO,
  3193. MCOMPAT_CONFIG_CHAIN6_RESET_GPIO,
  3194. MCOMPAT_CONFIG_CHAIN7_RESET_GPIO
  3195. };
  3196. const int pin_plug[] =
  3197. {
  3198. MCOMPAT_CONFIG_CHAIN0_PLUG_GPIO,
  3199. MCOMPAT_CONFIG_CHAIN1_PLUG_GPIO,
  3200. MCOMPAT_CONFIG_CHAIN2_PLUG_GPIO,
  3201. MCOMPAT_CONFIG_CHAIN3_PLUG_GPIO,
  3202. MCOMPAT_CONFIG_CHAIN4_PLUG_GPIO,
  3203. MCOMPAT_CONFIG_CHAIN5_PLUG_GPIO,
  3204. MCOMPAT_CONFIG_CHAIN6_PLUG_GPIO,
  3205. MCOMPAT_CONFIG_CHAIN7_PLUG_GPIO
  3206. };
  3207. const int pin_led[] =
  3208. {
  3209. MCOMPAT_CONFIG_CHAIN0_LED_GPIO,
  3210. MCOMPAT_CONFIG_CHAIN1_LED_GPIO,
  3211. MCOMPAT_CONFIG_CHAIN2_LED_GPIO,
  3212. MCOMPAT_CONFIG_CHAIN3_LED_GPIO,
  3213. MCOMPAT_CONFIG_CHAIN4_LED_GPIO,
  3214. MCOMPAT_CONFIG_CHAIN5_LED_GPIO,
  3215. MCOMPAT_CONFIG_CHAIN6_LED_GPIO,
  3216. MCOMPAT_CONFIG_CHAIN7_LED_GPIO
  3217. };
  3218. void spi_send_data_in_word(ZYNQ_SPI_T *spi, unsigned char *buf, int len)
  3219. {
  3220. int i;
  3221. for(i = 0; i < len; i = i + 2)
  3222. {
  3223. zynq_spi_write(spi, buf + i, 2);
  3224. }
  3225. }
  3226. void spi_recv_data_in_word(ZYNQ_SPI_T *spi, unsigned char *buf, int len)
  3227. {
  3228. int i;
  3229. for(i = 0; i < len; i = i + 2)
  3230. {
  3231. zynq_spi_read(spi, buf + i, 2);
  3232. }
  3233. }
  3234. void spi_send_data(ZYNQ_SPI_T *spi, unsigned char *buf, int len)
  3235. {
  3236. zynq_spi_write(spi, buf, len);
  3237. }
  3238. void spi_recv_data(ZYNQ_SPI_T *spi, unsigned char *buf, int len)
  3239. {
  3240. zynq_spi_read(spi, buf, len);
  3241. }
  3242. bool spi_send_command(ZYNQ_SPI_T *spi, unsigned char cmd, unsigned char chip_id, unsigned char *buff, int len)
  3243. {
  3244. int tx_len;
  3245. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3246. if ((len > 0) && (buff == NULL))
  3247. {
  3248. applog(LOG_ERR, "%s,%d: para error !", __FILE__, __LINE__);
  3249. return false;
  3250. }
  3251. memset(tx_buf, 0, sizeof(tx_buf));
  3252. tx_buf[0] = cmd;
  3253. tx_buf[1] = chip_id;
  3254. if (len > 0)
  3255. {
  3256. memcpy(tx_buf + 2, buff, len);
  3257. }
  3258. tx_len = (2 + len + 1) & ~1;
  3259. //spi_send_data_in_word(spi, tx_buf, tx_len);
  3260. spi_send_data(spi, tx_buf, tx_len);
  3261. return true;
  3262. }
  3263. bool spi_poll_result(ZYNQ_SPI_T *spi, unsigned char cmd, unsigned char __maybe_unused chip_id, unsigned char *buff, int len)
  3264. {
  3265. int i;
  3266. int max_len;
  3267. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3268. max_len = g_chip_num * 4;
  3269. memset(rx_buf, 0, sizeof(rx_buf));
  3270. for(i = 0; i < max_len; i = i + 2)
  3271. {
  3272. spi_recv_data(spi, rx_buf, 2);
  3273. if ((rx_buf[0] & 0x0f) == cmd)
  3274. {
  3275. break;
  3276. }
  3277. }
  3278. if (i >= max_len)
  3279. {
  3280. applog(LOG_ERR, "%s,%d: poll fail !", __FILE__, __LINE__);
  3281. return false;
  3282. }
  3283. spi_recv_data_in_word(spi, rx_buf+2, len);
  3284. memcpy(buff, rx_buf, len+2);
  3285. return true;
  3286. }
  3287. void init_spi_gpio(int chain_num)
  3288. {
  3289. int i;
  3290. for(i = 0; i < chain_num; i++)
  3291. {
  3292. zynq_gpio_init(pin_power_en[i], 0);
  3293. zynq_gpio_init(pin_start_en[i], 0);
  3294. zynq_gpio_init(pin_reset[i], 0);
  3295. zynq_gpio_init(pin_led[i], 0);
  3296. zynq_gpio_init(pin_plug[i], 1);
  3297. }
  3298. }
  3299. void exit_spi_gpio(int chain_num)
  3300. {
  3301. int i;
  3302. for(i = 0; i < chain_num; i++)
  3303. {
  3304. zynq_gpio_exit(pin_power_en[i]);
  3305. zynq_gpio_exit(pin_start_en[i]);
  3306. zynq_gpio_exit(pin_reset[i]);
  3307. zynq_gpio_exit(pin_led[i]);
  3308. zynq_gpio_exit(pin_plug[i]);
  3309. }
  3310. }
  3311. void spi_set_power_en(unsigned char chain_id, int val)
  3312. {
  3313. zynq_gpio_write(pin_power_en[chain_id], val);
  3314. }
  3315. void spi_set_start_en(unsigned char chain_id, int val)
  3316. {
  3317. zynq_gpio_write(pin_start_en[chain_id], val);
  3318. }
  3319. bool spi_set_reset(unsigned char chain_id, int val)
  3320. {
  3321. return zynq_gpio_write(pin_reset[chain_id], val);
  3322. }
  3323. void spi_set_led(unsigned char chain_id, int val)
  3324. {
  3325. zynq_gpio_write(pin_led[chain_id], val);
  3326. }
  3327. int spi_get_plug(unsigned char chain_id)
  3328. {
  3329. return zynq_gpio_read(pin_plug[chain_id]);
  3330. }
  3331. static int s_vid = 0;
  3332. bool spi_set_vid(unsigned char chain_id, int vid)
  3333. {
  3334. if (g_platform == PLATFORM_ZYNQ_SPI_G19)
  3335. {
  3336. zynq_gpio_g19_vid_set(chain_id, vid);
  3337. }
  3338. else if (g_platform == PLATFORM_ZYNQ_SPI_G9)
  3339. {
  3340. if (s_vid != vid)
  3341. {
  3342. zynq_gpio_g9_vid_set(vid);
  3343. }
  3344. }
  3345. else
  3346. {
  3347. applog(LOG_ERR, "platform[%d] error in set vid ", g_platform);
  3348. return false;
  3349. }
  3350. return true;
  3351. }
  3352. void spi_set_spi_speed(unsigned char __maybe_unused chain_id, int index)
  3353. {
  3354. uint32_t cfg[] = {390625, 781250, 1562500, 3125000, 6250000, 9960000};
  3355. zynq_set_spi_speed(cfg[index]);
  3356. }
  3357. bool zynq_chain_power_on(unsigned char chain_id)
  3358. {
  3359. if (mcompat_get_plug(chain_id) != 0)
  3360. {
  3361. applog(LOG_NOTICE, "chain %d >>> the board not inserted !!!", chain_id);
  3362. return false;
  3363. }
  3364. mcompat_set_power_en(chain_id, 1);
  3365. sleep(5);
  3366. mcompat_set_reset(chain_id, 1);
  3367. sleep(1);
  3368. mcompat_set_start_en(chain_id, 1);
  3369. return true;
  3370. }
  3371. bool zynq_chain_power_down(unsigned char chain_id)
  3372. {
  3373. mcompat_set_power_en(chain_id, 0);
  3374. sleep(1);
  3375. mcompat_set_reset(chain_id, 0);
  3376. mcompat_set_start_en(chain_id, 0);
  3377. mcompat_set_led(chain_id, 1);
  3378. return true;
  3379. }
  3380. bool zynq_chain_hw_reset(unsigned char chain_id)
  3381. {
  3382. mcompat_set_reset(chain_id, 0);
  3383. sleep(1);
  3384. mcompat_set_reset(chain_id, 1);
  3385. sleep(1);
  3386. return true;
  3387. }
  3388. bool zynq_chain_power_on_all(void)
  3389. {
  3390. int i;
  3391. for(i = 0; i < g_chain_num; i++)
  3392. {
  3393. if (mcompat_get_plug(i) != 0)
  3394. {
  3395. applog(LOG_NOTICE, "chain %d >>> the board not inserted !!! ", i);
  3396. }
  3397. }
  3398. for(i = 0; i < g_chain_num; i++) {
  3399. mcompat_set_power_en(i, 1);
  3400. cgsleep_ms(5);
  3401. }
  3402. sleep(5);
  3403. for(i = 0; i < g_chain_num; i++) {
  3404. mcompat_set_reset(i, 1);
  3405. cgsleep_ms(5);
  3406. }
  3407. sleep(1);
  3408. for(i = 0; i < g_chain_num; i++) {
  3409. mcompat_set_start_en(i, 1);
  3410. cgsleep_ms(5);
  3411. }
  3412. return true;
  3413. }
  3414. bool zynq_chain_power_down_all(void)
  3415. {
  3416. int i;
  3417. for(i = 0; i < g_chain_num; i++) {
  3418. mcompat_set_power_en(i, 0);
  3419. }
  3420. sleep(1);
  3421. for(i = 0; i < g_chain_num; i++) {
  3422. mcompat_set_reset(i, 0);
  3423. mcompat_set_start_en(i, 0);
  3424. mcompat_set_led(i, 1);
  3425. }
  3426. return true;
  3427. }
  3428. bool init_hub_cmd(int chain_num, int chip_num)
  3429. {
  3430. int i;
  3431. for(i = 0; i < chain_num; i++)
  3432. {
  3433. hub_spi_init(i, chip_num);
  3434. }
  3435. return true;
  3436. }
  3437. bool exit_hub_cmd(int __maybe_unused chain_num)
  3438. {
  3439. return true;
  3440. }
  3441. bool hub_cmd_reset(unsigned char chain_id, unsigned char chip_id, unsigned char *in, unsigned char *out)
  3442. {
  3443. uint32_t cfg_len = 0;
  3444. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3445. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3446. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3447. memset(spi_tx, 0, sizeof(spi_tx));
  3448. memset(spi_rx, 0, sizeof(spi_rx));
  3449. spi_tx[0] = CMD_RESET;
  3450. spi_tx[1] = chip_id;
  3451. spi_tx[2] = in[0];
  3452. spi_tx[3] = in[1];
  3453. //cfg_len = 0x03000200;
  3454. cfg_len += (0x03) << 24;
  3455. cfg_len += (0x00) << 16;
  3456. cfg_len += (0x02) << 8;
  3457. cfg_len += (0x00) << 0;
  3458. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3459. {
  3460. return false;
  3461. }
  3462. //print_data_hex("tx:", spi_tx, 8);
  3463. //print_data_hex("rx:", spi_rx, 8);
  3464. memcpy(out, spi_rx, 4);
  3465. return true;
  3466. }
  3467. int hub_cmd_bist_start(unsigned char chain_id, unsigned char chip_id)
  3468. {
  3469. uint32_t cfg_len = 0;
  3470. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3471. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3472. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3473. memset(spi_tx, 0, sizeof(spi_tx));
  3474. memset(spi_rx, 0, sizeof(spi_rx));
  3475. spi_tx[0] = CMD_BIST_START;
  3476. spi_tx[1] = chip_id;
  3477. //cfg_len = 0x02000200;
  3478. cfg_len += (0x02) << 24;
  3479. cfg_len += (0x00) << 16;
  3480. cfg_len += (0x02) << 8;
  3481. cfg_len += (0x00) << 0;
  3482. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3483. {
  3484. return -1;
  3485. }
  3486. //print_data_hex("tx:", spi_tx, 8);
  3487. //print_data_hex("rx:", spi_rx, 8);
  3488. return spi_rx[3];;
  3489. }
  3490. bool hub_cmd_bist_collect(unsigned char chain_id, unsigned char chip_id)
  3491. {
  3492. uint32_t cfg_len = 0;
  3493. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3494. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3495. memset(spi_tx, 0, sizeof(spi_tx));
  3496. memset(spi_rx, 0, sizeof(spi_rx));
  3497. spi_tx[0] = CMD_BIST_COLLECT;
  3498. spi_tx[1] = chip_id;
  3499. //cfg_len = 0x02000200;
  3500. cfg_len += (0x02) << 24;
  3501. cfg_len += (0x00) << 16;
  3502. cfg_len += (0x02) << 8;
  3503. cfg_len += (0x00) << 0;
  3504. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3505. {
  3506. return false;
  3507. }
  3508. return true;
  3509. }
  3510. bool hub_cmd_bist_fix(unsigned char chain_id, unsigned char chip_id)
  3511. {
  3512. uint32_t cfg_len = 0;
  3513. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3514. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3515. memset(spi_tx, 0, sizeof(spi_tx));
  3516. memset(spi_rx, 0, sizeof(spi_rx));
  3517. spi_tx[0] = CMD_BIST_FIX;
  3518. spi_tx[1] = chip_id;
  3519. //cfg_len = 0x02000200;
  3520. cfg_len += (0x02) << 24;
  3521. cfg_len += (0x00) << 16;
  3522. cfg_len += (0x02) << 8;
  3523. cfg_len += (0x00) << 0;
  3524. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3525. {
  3526. return false;
  3527. }
  3528. return true;
  3529. }
  3530. bool hub_cmd_write_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  3531. {
  3532. uint32_t cfg_len = 0;
  3533. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3534. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3535. memset(spi_tx, 0, sizeof(spi_tx));
  3536. memset(spi_rx, 0, sizeof(spi_rx));
  3537. spi_tx[0] = CMD_WRITE_REG;
  3538. spi_tx[1] = chip_id;
  3539. memcpy(spi_tx + 2, reg, len);
  3540. //cfg_len = 0x09070807;
  3541. cfg_len += (((len / 2) + 2) & 0xff) << 24;
  3542. cfg_len += (((len / 2) + 1) & 0xff) << 16;
  3543. cfg_len += (((len / 2) + 2) & 0xff) << 8;
  3544. cfg_len += (((len / 2) + 1) & 0xff) << 0;
  3545. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3546. {
  3547. return false;
  3548. }
  3549. return true;
  3550. }
  3551. bool hub_cmd_read_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  3552. {
  3553. uint32_t cfg_len = 0;
  3554. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3555. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3556. int i;
  3557. unsigned short crc1, crc2;
  3558. uint8_t tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3559. memset(spi_tx, 0, sizeof(spi_tx));
  3560. memset(spi_rx, 0, sizeof(spi_rx));
  3561. spi_tx[0] = CMD_READ_REG;
  3562. spi_tx[1] = chip_id;
  3563. //cfg_len = 0x02000807;
  3564. cfg_len += (0x02) << 24;
  3565. cfg_len += (0x00) << 16;
  3566. cfg_len += (((len / 2) + 2) & 0xff) << 8;
  3567. cfg_len += (((len / 2) + 1) & 0xff) << 0;
  3568. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3569. {
  3570. return false;
  3571. }
  3572. for(i = 0; i < len + 2; i = i + 2)
  3573. {
  3574. tmp_buf[i + 0] = spi_rx[i + 1];
  3575. tmp_buf[i + 1] = spi_rx[i + 0];
  3576. }
  3577. crc1 = CRC16_2(tmp_buf, len + 2);
  3578. crc2 = (spi_rx[2 + len + 0] << 8) + (spi_rx[2 + len + 1] << 0);
  3579. if (crc1 != crc2) {
  3580. applog(LOG_WARNING, "%s crc error !", __FUNCTION__);
  3581. return false;
  3582. }
  3583. memcpy(reg, spi_rx + 2, len);
  3584. return true;
  3585. }
  3586. bool hub_cmd_read_write_reg0d(unsigned char chain_id, unsigned char chip_id, unsigned char *in, int len, unsigned char *out)
  3587. {
  3588. uint32_t cfg_len = 0;
  3589. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3590. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3591. int i;
  3592. unsigned short crc1, crc2;
  3593. uint8_t tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3594. memset(spi_tx, 0, sizeof(spi_tx));
  3595. memset(spi_rx, 0, sizeof(spi_rx));
  3596. spi_tx[0] = CMD_WRITE_REG0d;
  3597. spi_tx[1] = chip_id;
  3598. memcpy(spi_tx + 2, in, len);
  3599. //cfg_len = 0x09070807;
  3600. cfg_len += (((len / 2) + 2) & 0xff) << 24;
  3601. cfg_len += (((len / 2) + 1) & 0xff) << 16;
  3602. cfg_len += (((len / 2) + 2) & 0xff) << 8;
  3603. cfg_len += (((len / 2) + 1) & 0xff) << 0;
  3604. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3605. {
  3606. return false;
  3607. }
  3608. for(i = 0; i < len + 2; i = i + 2)
  3609. {
  3610. tmp_buf[i + 0] = spi_rx[i + 1];
  3611. tmp_buf[i + 1] = spi_rx[i + 0];
  3612. }
  3613. crc1 = CRC16_2(tmp_buf, len + 2);
  3614. crc2 = (spi_rx[2 + len + 0] << 8) + (spi_rx[2 + len + 1] << 0);
  3615. if (crc1 != crc2) {
  3616. applog(LOG_WARNING, "%s crc error !", __FUNCTION__);
  3617. return false;
  3618. }
  3619. memcpy(out, spi_rx + 2, len);
  3620. return true;
  3621. }
  3622. bool hub_cmd_write_job(unsigned char chain_id, unsigned char chip_id, unsigned char *job, int len)
  3623. {
  3624. uint32_t cfg_len = 0;
  3625. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3626. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3627. memset(spi_tx, 0, sizeof(spi_tx));
  3628. memset(spi_rx, 0, sizeof(spi_rx));
  3629. memcpy(spi_tx, job, len);
  3630. //cfg_len = 0x504f0000;
  3631. cfg_len += (((len / 2) + 1) & 0xff) << 24;
  3632. cfg_len += (((len / 2) - 1) & 0xff) << 16;
  3633. cfg_len += (0x00) << 8;
  3634. cfg_len += (0x00) << 0;
  3635. if (send_job_queue(chain_id, spi_tx, spi_rx, cfg_len, (chip_id == 1)) == XST_FAILURE)
  3636. {
  3637. return false;
  3638. }
  3639. return true;
  3640. }
  3641. bool hub_cmd_read_result(unsigned char chain_id, unsigned char chip_id, unsigned char *res, int len)
  3642. {
  3643. uint32_t cfg_len = 0;
  3644. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3645. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3646. int i;
  3647. unsigned short crc1, crc2;
  3648. uint8_t tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3649. memset(spi_tx, 0, sizeof(spi_tx));
  3650. memset(spi_rx, 0, sizeof(spi_rx));
  3651. spi_tx[0] = CMD_READ_RESULT;
  3652. spi_tx[1] = chip_id;
  3653. // cfg_len = 0x02000605;
  3654. cfg_len += (0x02) << 24;
  3655. cfg_len += (0x00) << 16;
  3656. cfg_len += (((len / 2) + 3) & 0xff) << 8;
  3657. cfg_len += (((len / 2) + 2) & 0xff) << 0;
  3658. if (do_spi_cmd(chain_id, spi_tx, spi_rx, cfg_len) == XST_FAILURE)
  3659. return false;
  3660. if (spi_rx[1] == 0)
  3661. return false;
  3662. for (i = 0; i < len + 2; i = i + 2) {
  3663. tmp_buf[i + 0] = spi_rx[i + 1];
  3664. tmp_buf[i + 1] = spi_rx[i + 0];
  3665. }
  3666. crc1 = CRC16_2(tmp_buf, len + 2);
  3667. crc2 = (spi_rx[2 + len + 0] << 8) + (spi_rx[2 + len + 1] << 0);
  3668. if (crc1 != crc2) {
  3669. applog(LOG_WARNING, "%s crc error !", __FUNCTION__);
  3670. return false;
  3671. }
  3672. memcpy(res, spi_rx, len + 2);
  3673. return true;
  3674. }
  3675. bool hub_cmd_auto_nonce(unsigned char chain_id, int mode, int len)
  3676. {
  3677. uint16_t cmd08 = 0x0800;
  3678. uint32_t cfg_len = 0;
  3679. // cfg_len = 0x02000605;
  3680. cfg_len += (0x02) << 24;
  3681. cfg_len += (0x00) << 16;
  3682. cfg_len += (((len / 2) + 3) & 0xff) << 8;
  3683. cfg_len += (((len / 2) + 2) & 0xff) << 0;
  3684. if (mode == 0)
  3685. disable_auto_nonce(chain_id);
  3686. else
  3687. enable_auto_nonce(chain_id, cmd08, cfg_len);
  3688. return true;
  3689. }
  3690. bool hub_cmd_read_nonce(unsigned char chain_id, unsigned char *res, int len)
  3691. {
  3692. uint32_t cfg_len = 0;
  3693. uint8_t spi_tx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3694. uint8_t spi_rx[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3695. memset(spi_tx, 0, sizeof(spi_tx));
  3696. memset(spi_rx, 0, sizeof(spi_rx));
  3697. // cfg_len = 0x02000605;
  3698. cfg_len += (0x02) << 24;
  3699. cfg_len += (0x00) << 16;
  3700. cfg_len += (((len / 2) + 3) & 0xff) << 8;
  3701. cfg_len += (((len / 2) + 2) & 0xff) << 0;
  3702. if (!rece_queue_has_nonce(chain_id, 1000))
  3703. return false;
  3704. read_nonce_buffer(chain_id, spi_rx, cfg_len);
  3705. if (spi_rx[1] == 0)
  3706. return false;
  3707. //print_data_hex("read_nonce rx:", spi_rx, len + 2);
  3708. memcpy(res, spi_rx, len + 2);
  3709. return true;
  3710. }
  3711. bool hub_cmd_get_temp(mcompat_fan_temp_s *fan_temp_ctrl,unsigned char chain_id)
  3712. {
  3713. uint32_t val;
  3714. if (hub_get_plug(chain_id))
  3715. return false;
  3716. enable_auto_cmd0a(chain_id,g_dangerous_temp,33,24,0,0);
  3717. mcompat_temp_s *temp_ctrl = &fan_temp_ctrl->mcompat_temp[chain_id];
  3718. do{
  3719. val = Xil_SPI_In32(SPI_AXIBASE + SPI_BASEADDR_GAP*chain_id+AUTO_CMD0A_REG4_ADDR);
  3720. }while(!((val >> 24) & 0x1));
  3721. hub_get_hitemp_stat(chain_id,temp_ctrl);
  3722. hub_get_lotemp_stat(chain_id,temp_ctrl);
  3723. hub_get_avgtemp_stat(chain_id,temp_ctrl);
  3724. disable_auto_cmd0a(chain_id,g_dangerous_temp,33,24,0,0);
  3725. return true;
  3726. }
  3727. bool init_opi_cmd(void)
  3728. {
  3729. opi_spi_init();
  3730. return true;
  3731. }
  3732. bool exit_opi_cmd(void)
  3733. {
  3734. opi_spi_exit();
  3735. return true;
  3736. }
  3737. bool opi_cmd_reset(unsigned char chain_id, unsigned char chip_id, unsigned char *in, unsigned char *out)
  3738. {
  3739. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3740. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3741. if (!opi_send_command(chain_id, CMD_RESET, chip_id, in, 2))
  3742. {
  3743. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3744. return false;
  3745. }
  3746. memset(rx_buf, 0, sizeof(rx_buf));
  3747. if (!opi_poll_result(chain_id, CMD_RESET, chip_id, rx_buf, 2))
  3748. {
  3749. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3750. return false;
  3751. }
  3752. memcpy(out, rx_buf, 4);
  3753. return true;
  3754. }
  3755. int opi_cmd_bist_start(unsigned char chain_id, unsigned char chip_id)
  3756. {
  3757. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3758. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3759. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3760. memset(tx_buf, 0, sizeof(tx_buf));
  3761. if (!opi_send_command(chain_id, CMD_BIST_START, chip_id, tx_buf, 2))
  3762. {
  3763. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3764. return -1;
  3765. }
  3766. memset(rx_buf, 0, sizeof(rx_buf));
  3767. if (!opi_poll_result(chain_id, CMD_BIST_START, chip_id, rx_buf, 2))
  3768. {
  3769. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3770. return -1;
  3771. }
  3772. return rx_buf[3];
  3773. }
  3774. bool opi_cmd_bist_collect(unsigned char chain_id, unsigned char chip_id)
  3775. {
  3776. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3777. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3778. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3779. memset(tx_buf, 0, sizeof(tx_buf));
  3780. if (!opi_send_command(chain_id, CMD_BIST_COLLECT, chip_id, tx_buf, 2))
  3781. {
  3782. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3783. return false;
  3784. }
  3785. memset(rx_buf, 0, sizeof(rx_buf));
  3786. if (!opi_poll_result(chain_id, CMD_BIST_COLLECT, chip_id, rx_buf, 2))
  3787. {
  3788. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3789. return false;
  3790. }
  3791. return true;
  3792. }
  3793. bool opi_cmd_bist_fix(unsigned char chain_id, unsigned char chip_id)
  3794. {
  3795. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3796. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3797. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3798. memset(tx_buf, 0, sizeof(tx_buf));
  3799. if (!opi_send_command(chain_id, CMD_BIST_FIX, chip_id, tx_buf, 2))
  3800. {
  3801. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3802. return false;
  3803. }
  3804. memset(rx_buf, 0, sizeof(rx_buf));
  3805. if (!opi_poll_result(chain_id, CMD_BIST_FIX, chip_id, rx_buf, 2))
  3806. {
  3807. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3808. return false;
  3809. }
  3810. return true;
  3811. }
  3812. bool opi_cmd_write_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  3813. {
  3814. int i;
  3815. unsigned short crc;
  3816. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3817. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3818. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3819. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3820. if (reg == NULL)
  3821. {
  3822. applog(LOG_ERR, "%s,%d: %s para error !", __FILE__, __LINE__, __FUNCTION__);
  3823. return false;
  3824. }
  3825. memset(tx_buf, 0, sizeof(tx_buf));
  3826. tx_buf[0] = OPI_HI_BYTE(AX_CMD_SYNC_HEAD);
  3827. tx_buf[1] = OPI_LO_BYTE(AX_CMD_SYNC_HEAD);
  3828. tx_buf[2] = CMD_WRITE_REG;
  3829. tx_buf[3] = chip_id;
  3830. memcpy(tx_buf + 4, reg, len);
  3831. for(i = 0; i < len + 2; i = i + 2)
  3832. {
  3833. tmp_buf[i + 0] = tx_buf[i + 1 + 2];
  3834. tmp_buf[i + 1] = tx_buf[i + 0 + 2];
  3835. }
  3836. crc = CRC16_2(tmp_buf, len + 2);
  3837. tx_buf[4 + len + 0] = (unsigned char)((crc >> 8) & 0xff);
  3838. tx_buf[4 + len + 1] = (unsigned char)((crc >> 0) & 0xff);
  3839. opi_spi_read_write(chain_id, tx_buf, rx_buf, len + 6);
  3840. memset(rx_buf, 0, sizeof(rx_buf));
  3841. if (!opi_poll_result(chain_id, CMD_WRITE_REG, chip_id, rx_buf, len))
  3842. {
  3843. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3844. return false;
  3845. }
  3846. return true;
  3847. }
  3848. bool opi_cmd_read_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  3849. {
  3850. int i;
  3851. int max_len;
  3852. unsigned short crc1, crc2;
  3853. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3854. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3855. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3856. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3857. if (reg == NULL)
  3858. {
  3859. applog(LOG_ERR, "%s,%d: %s para error !", __FILE__, __LINE__, __FUNCTION__);
  3860. return false;
  3861. }
  3862. memset(tx_buf, 0, sizeof(tx_buf));
  3863. if (!opi_send_command(chain_id, CMD_READ_REG, chip_id, tx_buf, 0))
  3864. {
  3865. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3866. return false;
  3867. }
  3868. max_len = g_chip_num * 4;
  3869. memset(rx_buf, 0, sizeof(rx_buf));
  3870. for(i = 0; i < max_len; i = i + 2)
  3871. {
  3872. opi_spi_read_write(chain_id, NULL, rx_buf, 2);
  3873. if ((rx_buf[0] & 0x0f) == CMD_READ_REG)
  3874. {
  3875. break;
  3876. }
  3877. }
  3878. if (i >= max_len)
  3879. {
  3880. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3881. return false;
  3882. }
  3883. opi_spi_read_write(chain_id, NULL, rx_buf + 2, len + 2);
  3884. for(i = 0; i < len + 2; i = i + 2)
  3885. {
  3886. tmp_buf[i + 0] = rx_buf[i + 1];
  3887. tmp_buf[i + 1] = rx_buf[i + 0];
  3888. }
  3889. crc1 = CRC16_2(tmp_buf, len + 2);
  3890. crc2 = (rx_buf[2 + len + 0] << 8) + (rx_buf[2 + len + 1] << 0);
  3891. if (crc1 != crc2)
  3892. {
  3893. applog(LOG_WARNING, "%s,%d: %s crc fail !", __FILE__, __LINE__, __FUNCTION__);
  3894. return false;
  3895. }
  3896. memcpy(reg, rx_buf + 2, len);
  3897. return true;
  3898. }
  3899. bool opi_cmd_read_write_reg0d(unsigned char chain_id, unsigned char chip_id, unsigned char *in, int len, unsigned char *out)
  3900. {
  3901. int i;
  3902. int max_len;
  3903. unsigned short crc;
  3904. unsigned short crc1, crc2;
  3905. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3906. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3907. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3908. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3909. if ((in == NULL) || (out == NULL))
  3910. {
  3911. applog(LOG_ERR, "%s,%d: %s para error !", __FILE__, __LINE__, __FUNCTION__);
  3912. return false;
  3913. }
  3914. memset(tx_buf, 0, sizeof(tx_buf));
  3915. tx_buf[0] = OPI_HI_BYTE(AX_CMD_SYNC_HEAD);
  3916. tx_buf[1] = OPI_LO_BYTE(AX_CMD_SYNC_HEAD);
  3917. tx_buf[2] = CMD_WRITE_REG0d;
  3918. tx_buf[3] = chip_id;
  3919. memcpy(tx_buf + 4, in, len);
  3920. for(i = 0; i < len + 2; i = i + 2)
  3921. {
  3922. tmp_buf[i + 0] = tx_buf[i + 1 + 2];
  3923. tmp_buf[i + 1] = tx_buf[i + 0 + 2];
  3924. }
  3925. crc = CRC16_2(tmp_buf, len + 2);
  3926. tx_buf[4 + len + 0] = (unsigned char)((crc >> 8) & 0xff);
  3927. tx_buf[4 + len + 1] = (unsigned char)((crc >> 0) & 0xff);
  3928. opi_spi_read_write(chain_id, tx_buf, rx_buf, len + 6);
  3929. max_len = g_chip_num * 4;
  3930. memset(rx_buf, 0, sizeof(rx_buf));
  3931. for(i = 0; i < max_len; i = i + 2)
  3932. {
  3933. opi_spi_read_write(chain_id, NULL, rx_buf, 2);
  3934. if ((rx_buf[0] & 0x0f) == CMD_WRITE_REG0d)
  3935. {
  3936. break;
  3937. }
  3938. }
  3939. if (i >= max_len)
  3940. {
  3941. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3942. return false;
  3943. }
  3944. opi_spi_read_write(chain_id, NULL, rx_buf + 2, len + 2);
  3945. for(i = 0; i < len + 2; i = i + 2)
  3946. {
  3947. tmp_buf[i + 0] = rx_buf[i + 1];
  3948. tmp_buf[i + 1] = rx_buf[i + 0];
  3949. }
  3950. crc1 = CRC16_2(tmp_buf, len + 2);
  3951. crc2 = (rx_buf[2 + len + 0] << 8) + (rx_buf[2 + len + 1] << 0);
  3952. if (crc1 != crc2)
  3953. {
  3954. applog(LOG_WARNING, "%s,%d: %s crc fail !", __FILE__, __LINE__, __FUNCTION__);
  3955. return false;
  3956. }
  3957. memcpy(out, rx_buf + 2, len);
  3958. return true;
  3959. }
  3960. bool opi_cmd_read_result(unsigned char chain_id, unsigned char chip_id, unsigned char *res, int len)
  3961. {
  3962. int i;
  3963. int max_len;
  3964. unsigned short crc1, crc2;
  3965. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3966. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3967. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  3968. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  3969. if (res == NULL)
  3970. {
  3971. applog(LOG_ERR, "%s,%d: %s para error !", __FILE__, __LINE__, __FUNCTION__);
  3972. return false;
  3973. }
  3974. memset(tx_buf, 0, sizeof(tx_buf));
  3975. if (!opi_send_command(chain_id, CMD_READ_RESULT, chip_id, tx_buf, 2))
  3976. {
  3977. applog(LOG_WARNING, "%s,%d: %s send fail !", __FILE__, __LINE__, __FUNCTION__);
  3978. return false;
  3979. }
  3980. max_len = g_chip_num * 4;
  3981. memset(rx_buf, 0, sizeof(rx_buf));
  3982. for(i = 0; i < max_len; i = i + 2)
  3983. {
  3984. opi_spi_read_write(chain_id, NULL, rx_buf, 2);
  3985. if (((rx_buf[0] & 0x0f) == CMD_READ_RESULT) && (rx_buf[1] != 0))
  3986. {
  3987. break;
  3988. }
  3989. }
  3990. if (i >= max_len)
  3991. {
  3992. applog(LOG_WARNING, "%s,%d: %s poll fail !", __FILE__, __LINE__, __FUNCTION__);
  3993. return false;
  3994. }
  3995. opi_spi_read_write(chain_id, NULL, rx_buf + 2, len + 2);
  3996. for(i = 0; i < len + 2; i = i + 2)
  3997. {
  3998. tmp_buf[i + 0] = rx_buf[i + 1];
  3999. tmp_buf[i + 1] = rx_buf[i + 0];
  4000. }
  4001. crc1 = CRC16_2(tmp_buf, len + 2);
  4002. crc2 = (rx_buf[2 + len + 0] << 8) + (rx_buf[2 + len + 1] << 0);
  4003. if (crc1 != crc2)
  4004. {
  4005. applog(LOG_WARNING, "%s,%d: %s crc fail !", __FILE__, __LINE__, __FUNCTION__);
  4006. return false;
  4007. }
  4008. memcpy(res, rx_buf, len + 2);
  4009. return true;
  4010. }
  4011. bool opi_cmd_write_job(unsigned char chain_id, unsigned char chip_id, unsigned char *job, int len)
  4012. {
  4013. applog(LOG_DEBUG, "%s,%d: %s(%d, %d, %p, %d)", __FILE__, __LINE__, __FUNCTION__, chain_id, chip_id, job, len);
  4014. if (job == NULL)
  4015. {
  4016. applog(LOG_ERR, "%s,%d: %s para error !", __FILE__, __LINE__, __FUNCTION__);
  4017. return false;
  4018. }
  4019. return opi_spi_read_write(chain_id, NULL, job, len);
  4020. }
  4021. ZYNQ_SPI_T s_spi[MCOMPAT_CONFIG_MAX_CHAIN_NUM];
  4022. bool init_spi_cmd(int chain_num)
  4023. {
  4024. int i;
  4025. for(i = 0; i < chain_num; i++)
  4026. {
  4027. memset((void*)&s_spi[i], 0, sizeof(ZYNQ_SPI_T));
  4028. zynq_spi_init(&s_spi[i], i);
  4029. }
  4030. return true;
  4031. }
  4032. bool exit_spi_cmd(int chain_num)
  4033. {
  4034. int i;
  4035. for(i = 0; i < chain_num; i++)
  4036. {
  4037. zynq_spi_exit(&s_spi[i]);
  4038. }
  4039. return true;
  4040. }
  4041. bool spi_cmd_reset(unsigned char chain_id, unsigned char chip_id, unsigned char *in, unsigned char *out)
  4042. {
  4043. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4044. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4045. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4046. if (!spi_send_command(spi, CMD_RESET, chip_id, in, 2))
  4047. {
  4048. applog(LOG_WARNING, "%s send fail !", __FUNCTION__);
  4049. return false;
  4050. }
  4051. memset(rx_buf, 0, sizeof(rx_buf));
  4052. if (!spi_poll_result(spi, CMD_RESET, chip_id, rx_buf, 2))
  4053. {
  4054. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4055. return false;
  4056. }
  4057. memcpy(out, rx_buf, 4);
  4058. return true;
  4059. }
  4060. int spi_cmd_bist_start(unsigned char chain_id, unsigned char chip_id)
  4061. {
  4062. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4063. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4064. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4065. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4066. memset(tx_buf, 0, sizeof(tx_buf));
  4067. if (!spi_send_command(spi, CMD_BIST_START, chip_id, tx_buf, 2))
  4068. {
  4069. applog(LOG_WARNING, "%s send fail !", __FUNCTION__);
  4070. return -1;
  4071. }
  4072. memset(rx_buf, 0, sizeof(rx_buf));
  4073. if (!spi_poll_result(spi, CMD_BIST_START, chip_id, rx_buf, 2))
  4074. {
  4075. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4076. return -1;
  4077. }
  4078. return rx_buf[3];
  4079. }
  4080. bool spi_cmd_bist_collect(unsigned char chain_id, unsigned char chip_id)
  4081. {
  4082. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4083. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4084. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4085. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4086. memset(tx_buf, 0, sizeof(tx_buf));
  4087. if (!spi_send_command(spi, CMD_BIST_COLLECT, chip_id, tx_buf, 2))
  4088. {
  4089. applog(LOG_WARNING, "%s send fail !", __FUNCTION__);
  4090. return false;
  4091. }
  4092. memset(rx_buf, 0, sizeof(rx_buf));
  4093. if (!spi_poll_result(spi, CMD_BIST_COLLECT, chip_id, rx_buf, 2))
  4094. {
  4095. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4096. return false;
  4097. }
  4098. return true;
  4099. }
  4100. bool spi_cmd_bist_fix(unsigned char chain_id, unsigned char chip_id)
  4101. {
  4102. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4103. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4104. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4105. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4106. memset(tx_buf, 0, sizeof(tx_buf));
  4107. if (!spi_send_command(spi, CMD_BIST_FIX, chip_id, tx_buf, 2))
  4108. {
  4109. applog(LOG_WARNING, "%s send fail !", __FUNCTION__);
  4110. return false;
  4111. }
  4112. memset(rx_buf, 0, sizeof(rx_buf));
  4113. if (!spi_poll_result(spi, CMD_BIST_FIX, chip_id, rx_buf, 2))
  4114. {
  4115. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4116. return false;
  4117. }
  4118. return true;
  4119. }
  4120. bool spi_cmd_write_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  4121. {
  4122. int i;
  4123. unsigned short crc;
  4124. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4125. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4126. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4127. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4128. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4129. if (reg == NULL)
  4130. {
  4131. applog(LOG_ERR, "%s para error !", __FUNCTION__);
  4132. return false;
  4133. }
  4134. memset(tx_buf, 0, sizeof(tx_buf));
  4135. tx_buf[0] = CMD_WRITE_REG;
  4136. tx_buf[1] = chip_id;
  4137. memcpy(tx_buf + 2, reg, len);
  4138. for(i = 0; i < len + 2; i = i + 2)
  4139. {
  4140. tmp_buf[i + 0] = tx_buf[i + 1];
  4141. tmp_buf[i + 1] = tx_buf[i + 0];
  4142. }
  4143. crc = CRC16_2(tmp_buf, len + 2);
  4144. tx_buf[2 + len + 0] = (unsigned char)((crc >> 8) & 0xff);
  4145. tx_buf[2 + len + 1] = (unsigned char)((crc >> 0) & 0xff);
  4146. spi_send_data(spi, tx_buf, len + 4);
  4147. memset(rx_buf, 0, sizeof(rx_buf));
  4148. if (!spi_poll_result(spi, CMD_WRITE_REG, chip_id, rx_buf, len))
  4149. {
  4150. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4151. return false;
  4152. }
  4153. return true;
  4154. }
  4155. bool spi_cmd_read_register(unsigned char chain_id, unsigned char chip_id, unsigned char *reg, int len)
  4156. {
  4157. int i;
  4158. int max_len;
  4159. unsigned short crc1, crc2;
  4160. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4161. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4162. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4163. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4164. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4165. if (reg == NULL)
  4166. {
  4167. applog(LOG_ERR, "%s para error !", __FUNCTION__);
  4168. return false;
  4169. }
  4170. memset(tx_buf, 0, sizeof(tx_buf));
  4171. if (!spi_send_command(spi, CMD_READ_REG, chip_id, tx_buf, 0))
  4172. {
  4173. applog(LOG_WARNING, "%s send fail !", __FUNCTION__);
  4174. return false;
  4175. }
  4176. max_len = g_chip_num * 4;
  4177. memset(rx_buf, 0, sizeof(rx_buf));
  4178. for(i = 0; i < max_len; i = i + 2)
  4179. {
  4180. spi_recv_data(spi, rx_buf, 2);
  4181. if (rx_buf[0] == RESP_READ_REG)
  4182. {
  4183. break;
  4184. }
  4185. }
  4186. if (i >= max_len)
  4187. {
  4188. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4189. return false;
  4190. }
  4191. spi_recv_data_in_word(spi, rx_buf + 2, len + 2);
  4192. for(i = 0; i < len + 2; i = i + 2)
  4193. {
  4194. tmp_buf[i + 0] = rx_buf[i + 1];
  4195. tmp_buf[i + 1] = rx_buf[i + 0];
  4196. }
  4197. crc1 = CRC16_2(tmp_buf, len + 2);
  4198. crc2 = (rx_buf[2 + len + 0] << 8) + (rx_buf[2 + len + 1] << 0);
  4199. if (crc1 != crc2) {
  4200. applog(LOG_WARNING, "%s crc error !", __FUNCTION__);
  4201. return false;
  4202. }
  4203. memcpy(reg, rx_buf + 2, len);
  4204. return true;
  4205. }
  4206. bool spi_cmd_read_write_reg0d(unsigned char chain_id, unsigned char chip_id, unsigned char *in, int len, unsigned char *out)
  4207. {
  4208. int i;
  4209. int max_len;
  4210. unsigned short crc;
  4211. unsigned short crc1, crc2;
  4212. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4213. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4214. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4215. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4216. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4217. if ((in == NULL) || (out == NULL))
  4218. {
  4219. applog(LOG_ERR, "%s para error !", __FUNCTION__);
  4220. return false;
  4221. }
  4222. memset(tx_buf, 0, sizeof(tx_buf));
  4223. tx_buf[0] = CMD_WRITE_REG0d;
  4224. tx_buf[1] = chip_id;
  4225. memcpy(tx_buf + 2, in, len);
  4226. for(i = 0; i < len + 2; i = i + 2)
  4227. {
  4228. tmp_buf[i + 0] = tx_buf[i + 1];
  4229. tmp_buf[i + 1] = tx_buf[i + 0];
  4230. }
  4231. crc = CRC16_2(tmp_buf, len + 2);
  4232. tx_buf[2 + len + 0] = (unsigned char)((crc >> 8) & 0xff);
  4233. tx_buf[2 + len + 1] = (unsigned char)((crc >> 0) & 0xff);
  4234. spi_send_data(spi, tx_buf, len + 4);
  4235. max_len = g_chip_num * 4;
  4236. memset(rx_buf, 0, sizeof(rx_buf));
  4237. for(i = 0; i < max_len; i = i + 2)
  4238. {
  4239. spi_recv_data(spi, rx_buf, 2);
  4240. if ((rx_buf[0] & 0x0f) == CMD_WRITE_REG0d)
  4241. {
  4242. break;
  4243. }
  4244. }
  4245. if (i >= max_len)
  4246. {
  4247. applog(LOG_WARNING, "%s poll fail !", __FUNCTION__);
  4248. return false;
  4249. }
  4250. spi_recv_data_in_word(spi, rx_buf + 2, len + 2);
  4251. for(i = 0; i < len + 2; i = i + 2)
  4252. {
  4253. tmp_buf[i + 0] = rx_buf[i + 1];
  4254. tmp_buf[i + 1] = rx_buf[i + 0];
  4255. }
  4256. crc1 = CRC16_2(tmp_buf, len + 2);
  4257. crc2 = (rx_buf[2 + len + 0] << 8) + (rx_buf[2 + len + 1] << 0);
  4258. if (crc1 != crc2) {
  4259. applog(LOG_WARNING, "%s crc error !", __FUNCTION__);
  4260. return false;
  4261. }
  4262. memcpy(out, rx_buf + 2, len);
  4263. return true;
  4264. }
  4265. bool spi_cmd_read_result(unsigned char chain_id, unsigned char chip_id, unsigned char *res, int len)
  4266. {
  4267. int i;
  4268. int max_len;
  4269. unsigned short crc1, crc2;
  4270. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4271. unsigned char tx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4272. unsigned char rx_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4273. unsigned char tmp_buf[MCOMPAT_CONFIG_MAX_CMD_LENGTH];
  4274. applog(LOG_DEBUG, "%s,%d: %s ", __FILE__, __LINE__, __FUNCTION__);
  4275. if (res == NULL)
  4276. {
  4277. applog(LOG_ERR, "%s para error !", __FUNCTION__);
  4278. return false;
  4279. }
  4280. memset(tx_buf, 0, sizeof(tx_buf));
  4281. if (!spi_send_command(spi, CMD_READ_RESULT, chip_id, tx_buf, 2))
  4282. {
  4283. applog(LOG_WARNING, "%s send fail !", __FUNCTION__);
  4284. return false;
  4285. }
  4286. max_len = g_chip_num * 4;
  4287. memset(rx_buf, 0, sizeof(rx_buf));
  4288. for(i = 0; i < max_len; i = i + 2)
  4289. {
  4290. spi_recv_data(spi, rx_buf, 2);
  4291. if (((rx_buf[0] & 0x0f) == CMD_READ_RESULT) && (rx_buf[1] != 0))
  4292. {
  4293. break;
  4294. }
  4295. }
  4296. if (i >= max_len)
  4297. {
  4298. return false;
  4299. }
  4300. spi_recv_data_in_word(spi, rx_buf + 2, len + 2);
  4301. for(i = 0; i < len + 2; i = i + 2)
  4302. {
  4303. tmp_buf[i + 0] = rx_buf[i + 1];
  4304. tmp_buf[i + 1] = rx_buf[i + 0];
  4305. }
  4306. crc1 = CRC16_2(tmp_buf, len + 2);
  4307. crc2 = (rx_buf[2 + len + 0] << 8) + (rx_buf[2 + len + 1] << 0);
  4308. if (crc1 != crc2) {
  4309. applog(LOG_WARNING, "%s crc error !", __FUNCTION__);
  4310. return false;
  4311. }
  4312. memcpy(res, rx_buf, len + 2);
  4313. return true;
  4314. }
  4315. bool spi_cmd_write_job(unsigned char chain_id, unsigned char chip_id, unsigned char *job, int len)
  4316. {
  4317. ZYNQ_SPI_T *spi = &s_spi[chain_id];
  4318. applog(LOG_DEBUG, "%s,%d: %s(%d, %d, %p, %d)", __FILE__, __LINE__, __FUNCTION__, chain_id, chip_id, job, len);
  4319. if (job == NULL)
  4320. {
  4321. applog(LOG_ERR, "%s para error !", __FUNCTION__);
  4322. return false;
  4323. }
  4324. spi_send_data(spi, job, len);
  4325. return true;
  4326. }
  4327. const unsigned short wCRCTalbeAbs[] =
  4328. {
  4329. 0x0000, 0xCC01, 0xD801, 0x1400,
  4330. 0xF001, 0x3C00, 0x2800, 0xE401,
  4331. 0xA001, 0x6C00, 0x7800, 0xB401,
  4332. 0x5000, 0x9C01, 0x8801, 0x4400,
  4333. };
  4334. unsigned short CRC16_2(unsigned char* pchMsg, unsigned short wDataLen)
  4335. {
  4336. volatile unsigned short wCRC = 0xFFFF;
  4337. unsigned short i;
  4338. unsigned char chChar;
  4339. for (i = 0; i < wDataLen; i++)
  4340. {
  4341. chChar = *pchMsg++;
  4342. wCRC = wCRCTalbeAbs[(chChar ^ wCRC) & 15] ^ (wCRC >> 4);
  4343. wCRC = wCRCTalbeAbs[((chChar >> 4) ^ wCRC) & 15] ^ (wCRC >> 4);
  4344. }
  4345. return wCRC;
  4346. }
  4347. void print_data_hex(char *arg, unsigned char *buff, int len)
  4348. {
  4349. int i = 0;
  4350. printf("%s ", arg);
  4351. for(i = 0; i < len; i++)
  4352. {
  4353. printf("0x%02x ", buff[i]);
  4354. if ((i % 16) == 15)
  4355. {
  4356. //printf("");
  4357. }
  4358. }
  4359. if ((len % 16) != 0)
  4360. {
  4361. //printf("");
  4362. }
  4363. }
  4364. unsigned int SUNXI_IO_BASE = 0;
  4365. /*******************************************************************************************
  4366. * GPIO
  4367. *******************************************************************************************/
  4368. int gpio_init(void)
  4369. {
  4370. int fd;
  4371. unsigned int addr_start, addr_offset, PageSize, PageMask;
  4372. void *pc;
  4373. fd = open("/dev/mem", O_RDWR);
  4374. if (fd < 0)
  4375. return -1;
  4376. PageSize = sysconf(_SC_PAGESIZE);
  4377. PageMask = (~(PageSize - 1));
  4378. addr_start = SW_PORTC_IO_BASE & PageMask;
  4379. addr_offset = SW_PORTC_IO_BASE & ~PageMask;
  4380. pc = (void *)mmap(0, PageSize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr_start);
  4381. if (pc == MAP_FAILED)
  4382. return -1;
  4383. SUNXI_IO_BASE = (unsigned int) pc;
  4384. SUNXI_IO_BASE += addr_offset;
  4385. close(fd);
  4386. return 0;
  4387. }
  4388. int gpio_setcfg(unsigned int pin, unsigned int p1)
  4389. {
  4390. unsigned int cfg;
  4391. unsigned int bank = GPIO_BANK(pin);
  4392. unsigned int index = GPIO_CFG_INDEX(pin);
  4393. unsigned int offset = GPIO_CFG_OFFSET(pin);
  4394. if (SUNXI_IO_BASE == 0)
  4395. return -1;
  4396. struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *) SUNXI_IO_BASE)->gpio_bank[bank];
  4397. cfg = *(&pio->cfg[0] + index);
  4398. cfg &= ~(0xf << offset);
  4399. cfg |= p1 << offset;
  4400. *(&pio->cfg[0] + index) = cfg;
  4401. return 0;
  4402. }
  4403. int gpio_getcfg(unsigned int pin)
  4404. {
  4405. unsigned int cfg;
  4406. unsigned int bank = GPIO_BANK(pin);
  4407. unsigned int index = GPIO_CFG_INDEX(pin);
  4408. unsigned int offset = GPIO_CFG_OFFSET(pin);
  4409. if (SUNXI_IO_BASE == 0)
  4410. return -0;
  4411. struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *) SUNXI_IO_BASE)->gpio_bank[bank];
  4412. cfg = *(&pio->cfg[0] + index);
  4413. cfg >>= offset;
  4414. return (cfg & 0xf);
  4415. }
  4416. int gpio_output(unsigned int pin, unsigned int p1)
  4417. {
  4418. unsigned int bank = GPIO_BANK(pin);
  4419. unsigned int num = GPIO_NUM(pin);
  4420. if (SUNXI_IO_BASE == 0)
  4421. return -1;
  4422. struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *) SUNXI_IO_BASE)->gpio_bank[bank];
  4423. if (p1)
  4424. *(&pio->dat) |= 1 << num;
  4425. else
  4426. *(&pio->dat) &= ~(1 << num);
  4427. return 0;
  4428. }
  4429. int gpio_pullup(unsigned int pin, unsigned int p1)
  4430. {
  4431. unsigned int cfg;
  4432. unsigned int bank = GPIO_BANK(pin);
  4433. unsigned int index = GPIO_PUL_INDEX(pin);
  4434. unsigned int offset = GPIO_PUL_OFFSET(pin);
  4435. if (SUNXI_IO_BASE == 0)
  4436. return -1;
  4437. struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *) SUNXI_IO_BASE)->gpio_bank[bank];
  4438. cfg = *(&pio->pull[0] + index);
  4439. cfg &= ~(0x3 << offset);
  4440. cfg |= p1 << offset;
  4441. *(&pio->pull[0] + index) = cfg;
  4442. return 0;
  4443. }
  4444. int gpio_input(unsigned int pin)
  4445. {
  4446. unsigned int dat;
  4447. unsigned int bank = GPIO_BANK(pin);
  4448. unsigned int num = GPIO_NUM(pin);
  4449. if (SUNXI_IO_BASE == 0)
  4450. return -1;
  4451. struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *) SUNXI_IO_BASE)->gpio_bank[bank];
  4452. dat = *(&pio->dat);
  4453. dat >>= num;
  4454. return (dat & 0x1);
  4455. }
  4456. /*******************************************************************************************
  4457. * I2C
  4458. *******************************************************************************************/
  4459. int i2c_open(char *dev, uint8_t address)
  4460. {
  4461. int fd;
  4462. int ret;
  4463. fd = open(dev, O_RDWR);
  4464. if (fd < 0)
  4465. return fd;
  4466. ret = ioctl(fd, I2C_SLAVE_FORCE, address);
  4467. if (ret < 0)
  4468. return ret;
  4469. return fd;
  4470. }
  4471. int i2c_close(int fd)
  4472. {
  4473. return (close(fd));
  4474. }
  4475. int i2c_send(int fd, uint8_t *buf, uint8_t num_bytes)
  4476. {
  4477. return (write(fd, buf, num_bytes));
  4478. }
  4479. int i2c_read(int fd, uint8_t *buf, uint8_t num_bytes)
  4480. {
  4481. return (write(fd, buf, num_bytes));
  4482. }
  4483. /*******************************************************************************************
  4484. * spi
  4485. *******************************************************************************************/
  4486. int spi_open(char *dev, spi_config_t config)
  4487. {
  4488. int fd;
  4489. fd = open(dev, O_RDWR);
  4490. if (fd < 0)
  4491. return fd;
  4492. /* Set SPI_POL and SPI_PHA */
  4493. if (ioctl(fd, SPI_IOC_WR_MODE, &config.mode) < 0)
  4494. return -1;
  4495. if (ioctl(fd, SPI_IOC_RD_MODE, &config.mode) < 0)
  4496. return -1;
  4497. /* Set bits per word*/
  4498. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &config.bits) < 0)
  4499. return -1;
  4500. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &config.bits) < 0)
  4501. return -1;
  4502. /* Set SPI speed*/
  4503. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &config.speed) < 0)
  4504. return -1;
  4505. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &config.speed) < 0)
  4506. return -1;
  4507. return fd;
  4508. }
  4509. int spi_close(int fd)
  4510. {
  4511. return (close(fd));
  4512. }
  4513. int spi_xfer(int fd, uint8_t *tx_buf, uint8_t tx_len, uint8_t *rx_buf, uint8_t rx_len)
  4514. {
  4515. struct spi_ioc_transfer spi_message[2];
  4516. memset(spi_message, 0, sizeof(spi_message));
  4517. spi_message[0].rx_buf = (unsigned long)tx_buf;
  4518. spi_message[0].len = tx_len;
  4519. spi_message[1].tx_buf = (unsigned long)rx_buf;
  4520. spi_message[1].len = rx_len;
  4521. return ioctl(fd, SPI_IOC_MESSAGE(2), spi_message);
  4522. }
  4523. int spi_read(int fd, uint8_t *rx_buf, uint8_t rx_len)
  4524. {
  4525. struct spi_ioc_transfer spi_message[1];
  4526. memset(spi_message, 0, sizeof(spi_message));
  4527. spi_message[0].rx_buf = (unsigned long)rx_buf;
  4528. spi_message[0].len = rx_len;
  4529. return ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
  4530. }
  4531. int spi_write(int fd, uint8_t *tx_buffer, uint8_t tx_len)
  4532. {
  4533. struct spi_ioc_transfer spi_message[1];
  4534. memset(spi_message, 0, sizeof(spi_message));
  4535. spi_message[0].tx_buf = (unsigned long)tx_buffer;
  4536. spi_message[0].len = tx_len;
  4537. return ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
  4538. }
  4539. /*******************************************************************************************
  4540. * time
  4541. *******************************************************************************************/
  4542. void delay(unsigned int howLong)
  4543. {
  4544. struct timespec sleeper, dummy ;
  4545. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  4546. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  4547. nanosleep (&sleeper, &dummy) ;
  4548. }
  4549. static int opi_spi_fd = 0;
  4550. static pthread_mutex_t opi_spi_lock;
  4551. static struct spi_config opi_spi_config = {
  4552. .bus = DEFAULT_SPI_BUS,
  4553. .cs_line = DEFAULT_SPI_CS_LINE,
  4554. .mode = DEFAULT_SPI_MODE,
  4555. .speed = DEFAULT_SPI_SPEED,
  4556. .bits = DEFAULT_SPI_BITS_PER_WORD,
  4557. .delay = DEFAULT_SPI_DELAY_USECS,
  4558. };
  4559. void opi_spi_gpio_init(void)
  4560. {
  4561. if (gpio_init() == -1) {
  4562. printf("gpio initial fail");
  4563. }
  4564. gpio_setcfg(PIN_SPI_E1, OUTPUT);
  4565. gpio_output(PIN_SPI_E1, HIGH);
  4566. gpio_setcfg(PIN_SPI_A0, OUTPUT);
  4567. gpio_output(PIN_SPI_A0, HIGH);
  4568. gpio_setcfg(PIN_SPI_A1, OUTPUT);
  4569. gpio_output(PIN_SPI_A1, HIGH);
  4570. gpio_setcfg(PIN_SPI_A2, OUTPUT);
  4571. gpio_output(PIN_SPI_A2, HIGH);
  4572. }
  4573. void opi_spi_cs_enable(int id)
  4574. {
  4575. if (id & 01)
  4576. {
  4577. gpio_output(PIN_SPI_A0, HIGH);
  4578. }
  4579. else
  4580. {
  4581. gpio_output(PIN_SPI_A0, LOW);
  4582. }
  4583. if (id & 02)
  4584. {
  4585. gpio_output(PIN_SPI_A1, HIGH);
  4586. }
  4587. else
  4588. {
  4589. gpio_output(PIN_SPI_A1, LOW);
  4590. }
  4591. if (id & 04)
  4592. {
  4593. gpio_output(PIN_SPI_A2, HIGH);
  4594. }
  4595. else
  4596. {
  4597. gpio_output(PIN_SPI_A2, LOW);
  4598. }
  4599. gpio_output(PIN_SPI_E1, LOW);
  4600. }
  4601. void opi_spi_cs_disable(void)
  4602. {
  4603. gpio_output(PIN_SPI_E1, HIGH);
  4604. }
  4605. void opi_spi_init(void)
  4606. {
  4607. char dev_fname[PATH_MAX];
  4608. struct spi_config *config = &opi_spi_config;
  4609. pthread_mutex_init(&opi_spi_lock, NULL);
  4610. opi_spi_gpio_init();
  4611. sprintf(dev_fname, SPI_DEVICE_TEMPLATE, config->bus, config->cs_line);
  4612. int fd = open(dev_fname, O_RDWR);
  4613. if (fd < 0) {
  4614. applog(LOG_ERR, "SPI: Can not open SPI device %s ", dev_fname);
  4615. }
  4616. if ((ioctl(fd, SPI_IOC_WR_MODE, &config->mode) < 0) ||
  4617. (ioctl(fd, SPI_IOC_RD_MODE, &config->mode) < 0) ||
  4618. (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &config->bits) < 0) ||
  4619. (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &config->bits) < 0) ||
  4620. (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &config->speed) < 0) ||
  4621. (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &config->speed) < 0)) {
  4622. close(fd);
  4623. applog(LOG_ERR, "SPI: ioctl error on SPI device %s", dev_fname);
  4624. }
  4625. opi_spi_fd = fd;
  4626. applog(LOG_INFO, "SPI '%s': mode=%hhu, bits=%hhu, speed=%u ", dev_fname, config->mode, config->bits, config->speed);
  4627. }
  4628. void opi_spi_exit(void)
  4629. {
  4630. close(opi_spi_fd);
  4631. }
  4632. /*
  4633. * void opi_set_spi_speed(uint32_t speed)
  4634. * {
  4635. * pthread_mutex_lock(&opi_spi_lock);
  4636. *
  4637. * if ((ioctl(opi_spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, speed) < 0) ||
  4638. * (ioctl(opi_spi_fd, SPI_IOC_RD_MAX_SPEED_HZ, speed) < 0)) {
  4639. * applog(LOG_ERR, "SPI: ioctl error on SPI device");
  4640. * }
  4641. *
  4642. * opi_spi_config.speed = speed;
  4643. *
  4644. * pthread_mutex_unlock(&opi_spi_lock);
  4645. * }
  4646. */
  4647. bool opi_spi_transfer(uint8_t id, uint16_t *txbuf, uint16_t *rxbuf, int len)
  4648. {
  4649. struct spi_ioc_transfer xfr;
  4650. int ret;
  4651. if (rxbuf != NULL) {
  4652. memset(rxbuf, 0xff, len);
  4653. }
  4654. pthread_mutex_lock(&opi_spi_lock);
  4655. opi_spi_cs_enable(id);
  4656. cgsleep_ms(1); // usleep(1);
  4657. ret = len;
  4658. xfr.tx_buf = (unsigned long)txbuf;
  4659. xfr.rx_buf = (unsigned long)rxbuf;
  4660. xfr.len = len;
  4661. xfr.speed_hz = opi_spi_config.speed;
  4662. xfr.delay_usecs = opi_spi_config.delay;
  4663. xfr.bits_per_word = opi_spi_config.bits;
  4664. xfr.cs_change = 0;
  4665. xfr.pad = 0;
  4666. ret = ioctl(opi_spi_fd, SPI_IOC_MESSAGE(1), &xfr);
  4667. if (ret < 1) {
  4668. applog(LOG_ERR, "SPI: ioctl error on SPI device: %d", ret);
  4669. }
  4670. cgsleep_ms(1); // usleep(1);
  4671. opi_spi_cs_disable();
  4672. pthread_mutex_unlock(&opi_spi_lock);
  4673. return ret > 0;
  4674. }
  4675. int g_platform;
  4676. int g_miner_type;
  4677. int g_chain_num;
  4678. int g_chip_num;
  4679. bool sys_platform_init(int platform, int miner_type, int chain_num, int chip_num)
  4680. {
  4681. applog(LOG_NOTICE, "sys : platform[%d] miner_type[%d] chain_num[%d] chip_num[%d] ", platform, miner_type, chain_num, chip_num);
  4682. switch(platform)
  4683. {
  4684. case PLATFORM_ZYNQ_SPI_G9:
  4685. case PLATFORM_ZYNQ_SPI_G19:
  4686. case PLATFORM_ZYNQ_HUB_G9:
  4687. case PLATFORM_ZYNQ_HUB_G19:
  4688. case PLATFORM_SOC:
  4689. break;
  4690. default:
  4691. applog(LOG_ERR, "the platform is undefined !!! ");
  4692. break;
  4693. }
  4694. if (chain_num > MCOMPAT_CONFIG_MAX_CHAIN_NUM)
  4695. {
  4696. applog(LOG_ERR, "the chain_num is error !!! ");
  4697. return false;
  4698. }
  4699. if (chip_num > MCOMPAT_CONFIG_MAX_CHIP_NUM)
  4700. {
  4701. applog(LOG_ERR, "the chip_num is error !!! ");
  4702. return false;
  4703. }
  4704. g_platform = platform;
  4705. g_miner_type = miner_type;
  4706. g_chain_num = chain_num;
  4707. g_chip_num = chip_num;
  4708. init_mcompat_cmd();
  4709. init_mcompat_gpio();
  4710. init_mcompat_pwm();
  4711. init_mcompat_chain();
  4712. return true;
  4713. }
  4714. bool sys_platform_exit(void)
  4715. {
  4716. exit_mcompat_cmd();
  4717. exit_mcompat_gpio();
  4718. exit_mcompat_pwm();
  4719. exit_mcompat_chain();
  4720. return true;
  4721. }