driver-btm-soc.c 431 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083
  1. /*
  2. * Copyright 2016-2017 Fazio Bai <yang.bai@bitmain.com>
  3. * Copyright 2016-2017 Clement Duan <kai.duan@bitmain.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <assert.h>
  11. #include <limits.h>
  12. #include <pthread.h>
  13. #include <stdio.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #include <unistd.h>
  18. #include <sys/mman.h>
  19. #include <math.h>
  20. #ifndef WIN32
  21. #include <sys/select.h>
  22. #include <termios.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #ifndef O_CLOEXEC
  26. #define O_CLOEXEC 0
  27. #endif
  28. #else
  29. #include "compat.h"
  30. #include <windows.h>
  31. #include <io.h>
  32. #endif
  33. #include <sys/ioctl.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <net/if.h>
  37. #include <netdb.h>
  38. #include <arpa/inet.h>
  39. #include <errno.h>
  40. #include <string.h>
  41. #include <sys/sysinfo.h>
  42. #include "elist.h"
  43. #include "miner.h"
  44. // #include "usbutils.h"
  45. #include "util.h"
  46. #include "driver-btm-soc.h"
  47. #define MAX_CHAR_NUM 1024
  48. extern void reCalculateAVG();
  49. extern void setStartTimePoint();
  50. bool someBoardUpVoltage=false;
  51. bool isUseDefaultFreq=false;
  52. bool doTestPatten=false;
  53. bool startCheckNetworkJob=false;
  54. unsigned char reset_iic_pic(unsigned char chain);
  55. extern bool clement_doTestBoard(bool showlog);
  56. bool clement_doTestBoardOnce(bool showlog);
  57. int calculate_core_number(unsigned int actual_core_number);
  58. #define hex_print(p) applog(LOG_DEBUG, "%s", p)
  59. static char nibble[] =
  60. {
  61. '0', '1', '2', '3', '4', '5', '6', '7',
  62. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  63. };
  64. #define BYTES_PER_LINE 0x10
  65. static void hexdump(const uint8_t *p, unsigned int len)
  66. {
  67. unsigned int i, addr;
  68. unsigned int wordlen = sizeof(unsigned int);
  69. unsigned char v, line[BYTES_PER_LINE * 5];
  70. for (addr = 0; addr < len; addr += BYTES_PER_LINE)
  71. {
  72. /* clear line */
  73. for (i = 0; i < sizeof(line); i++)
  74. {
  75. if (i == wordlen * 2 + 52 ||
  76. i == wordlen * 2 + 69)
  77. {
  78. line[i] = '|';
  79. continue;
  80. }
  81. if (i == wordlen * 2 + 70)
  82. {
  83. line[i] = '\0';
  84. continue;
  85. }
  86. line[i] = ' ';
  87. }
  88. /* print address */
  89. for (i = 0; i < wordlen * 2; i++)
  90. {
  91. v = addr >> ((wordlen * 2 - i - 1) * 4);
  92. line[i] = nibble[v & 0xf];
  93. }
  94. /* dump content */
  95. for (i = 0; i < BYTES_PER_LINE; i++)
  96. {
  97. int pos = (wordlen * 2) + 3 + (i / 8);
  98. if (addr + i >= len)
  99. break;
  100. v = p[addr + i];
  101. line[pos + (i * 3) + 0] = nibble[v >> 4];
  102. line[pos + (i * 3) + 1] = nibble[v & 0xf];
  103. /* character printable? */
  104. line[(wordlen * 2) + 53 + i] =
  105. (v >= ' ' && v <= '~') ? v : '.';
  106. }
  107. hex_print(line);
  108. }
  109. }
  110. /**
  111. * \brief SHA-256 context structure
  112. */
  113. typedef struct
  114. {
  115. uint32_t total[2]; /*!< number of bytes processed */
  116. uint32_t state[8]; /*!< intermediate digest state */
  117. unsigned char buffer[64]; /*!< data block being processed */
  118. unsigned char ipad[64]; /*!< HMAC: inner padding */
  119. unsigned char opad[64]; /*!< HMAC: outer padding */
  120. }
  121. sha2_context;
  122. /*
  123. * 32-bit integer manipulation macros (big endian)
  124. */
  125. #ifndef GET_ULONG_BE
  126. #define GET_ULONG_BE(n,b,i) \
  127. { \
  128. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  129. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  130. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  131. | ( (uint32_t) (b)[(i) + 3] ); \
  132. }
  133. #endif
  134. #ifndef PUT_ULONG_BE
  135. #define PUT_ULONG_BE(n,b,i) \
  136. { \
  137. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  138. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  139. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  140. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  141. }
  142. #endif
  143. /*
  144. * SHA-256 context setup
  145. */
  146. void sha2_starts( sha2_context *ctx )
  147. {
  148. ctx->total[0] = 0;
  149. ctx->total[1] = 0;
  150. ctx->state[0] = 0x6A09E667;
  151. ctx->state[1] = 0xBB67AE85;
  152. ctx->state[2] = 0x3C6EF372;
  153. ctx->state[3] = 0xA54FF53A;
  154. ctx->state[4] = 0x510E527F;
  155. ctx->state[5] = 0x9B05688C;
  156. ctx->state[6] = 0x1F83D9AB;
  157. ctx->state[7] = 0x5BE0CD19;
  158. }
  159. void sha2_process( sha2_context *ctx, const unsigned char data[64] )
  160. {
  161. uint32_t temp1, temp2, W[64];
  162. uint32_t A, B, C, D, E, F, G, H;
  163. GET_ULONG_BE( W[ 0], data, 0 );
  164. GET_ULONG_BE( W[ 1], data, 4 );
  165. GET_ULONG_BE( W[ 2], data, 8 );
  166. GET_ULONG_BE( W[ 3], data, 12 );
  167. GET_ULONG_BE( W[ 4], data, 16 );
  168. GET_ULONG_BE( W[ 5], data, 20 );
  169. GET_ULONG_BE( W[ 6], data, 24 );
  170. GET_ULONG_BE( W[ 7], data, 28 );
  171. GET_ULONG_BE( W[ 8], data, 32 );
  172. GET_ULONG_BE( W[ 9], data, 36 );
  173. GET_ULONG_BE( W[10], data, 40 );
  174. GET_ULONG_BE( W[11], data, 44 );
  175. GET_ULONG_BE( W[12], data, 48 );
  176. GET_ULONG_BE( W[13], data, 52 );
  177. GET_ULONG_BE( W[14], data, 56 );
  178. GET_ULONG_BE( W[15], data, 60 );
  179. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  180. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  181. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  182. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  183. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  184. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  185. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  186. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  187. #define R(t) \
  188. ( \
  189. W[t] = S1(W[t - 2]) + W[t - 7] + \
  190. S0(W[t - 15]) + W[t - 16] \
  191. )
  192. #define P(a,b,c,d,e,f,g,h,x,K) \
  193. { \
  194. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  195. temp2 = S2(a) + F0(a,b,c); \
  196. d += temp1; h = temp1 + temp2; \
  197. }
  198. A = ctx->state[0];
  199. B = ctx->state[1];
  200. C = ctx->state[2];
  201. D = ctx->state[3];
  202. E = ctx->state[4];
  203. F = ctx->state[5];
  204. G = ctx->state[6];
  205. H = ctx->state[7];
  206. P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
  207. P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
  208. P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
  209. P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
  210. P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
  211. P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
  212. P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
  213. P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
  214. P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
  215. P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
  216. P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
  217. P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
  218. P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
  219. P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
  220. P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
  221. P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
  222. P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
  223. P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
  224. P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
  225. P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
  226. P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
  227. P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
  228. P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
  229. P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
  230. P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
  231. P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
  232. P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
  233. P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
  234. P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
  235. P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
  236. P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
  237. P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
  238. P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
  239. P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
  240. P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
  241. P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
  242. P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
  243. P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
  244. P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
  245. P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
  246. P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
  247. P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
  248. P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
  249. P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
  250. P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
  251. P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
  252. P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
  253. P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
  254. P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
  255. P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
  256. P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
  257. P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
  258. P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
  259. P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
  260. P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
  261. P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
  262. P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
  263. P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
  264. P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
  265. P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
  266. P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
  267. P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
  268. P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
  269. P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
  270. ctx->state[0] += A;
  271. ctx->state[1] += B;
  272. ctx->state[2] += C;
  273. ctx->state[3] += D;
  274. ctx->state[4] += E;
  275. ctx->state[5] += F;
  276. ctx->state[6] += G;
  277. ctx->state[7] += H;
  278. }
  279. /*
  280. * SHA-256 process buffer
  281. */
  282. void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
  283. {
  284. int fill;
  285. uint32_t left;
  286. if( ilen <= 0 )
  287. return;
  288. left = ctx->total[0] & 0x3F;
  289. fill = 64 - left;
  290. ctx->total[0] += ilen;
  291. ctx->total[0] &= 0xFFFFFFFF;
  292. if( ctx->total[0] < (uint32_t) ilen )
  293. ctx->total[1]++;
  294. if( left && ilen >= fill )
  295. {
  296. memcpy( (void *) (ctx->buffer + left),
  297. (void *) input, fill );
  298. sha2_process( ctx, ctx->buffer );
  299. input += fill;
  300. ilen -= fill;
  301. left = 0;
  302. }
  303. while( ilen >= 64 )
  304. {
  305. sha2_process( ctx, input );
  306. input += 64;
  307. ilen -= 64;
  308. }
  309. if( ilen > 0 )
  310. {
  311. memcpy((void *) (ctx->buffer + left),
  312. (void *) input, ilen );
  313. }
  314. /*
  315. printk("ctx sha2_update:");
  316. dump_hex((uint8_t*)ctx,sizeof(*ctx));
  317. */
  318. }
  319. static const unsigned char sha2_padding[64] =
  320. {
  321. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  322. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  323. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  324. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  325. };
  326. /*
  327. * SHA-256 final digest
  328. */
  329. void sha2_finish( sha2_context *ctx, unsigned char output[32] )
  330. {
  331. uint32_t last, padn;
  332. uint32_t high, low;
  333. unsigned char msglen[8];
  334. high = ( ctx->total[0] >> 29 )
  335. | ( ctx->total[1] << 3 );
  336. low = ( ctx->total[0] << 3 );
  337. PUT_ULONG_BE( high, msglen, 0 );
  338. PUT_ULONG_BE( low, msglen, 4 );
  339. last = ctx->total[0] & 0x3F;
  340. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  341. sha2_update( ctx, (unsigned char *) sha2_padding, padn );
  342. sha2_update( ctx, msglen, 8 );
  343. PUT_ULONG_BE( ctx->state[0], output, 0 );
  344. PUT_ULONG_BE( ctx->state[1], output, 4 );
  345. PUT_ULONG_BE( ctx->state[2], output, 8 );
  346. PUT_ULONG_BE( ctx->state[3], output, 12 );
  347. PUT_ULONG_BE( ctx->state[4], output, 16 );
  348. PUT_ULONG_BE( ctx->state[5], output, 20 );
  349. PUT_ULONG_BE( ctx->state[6], output, 24 );
  350. PUT_ULONG_BE( ctx->state[7], output, 28 );
  351. }
  352. /*
  353. * output = SHA-256( input buffer )
  354. */
  355. void sha2( const unsigned char *input, int ilen,
  356. unsigned char output[32] )
  357. {
  358. sha2_context ctx;
  359. sha2_starts( &ctx );
  360. sha2_update( &ctx, input, ilen );
  361. sha2_finish( &ctx, output );
  362. memset(&ctx, 0, sizeof(sha2_context));
  363. }
  364. #ifdef R4
  365. int MIN_PWM_PERCENT;
  366. int MID_PWM_PERCENT;
  367. int MAX_PWM_PERCENT;
  368. int MAX_TEMP;
  369. int MAX_FAN_TEMP;
  370. int MID_FAN_TEMP;
  371. int MIN_FAN_TEMP;
  372. int MAX_PCB_TEMP;
  373. int MAX_FAN_PCB_TEMP;
  374. #endif
  375. bool is218_Temp=false;
  376. //interface between bmminer and axi driver
  377. static struct init_config config_parameter;
  378. //global various
  379. int fd; // axi fpga
  380. int fd_fpga_mem; // fpga memory
  381. int fpga_version;
  382. int pcb_version;
  383. unsigned int *axi_fpga_addr = NULL; // axi address
  384. unsigned int *fpga_mem_addr = NULL; // fpga memory address
  385. unsigned int *nonce2_jobid_address = NULL; // the value should be filled in NONCE2_AND_JOBID_STORE_ADDRESS
  386. unsigned int *job_start_address_1 = NULL; // the value should be filled in JOB_START_ADDRESS
  387. unsigned int *job_start_address_2 = NULL; // the value should be filled in JOB_START_ADDRESS
  388. struct thr_info *read_nonce_reg_id; // thread id for read nonce and register
  389. struct thr_info *check_system_work_id; // thread id for check system
  390. struct thr_info *read_temp_id;
  391. struct thr_info *pic_heart_beat;
  392. struct thr_info *change_voltage_to_old;
  393. extern void writeLogFile(char *logstr);
  394. bool gBegin_get_nonce = false;
  395. struct timeval tv_send_job = {0, 0};
  396. struct timeval tv_send = {0, 0};
  397. pthread_mutex_t reg_mutex = PTHREAD_MUTEX_INITIALIZER;
  398. pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
  399. pthread_mutex_t iic_mutex = PTHREAD_MUTEX_INITIALIZER;
  400. pthread_mutex_t fpga_mutex = PTHREAD_MUTEX_INITIALIZER;
  401. pthread_mutex_t opencore_readtemp_mutex = PTHREAD_MUTEX_INITIALIZER;
  402. uint64_t h = 0;
  403. int opt_multi_version = 1;
  404. uint32_t given_id = 2;
  405. uint32_t c_coinbase_padding = 0;
  406. uint32_t c_merkles_num = 0;
  407. uint32_t l_coinbase_padding = 0;
  408. uint32_t l_merkles_num = 0;
  409. int last_temperature = 0, temp_highest = 0;
  410. bool opt_bitmain_fan_ctrl = false;
  411. int opt_bitmain_fan_pwm = 0;
  412. int opt_bitmain_soc_freq = 600;
  413. int opt_bitmain_soc_voltage = 176;
  414. int ADD_FREQ = 0;
  415. int ADD_FREQ1 = 0;
  416. uint8_t de_voltage = 176;
  417. #define ERROR_OVER_MAXTEMP 1 // temp is too high
  418. #define ERROR_FAN_LOST 2 // fan num is not right, some fan lost
  419. #define ERROR_FAN_SPEED 3 // fan speed error
  420. #define ERROR_UNKOWN_STATUS 4 // unkown error, never get this!
  421. int FatalErrorValue=0;
  422. bool opt_bitmain_new_cmd_type_vil = false;
  423. bool opt_fixed_freq = false;
  424. bool opt_pre_heat = true;
  425. bool status_error = false;
  426. bool once_error = false;
  427. bool iic_ok = false;
  428. int check_iic = 0;
  429. bool update_temp =false;
  430. bool check_temp_offside = false;
  431. double chain_asic_RT[BITMAIN_MAX_CHAIN_NUM][CHAIN_ASIC_NUM]= {0};
  432. uint64_t rate[BITMAIN_MAX_CHAIN_NUM] = {0};
  433. uint64_t nonce_num[BITMAIN_MAX_CHAIN_NUM][BITMAIN_DEFAULT_ASIC_NUM][TIMESLICE] = {0};
  434. int nonce_times = 0;
  435. int rate_error[BITMAIN_MAX_CHAIN_NUM] = {0};
  436. char displayed_rate[BITMAIN_MAX_CHAIN_NUM][32];
  437. uint8_t chain_voltage_pic[BITMAIN_MAX_CHAIN_NUM] = {0xff};
  438. int chain_voltage_value[BITMAIN_MAX_CHAIN_NUM] = {0};
  439. unsigned char hash_board_id[BITMAIN_MAX_CHAIN_NUM][12];
  440. int lowest_testOK_temp[BITMAIN_MAX_CHAIN_NUM]= {0}; // board test patten OK, we record temp in PIC, then we need keep board temp >= this lowest temp
  441. int chain_temp_toolow[BITMAIN_MAX_CHAIN_NUM]= {0};
  442. int LOWEST_TEMP_DOWN_FAN=MIN_TEMP_CONTINUE_DOWN_FAN;
  443. #ifdef T9_18
  444. unsigned char chain_pic_buf[BITMAIN_MAX_CHAIN_NUM][128] = {0};
  445. #else
  446. unsigned char last_freq[BITMAIN_MAX_CHAIN_NUM][256] = {0};
  447. unsigned char badcore_num_buf[BITMAIN_MAX_CHAIN_NUM][64] = {0};
  448. #endif
  449. int chain_badcore_num[BITMAIN_MAX_CHAIN_NUM][256] = {0};
  450. unsigned char show_last_freq[BITMAIN_MAX_CHAIN_NUM][256] = {0}; // only used to showed to users
  451. unsigned char chip_last_freq[BITMAIN_MAX_CHAIN_NUM][256] = {0}; // this is the real value , which set freq into chips
  452. unsigned char pic_temp_offset[BITMAIN_MAX_CHAIN_NUM] = {0};
  453. unsigned char base_freq_index[BITMAIN_MAX_CHAIN_NUM] = {0};
  454. int x_time[BITMAIN_MAX_CHAIN_NUM][256] = {0};
  455. int temp_offside[BITMAIN_MAX_CHAIN_NUM] = {0};
  456. static bool global_stop = false;
  457. //Test Core
  458. static int test_core = 8;
  459. struct nonce_content temp_nonce_buf[MAX_RETURNED_NONCE_NUM];
  460. struct reg_content temp_reg_buf[MAX_RETURNED_NONCE_NUM];
  461. volatile struct nonce_buf nonce_read_out;
  462. volatile struct reg_buf reg_value_buf;
  463. #define USE_IIC 1
  464. #define TEMP_CALI 0
  465. static int8_t bottom_Offset[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
  466. static int8_t middle_Offset[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
  467. static int8_t bottom_Offset_sw[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
  468. static int8_t middle_Offset_sw[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
  469. pthread_mutex_t init_log_mutex = PTHREAD_MUTEX_INITIALIZER;
  470. bool isC5_CtrlBoard=false;
  471. bool isChainAllCoresOpened[BITMAIN_MAX_CHAIN_NUM]= {false}; // is all cores opened flag
  472. void set_led(bool stop);
  473. void open_core(bool nullwork_enable);
  474. pthread_mutex_t reinit_mutex = PTHREAD_MUTEX_INITIALIZER;
  475. static int reinit_counter=0;
  476. void bitmain_core_reInit();
  477. signed char getMeddleOffsetForTestPatten(int chainIndex)
  478. {
  479. return middle_Offset[chainIndex][0];
  480. }
  481. unsigned int PHY_MEM_NONCE2_JOBID_ADDRESS=PHY_MEM_NONCE2_JOBID_ADDRESS_XILINX_1GB; // set to XILINX as default
  482. bool isFixedFreqMode()
  483. {
  484. return opt_fixed_freq;
  485. }
  486. bool isC5_Board()
  487. {
  488. FILE *fd;
  489. char board_type[32];
  490. int isC5=0;
  491. memset(board_type,'\0',32);
  492. fd=fopen("/usr/bin/ctrl_bd","rb");
  493. if(fd)
  494. {
  495. fread(board_type,1,32,fd);
  496. fclose(fd);
  497. if(strstr(board_type,"XILINX"))
  498. {
  499. isC5=0;
  500. }
  501. else isC5=1;
  502. }
  503. else
  504. {
  505. isC5=1;
  506. }
  507. if(isC5)
  508. return true;
  509. else return false;
  510. }
  511. void software_set_address();
  512. void set_asic_ticket_mask(unsigned int ticket_mask);
  513. void init_uart_baud();
  514. void open_core_one_chain(int chainIndex, bool nullwork_enable);
  515. void getAsicNum_preOpenCore(int chainIndex);
  516. void writeInitLogFile(char *logstr);
  517. void clearInitLogFile();
  518. void re_send_last_job();
  519. void saveSearchFailedFlagInfo(char *search_failed_info);
  520. extern void jump_to_app_CheckAndRestorePIC(int chainIndex); // defined in Clement-bitmain.c
  521. static unsigned char last_job_buffer[8192]= {23};
  522. ///////////// below they must be changed at same time!!!! ///////////////////////
  523. typedef enum
  524. {
  525. TEMP_BOTTOM = 0, // 0 is bottom , 1 is middle
  526. TEMP_MIDDLE
  527. } Temp_Type_E;
  528. ////////////////////////////////////////////////////////////////////////////
  529. #define MAX_ERROR_LIMIT_ABS ( 2 )
  530. #define MAX_RETRY_COUNT ( 16 + 1 )
  531. void *gpio0_vaddr=NULL;
  532. struct all_parameters *dev;
  533. unsigned int is_first_job = 0;
  534. //other equipment related
  535. // --------------------------------------------------------------
  536. // CRC16 check table
  537. // --------------------------------------------------------------
  538. const uint8_t chCRCHTalbe[] = // CRC high byte table
  539. {
  540. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  541. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  542. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  543. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  544. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  545. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  546. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  547. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  548. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  549. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  550. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  551. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  552. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  553. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  554. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  555. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  556. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  557. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  558. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  559. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  560. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  561. 0x00, 0xC1, 0x81, 0x40
  562. };
  563. const uint8_t chCRCLTalbe[] = // CRC low byte table
  564. {
  565. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  566. 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
  567. 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
  568. 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
  569. 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  570. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  571. 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  572. 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
  573. 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
  574. 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  575. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  576. 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
  577. 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
  578. 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  579. 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  580. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  581. 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
  582. 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
  583. 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
  584. 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  585. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  586. 0x41, 0x81, 0x80, 0x40
  587. };
  588. //crc
  589. uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
  590. {
  591. uint8_t chCRCHi = 0xFF; // CRC high byte initialize
  592. uint8_t chCRCLo = 0xFF; // CRC low byte initialize
  593. uint16_t wIndex = 0; // CRC cycling index
  594. while (w_len--)
  595. {
  596. wIndex = chCRCLo ^ *p_data++;
  597. chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex];
  598. chCRCHi = chCRCLTalbe[wIndex];
  599. }
  600. return ((chCRCHi << 8) | chCRCLo);
  601. }
  602. unsigned char CRC5(unsigned char *ptr, unsigned char len)
  603. {
  604. unsigned char i, j, k;
  605. unsigned char crc = 0x1f;
  606. unsigned char crcin[5] = {1, 1, 1, 1, 1};
  607. unsigned char crcout[5] = {1, 1, 1, 1, 1};
  608. unsigned char din = 0;
  609. j = 0x80;
  610. k = 0;
  611. for (i = 0; i < len; i++)
  612. {
  613. if (*ptr & j)
  614. {
  615. din = 1;
  616. }
  617. else
  618. {
  619. din = 0;
  620. }
  621. crcout[0] = crcin[4] ^ din;
  622. crcout[1] = crcin[0];
  623. crcout[2] = crcin[1] ^ crcin[4] ^ din;
  624. crcout[3] = crcin[2];
  625. crcout[4] = crcin[3];
  626. j = j >> 1;
  627. k++;
  628. if (k == 8)
  629. {
  630. j = 0x80;
  631. k = 0;
  632. ptr++;
  633. }
  634. memcpy(crcin, crcout, 5);
  635. }
  636. crc = 0;
  637. if(crcin[4])
  638. {
  639. crc |= 0x10;
  640. }
  641. if(crcin[3])
  642. {
  643. crc |= 0x08;
  644. }
  645. if(crcin[2])
  646. {
  647. crc |= 0x04;
  648. }
  649. if(crcin[1])
  650. {
  651. crc |= 0x02;
  652. }
  653. if(crcin[0])
  654. {
  655. crc |= 0x01;
  656. }
  657. return crc;
  658. }
  659. unsigned char getPICvoltageFromValue(int vol_value) // vol_value = 940 means 9.4V
  660. {
  661. #ifdef S9_PLUS
  662. #ifdef S9_PLUS_VOLTAGE2
  663. unsigned char temp_voltage=1250.809516-127.817623*(vol_value*1.0)/100;
  664. #else
  665. unsigned char temp_voltage=824.784-73.1705*((vol_value*1.0)/100.0);
  666. #endif
  667. #endif
  668. #ifdef S9_63
  669. unsigned char temp_voltage = 1608.420446 - 170.423497*(vol_value*1.0)/100.0;
  670. #endif
  671. #ifdef R4
  672. unsigned char temp_voltage = 1608.420446 - 170.423497*(vol_value*1.0)/100.0;
  673. #endif
  674. #ifdef T9_18
  675. unsigned char temp_voltage = 364.0704 / (4.75*(vol_value*1.0)/100 - 32.79) - 30.72;
  676. #endif
  677. return temp_voltage;
  678. }
  679. int getVolValueFromPICvoltage(unsigned char vol_pic)
  680. {
  681. #ifdef S9_PLUS
  682. #ifdef S9_PLUS_VOLTAGE2
  683. int vol_value = ((1250.809516 - vol_pic)/127.817623)*100.0;
  684. #else
  685. int vol_value = ((824.784 - vol_pic)/73.1705)*100.0;
  686. #endif
  687. #endif
  688. #ifdef S9_63
  689. int vol_value = ((1608.420446 - vol_pic) *100.0)/170.423497;
  690. #endif
  691. #ifdef R4
  692. int vol_value = ((1608.420446 - vol_pic) *100.0)/170.423497;
  693. #endif
  694. #ifdef T9_18
  695. int vol_value = ((364.0704/(vol_pic+30.72))+32.79)*100/4.75;
  696. #endif
  697. vol_value=(vol_value/10)*10;
  698. return vol_value;
  699. }
  700. int getVoltageLimitedFromHashrate(int hashrate_GHz)
  701. {
  702. int vol_value;
  703. #ifdef DEBUG_WITHOUT_FREQ_VOLTAGE_LIMIT
  704. return 940; // just fixed to highest voltage
  705. #endif
  706. #ifdef R4
  707. if(isC5_CtrlBoard)
  708. vol_value=R4_MAX_VOLTAGE_C5;
  709. else vol_value=R4_MAX_VOLTAGE_XILINX;
  710. #endif
  711. #ifdef S9_PLUS
  712. if(hashrate_GHz>=12500)
  713. vol_value=840;
  714. else if(hashrate_GHz>=12000)
  715. vol_value=850;
  716. else if(hashrate_GHz>=11500)
  717. vol_value=870;
  718. else if(hashrate_GHz>=11000)
  719. vol_value=890;
  720. else if(hashrate_GHz>=10500)
  721. vol_value=910;
  722. else if(hashrate_GHz>=10000)
  723. vol_value=930;
  724. else if(hashrate_GHz>=9500)
  725. vol_value=960;
  726. else if(hashrate_GHz>=9000)
  727. vol_value=970;
  728. else
  729. vol_value=970;
  730. #endif
  731. #ifdef S9_63
  732. if(hashrate_GHz>=14500)
  733. vol_value=870;
  734. else if(hashrate_GHz>=14000)
  735. vol_value=880;
  736. else if(hashrate_GHz>=13500)
  737. vol_value=900;
  738. else if(hashrate_GHz>=13000)
  739. vol_value=910;
  740. else if(hashrate_GHz>=12500)
  741. vol_value=930;
  742. else
  743. vol_value=940;
  744. #endif
  745. #ifdef T9_18
  746. if(hashrate_GHz>=12000)
  747. vol_value=810;
  748. else if(hashrate_GHz>=11500)
  749. vol_value=830;
  750. else if(hashrate_GHz>=11000)
  751. vol_value=850;
  752. else if(hashrate_GHz>=10500)
  753. vol_value=870;
  754. else if(hashrate_GHz>=10000)
  755. vol_value=890;
  756. else if(hashrate_GHz>=9500)
  757. vol_value=920;
  758. else if(hashrate_GHz>=9000)
  759. vol_value=930;
  760. else
  761. vol_value=930;
  762. #endif
  763. return vol_value;
  764. }
  765. int getFixedFreqVoltageValue(int freq)
  766. {
  767. int vol_value;
  768. #ifdef R4
  769. if(isC5_CtrlBoard)
  770. vol_value=890;
  771. else vol_value=910;
  772. #endif
  773. #ifdef S9_PLUS
  774. if(freq>=643) // hashrate 12500
  775. vol_value=840;
  776. else if(freq>=618) // hashrate 12000
  777. vol_value=850;
  778. else if(freq>=593) // hashrate 11500
  779. vol_value=870;
  780. else if(freq>=568) // hashrate 11000
  781. vol_value=890;
  782. else if(freq>=543) // hashrate 10500
  783. vol_value=910;
  784. else if(freq>=516) // hashrate 10000
  785. vol_value=930;
  786. else if(freq>=491) // hashrate 9500
  787. vol_value=960;
  788. else if(freq>=462) // hashrate 9000
  789. vol_value=970;
  790. else
  791. vol_value=970;
  792. #endif
  793. #ifdef S9_63
  794. if(freq>=675) // hashrate 14500
  795. vol_value=870;
  796. else if(freq>=650) // hashrate 14000
  797. vol_value=880;
  798. else if(freq>=631) // hashrate 13500
  799. vol_value=900;
  800. else if(freq>=606) // hashrate 13000
  801. vol_value=910;
  802. else if(freq>=581) // hashrate 12500
  803. vol_value=930;
  804. else
  805. vol_value=940;
  806. #endif
  807. #ifdef T9_18
  808. if(freq>=650) // hashrate 12000
  809. vol_value=810;
  810. else if(freq>=625) // hashrate 11500
  811. vol_value=830;
  812. else if(freq>=600) // hashrate 11000
  813. vol_value=850;
  814. else if(freq>=575) // hashrate 10500
  815. vol_value=870;
  816. else if(freq>=543) // hashrate 10000
  817. vol_value=890;
  818. else if(freq>=516) // hashrate 9500
  819. vol_value=920;
  820. else if(freq>=491) // hashrate 9000
  821. vol_value=930;
  822. else
  823. vol_value=930;
  824. #endif
  825. return vol_value;
  826. }
  827. #ifdef T9_18
  828. void getPICChainIndexOffset(int chainIndex, int *pChain, int *pOffset)
  829. {
  830. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  831. switch(chainIndex)
  832. {
  833. case 1:
  834. new_T9_PLUS_chainIndex=1;
  835. new_T9_PLUS_chainOffset=0;
  836. break;
  837. case 8:
  838. new_T9_PLUS_chainIndex=1;
  839. new_T9_PLUS_chainOffset=1;
  840. break;
  841. case 9:
  842. new_T9_PLUS_chainIndex=1;
  843. new_T9_PLUS_chainOffset=2;
  844. break;
  845. case 2:
  846. new_T9_PLUS_chainIndex=2;
  847. new_T9_PLUS_chainOffset=0;
  848. break;
  849. case 10:
  850. new_T9_PLUS_chainIndex=2;
  851. new_T9_PLUS_chainOffset=1;
  852. break;
  853. case 11:
  854. new_T9_PLUS_chainIndex=2;
  855. new_T9_PLUS_chainOffset=2;
  856. break;
  857. case 3:
  858. new_T9_PLUS_chainIndex=3;
  859. new_T9_PLUS_chainOffset=0;
  860. break;
  861. case 12:
  862. new_T9_PLUS_chainIndex=3;
  863. new_T9_PLUS_chainOffset=1;
  864. break;
  865. case 13:
  866. new_T9_PLUS_chainIndex=3;
  867. new_T9_PLUS_chainOffset=2;
  868. break;
  869. default:
  870. new_T9_PLUS_chainIndex=0;
  871. new_T9_PLUS_chainOffset=0;
  872. break;
  873. }
  874. *pChain=new_T9_PLUS_chainIndex;
  875. *pOffset=new_T9_PLUS_chainOffset;
  876. }
  877. #endif
  878. int getChainAsicFreqIndex(int chainIndex, int asicIndex)
  879. {
  880. #ifdef T9_18
  881. if(fpga_version>=0xE)
  882. {
  883. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset; // only used by new T9+ FPGA
  884. getPICChainIndexOffset(chainIndex,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  885. return chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+asicIndex];
  886. }
  887. else
  888. {
  889. return chain_pic_buf[((chainIndex/3)*3)][7+(chainIndex%3)*31+4+asicIndex];
  890. }
  891. #else
  892. return last_freq[chainIndex][asicIndex*2+3];
  893. #endif
  894. }
  895. // pic
  896. unsigned int get_pic_iic()
  897. {
  898. int ret = -1;
  899. ret = *(axi_fpga_addr + IIC_COMMAND);
  900. applog(LOG_DEBUG,"%s: IIC_COMMAND is 0x%x\n", __FUNCTION__, ret);
  901. return ret;
  902. }
  903. unsigned char set_pic_iic(unsigned int data)
  904. {
  905. unsigned int ret=0;
  906. unsigned char ret_data = 0;
  907. *((unsigned int *)(axi_fpga_addr + IIC_COMMAND)) = data & 0x7fffffff;
  908. applog(LOG_DEBUG,"%s: set IIC_COMMAND is 0x%x\n", __FUNCTION__, data & 0x7fffffff);
  909. while(1)
  910. {
  911. ret = get_pic_iic();
  912. if(ret & 0x80000000)
  913. {
  914. ret_data = (unsigned char)(ret & 0x000000ff);
  915. return ret_data;
  916. }
  917. else
  918. {
  919. applog(LOG_DEBUG,"%s: waiting write pic iic\n", __FUNCTION__);
  920. cgsleep_us(1000);
  921. }
  922. }
  923. }
  924. unsigned char write_pic_iic(bool read, bool reg_addr_valid, unsigned char reg_addr, unsigned char chain, unsigned char data)
  925. {
  926. unsigned int value = 0x00000000;
  927. unsigned char ret = 0;
  928. if(read)
  929. {
  930. value |= IIC_READ;
  931. }
  932. if(reg_addr_valid)
  933. {
  934. value |= IIC_REG_ADDR_VALID;
  935. value |= IIC_REG_ADDR(reg_addr);
  936. }
  937. value |= IIC_ADDR_HIGH_4_BIT;
  938. value |= IIC_CHAIN_NUMBER(chain);
  939. value |= data;
  940. ret = set_pic_iic(value);
  941. return ret;
  942. }
  943. void send_pic_command(unsigned char chain)
  944. {
  945. write_pic_iic(false, false, 0x0, chain, PIC_COMMAND_1);
  946. write_pic_iic(false, false, 0x0, chain, PIC_COMMAND_2);
  947. }
  948. void get_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char *addr_H, unsigned char *addr_L);
  949. void set_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char addr_H, unsigned char addr_L)
  950. {
  951. unsigned char check_addr_H, check_addr_L;
  952. char logstr[1024];
  953. int try_count=0;
  954. #ifdef ENABLE_CHECK_PIC_FLASH_ADDR
  955. do
  956. {
  957. #endif
  958. send_pic_command(chain);
  959. write_pic_iic(false, false, 0x0, chain, SET_PIC_FLASH_POINTER);
  960. write_pic_iic(false, false, 0x0, chain, addr_H);
  961. write_pic_iic(false, false, 0x0, chain, addr_L);
  962. // we need check this address, because some PIC lost data of flash!!!
  963. get_pic_iic_flash_addr_pointer(chain,&check_addr_H,&check_addr_L);
  964. if(check_addr_H!=addr_H || check_addr_L!=addr_L)
  965. {
  966. sprintf(logstr,"Error of set PIC FLASH addr: addr_H=%x(%x) addr_L=%x(%x) on Chain[%d]\n",addr_H,check_addr_H,addr_L,check_addr_L,chain);
  967. writeInitLogFile(logstr);
  968. #ifndef ENABLE_CHECK_PIC_FLASH_ADDR
  969. }
  970. #else
  971. try_count++;
  972. if(try_count>3)
  973. break;
  974. reset_iic_pic(chain);
  975. sleep(5);
  976. }
  977. else break;
  978. }
  979. while(1);
  980. #endif
  981. }
  982. void send_data_to_pic_iic(unsigned char chain, unsigned char command, unsigned char *buf, unsigned char length)
  983. {
  984. int i=0;
  985. write_pic_iic(false, false, 0x0, chain, command);
  986. for(i=0; i<length; i++)
  987. {
  988. write_pic_iic(false, false, 0x0, chain, *(buf + i));
  989. }
  990. }
  991. void get_data_from_pic_iic(unsigned char chain, unsigned char command, unsigned char *buf, unsigned char length)
  992. {
  993. int i=0;
  994. write_pic_iic(false, false, 0x0, chain, command);
  995. for(i=0; i<length; i++)
  996. {
  997. *(buf + i) = write_pic_iic(true, false, 0x0, chain, 0);
  998. }
  999. }
  1000. void send_data_to_pic_flash(unsigned char chain, unsigned char *buf)
  1001. {
  1002. send_pic_command(chain);
  1003. send_data_to_pic_iic(chain, SEND_DATA_TO_IIC, buf, 16);
  1004. }
  1005. void get_data_from_pic_flash(unsigned char chain, unsigned char *buf)
  1006. {
  1007. send_pic_command(chain);
  1008. get_data_from_pic_iic(chain, READ_DATA_FROM_IIC, buf, 16);
  1009. }
  1010. unsigned char erase_pic_flash(unsigned char chain)
  1011. {
  1012. send_pic_command(chain);
  1013. write_pic_iic(false, false, 0x0, chain, ERASE_IIC_FLASH);
  1014. usleep(100000);
  1015. /* while(1)
  1016. {
  1017. usleep(3000);
  1018. ret = write_pic_iic(true, false, 0x0, chain, 0);
  1019. if(ret == 0x0)
  1020. {
  1021. printf("erase done\n");
  1022. return ret;
  1023. }
  1024. }
  1025. */
  1026. return 0;
  1027. }
  1028. unsigned char erase_pic_flash_all(unsigned char chain)
  1029. {
  1030. unsigned int i=0, erase_loop = 0;
  1031. unsigned char start_addr_h = PIC_FLASH_POINTER_START_ADDRESS_H, start_addr_l = PIC_FLASH_POINTER_START_ADDRESS_L;
  1032. unsigned char end_addr_h = PIC_FLASH_POINTER_END_ADDRESS_H, end_addr_l = PIC_FLASH_POINTER_END_ADDRESS_L;
  1033. unsigned int pic_flash_length=0;
  1034. set_pic_iic_flash_addr_pointer(chain, PIC_FLASH_POINTER_START_ADDRESS_H, PIC_FLASH_POINTER_START_ADDRESS_L);
  1035. pic_flash_length = (((unsigned int)end_addr_h << 8) + end_addr_l) - (((unsigned int)start_addr_h << 8) + start_addr_l) + 1;
  1036. erase_loop = pic_flash_length/PIC_FLASH_SECTOR_LENGTH;
  1037. for(i=0; i<erase_loop; i++)
  1038. {
  1039. erase_pic_flash(chain);
  1040. }
  1041. }
  1042. void set_temperature_offset_value(unsigned char chain, unsigned char *value)
  1043. {
  1044. send_pic_command(chain);
  1045. send_data_to_pic_iic(chain, WR_TEMP_OFFSET_VALUE, value, 8);
  1046. cgsleep_ms(100000);
  1047. }
  1048. #ifdef T9_18
  1049. void AT24C02_read_bytes(unsigned char address, unsigned char *buf, unsigned char which_iic, unsigned char length);
  1050. // only the first chain of hashboard has temp sensor!!!
  1051. void get_temperature_offset_value(unsigned char chain, unsigned char *value)
  1052. {
  1053. if(fpga_version>=0xE)
  1054. {
  1055. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  1056. getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1057. AT24C02_read_bytes(SENSOR_OFFSET_ADDR, value, new_T9_PLUS_chainIndex, 8);
  1058. }
  1059. else
  1060. {
  1061. AT24C02_read_bytes(SENSOR_OFFSET_ADDR, value, (chain/3), 8);
  1062. }
  1063. }
  1064. #else
  1065. void get_temperature_offset_value(unsigned char chain, unsigned char *value)
  1066. {
  1067. send_pic_command(chain);
  1068. get_data_from_pic_iic(chain, RD_TEMP_OFFSET_VALUE, value, 8);
  1069. }
  1070. #endif
  1071. unsigned char write_data_into_pic_flash(unsigned char chain)
  1072. {
  1073. send_pic_command(chain);
  1074. write_pic_iic(false, false, 0x0, chain, WRITE_DATA_INTO_PIC);
  1075. usleep(100000);
  1076. return 0;
  1077. }
  1078. #ifdef T9_18
  1079. int dsPIC33EP16GS202_get_pic_sw_version(unsigned char which_iic, unsigned char *version)
  1080. {
  1081. unsigned char length = 0x04, crc_data[2] = {0xff}, read_back_data[5] = {0xff};
  1082. unsigned short crc = 0;
  1083. int retry_count=0;
  1084. char logstr[1024];
  1085. //printf("\n--- %s\n", __FUNCTION__);
  1086. *version = 0xff;
  1087. crc = length + GET_PIC_SOFTWARE_VERSION;
  1088. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1089. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1090. //printf("--- %s: crc_data[0] = 0x%x, crc_data[1] = 0x%x\n", __FUNCTION__, crc_data[0], crc_data[1]);
  1091. while(retry_count++<3)
  1092. {
  1093. // pthread_mutex_lock(&iic_mutex);
  1094. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  1095. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  1096. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  1097. T9_plus_write_pic_iic(false, false, 0x0, which_iic, GET_PIC_SOFTWARE_VERSION);
  1098. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  1099. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  1100. usleep(100*1000);
  1101. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1102. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1103. read_back_data[2] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1104. read_back_data[3] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1105. read_back_data[4] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1106. // pthread_mutex_unlock(&iic_mutex);
  1107. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  1108. usleep(100*1000);
  1109. if((read_back_data[1] != GET_PIC_SOFTWARE_VERSION) || (read_back_data[0] != 5))
  1110. {
  1111. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  1112. writeInitLogFile(logstr);
  1113. sleep(1);
  1114. // return 0; // error
  1115. }
  1116. else
  1117. {
  1118. crc = read_back_data[0] + read_back_data[1] + read_back_data[2];
  1119. if(((unsigned char)((crc >> 8) & 0x00ff) != read_back_data[3]) || ((unsigned char)((crc >> 0) & 0x00ff) != read_back_data[4]))
  1120. {
  1121. //printf("\n--- %s failed!\n\n", __FUNCTION__);
  1122. // return 0; // error
  1123. sleep(1);
  1124. }
  1125. else
  1126. {
  1127. *version = read_back_data[2];
  1128. //printf("\n--- %s ok\n\n", __FUNCTION__);
  1129. return 1; // ok
  1130. }
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. void get_pic_software_version(unsigned char chain, unsigned char *version)
  1136. {
  1137. if(fpga_version>=0xE)
  1138. {
  1139. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  1140. getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1141. dsPIC33EP16GS202_get_pic_sw_version(new_T9_PLUS_chainIndex,version);
  1142. }
  1143. else
  1144. {
  1145. dsPIC33EP16GS202_get_pic_sw_version(chain/3,version);
  1146. }
  1147. }
  1148. int dsPIC33EP16GS202_jump_to_app_from_loader(unsigned char which_iic)
  1149. {
  1150. unsigned char length = 0x04, crc_data[2] = {0xff}, read_back_data[2] = {0xff};
  1151. unsigned short crc = 0;
  1152. char logstr[1024];
  1153. int retry_count=0;
  1154. //printf("\n--- %s\n", __FUNCTION__);
  1155. crc = length + JUMP_FROM_LOADER_TO_APP;
  1156. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1157. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1158. //printf("--- %s: crc_data[0] = 0x%x, crc_data[1] = 0x%x\n", __FUNCTION__, crc_data[0], crc_data[1]);
  1159. while(retry_count++<3)
  1160. {
  1161. // pthread_mutex_lock(&iic_mutex);
  1162. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  1163. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  1164. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  1165. T9_plus_write_pic_iic(false, false, 0x0, which_iic, JUMP_FROM_LOADER_TO_APP);
  1166. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  1167. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  1168. usleep(100*1000);
  1169. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1170. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1171. // pthread_mutex_unlock(&iic_mutex);
  1172. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  1173. if((read_back_data[0] != JUMP_FROM_LOADER_TO_APP) || (read_back_data[1] != 1))
  1174. {
  1175. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  1176. writeInitLogFile(logstr);
  1177. sleep(1);
  1178. // return 0; // error
  1179. }
  1180. else
  1181. {
  1182. sleep(1);
  1183. //printf("\n--- %s ok\n\n", __FUNCTION__);
  1184. return 1; // ok
  1185. }
  1186. }
  1187. return 0;
  1188. }
  1189. int dsPIC33EP16GS202_reset_pic(unsigned char which_iic)
  1190. {
  1191. unsigned char length = 0x04, crc_data[2] = {0xff}, read_back_data[2] = {0xff};
  1192. unsigned short crc = 0;
  1193. char logstr[1024];
  1194. int retry_count=0;
  1195. //printf("\n--- %s\n", __FUNCTION__);
  1196. crc = length + RESET_PIC;
  1197. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1198. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1199. //printf("--- %s: which_iic=%d crc_data[0] = 0x%x, crc_data[1] = 0x%x\n", __FUNCTION__,which_iic, crc_data[0], crc_data[1]);
  1200. while(retry_count++<3)
  1201. {
  1202. // pthread_mutex_lock(&iic_mutex);
  1203. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  1204. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  1205. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  1206. T9_plus_write_pic_iic(false, false, 0x0, which_iic, RESET_PIC);
  1207. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  1208. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  1209. usleep(400*1000);
  1210. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1211. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1212. // pthread_mutex_unlock(&iic_mutex);
  1213. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  1214. usleep(100*1000);
  1215. if((read_back_data[0] != RESET_PIC) || (read_back_data[1] != 1))
  1216. {
  1217. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  1218. writeInitLogFile(logstr);
  1219. sleep(1);
  1220. // return 0; // error
  1221. }
  1222. else
  1223. {
  1224. //printf("\n--- %s ok\n\n", __FUNCTION__);
  1225. sleep(1);
  1226. return 1; // ok
  1227. }
  1228. }
  1229. return 0;
  1230. }
  1231. int dsPIC33EP16GS202_update_pic_app_program(unsigned char which_iic);
  1232. void jump_to_app_CheckAndRestorePIC_T9_18(int chainIndex)
  1233. {
  1234. unsigned char pic_version;
  1235. char logstr[1024];
  1236. int try_count=0;
  1237. if(fpga_version>=0xE)
  1238. {
  1239. if(chainIndex<1 || chainIndex>3)
  1240. return;
  1241. sprintf(logstr,"chain[%d] PIC jump to app\n",chainIndex);
  1242. writeInitLogFile(logstr);
  1243. dsPIC33EP16GS202_jump_to_app_from_loader(chainIndex);
  1244. get_pic_software_version(chainIndex,&pic_version);
  1245. sprintf(logstr,"Check chain[%d] PIC fw version=0x%02x\n",chainIndex,pic_version);
  1246. writeInitLogFile(logstr);
  1247. #ifdef ENABLE_RESTORE_PIC_APP
  1248. #ifndef DEBUG_PIC_UPGRADE
  1249. while(pic_version!=PIC_VERSION && try_count<2)
  1250. #endif
  1251. {
  1252. try_count++;
  1253. sprintf(logstr,"chain[%d] PIC need restore ...\n",chainIndex);
  1254. writeInitLogFile(logstr);
  1255. dsPIC33EP16GS202_update_pic_app_program(chainIndex);
  1256. dsPIC33EP16GS202_jump_to_app_from_loader(chainIndex);
  1257. get_pic_software_version(chainIndex,&pic_version);
  1258. sprintf(logstr,"After restore: chain[%d] PIC fw version=0x%02x\n",chainIndex,pic_version);
  1259. writeInitLogFile(logstr);
  1260. }
  1261. #endif
  1262. }
  1263. else
  1264. {
  1265. if(chainIndex%3 != 0)
  1266. return;
  1267. sprintf(logstr,"chain[%d] PIC jump to app\n",chainIndex);
  1268. writeInitLogFile(logstr);
  1269. dsPIC33EP16GS202_jump_to_app_from_loader(chainIndex/3);
  1270. get_pic_software_version(chainIndex,&pic_version);
  1271. sprintf(logstr,"Check chain[%d] PIC fw version=0x%02x\n",chainIndex,pic_version);
  1272. writeInitLogFile(logstr);
  1273. #ifdef ENABLE_RESTORE_PIC_APP
  1274. #ifndef DEBUG_PIC_UPGRADE
  1275. while(pic_version!=PIC_VERSION && try_count<2)
  1276. #endif
  1277. {
  1278. try_count++;
  1279. sprintf(logstr,"chain[%d] PIC need restore ...\n",chainIndex);
  1280. writeInitLogFile(logstr);
  1281. dsPIC33EP16GS202_update_pic_app_program(chainIndex/3);
  1282. dsPIC33EP16GS202_jump_to_app_from_loader(chainIndex/3);
  1283. get_pic_software_version(chainIndex,&pic_version);
  1284. sprintf(logstr,"After restore: chain[%d] PIC fw version=0x%02x\n",chainIndex,pic_version);
  1285. writeInitLogFile(logstr);
  1286. }
  1287. #endif
  1288. }
  1289. }
  1290. unsigned char reset_iic_pic(unsigned char chain)
  1291. {
  1292. if(fpga_version>=0xE)
  1293. {
  1294. if(chain<1 || chain>3) // T9+ only chain[1] [2] [3] can control the PIC with new FPGA which can support S9 too.
  1295. return 1;
  1296. return dsPIC33EP16GS202_reset_pic(chain);
  1297. }
  1298. else
  1299. {
  1300. if(chain%3 != 0)
  1301. return 1;
  1302. return dsPIC33EP16GS202_reset_pic(chain/3);
  1303. }
  1304. }
  1305. #else
  1306. unsigned char jump_to_app_from_loader(unsigned char chain)
  1307. {
  1308. unsigned char ret=0xff;
  1309. send_pic_command(chain);
  1310. write_pic_iic(false, false, 0x0, chain, JUMP_FROM_LOADER_TO_APP);
  1311. cgsleep_us(100000);
  1312. }
  1313. unsigned char reset_iic_pic(unsigned char chain)
  1314. {
  1315. unsigned char ret=0xff;
  1316. send_pic_command(chain);
  1317. write_pic_iic(false, false, 0x0, chain, RESET_PIC);
  1318. cgsleep_us(100000);
  1319. }
  1320. void get_pic_software_version(unsigned char chain, unsigned char *version)
  1321. {
  1322. int i=0;
  1323. send_pic_command(chain);
  1324. write_pic_iic(false, false, 0x0, chain, GET_PIC_SOFTWARE_VERSION);
  1325. for(i=0; i<PIC_SOFTWARE_VERSION_LENGTH; i++)
  1326. {
  1327. *(version + i) = write_pic_iic(true, false, 0x0, chain, 0);
  1328. }
  1329. }
  1330. void update_pic_program(unsigned char chain)
  1331. {
  1332. unsigned char program_data[10*MAX_CHAR_NUM] = {0};
  1333. FILE * pic_program_file;
  1334. unsigned int i=0,j;
  1335. unsigned char data_read[5]= {0,0,0,0,'\0'}, buf[16]= {0};
  1336. unsigned int data_int = 0;
  1337. unsigned char start_addr_h = PIC_FLASH_POINTER_START_ADDRESS_H, start_addr_l = PIC_FLASH_POINTER_START_ADDRESS_L;
  1338. unsigned char end_addr_h = PIC_FLASH_POINTER_END_ADDRESS_H, end_addr_l = PIC_FLASH_POINTER_END_ADDRESS_L;
  1339. unsigned int pic_flash_length=0;
  1340. //printf("\n--- update pic program\n");
  1341. // read upgrade file first, if it is wrong, don't erase pic, but just return;
  1342. pic_program_file = fopen(PIC_PROGRAM, "r");
  1343. if(!pic_program_file)
  1344. {
  1345. printf("\n%s: open hash_s8_app.txt failed\n", __FUNCTION__);
  1346. return;
  1347. }
  1348. fseek(pic_program_file,0,SEEK_SET);
  1349. memset(program_data, 0x0, 10*MAX_CHAR_NUM);
  1350. pic_flash_length = (((unsigned int)end_addr_h << 8) + end_addr_l) - (((unsigned int)start_addr_h << 8) + start_addr_l) + 1;
  1351. //printf("pic_flash_length = %d\n", pic_flash_length);
  1352. for(i=0; i<pic_flash_length; i++)
  1353. {
  1354. fgets((char *)data_read, MAX_CHAR_NUM - 1 , pic_program_file);
  1355. //printf("data_read[0]=%c, data_read[1]=%c, data_read[2]=%c, data_read[3]=%c\n", data_read[0], data_read[1], data_read[2], data_read[3]);
  1356. data_int = strtoul((char *)data_read, NULL, 16);
  1357. //printf("data_int = 0x%04x\n", data_int);
  1358. program_data[2*i + 0] = (unsigned char)((data_int >> 8) & 0x000000ff);
  1359. program_data[2*i + 1] = (unsigned char)(data_int & 0x000000ff);
  1360. //printf("program_data[%d]=0x%02x, program_data[%d]=0x%02x\n\n", 2*i + 0, program_data[2*i + 0], 2*i + 1, program_data[2*i + 1]);
  1361. }
  1362. fclose(pic_program_file);
  1363. // after read upgrade file correct, erase pic
  1364. reset_iic_pic(chain);
  1365. erase_pic_flash_all(chain);
  1366. // write data into pic
  1367. set_pic_iic_flash_addr_pointer(chain, PIC_FLASH_POINTER_START_ADDRESS_H, PIC_FLASH_POINTER_START_ADDRESS_L);
  1368. for(i=0; i<pic_flash_length/PIC_FLASH_SECTOR_LENGTH*4; i++)
  1369. {
  1370. memcpy(buf, program_data+i*16, 16);
  1371. /**/
  1372. printf("send pic program time: %d\n",i);
  1373. for(j=0; j<16; j++)
  1374. {
  1375. printf("buf[%d] = 0x%02x\n", j, *(buf+j));
  1376. }
  1377. printf("\n");
  1378. send_data_to_pic_flash(chain, buf);
  1379. write_data_into_pic_flash(chain);
  1380. }
  1381. }
  1382. // this check and restore PIC function must be called after reset_iic_pic process, that means only can be called when in bootloader mode!
  1383. void jump_to_app_CheckAndRestorePIC(int chainIndex) // check PIC app is OK or not, if not right, write flash to restore app and jump to app mode again!
  1384. {
  1385. unsigned char pic_version;
  1386. char logstr[1024];
  1387. int try_count=0;
  1388. jump_to_app_from_loader(chainIndex);
  1389. get_pic_software_version(chainIndex,&pic_version);
  1390. sprintf(logstr,"Check chain[%d] PIC fw version=0x%02x\n",chainIndex,pic_version);
  1391. writeInitLogFile(logstr);
  1392. #ifdef ENABLE_RESTORE_PIC_APP
  1393. #ifndef DEBUG_PIC_UPGRADE
  1394. while(pic_version!=PIC_VERSION && try_count<2)
  1395. #endif
  1396. {
  1397. try_count++;
  1398. sprintf(logstr,"chain[%d] PIC need restore ...\n",chainIndex);
  1399. writeInitLogFile(logstr);
  1400. update_pic_program(chainIndex);
  1401. jump_to_app_from_loader(chainIndex);
  1402. get_pic_software_version(chainIndex,&pic_version);
  1403. sprintf(logstr,"After restore: chain[%d] PIC fw version=0x%02x\n",chainIndex,pic_version);
  1404. writeInitLogFile(logstr);
  1405. }
  1406. #endif
  1407. }
  1408. #endif
  1409. void get_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char *addr_H, unsigned char *addr_L)
  1410. {
  1411. send_pic_command(chain);
  1412. write_pic_iic(false, false, 0x0, chain, GET_PIC_FLASH_POINTER);
  1413. *addr_H = write_pic_iic(true, false, 0x0, chain, 0);
  1414. *addr_L = write_pic_iic(true, false, 0x0, chain, 0);
  1415. }
  1416. #ifdef T9_18
  1417. unsigned char write_EEPROM_iic(bool read, bool reg_addr_valid, unsigned char reg_addr, unsigned char which_iic, unsigned char data)
  1418. {
  1419. unsigned int value = 0x00000000, counter = 0;
  1420. unsigned char ret = 0;
  1421. while(1)
  1422. {
  1423. ret = get_iic();
  1424. //printf("iic command ret = 0x%08x\n", ret);
  1425. if(ret & 0x80000000)
  1426. {
  1427. break;
  1428. }
  1429. else if(counter++>3)
  1430. {
  1431. //printf("^^^^^^^^^^^^^^^^^^^^^\n");
  1432. break;
  1433. }
  1434. usleep(1*1000);
  1435. }
  1436. if(read)
  1437. {
  1438. value |= IIC_READ;
  1439. }
  1440. if(reg_addr_valid)
  1441. {
  1442. value |= IIC_REG_ADDR_VALID;
  1443. value |= IIC_REG_ADDR(reg_addr);
  1444. }
  1445. value |= EEPROM_ADDR_HIGH_4_BIT;
  1446. if(fpga_version>=0xE && fpga_version<=0xF)// decrease one , because chain[1] [2] [3] use PIC [0] [1] [2]
  1447. which_iic--;
  1448. value |= IIC_SELECT(which_iic);
  1449. value |= data;
  1450. ret = set_iic(value);
  1451. return ret;
  1452. }
  1453. void AT24C02_write_one_byte(unsigned char address, unsigned char data, unsigned char which_iic)
  1454. {
  1455. // pthread_mutex_lock(&iic_mutex);
  1456. write_EEPROM_iic(false, true, address, which_iic, data);
  1457. // pthread_mutex_unlock(&iic_mutex);
  1458. }
  1459. unsigned char AT24C02_read_one_byte(unsigned char address, unsigned char which_iic)
  1460. {
  1461. unsigned char data = 0;
  1462. // pthread_mutex_lock(&iic_mutex);
  1463. data = write_EEPROM_iic(true, true, address, which_iic, 0);
  1464. // pthread_mutex_unlock(&iic_mutex);
  1465. return data;
  1466. }
  1467. void AT24C02_write_bytes(unsigned char address, unsigned char *buf, unsigned char which_iic, unsigned char length)
  1468. {
  1469. unsigned char i = 0;
  1470. //printf("--- %s\n", __FUNCTION__);
  1471. if((address + length) > EEPROM_LENGTH)
  1472. {
  1473. //printf("\n--- %s: address + length = %d > EEPROM_LENGTH(%d)\n", __FUNCTION__, address + length, EEPROM_LENGTH);
  1474. return;
  1475. }
  1476. for(i=0; i<length; i++)
  1477. {
  1478. AT24C02_write_one_byte(address+i, *(buf + i), which_iic);
  1479. }
  1480. }
  1481. void AT24C02_read_bytes(unsigned char address, unsigned char *buf, unsigned char which_iic, unsigned char length)
  1482. {
  1483. unsigned char i = 0;
  1484. //printf("--- %s\n", __FUNCTION__);
  1485. if((address + length) > EEPROM_LENGTH)
  1486. {
  1487. //printf("\n--- %s: address + length = %d > EEPROM_LENGTH(%d)\n", __FUNCTION__, address + length, EEPROM_LENGTH);
  1488. return;
  1489. }
  1490. for(i=0; i<length; i++)
  1491. {
  1492. *(buf + i) = AT24C02_read_one_byte(address+i, which_iic);
  1493. }
  1494. }
  1495. unsigned char read_freq_badcores(unsigned char chain, unsigned char *buf)
  1496. {
  1497. if(fpga_version>=0xE)
  1498. {
  1499. if(chain<1 || chain>3)
  1500. return 0;
  1501. AT24C02_read_bytes(FREQ_BADCORE_ADDR,buf,chain,128);
  1502. }
  1503. else
  1504. {
  1505. if(chain%3 != 0)
  1506. return 0;
  1507. AT24C02_read_bytes(FREQ_BADCORE_ADDR,buf,chain/3,128);
  1508. }
  1509. return 128;
  1510. }
  1511. unsigned char save_freq_badcores(unsigned char chain, unsigned char *buf)
  1512. {
  1513. if(fpga_version>=0xE)
  1514. {
  1515. if(chain<1 || chain>3)
  1516. return 0;
  1517. AT24C02_write_bytes(FREQ_BADCORE_ADDR,buf,chain,128);
  1518. }
  1519. else
  1520. {
  1521. if(chain%3 != 0)
  1522. return 0;
  1523. AT24C02_write_bytes(FREQ_BADCORE_ADDR,buf,chain/3,128);
  1524. }
  1525. return 128;
  1526. }
  1527. void AT24C02_save_voltage(unsigned char which_iic, unsigned char voltage)
  1528. {
  1529. //printf("\n--- %s\n", __FUNCTION__);
  1530. AT24C02_write_one_byte(VOLTAGE_ADDR, voltage, which_iic);
  1531. //printf("%s: voltage = 0x%02x\n", __FUNCTION__, voltage);
  1532. }
  1533. unsigned char AT24C02_read_voltage(unsigned char which_iic)
  1534. {
  1535. return AT24C02_read_one_byte(VOLTAGE_ADDR, which_iic);
  1536. }
  1537. int set_Voltage_S9_plus_plus_BM1387_54(unsigned char which_iic, unsigned char pic_voltage)
  1538. {
  1539. double temp_voltage = 0;
  1540. unsigned char length = 0x07, crc_data[2] = {0xff}, read_back_data[2] = {0xff};
  1541. unsigned short crc = 0;
  1542. unsigned char voltage1 = pic_voltage, voltage2 = 0, voltage3 = 0;
  1543. int i;
  1544. char logstr[1024];
  1545. int retry_count=0;
  1546. //printf("voltage1 = %d\n", voltage1);
  1547. if((voltage1 > 127) || (voltage2 > 127) || (voltage3 > 127))
  1548. {
  1549. //printf("\n--- %s voltage1(%d) > 127 \n\n", __FUNCTION__, voltage1);
  1550. return 0; // because 4017 just have 127 level
  1551. }
  1552. crc = length + SET_VOLTAGE + voltage1 + voltage2 + voltage3;
  1553. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1554. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1555. //printf("--- %s: crc_data[0] = 0x%x, crc_data[1] = 0x%x\n", __FUNCTION__, crc_data[0], crc_data[1]);
  1556. while(retry_count++<3)
  1557. {
  1558. // pthread_mutex_lock(&iic_mutex);
  1559. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  1560. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  1561. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  1562. T9_plus_write_pic_iic(false, false, 0x0, which_iic, SET_VOLTAGE);
  1563. T9_plus_write_pic_iic(false, false, 0x0, which_iic, voltage1);
  1564. T9_plus_write_pic_iic(false, false, 0x0, which_iic, voltage2);
  1565. T9_plus_write_pic_iic(false, false, 0x0, which_iic, voltage3);
  1566. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  1567. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  1568. usleep(100*1000);
  1569. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1570. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1571. // pthread_mutex_unlock(&iic_mutex);
  1572. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  1573. if((read_back_data[0] != SET_VOLTAGE) || (read_back_data[1] != 1))
  1574. {
  1575. sprintf(logstr,"%s failed on Chain[%d]!\n\n", __FUNCTION__,which_iic);
  1576. writeInitLogFile(logstr);
  1577. sleep(1);
  1578. // return 0; // error
  1579. }
  1580. else
  1581. {
  1582. //printf("\n--- %s ok!\n\n", __FUNCTION__);
  1583. AT24C02_save_voltage(which_iic, voltage1);
  1584. return 1; // ok
  1585. }
  1586. }
  1587. return 0;
  1588. }
  1589. void set_voltage_T9_18_into_PIC(unsigned char chain, unsigned char voltage)
  1590. {
  1591. if(fpga_version>=0xE)
  1592. {
  1593. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset; // only used by new T9+ FPGA
  1594. getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1595. set_Voltage_S9_plus_plus_BM1387_54(new_T9_PLUS_chainIndex,voltage);
  1596. }
  1597. else
  1598. {
  1599. set_Voltage_S9_plus_plus_BM1387_54(chain/3,voltage);
  1600. }
  1601. }
  1602. unsigned char getHighestVoltagePIC(int chainIndex)
  1603. {
  1604. int startIndex;
  1605. int i;
  1606. unsigned char minVolPIC;
  1607. if(fpga_version>=0xE)
  1608. {
  1609. switch(chainIndex)
  1610. {
  1611. case 1:
  1612. case 8:
  1613. case 9:
  1614. minVolPIC=chain_voltage_pic[1];
  1615. for(i=8; i<10; i++)
  1616. {
  1617. if(minVolPIC>chain_voltage_pic[i])
  1618. minVolPIC=chain_voltage_pic[i]; // the value is lower, means voltage is higher!!!
  1619. }
  1620. break;
  1621. case 2:
  1622. case 10:
  1623. case 11:
  1624. minVolPIC=chain_voltage_pic[2];
  1625. for(i=10; i<12; i++)
  1626. {
  1627. if(minVolPIC>chain_voltage_pic[i])
  1628. minVolPIC=chain_voltage_pic[i]; // the value is lower, means voltage is higher!!!
  1629. }
  1630. break;
  1631. case 3:
  1632. case 12:
  1633. case 13:
  1634. minVolPIC=chain_voltage_pic[3];
  1635. for(i=12; i<14; i++)
  1636. {
  1637. if(minVolPIC>chain_voltage_pic[i])
  1638. minVolPIC=chain_voltage_pic[i]; // the value is lower, means voltage is higher!!!
  1639. }
  1640. break;
  1641. default:
  1642. minVolPIC=0;
  1643. break;
  1644. }
  1645. }
  1646. else
  1647. {
  1648. startIndex=(chainIndex/3)*3; // get start chainindex, 0,1,2 = 0 3,4,5 = 3 ...
  1649. minVolPIC=chain_voltage_pic[chainIndex];
  1650. for(i=startIndex; i<startIndex+3; i++)
  1651. {
  1652. if(minVolPIC>chain_voltage_pic[i])
  1653. minVolPIC=chain_voltage_pic[i]; // the value is lower, means voltage is higher!!!
  1654. }
  1655. }
  1656. return minVolPIC;
  1657. }
  1658. void set_pic_voltage_T9_18(unsigned char chain)
  1659. {
  1660. char logstr[1024];
  1661. unsigned char vol_pic;
  1662. // before call this function, must set chain_voltage_pic[chain] value at first! because T9_18 will only set the highest voltage for 3 chains!
  1663. vol_pic=getHighestVoltagePIC(chain);
  1664. sprintf(logstr,"set voltage=%d on chain[%d], the real voltage=%d\n",getVolValueFromPICvoltage(chain_voltage_pic[chain]),chain,getVolValueFromPICvoltage(vol_pic));
  1665. writeInitLogFile(logstr);
  1666. set_voltage_T9_18_into_PIC(chain, vol_pic);
  1667. }
  1668. void set_pic_voltage(unsigned char chain, unsigned char voltage)
  1669. {
  1670. if(fpga_version>=0xE)
  1671. {
  1672. if(chain>=1 && chain<=3)
  1673. set_pic_voltage_T9_18(chain);
  1674. }
  1675. else
  1676. {
  1677. if(chain%3==0)
  1678. set_pic_voltage_T9_18(chain);
  1679. }
  1680. }
  1681. unsigned char get_pic_voltage(unsigned char chain)
  1682. {
  1683. if(fpga_version>=0xE)
  1684. {
  1685. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  1686. getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1687. return AT24C02_read_voltage(new_T9_PLUS_chainIndex);
  1688. }
  1689. else
  1690. {
  1691. return AT24C02_read_voltage(chain/3);
  1692. }
  1693. }
  1694. #else
  1695. void set_pic_voltage(unsigned char chain, unsigned char voltage)
  1696. {
  1697. send_pic_command(chain);
  1698. applog(LOG_NOTICE,"%s voltage %u",__FUNCTION__,voltage);
  1699. write_pic_iic(false, false, 0x0, chain, SET_VOLTAGE);
  1700. write_pic_iic(false, false, 0x0, chain, voltage);
  1701. cgsleep_us(100000);
  1702. }
  1703. unsigned char get_pic_voltage(unsigned char chain)
  1704. {
  1705. unsigned char ret=0;
  1706. send_pic_command(chain);
  1707. write_pic_iic(false, false, 0x0, chain, GET_VOLTAGE);
  1708. ret = write_pic_iic(true, false, 0x0, chain, 0);
  1709. applog(LOG_NOTICE,"%s: voltage = %d\n", __FUNCTION__, ret);
  1710. return ret;
  1711. }
  1712. #endif
  1713. void set_voltage_setting_time(unsigned char chain, unsigned char *time)
  1714. {
  1715. send_pic_command(chain);
  1716. send_data_to_pic_iic(chain, SET_VOLTAGE_TIME, time, 6);
  1717. cgsleep_us(100000);
  1718. }
  1719. void set_hash_board_id_number(unsigned char chain, unsigned char *id)
  1720. {
  1721. send_pic_command(chain);
  1722. send_data_to_pic_iic(chain, SET_HASH_BOARD_ID, id, 12);
  1723. cgsleep_us(100000);
  1724. }
  1725. #ifdef T9_18
  1726. void get_hash_board_id_number(unsigned char chain, unsigned char *id)
  1727. {
  1728. if(fpga_version>=0xE)
  1729. {
  1730. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  1731. getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1732. AT24C02_read_bytes(HASH_ID_ADDR, id, new_T9_PLUS_chainIndex, 12);
  1733. }
  1734. else
  1735. {
  1736. AT24C02_read_bytes(HASH_ID_ADDR, id, chain/3, 12);
  1737. }
  1738. }
  1739. #else
  1740. void get_hash_board_id_number(unsigned char chain, unsigned char *id)
  1741. {
  1742. send_pic_command(chain);
  1743. get_data_from_pic_iic(chain, GET_HASH_BOARD_ID, id, 12);
  1744. }
  1745. #endif
  1746. void write_host_MAC_and_time(unsigned char chain, unsigned char *buf)
  1747. {
  1748. send_pic_command(chain);
  1749. send_data_to_pic_iic(chain, SET_HOST_MAC_ADDRESS, buf, 12);
  1750. cgsleep_us(100000);
  1751. }
  1752. void enable_pic_dc_dc(unsigned char chain)
  1753. {
  1754. send_pic_command(chain);
  1755. write_pic_iic(false, false, 0x0, chain, ENABLE_VOLTAGE);
  1756. write_pic_iic(false, false, 0x0, chain, 1);
  1757. }
  1758. void enable_pic_dc_dc_all()
  1759. {
  1760. int i;
  1761. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  1762. {
  1763. if(dev->chain_exist[i] == 1)
  1764. {
  1765. enable_pic_dc_dc(i);
  1766. cgsleep_ms(1);
  1767. }
  1768. }
  1769. }
  1770. #ifdef T9_18
  1771. int getChainPICMagicNumber(int chainIndex)
  1772. {
  1773. if(fpga_version>=0xE)
  1774. {
  1775. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  1776. getPICChainIndexOffset(chainIndex,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1777. return chain_pic_buf[new_T9_PLUS_chainIndex][0];
  1778. }
  1779. else
  1780. {
  1781. return chain_pic_buf[((chainIndex/3)*3)][0];
  1782. }
  1783. }
  1784. int dsPIC33EP16GS202_enable_pic_dc_dc(unsigned char which_iic, unsigned char enable)
  1785. {
  1786. unsigned char length = 0x05, crc_data[2] = {0xff}, read_back_data[2] = {0xff};
  1787. unsigned short crc = 0;
  1788. char logstr[1024];
  1789. int retry_count=0;
  1790. //printf("\n--- %s\n", __FUNCTION__);
  1791. crc = length + ENABLE_VOLTAGE + enable;
  1792. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1793. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1794. //printf("--- %s: crc_data[0] = 0x%x, crc_data[1] = 0x%x\n", __FUNCTION__, crc_data[0], crc_data[1]);
  1795. while(retry_count++<3)
  1796. {
  1797. // pthread_mutex_lock(&iic_mutex);
  1798. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  1799. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  1800. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  1801. T9_plus_write_pic_iic(false, false, 0x0, which_iic, ENABLE_VOLTAGE);
  1802. T9_plus_write_pic_iic(false, false, 0x0, which_iic, enable);
  1803. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  1804. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  1805. usleep(10*1000);
  1806. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1807. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1808. // pthread_mutex_unlock(&iic_mutex);
  1809. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  1810. if((read_back_data[0] != ENABLE_VOLTAGE) || (read_back_data[1] != 1))
  1811. {
  1812. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  1813. writeInitLogFile(logstr);
  1814. sleep(1);
  1815. // return 0; // error
  1816. }
  1817. else
  1818. {
  1819. //printf("\n--- %s ok\n\n", __FUNCTION__);
  1820. return 1; // ok
  1821. }
  1822. }
  1823. return 0;
  1824. }
  1825. void enable_pic_dac(unsigned char chain)
  1826. {
  1827. if(fpga_version>=0xE)
  1828. {
  1829. if(chain<1 || chain>3)
  1830. return;
  1831. dsPIC33EP16GS202_enable_pic_dc_dc(chain, 1); // open pic dc-dc
  1832. }
  1833. else
  1834. {
  1835. if(chain%3!=0) // only enable DC when enable the first chain of 3 chains, like 0, 3, 6 ...
  1836. return;
  1837. dsPIC33EP16GS202_enable_pic_dc_dc(chain/3, 1); // open pic dc-dc
  1838. }
  1839. }
  1840. void disable_pic_dac(unsigned char chain)
  1841. {
  1842. if(fpga_version>=0xE)
  1843. {
  1844. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  1845. getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  1846. if(chain!=9 && chain!=11 && chain!=13) // only disable DC when close the last chain of 3 chains, like 8, 11, 13 ...
  1847. return;
  1848. dsPIC33EP16GS202_enable_pic_dc_dc(new_T9_PLUS_chainIndex, 0); // open pic dc-dc
  1849. }
  1850. else
  1851. {
  1852. if(chain%3 !=2) // only disable DC when close the last chain of 3 chains, like 2, 5, 8 ...
  1853. return;
  1854. dsPIC33EP16GS202_enable_pic_dc_dc(chain/3, 0); // open pic dc-dc
  1855. }
  1856. }
  1857. unsigned int get_iic()
  1858. {
  1859. int ret = -1;
  1860. ret = *(axi_fpga_addr + IIC_COMMAND);
  1861. //applog(LOG_DEBUG,"%s: IIC_COMMAND is 0x%x\n", __FUNCTION__, ret);
  1862. return ret;
  1863. }
  1864. unsigned char set_iic(unsigned int data)
  1865. {
  1866. unsigned int ret=0;
  1867. unsigned char ret_data = 0;
  1868. int wait_counter=0;
  1869. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  1870. char logstr[1024];
  1871. #endif
  1872. *((unsigned int *)(axi_fpga_addr + IIC_COMMAND)) = data & 0x7fffffff;
  1873. //applog(LOG_DEBUG,"%s: set IIC_COMMAND is 0x%x\n", __FUNCTION__, data & 0x7fffffff);
  1874. while(1)
  1875. {
  1876. ret = get_iic();
  1877. if(ret & 0x80000000)
  1878. {
  1879. ret_data = (unsigned char)(ret & 0x000000ff);
  1880. return ret_data;
  1881. }
  1882. #ifdef DEBUG_ENABLE_I2C_TIMEOUT_PROCESS
  1883. else
  1884. {
  1885. wait_counter++;
  1886. if(wait_counter>3)
  1887. {
  1888. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  1889. set_red_led(true); // open red led
  1890. sprintf(logstr,"Error: set_iic wait IIC timeout!\n");
  1891. writeInitLogFile(logstr);
  1892. #endif
  1893. break;
  1894. }
  1895. //applog(LOG_DEBUG,"%s: waiting write pic iic\n", __FUNCTION__);
  1896. }
  1897. #endif
  1898. usleep(1000);
  1899. }
  1900. }
  1901. unsigned char T9_plus_write_pic_iic(bool read, bool reg_addr_valid, unsigned char reg_addr, unsigned char which_iic, unsigned char data)
  1902. {
  1903. unsigned int value = 0x00000000, counter = 0;
  1904. unsigned int ret = 0;
  1905. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  1906. char logstr[1024];
  1907. #endif
  1908. while(1)
  1909. {
  1910. ret = get_iic();
  1911. //printf("iic command ret = 0x%08x\n", ret);
  1912. if(ret & 0x80000000)
  1913. {
  1914. break;
  1915. }
  1916. #ifdef DEBUG_ENABLE_I2C_TIMEOUT_PROCESS
  1917. else if(counter++>3)
  1918. {
  1919. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  1920. sprintf(logstr,"Error: T9_plus_write_pic_iic wait IIC timeout!\n");
  1921. writeInitLogFile(logstr);
  1922. #endif
  1923. break;
  1924. }
  1925. #endif
  1926. usleep(1*1000);
  1927. }
  1928. if(read)
  1929. {
  1930. value |= IIC_READ;
  1931. }
  1932. if(reg_addr_valid)
  1933. {
  1934. value |= IIC_REG_ADDR_VALID;
  1935. value |= IIC_REG_ADDR(reg_addr);
  1936. }
  1937. value |= IIC_ADDR_HIGH_4_BIT;
  1938. if(fpga_version>=0xE && fpga_version<=0xF)// decrease one , because chain[1] [2] [3] use PIC [0] [1] [2]
  1939. which_iic--;
  1940. value |= IIC_SELECT(which_iic);
  1941. value |= data;
  1942. return set_iic(value);
  1943. }
  1944. int dsPIC33EP16GS202_erase_pic_app_program(unsigned char which_iic)
  1945. {
  1946. unsigned char length = 0x04, crc_data[2] = {0xff}, read_back_data[2] = {0xff};
  1947. unsigned short crc = 0;
  1948. char logstr[1024];
  1949. int retry_count=0;
  1950. crc = length + ERASE_PIC_APP_PROGRAM;
  1951. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1952. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1953. while(retry_count++<3)
  1954. {
  1955. // pthread_mutex_lock(&iic_mutex);
  1956. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  1957. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  1958. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  1959. T9_plus_write_pic_iic(false, false, 0x0, which_iic, ERASE_PIC_APP_PROGRAM);
  1960. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  1961. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  1962. usleep(100*1000);
  1963. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1964. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  1965. // pthread_mutex_unlock(&iic_mutex);
  1966. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  1967. usleep(200*1000);
  1968. if((read_back_data[0] != ERASE_PIC_APP_PROGRAM) || (read_back_data[1] != 1))
  1969. {
  1970. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  1971. writeInitLogFile(logstr);
  1972. sleep(1);
  1973. // return 0; // error
  1974. }
  1975. else
  1976. {
  1977. //printf("\n--- %s ok\n\n", __FUNCTION__);
  1978. return 1; // ok
  1979. }
  1980. }
  1981. return 0;
  1982. }
  1983. int dsPIC33EP16GS202_send_data_to_pic(unsigned char which_iic, unsigned char *buf)
  1984. {
  1985. unsigned char length = 0x14, crc_data[2] = {0xff}, read_back_data[2] = {0xff}, i;
  1986. unsigned short crc = 0;
  1987. char logstr[1024];
  1988. int retry_count=0;
  1989. crc = length + SEND_DATA_TO_IIC;
  1990. for(i=0; i<16; i++)
  1991. {
  1992. crc += *(buf + i);
  1993. }
  1994. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  1995. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  1996. while(retry_count++<3)
  1997. {
  1998. // pthread_mutex_lock(&iic_mutex);
  1999. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  2000. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  2001. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  2002. T9_plus_write_pic_iic(false, false, 0x0, which_iic, SEND_DATA_TO_IIC);
  2003. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 0));
  2004. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 1));
  2005. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 2));
  2006. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 3));
  2007. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 4));
  2008. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 5));
  2009. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 6));
  2010. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 7));
  2011. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 8));
  2012. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 9));
  2013. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 10));
  2014. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 11));
  2015. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 12));
  2016. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 13));
  2017. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 14));
  2018. T9_plus_write_pic_iic(false, false, 0x0, which_iic, *(buf + 15));
  2019. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  2020. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  2021. usleep(200*1000);
  2022. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2023. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2024. // pthread_mutex_unlock(&iic_mutex);
  2025. //printf("--- %s: read_back_data[0] = 0x%x, read_back_data[1] = 0x%x\n", __FUNCTION__, read_back_data[0], read_back_data[1]);
  2026. usleep(100*1000);
  2027. if((read_back_data[0] != SEND_DATA_TO_IIC) || (read_back_data[1] != 1))
  2028. {
  2029. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  2030. writeInitLogFile(logstr);
  2031. sleep(1);
  2032. // return 0; // error
  2033. }
  2034. else
  2035. {
  2036. //printf("\n--- %s ok\n\n", __FUNCTION__);
  2037. return 1; // ok
  2038. }
  2039. }
  2040. return 0;
  2041. }
  2042. int dsPIC33EP16GS202_update_pic_app_program(unsigned char which_iic)
  2043. {
  2044. unsigned char program_data[14080] = {0};
  2045. FILE * pic_program_file;
  2046. unsigned int filesize = 0,i=0,j;
  2047. unsigned char data_read[7]= {0,0,0,0,0,0,'\0'}, buf[16]= {0};
  2048. unsigned int data_int = 0;
  2049. struct stat statbuff;
  2050. unsigned int data_len = 3520, loop = 880;
  2051. unsigned int pic_flash_length=0;
  2052. int ret = 0;
  2053. //printf("\n--- update pic program\n");
  2054. // read upgrade file first, if it is wrong, don't erase pic, but just return;
  2055. pic_program_file = fopen(DSPIC33EP16GS202_PIC_PROGRAM, "r");
  2056. if(!pic_program_file)
  2057. {
  2058. //printf("\n%s: open hash_s8_app.txt failed\n", __FUNCTION__);
  2059. return;
  2060. }
  2061. fseek(pic_program_file,0,SEEK_SET);
  2062. memset(program_data, 0x0, 14080);
  2063. for(i=0; i<data_len; i++)
  2064. {
  2065. fgets(data_read, MAX_CHAR_NUM - 1 , pic_program_file);
  2066. //printf("data_read[0]=%c, data_read[1]=%c, data_read[2]=%c, data_read[3]=%c, data_read[4]=%c, data_read[5]=%c\n",
  2067. // data_read[0], data_read[1], data_read[2], data_read[3], data_read[4], data_read[5]);
  2068. data_int = strtoul(data_read, NULL, 16);
  2069. //printf("data_int = 0x%08x\n", data_int);
  2070. program_data[4*i + 0] = (unsigned char)((data_int >> 24) & 0x000000ff);
  2071. program_data[4*i + 1] = (unsigned char)((data_int >> 16) & 0x000000ff);
  2072. program_data[4*i + 2] = (unsigned char)((data_int >> 8) & 0x000000ff);
  2073. program_data[4*i + 3] = (unsigned char)((data_int >> 0) & 0x000000ff);
  2074. //printf("program_data[%d]=0x%02x, program_data[%d]=0x%02x, program_data[%d]=0x%02x, program_data[%d]=0x%02x\n\n",
  2075. // 4*i + 0, program_data[4*i + 0], 4*i + 1, program_data[4*i + 1], 4*i + 2, program_data[4*i + 2], 4*i + 3, program_data[4*i + 3]);
  2076. }
  2077. fclose(pic_program_file);
  2078. // after read upgrade file correct, erase pic
  2079. ret = dsPIC33EP16GS202_reset_pic(which_iic);
  2080. if(ret == 0)
  2081. {
  2082. //printf("!!! %s: reset pic error!\n\n", __FUNCTION__);
  2083. return 0;
  2084. }
  2085. ret = dsPIC33EP16GS202_erase_pic_app_program(which_iic);
  2086. if(ret == 0)
  2087. {
  2088. //printf("!!! %s: erase flash error!\n\n", __FUNCTION__);
  2089. return 0;
  2090. }
  2091. for(i=0; i<loop; i++)
  2092. {
  2093. memcpy(buf, program_data+i*16, 16);
  2094. /**/
  2095. //printf("send pic program time: %d\n",i);
  2096. for(j=0; j<16; j++)
  2097. {
  2098. //printf("buf[%d] = 0x%02x\n", j, *(buf+j));
  2099. }
  2100. //printf("\n");
  2101. ret = dsPIC33EP16GS202_send_data_to_pic(which_iic, buf);
  2102. if(ret == 0)
  2103. {
  2104. //printf("!!! %s: send flash data error!\n\n", __FUNCTION__);
  2105. return 0;
  2106. }
  2107. //usleep(200*1000);
  2108. }
  2109. ret = dsPIC33EP16GS202_reset_pic(which_iic);
  2110. if(ret == 0)
  2111. {
  2112. //printf("!!! %s: reset pic error!\n\n", __FUNCTION__);
  2113. return 0;
  2114. }
  2115. return 1;
  2116. }
  2117. int dsPIC33EP16GS202_pic_heart_beat(unsigned char which_iic)
  2118. {
  2119. unsigned char length = 0x04, crc_data[2] = {0xff}, read_back_data[6] = {0xff};
  2120. unsigned short crc = 0;
  2121. char logstr[1024];
  2122. int retry_count=0;
  2123. crc = length + SEND_HEART_BEAT;
  2124. crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
  2125. crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);
  2126. //printf("--- %s: crc_data[0] = 0x%x, crc_data[1] = 0x%x\n", __FUNCTION__, crc_data[0], crc_data[1]);
  2127. while(retry_count++<3)
  2128. {
  2129. // pthread_mutex_lock(&iic_mutex);
  2130. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_1);
  2131. T9_plus_write_pic_iic(false, false, 0x0, which_iic, PIC_COMMAND_2);
  2132. T9_plus_write_pic_iic(false, false, 0x0, which_iic, length);
  2133. T9_plus_write_pic_iic(false, false, 0x0, which_iic, SEND_HEART_BEAT);
  2134. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[0]);
  2135. T9_plus_write_pic_iic(false, false, 0x0, which_iic, crc_data[1]);
  2136. usleep(500*1000);
  2137. read_back_data[0] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2138. read_back_data[1] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2139. read_back_data[2] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2140. read_back_data[3] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2141. read_back_data[4] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2142. read_back_data[5] = T9_plus_write_pic_iic(true, false, 0x0, which_iic, 0);
  2143. // pthread_mutex_unlock(&iic_mutex);
  2144. if((read_back_data[1] != SEND_HEART_BEAT) || (read_back_data[2] != 1))
  2145. {
  2146. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  2147. sprintf(logstr,"%s read_back: %02x %02x %02x %02x %02x %02x\n",
  2148. __FUNCTION__, read_back_data[0], read_back_data[1], read_back_data[2], read_back_data[3], read_back_data[4], read_back_data[5]);
  2149. writeInitLogFile(logstr);
  2150. sprintf(logstr,"%s failed on Chain[%d]!\n", __FUNCTION__,which_iic);
  2151. writeInitLogFile(logstr);
  2152. #endif
  2153. sleep(1);
  2154. // return 0; // error
  2155. }
  2156. else
  2157. {
  2158. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  2159. // sprintf(logstr,"%s ok, HeartBeatReturnWord = %d\n\n", __FUNCTION__, read_back_data[3]);
  2160. // writeInitLogFile(logstr);
  2161. #endif
  2162. return 1; // ok
  2163. }
  2164. }
  2165. return 0;
  2166. }
  2167. void pic_heart_beat_each_chain(unsigned char chain)
  2168. {
  2169. if(fpga_version>=0xE)
  2170. {
  2171. if(chain<1 || chain>3) // only enable DC when enable the first chain of 3 chains, like 0, 3, 6 ...
  2172. return;
  2173. dsPIC33EP16GS202_pic_heart_beat(chain);
  2174. }
  2175. else
  2176. {
  2177. if(chain%3!=0) // only enable DC when enable the first chain of 3 chains, like 0, 3, 6 ...
  2178. return;
  2179. dsPIC33EP16GS202_pic_heart_beat(chain/3);
  2180. }
  2181. }
  2182. #else
  2183. void enable_pic_dac(unsigned char chain)
  2184. {
  2185. send_pic_command(chain);
  2186. write_pic_iic(false, false, 0x0, chain, ENABLE_VOLTAGE);
  2187. write_pic_iic(false, false, 0x0, chain, 1);
  2188. }
  2189. void disable_pic_dac(unsigned char chain)
  2190. {
  2191. send_pic_command(chain);
  2192. write_pic_iic(false, false, 0x0, chain, ENABLE_VOLTAGE);
  2193. write_pic_iic(false, false, 0x0, chain, 0);
  2194. }
  2195. void pic_heart_beat_each_chain(unsigned char chain)
  2196. {
  2197. send_pic_command(chain);
  2198. write_pic_iic(false, false, 0x0, chain, SEND_HEART_BEAT);
  2199. }
  2200. #endif
  2201. //FPGA related
  2202. int get_nonce2_and_job_id_store_address(void)
  2203. {
  2204. // char logstr[256];
  2205. int ret = -1;
  2206. ret = *((unsigned int *)(axi_fpga_addr + NONCE2_AND_JOBID_STORE_ADDRESS));
  2207. // sprintf(logstr,"get NONCE2_AND_JOBID_STORE_ADDRESS is 0x%x\n", ret);
  2208. // writeInitLogFile(logstr);
  2209. return ret;
  2210. }
  2211. void set_nonce2_and_job_id_store_address(unsigned int value)
  2212. {
  2213. get_nonce2_and_job_id_store_address();
  2214. *((unsigned int *)(axi_fpga_addr + NONCE2_AND_JOBID_STORE_ADDRESS)) = value;
  2215. applog(LOG_DEBUG,"%s: set NONCE2_AND_JOBID_STORE_ADDRESS is 0x%x\n", __FUNCTION__, value);
  2216. get_nonce2_and_job_id_store_address();
  2217. }
  2218. int get_job_start_address(void)
  2219. {
  2220. int ret = -1;
  2221. ret = *((unsigned int *)(axi_fpga_addr + JOB_START_ADDRESS));
  2222. applog(LOG_DEBUG,"%s: JOB_START_ADDRESS is 0x%x\n", __FUNCTION__, ret);
  2223. return ret;
  2224. }
  2225. void set_job_start_address(unsigned int value)
  2226. {
  2227. *((unsigned int *)(axi_fpga_addr + JOB_START_ADDRESS)) = value;
  2228. applog(LOG_DEBUG,"%s: set JOB_START_ADDRESS is 0x%x\n", __FUNCTION__, value);
  2229. get_job_start_address();
  2230. }
  2231. void set_bmc_counter(unsigned int value)
  2232. {
  2233. *((unsigned int *)(axi_fpga_addr + BMC_CMD_COUNTER)) = value;
  2234. }
  2235. unsigned int read_bmc_counter()
  2236. {
  2237. unsigned int ret;
  2238. ret=*((unsigned int *)(axi_fpga_addr + BMC_CMD_COUNTER));
  2239. return ret;
  2240. }
  2241. int get_QN_write_data_command(void)
  2242. {
  2243. int ret = -1;
  2244. ret = *((axi_fpga_addr + QN_WRITE_DATA_COMMAND));
  2245. applog(LOG_DEBUG,"%s: QN_WRITE_DATA_COMMAND is 0x%x\n", __FUNCTION__, ret);
  2246. return ret;
  2247. }
  2248. void set_QN_write_data_command(unsigned int value)
  2249. {
  2250. *(axi_fpga_addr + QN_WRITE_DATA_COMMAND) = value;
  2251. applog(LOG_DEBUG,"%s: set QN_WRITE_DATA_COMMAND is 0x%x\n", __FUNCTION__, value);
  2252. get_QN_write_data_command();
  2253. }
  2254. void set_reset_hashboard(int chainIndex, int resetBit)
  2255. {
  2256. unsigned int ret;
  2257. unsigned int resetFlag;
  2258. char logstr[1024];
  2259. ret = *((axi_fpga_addr + RESET_HASHBOARD_COMMAND));
  2260. resetFlag=(1<<chainIndex);
  2261. if(resetBit>0)
  2262. ret = ret | resetFlag;
  2263. else
  2264. ret = ret & (~resetFlag);
  2265. sprintf(logstr,"set_reset_hashboard = 0x%08x\n",ret);
  2266. writeInitLogFile(logstr);
  2267. *(axi_fpga_addr + RESET_HASHBOARD_COMMAND) = ret;
  2268. }
  2269. void set_reset_allhashboard(int resetBit)
  2270. {
  2271. unsigned int ret;
  2272. char logstr[1024];
  2273. ret = *((axi_fpga_addr + RESET_HASHBOARD_COMMAND));
  2274. if(resetBit>0)
  2275. ret = ret | 0x0000ffff;
  2276. else
  2277. ret = ret & 0xffff0000;
  2278. sprintf(logstr,"set_reset_allhashboard = 0x%08x\n",ret);
  2279. writeInitLogFile(logstr);
  2280. *(axi_fpga_addr + RESET_HASHBOARD_COMMAND) = ret;
  2281. }
  2282. int bitmain_axi_init()
  2283. {
  2284. unsigned int data;
  2285. int ret=0;
  2286. fd = open("/dev/axi_fpga_dev", O_RDWR);
  2287. if(fd < 0)
  2288. {
  2289. applog(LOG_DEBUG,"/dev/axi_fpga_dev open failed. fd = %d\n", fd);
  2290. perror("open");
  2291. return -1;
  2292. }
  2293. axi_fpga_addr = mmap(NULL, TOTAL_LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  2294. if(!axi_fpga_addr)
  2295. {
  2296. applog(LOG_DEBUG,"mmap axi_fpga_addr failed. axi_fpga_addr = 0x%x\n", axi_fpga_addr);
  2297. return -1;
  2298. }
  2299. applog(LOG_DEBUG,"mmap axi_fpga_addr = 0x%x\n", axi_fpga_addr);
  2300. //check the value in address 0xff200000
  2301. data = *axi_fpga_addr;
  2302. if((data & 0x0000FFFF) != HARDWARE_VERSION_VALUE)
  2303. {
  2304. applog(LOG_DEBUG,"data = 0x%x, and it's not equal to HARDWARE_VERSION_VALUE : 0x%x\n", data, HARDWARE_VERSION_VALUE);
  2305. //return -1;
  2306. }
  2307. applog(LOG_DEBUG,"axi_fpga_addr data = 0x%x\n", data);
  2308. fd_fpga_mem = open("/dev/fpga_mem", O_RDWR);
  2309. if(fd_fpga_mem < 0)
  2310. {
  2311. applog(LOG_DEBUG,"/dev/fpga_mem open failed. fd_fpga_mem = %d\n", fd_fpga_mem);
  2312. perror("open");
  2313. return -1;
  2314. }
  2315. fpga_mem_addr = mmap(NULL, FPGA_MEM_TOTAL_LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd_fpga_mem, 0);
  2316. if(!fpga_mem_addr)
  2317. {
  2318. applog(LOG_DEBUG,"mmap fpga_mem_addr failed. fpga_mem_addr = 0x%x\n", fpga_mem_addr);
  2319. return -1;
  2320. }
  2321. applog(LOG_DEBUG,"mmap fpga_mem_addr = 0x%x\n", fpga_mem_addr);
  2322. nonce2_jobid_address = fpga_mem_addr;
  2323. job_start_address_1 = fpga_mem_addr + NONCE2_AND_JOBID_STORE_SPACE/sizeof(int);
  2324. job_start_address_2 = fpga_mem_addr + (NONCE2_AND_JOBID_STORE_SPACE + JOB_STORE_SPACE)/sizeof(int);
  2325. applog(LOG_DEBUG,"job_start_address_1 = 0x%x\n", job_start_address_1);
  2326. applog(LOG_DEBUG,"job_start_address_2 = 0x%x\n", job_start_address_2);
  2327. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  2328. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  2329. dev = calloc(sizeof(struct all_parameters), sizeof(char));
  2330. if(!dev)
  2331. {
  2332. applog(LOG_DEBUG,"kmalloc for dev failed.\n");
  2333. return -1;
  2334. }
  2335. else
  2336. {
  2337. dev->current_job_start_address = job_start_address_1;
  2338. applog(LOG_DEBUG,"kmalloc for dev success.\n");
  2339. }
  2340. return ret;
  2341. }
  2342. int bitmain_axi_Reinit()
  2343. {
  2344. int ret=0;
  2345. unsigned int data;
  2346. char logstr[1024];
  2347. data = *axi_fpga_addr;
  2348. if((data & 0x0000FFFF) != HARDWARE_VERSION_VALUE)
  2349. {
  2350. sprintf(logstr,"data = 0x%x, and it's not equal to HARDWARE_VERSION_VALUE : 0x%x\n", data, HARDWARE_VERSION_VALUE);
  2351. writeInitLogFile(logstr);
  2352. }
  2353. sprintf(logstr,"axi_fpga_addr data = 0x%x\n", data);
  2354. writeInitLogFile(logstr);
  2355. nonce2_jobid_address = fpga_mem_addr;
  2356. job_start_address_1 = fpga_mem_addr + NONCE2_AND_JOBID_STORE_SPACE/sizeof(int);
  2357. job_start_address_2 = fpga_mem_addr + (NONCE2_AND_JOBID_STORE_SPACE + JOB_STORE_SPACE)/sizeof(int);
  2358. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  2359. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  2360. return ret;
  2361. }
  2362. int bitmain_axi_close()
  2363. {
  2364. int ret = 0;
  2365. ret = munmap((void *)axi_fpga_addr, TOTAL_LEN);
  2366. if(ret<0)
  2367. {
  2368. applog(LOG_DEBUG,"munmap failed!\n");
  2369. }
  2370. ret = munmap((void *)fpga_mem_addr, FPGA_MEM_TOTAL_LEN);
  2371. if(ret<0)
  2372. {
  2373. applog(LOG_DEBUG,"munmap failed!\n");
  2374. }
  2375. //free_pages((unsigned long)nonce2_jobid_address, NONCE2_AND_JOBID_STORE_SPACE_ORDER);
  2376. //free(temp_job_start_address_1);
  2377. //free(temp_job_start_address_2);
  2378. close(fd);
  2379. close(fd_fpga_mem);
  2380. }
  2381. int get_fan_control(void)
  2382. {
  2383. int ret = -1;
  2384. ret = *((unsigned int *)(axi_fpga_addr + FAN_CONTROL));
  2385. applog(LOG_DEBUG,"%s: FAN_CONTROL is 0x%x\n", __FUNCTION__, ret);
  2386. return ret;
  2387. }
  2388. void set_fan_control(unsigned int value)
  2389. {
  2390. *((unsigned int *)(axi_fpga_addr + FAN_CONTROL)) = value;
  2391. applog(LOG_DEBUG,"%s: set FAN_CONTROL is 0x%x\n", __FUNCTION__, value);
  2392. get_fan_control();
  2393. }
  2394. int get_hash_on_plug(void)
  2395. {
  2396. int ret = -1;
  2397. ret = *(axi_fpga_addr + HASH_ON_PLUG);
  2398. applog(LOG_DEBUG,"%s: HASH_ON_PLUG is 0x%x\n", __FUNCTION__, ret);
  2399. return ret;
  2400. }
  2401. unsigned int get_crc_count()
  2402. {
  2403. unsigned int ret;
  2404. ret= *((unsigned int *)(axi_fpga_addr + CRC_ERROR_CNT_ADDR));
  2405. return (ret&0xffff);
  2406. }
  2407. int get_hardware_version(void)
  2408. {
  2409. int ret = -1;
  2410. ret = *((int *)(axi_fpga_addr + HARDWARE_VERSION));
  2411. applog(LOG_DEBUG,"%s: HARDWARE_VERSION is 0x%x\n", __FUNCTION__, ret);
  2412. return ret;
  2413. }
  2414. void set_Hardware_version(unsigned int value)
  2415. {
  2416. *((unsigned int *)(axi_fpga_addr + HARDWARE_VERSION)) = value;
  2417. }
  2418. int get_fan_speed(unsigned char *fan_id, unsigned int *fan_speed)
  2419. {
  2420. int ret = -1;
  2421. ret = *((unsigned int *)(axi_fpga_addr + FAN_SPEED));
  2422. *fan_speed = 0x000000ff & ret;
  2423. *fan_id = (unsigned char)(0x00000007 & (ret >> 8));
  2424. if(*fan_speed > 0)
  2425. {
  2426. applog(LOG_DEBUG,"%s: fan_id is 0x%x, fan_speed is 0x%x\n", __FUNCTION__, *fan_id, *fan_speed);
  2427. }
  2428. return ret;
  2429. }
  2430. int get_temperature_0_3(void)
  2431. {
  2432. int ret = -1;
  2433. ret = *((int *)(axi_fpga_addr + TEMPERATURE_0_3));
  2434. //applog(LOG_DEBUG,"%s: TEMPERATURE_0_3 is 0x%x\n", __FUNCTION__, ret);
  2435. return ret;
  2436. }
  2437. int get_temperature_4_7(void)
  2438. {
  2439. int ret = -1;
  2440. ret = *((int *)(axi_fpga_addr + TEMPERATURE_4_7));
  2441. //applog(LOG_DEBUG,"%s: TEMPERATURE_4_7 is 0x%x\n", __FUNCTION__, ret);
  2442. return ret;
  2443. }
  2444. int get_temperature_8_11(void)
  2445. {
  2446. int ret = -1;
  2447. ret = *((int *)(axi_fpga_addr + TEMPERATURE_8_11));
  2448. //applog(LOG_DEBUG,"%s: TEMPERATURE_8_11 is 0x%x\n", __FUNCTION__, ret);
  2449. return ret;
  2450. }
  2451. int get_temperature_12_15(void)
  2452. {
  2453. int ret = -1;
  2454. ret = *((int *)(axi_fpga_addr + TEMPERATURE_12_15));
  2455. //applog(LOG_DEBUG,"%s: TEMPERATURE_12_15 is 0x%x\n", __FUNCTION__, ret);
  2456. return ret;
  2457. }
  2458. int get_time_out_control(void)
  2459. {
  2460. int ret = -1;
  2461. ret = *((unsigned int *)(axi_fpga_addr + TIME_OUT_CONTROL));
  2462. applog(LOG_DEBUG,"%s: TIME_OUT_CONTROL is 0x%x\n", __FUNCTION__, ret);
  2463. return ret;
  2464. }
  2465. void set_time_out_control(unsigned int value)
  2466. {
  2467. *((unsigned int *)(axi_fpga_addr + TIME_OUT_CONTROL)) = value;
  2468. applog(LOG_DEBUG,"%s: set FAN_CONTROL is 0x%x\n", __FUNCTION__, value);
  2469. get_time_out_control();
  2470. }
  2471. int get_BC_command_buffer(unsigned int *buf)
  2472. {
  2473. int ret = -1;
  2474. ret = *((unsigned int *)(axi_fpga_addr + BC_COMMAND_BUFFER));
  2475. *(buf + 0) = ret; //this is for FIL
  2476. ret = *((unsigned int *)(axi_fpga_addr + BC_COMMAND_BUFFER + 1));
  2477. *(buf + 1) = ret;
  2478. ret = *((unsigned int *)(axi_fpga_addr + BC_COMMAND_BUFFER + 2));
  2479. *(buf + 2) = ret;
  2480. applog(LOG_DEBUG,"%s: BC_COMMAND_BUFFER buf[0]: 0x%x, buf[1]: 0x%x, buf[2]: 0x%x\n", __FUNCTION__, *(buf + 0), *(buf + 1), *(buf + 2));
  2481. return ret;
  2482. }
  2483. void set_BC_command_buffer(unsigned int *value)
  2484. {
  2485. unsigned int buf[4] = {0};
  2486. *((unsigned int *)(axi_fpga_addr + BC_COMMAND_BUFFER)) = *(value + 0); //this is for FIL
  2487. *((unsigned int *)(axi_fpga_addr + BC_COMMAND_BUFFER + 1)) = *(value + 1);
  2488. *((unsigned int *)(axi_fpga_addr + BC_COMMAND_BUFFER + 2)) = *(value + 2);
  2489. applog(LOG_DEBUG,"%s: set BC_COMMAND_BUFFER value[0]: 0x%x, value[1]: 0x%x, value[2]: 0x%x\n", __FUNCTION__, *(value + 0), *(value + 1), *(value + 2));
  2490. get_BC_command_buffer(buf);
  2491. }
  2492. int get_nonce_number_in_fifo(void)
  2493. {
  2494. int ret = -1;
  2495. ret = *((unsigned int *)(axi_fpga_addr + NONCE_NUMBER_IN_FIFO));
  2496. //applog(LOG_DEBUG,"%s: NONCE_NUMBER_IN_FIFO is 0x%x\n", __FUNCTION__, ret);
  2497. return ret;
  2498. }
  2499. int get_return_nonce(unsigned int *buf)
  2500. {
  2501. int ret = -1;
  2502. ret = *((unsigned int *)(axi_fpga_addr + RETURN_NONCE));
  2503. *(buf + 0) = ret;
  2504. ret = *((unsigned int *)(axi_fpga_addr + RETURN_NONCE + 1));
  2505. *(buf + 1) = ret; //there is nonce3
  2506. //applog(LOG_DEBUG,"%s: RETURN_NONCE buf[0] is 0x%x, buf[1] is 0x%x\n", __FUNCTION__, *(buf + 0), *(buf + 1));
  2507. return ret;
  2508. }
  2509. int get_BC_write_command(void)
  2510. {
  2511. int ret = -1;
  2512. ret = *((unsigned int *)(axi_fpga_addr + BC_WRITE_COMMAND));
  2513. applog(LOG_DEBUG,"%s: BC_WRITE_COMMAND is 0x%x\n", __FUNCTION__, ret);
  2514. return ret;
  2515. }
  2516. void set_BC_write_command(unsigned int value)
  2517. {
  2518. char logstr[1024];
  2519. int wait_count=0;
  2520. *((unsigned int *)(axi_fpga_addr + BC_WRITE_COMMAND)) = value;
  2521. //applog(LOG_DEBUG,"%s: set BC_WRITE_COMMAND is 0x%x\n", __FUNCTION__, value);
  2522. if(value & BC_COMMAND_BUFFER_READY)
  2523. {
  2524. while(get_BC_write_command() & BC_COMMAND_BUFFER_READY)
  2525. {
  2526. cgsleep_ms(1);
  2527. wait_count++;
  2528. if(wait_count>3000)
  2529. {
  2530. sprintf(logstr,"Error: set_BC_write_command wait buffer ready timeout!\n");
  2531. writeInitLogFile(logstr);
  2532. break;
  2533. }
  2534. //applog(LOG_DEBUG,"%s ---\n", __FUNCTION__);
  2535. }
  2536. }
  2537. else
  2538. {
  2539. get_BC_write_command();
  2540. }
  2541. }
  2542. int get_ticket_mask(void)
  2543. {
  2544. int ret = -1;
  2545. ret = *((unsigned int *)(axi_fpga_addr + TICKET_MASK_FPGA));
  2546. applog(LOG_DEBUG,"%s: TICKET_MASK_FPGA is 0x%x\n", __FUNCTION__, ret);
  2547. return ret;
  2548. }
  2549. void set_ticket_mask(unsigned int value)
  2550. {
  2551. *((unsigned int *)(axi_fpga_addr + TICKET_MASK_FPGA)) = value;
  2552. applog(LOG_DEBUG,"%s: set TICKET_MASK_FPGA is 0x%x\n", __FUNCTION__, value);
  2553. get_ticket_mask();
  2554. }
  2555. int get_job_id(void)
  2556. {
  2557. int ret = -1;
  2558. ret = *((unsigned int *)(axi_fpga_addr + JOB_ID));
  2559. applog(LOG_DEBUG,"%s: JOB_ID is 0x%x\n", __FUNCTION__, ret);
  2560. return ret;
  2561. }
  2562. void set_job_id(unsigned int value)
  2563. {
  2564. *((unsigned int *)(axi_fpga_addr + JOB_ID)) = value;
  2565. applog(LOG_DEBUG,"%s: set JOB_ID is 0x%x\n", __FUNCTION__, value);
  2566. get_job_id();
  2567. }
  2568. int get_job_length(void)
  2569. {
  2570. int ret = -1;
  2571. ret = *((unsigned int *)(axi_fpga_addr + JOB_LENGTH));
  2572. applog(LOG_DEBUG,"%s: JOB_LENGTH is 0x%x\n", __FUNCTION__, ret);
  2573. return ret;
  2574. }
  2575. void set_job_length(unsigned int value)
  2576. {
  2577. *((unsigned int *)(axi_fpga_addr + JOB_LENGTH)) = value;
  2578. applog(LOG_DEBUG,"%s: set JOB_LENGTH is 0x%x\n", __FUNCTION__, value);
  2579. get_job_id();
  2580. }
  2581. int get_block_header_version(void)
  2582. {
  2583. int ret = -1;
  2584. ret = *((unsigned int *)(axi_fpga_addr + BLOCK_HEADER_VERSION));
  2585. applog(LOG_DEBUG,"%s: BLOCK_HEADER_VERSION is 0x%x\n", __FUNCTION__, ret);
  2586. return ret;
  2587. }
  2588. void set_block_header_version(unsigned int value)
  2589. {
  2590. *((unsigned int *)(axi_fpga_addr + BLOCK_HEADER_VERSION)) = value;
  2591. applog(LOG_DEBUG,"%s: set BLOCK_HEADER_VERSION is 0x%x\n", __FUNCTION__, value);
  2592. get_block_header_version();
  2593. }
  2594. int get_time_stamp()
  2595. {
  2596. int ret = -1;
  2597. ret = *((unsigned int *)(axi_fpga_addr + TIME_STAMP));
  2598. applog(LOG_DEBUG,"%s: TIME_STAMP is 0x%x\n", __FUNCTION__, ret);
  2599. return ret;
  2600. }
  2601. void set_time_stamp(unsigned int value)
  2602. {
  2603. *((unsigned int *)(axi_fpga_addr + TIME_STAMP)) = value;
  2604. applog(LOG_DEBUG,"%s: set TIME_STAMP is 0x%x\n", __FUNCTION__, value);
  2605. get_time_stamp();
  2606. }
  2607. int get_target_bits(void)
  2608. {
  2609. int ret = -1;
  2610. ret = *((unsigned int *)(axi_fpga_addr + TARGET_BITS));
  2611. applog(LOG_DEBUG,"%s: TARGET_BITS is 0x%x\n", __FUNCTION__, ret);
  2612. return ret;
  2613. }
  2614. void set_target_bits(unsigned int value)
  2615. {
  2616. *((unsigned int *)(axi_fpga_addr + TARGET_BITS)) = value;
  2617. applog(LOG_DEBUG,"%s: set TARGET_BITS is 0x%x\n", __FUNCTION__, value);
  2618. get_target_bits();
  2619. }
  2620. int get_pre_header_hash(unsigned int *buf)
  2621. {
  2622. int ret = -1;
  2623. *(buf + 0) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH));
  2624. *(buf + 1) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 1));
  2625. *(buf + 2) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 2));
  2626. *(buf + 3) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 3));
  2627. *(buf + 4) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 4));
  2628. *(buf + 5) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 5));
  2629. *(buf + 6) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 6));
  2630. *(buf + 7) = *((unsigned int *)(axi_fpga_addr + PRE_HEADER_HASH + 7));
  2631. applog(LOG_DEBUG,"%s: PRE_HEADER_HASH buf[0]: 0x%x, buf[1]: 0x%x, buf[2]: 0x%x, buf[3]: 0x%x, buf[4]: 0x%x, buf[5]: 0x%x, buf[6]: 0x%x, buf[7]: 0x%x\n", __FUNCTION__, *(buf + 0), *(buf + 1), *(buf + 2), *(buf + 3), *(buf + 4), *(buf + 5), *(buf + 6), *(buf + 7));
  2632. ret = *(buf + 7);
  2633. return ret;
  2634. }
  2635. void set_pre_header_hash(unsigned int *value)
  2636. {
  2637. unsigned int buf[8] = {0};
  2638. *(axi_fpga_addr + PRE_HEADER_HASH) = *(value + 0);
  2639. *(axi_fpga_addr + PRE_HEADER_HASH + 1) = *(value + 1);
  2640. *(axi_fpga_addr + PRE_HEADER_HASH + 2) = *(value + 2);
  2641. *(axi_fpga_addr + PRE_HEADER_HASH + 3) = *(value + 3);
  2642. *(axi_fpga_addr + PRE_HEADER_HASH + 4) = *(value + 4);
  2643. *(axi_fpga_addr + PRE_HEADER_HASH + 5) = *(value + 5);
  2644. *(axi_fpga_addr + PRE_HEADER_HASH + 6) = *(value + 6);
  2645. *(axi_fpga_addr + PRE_HEADER_HASH + 7) = *(value + 7);
  2646. applog(LOG_DEBUG,"%s: set PRE_HEADER_HASH value[0]: 0x%x, value[1]: 0x%x, value[2]: 0x%x, value[3]: 0x%x, value[4]: 0x%x, value[5]: 0x%x, value[6]: 0x%x, value[7]: 0x%x\n", __FUNCTION__, *(value + 0), *(value + 1), *(value + 2), *(value + 3), *(value + 4), *(value + 5), *(value + 6), *(value + 7));
  2647. //get_pre_header_hash(buf);
  2648. }
  2649. int get_coinbase_length_and_nonce2_length(void)
  2650. {
  2651. int ret = -1;
  2652. ret = *((unsigned int *)(axi_fpga_addr + COINBASE_AND_NONCE2_LENGTH));
  2653. applog(LOG_DEBUG,"%s: COINBASE_AND_NONCE2_LENGTH is 0x%x\n", __FUNCTION__, ret);
  2654. return ret;
  2655. }
  2656. void set_coinbase_length_and_nonce2_length(unsigned int value)
  2657. {
  2658. *((unsigned int *)(axi_fpga_addr + COINBASE_AND_NONCE2_LENGTH)) = value;
  2659. applog(LOG_DEBUG,"%s: set COINBASE_AND_NONCE2_LENGTH is 0x%x\n", __FUNCTION__, value);
  2660. get_coinbase_length_and_nonce2_length();
  2661. }
  2662. int get_work_nonce2(unsigned int *buf)
  2663. {
  2664. int ret = -1;
  2665. *(buf + 0) = *((unsigned int *)(axi_fpga_addr + WORK_NONCE_2));
  2666. *(buf + 1) = *((unsigned int *)(axi_fpga_addr + WORK_NONCE_2 + 1));
  2667. applog(LOG_DEBUG,"%s: WORK_NONCE_2 buf[0]: 0x%x, buf[1]: 0x%x\n", __FUNCTION__, *(buf + 0), *(buf + 1));
  2668. return ret;
  2669. }
  2670. void set_work_nonce2(unsigned int *value)
  2671. {
  2672. unsigned int buf[2] = {0};
  2673. *((unsigned int *)(axi_fpga_addr + WORK_NONCE_2)) = *(value + 0);
  2674. *((unsigned int *)(axi_fpga_addr + WORK_NONCE_2 + 1)) = *(value + 1);
  2675. applog(LOG_DEBUG,"%s: set WORK_NONCE_2 value[0]: 0x%x, value[1]: 0x%x\n", __FUNCTION__, *(value + 0), *(value + 1));
  2676. get_work_nonce2(buf);
  2677. }
  2678. int get_merkle_bin_number(void)
  2679. {
  2680. int ret = -1;
  2681. ret = *((unsigned int *)(axi_fpga_addr + MERKLE_BIN_NUMBER));
  2682. ret = ret & 0x0000ffff;
  2683. applog(LOG_DEBUG,"%s: MERKLE_BIN_NUMBER is 0x%x\n", __FUNCTION__, ret);
  2684. return ret;
  2685. }
  2686. void set_merkle_bin_number(unsigned int value)
  2687. {
  2688. *((unsigned int *)(axi_fpga_addr + MERKLE_BIN_NUMBER)) = value & 0x0000ffff;
  2689. applog(LOG_DEBUG,"%s: set MERKLE_BIN_NUMBER is 0x%x\n", __FUNCTION__, value & 0x0000ffff);
  2690. get_merkle_bin_number();
  2691. }
  2692. int get_nonce_fifo_interrupt(void)
  2693. {
  2694. int ret = -1;
  2695. ret = *((unsigned int *)(axi_fpga_addr + NONCE_FIFO_INTERRUPT));
  2696. applog(LOG_DEBUG,"%s: NONCE_FIFO_INTERRUPT is 0x%x\n", __FUNCTION__, ret);
  2697. return ret;
  2698. }
  2699. void set_nonce_fifo_interrupt(unsigned int value)
  2700. {
  2701. *((unsigned int *)(axi_fpga_addr + NONCE_FIFO_INTERRUPT)) = value;
  2702. applog(LOG_DEBUG,"%s: set NONCE_FIFO_INTERRUPT is 0x%x\n", __FUNCTION__, value);
  2703. get_nonce_fifo_interrupt();
  2704. }
  2705. int get_dhash_acc_control(void)
  2706. {
  2707. int ret = -1;
  2708. ret = *((unsigned int *)(axi_fpga_addr + DHASH_ACC_CONTROL));
  2709. applog(LOG_DEBUG,"%s: DHASH_ACC_CONTROL is 0x%x\n", __FUNCTION__, ret);
  2710. return ret;
  2711. }
  2712. void set_dhash_acc_control(unsigned int value)
  2713. {
  2714. int a = 10;
  2715. *((unsigned int *)(axi_fpga_addr + DHASH_ACC_CONTROL)) = value;
  2716. applog(LOG_DEBUG,"%s: set DHASH_ACC_CONTROL is 0x%x\n", __FUNCTION__, value);
  2717. while (a>0)
  2718. {
  2719. if ((value | NEW_BLOCK) == (get_dhash_acc_control() |NEW_BLOCK))
  2720. break;
  2721. *((unsigned int *)(axi_fpga_addr + DHASH_ACC_CONTROL)) = value;
  2722. a--;
  2723. cgsleep_ms(2);
  2724. }
  2725. if (a == 0)
  2726. applog(LOG_DEBUG,"%s set DHASH_ACC_CONTROL failed!",__FUNCTION__);
  2727. }
  2728. void set_TW_write_command(unsigned int *value)
  2729. {
  2730. unsigned int i;
  2731. for(i=0; i<TW_WRITE_COMMAND_LEN/sizeof(unsigned int); i++)
  2732. {
  2733. *((unsigned int *)(axi_fpga_addr + TW_WRITE_COMMAND + i)) = *(value + i); //this is for FIL
  2734. //applog(LOG_DEBUG,"%s: set TW_WRITE_COMMAND value[%d]: 0x%x\n", __FUNCTION__, i, *(value + i));
  2735. }
  2736. //applog(LOG_DEBUG,"%s: set TW_WRITE_COMMAND value[0]: 0x%x, value[1]: 0x%x, value[2]: 0x%x, value[3]: 0x%x\n", __FUNCTION__, *(value + 0), *(value + 1), *(value + 2), *(value + 3));
  2737. }
  2738. void set_TW_write_command_vil(unsigned int *value)
  2739. {
  2740. unsigned int i;
  2741. pthread_mutex_lock(&fpga_mutex);
  2742. for(i=0; i<TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int); i++)
  2743. {
  2744. if(i==0)
  2745. *((unsigned int *)(axi_fpga_addr + TW_WRITE_COMMAND + i)) = *(value + i);
  2746. else *((unsigned int *)(axi_fpga_addr + TW_WRITE_COMMAND + 1)) = *(value + i);
  2747. }
  2748. pthread_mutex_unlock(&fpga_mutex);
  2749. }
  2750. int get_buffer_space(void)
  2751. {
  2752. int ret = -1;
  2753. ret = *((unsigned int *)(axi_fpga_addr + BUFFER_SPACE));
  2754. //applog(LOG_DEBUG,"%s: work fifo ready is 0x%x\n", __FUNCTION__, ret);
  2755. return ret;
  2756. }
  2757. int get_hash_counting_number(void)
  2758. {
  2759. int ret = -1;
  2760. ret = *((unsigned int *)(axi_fpga_addr + HASH_COUNTING_NUMBER_FPGA));
  2761. applog(LOG_DEBUG,"%s: DHASH_ACC_CONTROL is 0x%x\n", __FUNCTION__, ret);
  2762. return ret;
  2763. }
  2764. void set_hash_counting_number(unsigned int value)
  2765. {
  2766. *((unsigned int *)(axi_fpga_addr + HASH_COUNTING_NUMBER_FPGA)) = value;
  2767. applog(LOG_DEBUG,"%s: set DHASH_ACC_CONTROL is 0x%x\n", __FUNCTION__, value);
  2768. get_hash_counting_number();
  2769. }
  2770. //application related
  2771. void check_chain()
  2772. {
  2773. int ret = 0, i;
  2774. dev->chain_num = 0;
  2775. ret = get_hash_on_plug();
  2776. if(ret < 0)
  2777. {
  2778. applog(LOG_DEBUG,"%s: get_hash_on_plug functions error\n");
  2779. }
  2780. else
  2781. {
  2782. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  2783. {
  2784. if((ret >> i) & 0x1)
  2785. {
  2786. dev->chain_exist[i] = 1;
  2787. dev->chain_num++;
  2788. }
  2789. else
  2790. {
  2791. dev->chain_exist[i] = 0;
  2792. }
  2793. }
  2794. }
  2795. }
  2796. void check_fan()
  2797. {
  2798. int i=0, j=0;
  2799. unsigned char fan_id = 0;
  2800. unsigned int fan_speed;
  2801. for(j=0; j < 2; j++) //means check for twice to make sure find out all fan
  2802. {
  2803. for(i=0; i < BITMAIN_MAX_FAN_NUM; i++)
  2804. {
  2805. if(get_fan_speed(&fan_id, &fan_speed) != -1)
  2806. {
  2807. dev->fan_speed_value[fan_id] = fan_speed * 60 * 2;
  2808. if((fan_speed > 0) && (dev->fan_exist[fan_id] == 0))
  2809. {
  2810. dev->fan_exist[fan_id] = 1;
  2811. dev->fan_num++;
  2812. dev->fan_exist_map |= (0x1 << fan_id);
  2813. }
  2814. else if((fan_speed == 0) && (dev->fan_exist[fan_id] == 1))
  2815. {
  2816. dev->fan_exist[fan_id] = 0;
  2817. dev->fan_num--;
  2818. dev->fan_exist_map &= !(0x1 << fan_id);
  2819. }
  2820. if(dev->fan_speed_top1 < dev->fan_speed_value[fan_id])
  2821. dev->fan_speed_top1 = dev->fan_speed_value[fan_id];
  2822. }
  2823. }
  2824. }
  2825. }
  2826. void set_PWM(unsigned char pwm_percent)
  2827. {
  2828. uint16_t pwm_high_value = 0, pwm_low_value = 0;
  2829. int temp_pwm_percent = 0;
  2830. temp_pwm_percent = pwm_percent;
  2831. if(temp_pwm_percent < MIN_PWM_PERCENT)
  2832. {
  2833. temp_pwm_percent = MIN_PWM_PERCENT;
  2834. }
  2835. if(temp_pwm_percent > MAX_PWM_PERCENT)
  2836. {
  2837. temp_pwm_percent = MAX_PWM_PERCENT;
  2838. }
  2839. pwm_high_value = temp_pwm_percent * PWM_SCALE / 100;
  2840. pwm_low_value = (100 - temp_pwm_percent) * PWM_SCALE / 100;
  2841. dev->pwm_value = (pwm_high_value << 16) | pwm_low_value;
  2842. dev->pwm_percent = temp_pwm_percent;
  2843. set_fan_control(dev->pwm_value);
  2844. }
  2845. bool isTempTooLow()
  2846. {
  2847. int i;
  2848. char logstr[1024];
  2849. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  2850. {
  2851. if(dev->chain_exist[i] == 1 && chain_temp_toolow[i]==0 && dev->chain_asic_temp[i][1][PWM_T]>0)
  2852. {
  2853. if(lowest_testOK_temp[i]<=0)
  2854. {
  2855. // for old version, there is no lowest_testOK_temp value in PIC
  2856. if(dev->chain_asic_temp[i][1][PWM_T]<MIN_TEMP_CONTINUE_DOWN_FAN)
  2857. {
  2858. sprintf(logstr,"Detect temp too low: Chain[%d] curtemp=%d\n",i, dev->chain_asic_temp[i][1][PWM_T]);
  2859. writeLogFile(logstr);
  2860. return true;
  2861. }
  2862. }
  2863. else if(dev->chain_asic_temp[i][1][PWM_T]<lowest_testOK_temp[i])
  2864. {
  2865. // for new version, there is lowest_testOK_temp value in PIC
  2866. sprintf(logstr,"Detect temp too low: Chain[%d] lowest_testOK_temp=%d curtemp=%d\n",i,lowest_testOK_temp[i],dev->chain_asic_temp[i][1][PWM_T]);
  2867. writeLogFile(logstr);
  2868. return true;
  2869. }
  2870. }
  2871. }
  2872. return false;
  2873. }
  2874. void CheckChainTempTooLowFlag()
  2875. {
  2876. int i;
  2877. bool isSomeBoardNotTooLow=false;
  2878. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  2879. {
  2880. if(dev->chain_exist[i] == 1)
  2881. {
  2882. if(chain_temp_toolow[i]==0)
  2883. isSomeBoardNotTooLow=true;
  2884. }
  2885. }
  2886. if(!isSomeBoardNotTooLow)
  2887. {
  2888. // all board temp are too low, we will set flag to 0 again!
  2889. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  2890. {
  2891. if(dev->chain_exist[i] == 1)
  2892. {
  2893. chain_temp_toolow[i]=0;
  2894. }
  2895. }
  2896. }
  2897. }
  2898. void setChainTempTooLowFlag()
  2899. {
  2900. int i;
  2901. char logstr[1024];
  2902. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  2903. {
  2904. chain_temp_toolow[i]=0;
  2905. if(dev->chain_exist[i] == 1 && dev->chain_asic_maxtemp[i][PWM_T]>0)
  2906. {
  2907. if(lowest_testOK_temp[i]<=0)
  2908. {
  2909. // for old version, there is no lowest_testOK_temp value in PIC
  2910. if(dev->chain_asic_maxtemp[i][PWM_T]<MIN_TEMP_CONTINUE_DOWN_FAN)
  2911. {
  2912. sprintf(logstr,"Detect Chain[%d] temp too low, will ignore: temp=%d\n",i, dev->chain_asic_maxtemp[i][PWM_T]);
  2913. writeLogFile(logstr);
  2914. chain_temp_toolow[i]=1; //set 1, will ignore this board's temp
  2915. }
  2916. }
  2917. else if(dev->chain_asic_maxtemp[i][PWM_T]<lowest_testOK_temp[i] )
  2918. {
  2919. // for new version, there is lowest_testOK_temp value in PIC
  2920. sprintf(logstr,"Detect Chain[%d] temp too low, will ignore: temp=%d < %d\n",i, dev->chain_asic_maxtemp[i][PWM_T],lowest_testOK_temp[i]);
  2921. writeLogFile(logstr);
  2922. chain_temp_toolow[i]=1; //set 1, will ignore this board's temp
  2923. }
  2924. }
  2925. }
  2926. CheckChainTempTooLowFlag();
  2927. }
  2928. #ifdef R4
  2929. void set_PWM_according_to_temperature()
  2930. {
  2931. char logstr[1024];
  2932. int pwm_percent = 0, temp_change = 0;
  2933. temp_highest = dev->temp_top1[PWM_T];
  2934. #ifdef DEBUG_218_FAN_FULLSPEED
  2935. if(is218_Temp)
  2936. {
  2937. set_PWM(MAX_PWM_PERCENT);
  2938. dev->fan_pwm = MAX_PWM_PERCENT;
  2939. return;
  2940. }
  2941. #endif
  2942. if(temp_highest >= MAX_FAN_TEMP)
  2943. {
  2944. applog(LOG_DEBUG,"%s: Temperature is higher than %d 'C\n", __FUNCTION__, temp_highest);
  2945. }
  2946. temp_change = temp_highest - last_temperature;
  2947. sprintf(logstr,"set FAN speed according to: temp_highest=%d temp_top1[PWM_T]=%d temp_change=%d\n",temp_highest,dev->temp_top1[PWM_T],temp_change);
  2948. writeLogFile(logstr);
  2949. if(temp_highest >= MAX_FAN_TEMP || temp_highest == 0)
  2950. {
  2951. set_PWM(MAX_PWM_PERCENT);
  2952. dev->fan_pwm = MAX_PWM_PERCENT;
  2953. applog(LOG_DEBUG,"%s: Set PWM percent : MAX_PWM_PERCENT\n", __FUNCTION__);
  2954. sprintf(logstr,"temp_highest >= MAX_FAN_TEMP || temp_highest == 0 PWM=%d\n",dev->fan_pwm);
  2955. writeLogFile(logstr);
  2956. return;
  2957. }
  2958. if(temp_highest <= MIN_FAN_TEMP)
  2959. {
  2960. set_PWM(MIN_PWM_PERCENT);
  2961. dev->fan_pwm = MIN_PWM_PERCENT;
  2962. applog(LOG_DEBUG,"%s: Set PWM percent : MIN_PWM_PERCENT\n", __FUNCTION__);
  2963. sprintf(logstr,"temp_highest <= MIN_FAN_TEMP PWM=%d\n",dev->fan_pwm);
  2964. writeLogFile(logstr);
  2965. return;
  2966. }
  2967. if(temp_change >= TEMP_INTERVAL || temp_change <= -TEMP_INTERVAL)
  2968. {
  2969. if(temp_highest > MID_FAN_TEMP)
  2970. pwm_percent = MID_PWM_PERCENT + (temp_highest -MID_FAN_TEMP) * MID_PWM_ADJUST_FACTOR;
  2971. else
  2972. pwm_percent = MIN_PWM_PERCENT + (temp_highest -MIN_FAN_TEMP) * PWM_ADJUST_FACTOR;
  2973. if(dev->temp_top1[PWM_T] > MAX_FAN_TEMP)
  2974. pwm_percent = MAX_PWM_PERCENT;
  2975. if(pwm_percent < 0)
  2976. {
  2977. pwm_percent = 0;
  2978. }
  2979. if(pwm_percent > MAX_PWM_PERCENT)
  2980. {
  2981. pwm_percent = MAX_PWM_PERCENT;
  2982. }
  2983. dev->fan_pwm = pwm_percent;
  2984. applog(LOG_DEBUG,"%s: Set PWM percent : %d\n", __FUNCTION__, pwm_percent);
  2985. set_PWM(pwm_percent);
  2986. last_temperature = temp_highest;
  2987. sprintf(logstr,"temp_change >= TEMP_INTERVAL || temp_change <= -TEMP_INTERVAL PWM=%d[%d]\n",pwm_percent,dev->fan_pwm);
  2988. writeLogFile(logstr);
  2989. }
  2990. else
  2991. {
  2992. sprintf(logstr,"keep PWM=%d\n",dev->fan_pwm);
  2993. writeLogFile(logstr);
  2994. }
  2995. }
  2996. #else
  2997. void set_PWM_according_to_temperature()
  2998. {
  2999. static int fix_fan_steps=0;
  3000. int pwm_percent = dev->fan_pwm, temp_change = 0;
  3001. char logstr[1024];
  3002. #ifdef TWO_CHIP_TEMP_S9
  3003. temp_highest = dev->temp_low1[PWM_T];
  3004. #else
  3005. if(is218_Temp)
  3006. temp_highest=dev->temp_top1[TEMP_POS_LOCAL];
  3007. else
  3008. temp_highest = dev->temp_top1[PWM_T];
  3009. #endif
  3010. temp_change = temp_highest - last_temperature;
  3011. #ifdef DEBUG_218_FAN_FULLSPEED
  3012. if(is218_Temp)
  3013. {
  3014. set_PWM(MAX_PWM_PERCENT);
  3015. dev->fan_pwm = MAX_PWM_PERCENT;
  3016. return;
  3017. }
  3018. #endif
  3019. sprintf(logstr,"set FAN speed according to: temp_highest=%d temp_top1[PWM_T]=%d temp_top1[TEMP_POS_LOCAL]=%d temp_change=%d fix_fan_steps=%d\n",temp_highest,dev->temp_top1[PWM_T],dev->temp_top1[TEMP_POS_LOCAL],temp_change,fix_fan_steps);
  3020. writeLogFile(logstr);
  3021. #ifndef TWO_CHIP_TEMP_S9
  3022. if(is218_Temp)
  3023. {
  3024. if(temp_highest >= MAX_FAN_PCB_TEMP || temp_highest == 0)//some board temp is very high than others!!!
  3025. {
  3026. set_PWM(MAX_PWM_PERCENT);
  3027. dev->fan_pwm = MAX_PWM_PERCENT;
  3028. sprintf(logstr,"set full FAN speed...\n");
  3029. writeLogFile(logstr);
  3030. return;
  3031. }
  3032. if(temp_change >= TEMP_INTERVAL || temp_change <= -TEMP_INTERVAL)
  3033. {
  3034. sprintf(logstr,"set normal FAN speed...\n");
  3035. writeLogFile(logstr);
  3036. pwm_percent = MIN_PWM_PERCENT + (temp_highest-MIN_FAN_PCB_TEMP) * PWM_ADJUST_FACTOR;
  3037. if(pwm_percent < 0)
  3038. pwm_percent = 0;
  3039. if(pwm_percent > MAX_PWM_PERCENT)
  3040. pwm_percent=MAX_PWM_PERCENT;
  3041. dev->fan_pwm = pwm_percent;
  3042. last_temperature = temp_highest;
  3043. applog(LOG_DEBUG,"%s: Set PWM percent : %d\n", __FUNCTION__, pwm_percent);
  3044. set_PWM(pwm_percent);
  3045. }
  3046. }
  3047. else
  3048. #endif
  3049. {
  3050. if(temp_highest >= MAX_FAN_TEMP || temp_highest == 0 || dev->temp_top1[PWM_T]>=MAX_FAN_TEMP || dev->temp_top1[TEMP_POS_LOCAL]>=MAX_FAN_PCB_TEMP)//some board temp is very high than others!!!
  3051. {
  3052. set_PWM(MAX_PWM_PERCENT);
  3053. dev->fan_pwm = MAX_PWM_PERCENT;
  3054. if(fix_fan_steps<0) // if full speed, means too hot, we need set fix_fan_steps=0 , to make sure fan speed up
  3055. fix_fan_steps=0;
  3056. sprintf(logstr,"set full FAN speed...\n");
  3057. writeLogFile(logstr);
  3058. return;
  3059. }
  3060. if(temp_change >= TEMP_INTERVAL || temp_change <= -TEMP_INTERVAL)
  3061. {
  3062. sprintf(logstr,"set normal FAN speed...with fix_fan_steps=%d\n",fix_fan_steps);
  3063. writeLogFile(logstr);
  3064. pwm_percent = MIN_PWM_PERCENT + (temp_highest-MIN_FAN_TEMP+fix_fan_steps) * PWM_ADJUST_FACTOR;
  3065. if(pwm_percent < 0)
  3066. pwm_percent = 0;
  3067. if(pwm_percent > MAX_PWM_PERCENT)
  3068. pwm_percent=MAX_PWM_PERCENT;
  3069. #ifdef TWO_CHIP_TEMP_S9
  3070. if(dev->temp_top1[PWM_T] > 110 && pwm_percent<MAX_PWM_PERCENT)
  3071. #else
  3072. if(temp_highest>=MAX_TEMP_NEED_UP_FANSTEP && pwm_percent<MAX_PWM_PERCENT)
  3073. #endif
  3074. {
  3075. // if fan speed is not max, and temp is too high, we need add fix fan steps
  3076. fix_fan_steps++;
  3077. pwm_percent = MIN_PWM_PERCENT + (temp_highest-MIN_FAN_TEMP+fix_fan_steps) * PWM_ADJUST_FACTOR;
  3078. if(pwm_percent < 0)
  3079. pwm_percent = 0;
  3080. if(pwm_percent > MAX_PWM_PERCENT)
  3081. pwm_percent=MAX_PWM_PERCENT;
  3082. }
  3083. dev->fan_pwm = pwm_percent;
  3084. last_temperature = temp_highest;
  3085. applog(LOG_DEBUG,"%s: Set PWM percent : %d\n", __FUNCTION__, pwm_percent);
  3086. set_PWM(pwm_percent);
  3087. }
  3088. else
  3089. {
  3090. #ifdef TWO_CHIP_TEMP_S9
  3091. if(isTempTooLow() && fix_fan_steps > MIN_FAN_TEMP-MIN_TEMP_CONTINUE_DOWN_FAN && pwm_percent > MIN_PWM_PERCENT)
  3092. #else
  3093. if(temp_highest<=MIN_TEMP_CONTINUE_DOWN_FAN && fix_fan_steps > MIN_FAN_TEMP-MIN_TEMP_CONTINUE_DOWN_FAN && pwm_percent > MIN_PWM_PERCENT)
  3094. #endif
  3095. {
  3096. // if temp is too low, and fan speed > 0 , then we need decrease fix fan steps
  3097. fix_fan_steps--;
  3098. sprintf(logstr,"set normal FAN speed... with fix_fan_steps=%d\n",fix_fan_steps);
  3099. writeLogFile(logstr);
  3100. pwm_percent = MIN_PWM_PERCENT + (temp_highest-MIN_FAN_TEMP+fix_fan_steps) * PWM_ADJUST_FACTOR;
  3101. if(pwm_percent < 0)
  3102. pwm_percent = 0;
  3103. if(pwm_percent > MAX_PWM_PERCENT)
  3104. pwm_percent=MAX_PWM_PERCENT;
  3105. dev->fan_pwm = pwm_percent;
  3106. set_PWM(pwm_percent);
  3107. }
  3108. #ifdef TWO_CHIP_TEMP_S9
  3109. else if(dev->temp_top1[PWM_T] > 110 && pwm_percent<MAX_PWM_PERCENT)
  3110. #else
  3111. else if(temp_highest>=MAX_TEMP_NEED_UP_FANSTEP && pwm_percent<MAX_PWM_PERCENT)
  3112. #endif
  3113. {
  3114. fix_fan_steps++;
  3115. sprintf(logstr,"set normal FAN speed... with fix_fan_steps=%d\n",fix_fan_steps);
  3116. writeLogFile(logstr);
  3117. pwm_percent = MIN_PWM_PERCENT + (temp_highest-MIN_FAN_TEMP+fix_fan_steps) * PWM_ADJUST_FACTOR;
  3118. if(pwm_percent < 0)
  3119. pwm_percent = 0;
  3120. if(pwm_percent > MAX_PWM_PERCENT)
  3121. pwm_percent=MAX_PWM_PERCENT;
  3122. dev->fan_pwm = pwm_percent;
  3123. set_PWM(pwm_percent);
  3124. }
  3125. }
  3126. }
  3127. }
  3128. #endif
  3129. static void get_plldata(int type,int freq,uint32_t * reg_data,uint16_t * reg_data2, uint32_t *vil_data)
  3130. {
  3131. uint32_t i;
  3132. char freq_str[10];
  3133. sprintf(freq_str,"%d", freq);
  3134. char plldivider1[32] = {0};
  3135. char plldivider2[32] = {0};
  3136. char vildivider[32] = {0};
  3137. if(type == 1385)
  3138. {
  3139. for(i=0; i < sizeof(freq_pll_1385)/sizeof(freq_pll_1385[0]); i++)
  3140. {
  3141. if( memcmp(freq_pll_1385[i].freq, freq_str, sizeof(freq_pll_1385[i].freq)) == 0)
  3142. break;
  3143. }
  3144. }
  3145. if(i == sizeof(freq_pll_1385)/sizeof(freq_pll_1385[0]))
  3146. {
  3147. i = 4;
  3148. }
  3149. sprintf(plldivider1, "%08x", freq_pll_1385[i].fildiv1);
  3150. sprintf(plldivider2, "%04x", freq_pll_1385[i].fildiv2);
  3151. sprintf(vildivider, "%04x", freq_pll_1385[i].vilpll);
  3152. *reg_data = freq_pll_1385[i].fildiv1;
  3153. *reg_data2 = freq_pll_1385[i].fildiv2;
  3154. *vil_data = freq_pll_1385[i].vilpll;
  3155. }
  3156. void set_frequency_with_addr_plldatai(int pllindex,unsigned char mode,unsigned char addr, unsigned char chain)
  3157. {
  3158. unsigned char buf[9] = {0,0,0,0,0,0,0,0,0};
  3159. unsigned int cmd_buf[3] = {0,0,0};
  3160. int i;
  3161. unsigned int ret, value;
  3162. uint32_t reg_data_pll = 0;
  3163. uint16_t reg_data_pll2 = 0;
  3164. uint32_t reg_data_vil = 0;
  3165. i = chain;
  3166. reg_data_vil = freq_pll_1385[pllindex].vilpll;;
  3167. //applog(LOG_DEBUG,"%s: i = %d\n", __FUNCTION__, i);
  3168. if(!opt_multi_version) // fil mode
  3169. {
  3170. memset(buf,0,sizeof(buf));
  3171. memset(cmd_buf,0,sizeof(cmd_buf));
  3172. buf[0] = 0;
  3173. buf[0] |= SET_PLL_DIVIDER1;
  3174. buf[1] = (reg_data_pll >> 16) & 0xff;
  3175. buf[2] = (reg_data_pll >> 8) & 0xff;
  3176. buf[3] = (reg_data_pll >> 0) & 0xff;
  3177. buf[3] |= CRC5(buf, 4*8 - 5);
  3178. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  3179. set_BC_command_buffer(cmd_buf);
  3180. ret = get_BC_write_command();
  3181. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  3182. set_BC_write_command(value);
  3183. cgsleep_us(3000);
  3184. memset(buf,0,sizeof(buf));
  3185. memset(cmd_buf,0,sizeof(cmd_buf));
  3186. buf[0] = SET_PLL_DIVIDER2;
  3187. buf[0] |= COMMAND_FOR_ALL;
  3188. buf[1] = 0; //addr
  3189. buf[2] = reg_data_pll2 >> 8;
  3190. buf[3] = reg_data_pll2& 0x0ff;
  3191. buf[3] |= CRC5(buf, 4*8 - 5);
  3192. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  3193. set_BC_command_buffer(cmd_buf);
  3194. ret = get_BC_write_command();
  3195. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  3196. set_BC_write_command(value);
  3197. cgsleep_us(5000);
  3198. }
  3199. else // vil
  3200. {
  3201. memset(buf,0,9);
  3202. memset(cmd_buf,0,3*sizeof(int));
  3203. if(mode)
  3204. {
  3205. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  3206. }
  3207. else
  3208. {
  3209. buf[0] = VIL_COMMAND_TYPE | SET_CONFIG;
  3210. }
  3211. buf[1] = 0x09;
  3212. buf[2] = addr;
  3213. buf[3] = PLL_PARAMETER;
  3214. buf[4] = (reg_data_vil >> 24) & 0xff;
  3215. buf[5] = (reg_data_vil >> 16) & 0xff;
  3216. buf[6] = (reg_data_vil >> 8) & 0xff;
  3217. buf[7] = (reg_data_vil >> 0) & 0xff;
  3218. buf[8] = CRC5(buf, 8*8);
  3219. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  3220. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];;
  3221. cmd_buf[2] = buf[8]<<24;
  3222. while (1)
  3223. {
  3224. ret = get_BC_write_command();
  3225. if ((ret & 0x80000000) == 0)
  3226. break;
  3227. cgsleep_us(500);
  3228. }
  3229. set_BC_command_buffer(cmd_buf);
  3230. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  3231. set_BC_write_command(value);
  3232. cgsleep_us(10000);
  3233. }
  3234. }
  3235. int get_pll_index(int freq)
  3236. {
  3237. int i;
  3238. char freq_str[10];
  3239. sprintf(freq_str,"%d", freq);
  3240. for(i=0; i < sizeof(freq_pll_1385)/sizeof(freq_pll_1385[0]); i++)
  3241. {
  3242. if( memcmp(freq_pll_1385[i].freq, freq_str, sizeof(freq_pll_1385[i].freq)) == 0)
  3243. break;
  3244. }
  3245. if(i == sizeof(freq_pll_1385)/sizeof(freq_pll_1385[0]))
  3246. {
  3247. i = -1;
  3248. }
  3249. return i;
  3250. }
  3251. int get_freqvalue_by_index(int index)
  3252. {
  3253. return atoi(freq_pll_1385[index].freq);
  3254. }
  3255. int GetTotalRate()
  3256. {
  3257. int i,j;
  3258. int totalrate=0;
  3259. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  3260. {
  3261. if(dev->chain_exist[i] == 1)
  3262. {
  3263. for(j = 0; j < CHAIN_ASIC_NUM; j ++)
  3264. {
  3265. #ifdef T9_18
  3266. if(fpga_version>=0xE)
  3267. {
  3268. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3269. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3270. totalrate+=atoi(freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  3271. }
  3272. else
  3273. {
  3274. totalrate+=atoi(freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  3275. }
  3276. #else
  3277. totalrate+=atoi(freq_pll_1385[last_freq[i][j*2+3]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  3278. #endif
  3279. }
  3280. }
  3281. }
  3282. return (totalrate/1000);
  3283. }
  3284. int GetBoardRate(int chainIndex)
  3285. {
  3286. int j;
  3287. int totalrate=0;
  3288. if(dev->chain_exist[chainIndex] == 1)
  3289. {
  3290. for(j = 0; j < CHAIN_ASIC_NUM; j ++)
  3291. {
  3292. #ifdef T9_18
  3293. if(fpga_version>=0xE)
  3294. {
  3295. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3296. getPICChainIndexOffset(chainIndex,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3297. totalrate+=atoi(freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[chainIndex][j]);
  3298. }
  3299. else
  3300. {
  3301. totalrate+=atoi(freq_pll_1385[chain_pic_buf[((chainIndex/3)*3)][7+(chainIndex%3)*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[chainIndex][j]);
  3302. }
  3303. #else
  3304. totalrate+=atoi(freq_pll_1385[last_freq[chainIndex][j*2+3]].freq)*(BM1387_CORE_NUM-chain_badcore_num[chainIndex][j]);
  3305. #endif
  3306. }
  3307. }
  3308. return (totalrate/1000);
  3309. }
  3310. #ifdef R4
  3311. bool isChainEnough()
  3312. {
  3313. int i,j;
  3314. int chainnum=0;
  3315. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  3316. {
  3317. if(dev->chain_exist[i] == 1)
  3318. {
  3319. chainnum++;
  3320. }
  3321. }
  3322. if(chainnum>=2)
  3323. return true;
  3324. return false;
  3325. }
  3326. static int ConvirtTotalRate(int totalRate)
  3327. {
  3328. int lowPart;
  3329. if(totalRate>=8000 && totalRate<8700)
  3330. {
  3331. return 8000;
  3332. }
  3333. else if(totalRate>=8700 && totalRate<9500)
  3334. {
  3335. return 8700;
  3336. }
  3337. else
  3338. {
  3339. lowPart=totalRate%1000; // get the low part rate, GH/s
  3340. if(lowPart>500)
  3341. lowPart=500;
  3342. else lowPart=0; //if lower than 500G, just set zero
  3343. return (((totalRate/1000)*1000)+lowPart);
  3344. }
  3345. }
  3346. #else
  3347. bool isChainEnough()
  3348. {
  3349. int i,j;
  3350. int chainnum=0;
  3351. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  3352. {
  3353. if(dev->chain_exist[i] == 1)
  3354. {
  3355. chainnum++;
  3356. }
  3357. }
  3358. #ifdef T9_18
  3359. if(chainnum>=9)
  3360. return true;
  3361. #else
  3362. if(chainnum>=3)
  3363. return true;
  3364. #endif
  3365. return false;
  3366. }
  3367. static int ConvirtTotalRate(int totalRate)
  3368. {
  3369. int lowPart=totalRate%1000; // get the low part rate, GH/s
  3370. if(lowPart>500)
  3371. lowPart=500;
  3372. else lowPart=0; //if lower than 500G, just set zero
  3373. return (((totalRate/1000)*1000)+lowPart);
  3374. }
  3375. #endif
  3376. #if 0
  3377. static void DownOneChipFreqOneStep()
  3378. {
  3379. int i,j;
  3380. uint8_t voltage_array[BITMAIN_MAX_CHAIN_NUM] = {0};
  3381. uint8_t tmp_vol;
  3382. int board_rate=0;
  3383. int max_freq=0,max_freq_chipIndex=0,max_rate_chainIndex=0;
  3384. memcpy(voltage_array,chain_voltage_pic,sizeof(chain_voltage_pic));
  3385. // desc order for voltage value
  3386. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  3387. {
  3388. for(j=i+1; j<BITMAIN_MAX_CHAIN_NUM; j++)
  3389. {
  3390. if(voltage_array[i]>voltage_array[j])
  3391. {
  3392. tmp_vol=voltage_array[i];
  3393. voltage_array[i]=voltage_array[j];
  3394. voltage_array[j]=tmp_vol;
  3395. }
  3396. }
  3397. }
  3398. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  3399. {
  3400. if(voltage_array[i]>0)
  3401. {
  3402. board_rate=0;
  3403. max_rate_chainIndex=-1;
  3404. //find the highest rate board with this voltage
  3405. for(j=0; j<BITMAIN_MAX_CHAIN_NUM; j++)
  3406. {
  3407. if(dev->chain_exist[j] == 1 && chain_voltage_pic[j]==voltage_array[i])
  3408. {
  3409. if(board_rate==0 || board_rate<GetBoardRate(j))
  3410. {
  3411. board_rate=GetBoardRate(j);
  3412. max_rate_chainIndex=j;
  3413. }
  3414. }
  3415. }
  3416. if(max_rate_chainIndex<0)
  3417. continue;
  3418. max_freq=0;
  3419. max_freq_chipIndex=-1;
  3420. for(j = 0; j < dev->chain_asic_num[max_rate_chainIndex]; j ++)
  3421. {
  3422. if(max_freq==0 || max_freq<last_freq[max_rate_chainIndex][j*2+3])
  3423. {
  3424. max_freq_chipIndex=j;
  3425. max_freq=last_freq[max_rate_chainIndex][j*2+3];
  3426. }
  3427. }
  3428. if(max_freq_chipIndex<0)
  3429. continue;
  3430. //down one step on highest chip, but freq must be > 250M
  3431. if(max_freq<=MIN_FREQ)
  3432. continue;
  3433. else
  3434. {
  3435. //down one step
  3436. last_freq[max_rate_chainIndex][max_freq_chipIndex*2+3]-=1; // down one step
  3437. return;
  3438. }
  3439. }
  3440. }
  3441. }
  3442. #else
  3443. static bool DownOneChipFreqOneStep()
  3444. {
  3445. int j;
  3446. uint8_t tmp_vol;
  3447. int board_rate=0;
  3448. int max_freq=0,max_freq_chipIndex=0,max_rate_chainIndex=0;
  3449. char logstr[1024];
  3450. board_rate=0;
  3451. max_rate_chainIndex=-1;
  3452. //find the highest rate board with this voltage
  3453. for(j=0; j<BITMAIN_MAX_CHAIN_NUM; j++)
  3454. {
  3455. if(dev->chain_exist[j] == 1)
  3456. {
  3457. if(dev->chain_asic_num[j]!=CHAIN_ASIC_NUM)
  3458. return false;
  3459. if(board_rate==0 || board_rate<GetBoardRate(j))
  3460. {
  3461. board_rate=GetBoardRate(j);
  3462. max_rate_chainIndex=j;
  3463. }
  3464. }
  3465. }
  3466. if(max_rate_chainIndex<0)
  3467. {
  3468. sprintf(logstr,"Fatal Error: DownOneChipFreqOneStep has Wrong chain index=%d\n",max_rate_chainIndex);
  3469. writeInitLogFile(logstr);
  3470. return false;
  3471. }
  3472. max_freq=0;
  3473. max_freq_chipIndex=-1;
  3474. for(j = 0; j < dev->chain_asic_num[max_rate_chainIndex]; j ++)
  3475. {
  3476. #ifdef T9_18
  3477. if(fpga_version>=0xE)
  3478. {
  3479. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3480. getPICChainIndexOffset(max_rate_chainIndex,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3481. if(max_freq==0 || max_freq<chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j])
  3482. {
  3483. max_freq_chipIndex=j;
  3484. max_freq=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  3485. }
  3486. }
  3487. else
  3488. {
  3489. if(max_freq==0 || max_freq<chain_pic_buf[((max_rate_chainIndex/3)*3)][7+(max_rate_chainIndex%3)*31+4+j])
  3490. {
  3491. max_freq_chipIndex=j;
  3492. max_freq=chain_pic_buf[((max_rate_chainIndex/3)*3)][7+(max_rate_chainIndex%3)*31+4+j];
  3493. }
  3494. }
  3495. #else
  3496. if(max_freq==0 || max_freq<last_freq[max_rate_chainIndex][j*2+3])
  3497. {
  3498. max_freq_chipIndex=j;
  3499. max_freq=last_freq[max_rate_chainIndex][j*2+3];
  3500. }
  3501. #endif
  3502. }
  3503. if(max_freq_chipIndex<0)
  3504. {
  3505. sprintf(logstr,"Fatal Error: DownOneChipFreqOneStep Chain[%d] has Wrong chip index=%d\n",max_freq_chipIndex);
  3506. writeInitLogFile(logstr);
  3507. return false;
  3508. }
  3509. //down one step on highest chip, but freq must be > 250M
  3510. if(max_freq<=MIN_FREQ)
  3511. {
  3512. sprintf(logstr,"Fatal Error: DownOneChipFreqOneStep Chain[%d] has no chip can down freq!!!\n",max_rate_chainIndex);
  3513. writeInitLogFile(logstr);
  3514. return false;
  3515. }
  3516. else
  3517. {
  3518. //down one step
  3519. #ifdef T9_18
  3520. if(fpga_version>=0xE)
  3521. {
  3522. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3523. getPICChainIndexOffset(max_rate_chainIndex,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3524. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+max_freq_chipIndex]-=1; // down one step
  3525. }
  3526. else
  3527. {
  3528. chain_pic_buf[((max_rate_chainIndex/3)*3)][7+(max_rate_chainIndex%3)*31+4+max_freq_chipIndex]-=1; // down one step
  3529. }
  3530. #else
  3531. last_freq[max_rate_chainIndex][max_freq_chipIndex*2+3]-=1; // down one step
  3532. #endif
  3533. return true;
  3534. }
  3535. }
  3536. #endif
  3537. static int last_record_freq[BITMAIN_MAX_CHAIN_NUM][256];
  3538. static void ProcessFixFreq()
  3539. {
  3540. int i,j;
  3541. int totalRate=GetTotalRate();
  3542. int fixed_totalRate=ConvirtTotalRate(totalRate);
  3543. if(GetTotalRate()>fixed_totalRate)
  3544. {
  3545. //need down one step
  3546. do
  3547. {
  3548. //record the current freq
  3549. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  3550. {
  3551. if(dev->chain_exist[i] == 1)
  3552. {
  3553. for(j = 0; j < CHAIN_ASIC_NUM; j ++)
  3554. {
  3555. #ifdef T9_18
  3556. if(fpga_version>=0xE)
  3557. {
  3558. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3559. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3560. last_record_freq[i][j]=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  3561. }
  3562. else
  3563. {
  3564. last_record_freq[i][j]=chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  3565. }
  3566. #else
  3567. last_record_freq[i][j]=last_freq[i][j*2+3];
  3568. #endif
  3569. }
  3570. }
  3571. }
  3572. //do: down one step
  3573. if(!DownOneChipFreqOneStep())
  3574. break; // if fatal error, we just break!!!
  3575. }
  3576. while(GetTotalRate()>fixed_totalRate);
  3577. // resume the last freq records
  3578. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  3579. {
  3580. if(dev->chain_exist[i] == 1)
  3581. {
  3582. for(j = 0; j < CHAIN_ASIC_NUM; j ++)
  3583. {
  3584. #ifdef T9_18
  3585. if(fpga_version>=0xE)
  3586. {
  3587. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3588. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3589. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]=last_record_freq[i][j];
  3590. }
  3591. else
  3592. {
  3593. chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]=last_record_freq[i][j];
  3594. }
  3595. #else
  3596. last_freq[i][j*2+3]=last_record_freq[i][j];
  3597. #endif
  3598. }
  3599. }
  3600. }
  3601. }
  3602. }
  3603. static void ProcessFixFreqForChips()
  3604. {
  3605. int i,j;
  3606. int totalRate=GetTotalRate();
  3607. int fixed_totalRate=ConvirtTotalRate(totalRate);
  3608. fixed_totalRate=fixed_totalRate*(100+UPRATE_PERCENT)/100;
  3609. if(GetTotalRate()>fixed_totalRate)
  3610. {
  3611. //need down one step
  3612. do
  3613. {
  3614. //record the current freq
  3615. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  3616. {
  3617. if(dev->chain_exist[i] == 1)
  3618. {
  3619. for(j = 0; j < CHAIN_ASIC_NUM; j ++)
  3620. {
  3621. #ifdef T9_18
  3622. if(fpga_version>=0xE)
  3623. {
  3624. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3625. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3626. last_record_freq[i][j]=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  3627. }
  3628. else
  3629. {
  3630. last_record_freq[i][j]=chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  3631. }
  3632. #else
  3633. last_record_freq[i][j]=last_freq[i][j*2+3];
  3634. #endif
  3635. }
  3636. }
  3637. }
  3638. //do: down one step
  3639. if(!DownOneChipFreqOneStep())
  3640. break; // if fatal error, we just break!!!
  3641. }
  3642. while(GetTotalRate()>fixed_totalRate);
  3643. // resume the last freq records
  3644. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  3645. {
  3646. if(dev->chain_exist[i] == 1)
  3647. {
  3648. for(j = 0; j < CHAIN_ASIC_NUM; j ++)
  3649. {
  3650. #ifdef T9_18
  3651. if(fpga_version>=0xE)
  3652. {
  3653. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3654. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3655. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]=last_record_freq[i][j];
  3656. }
  3657. else
  3658. {
  3659. chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]=last_record_freq[i][j];
  3660. }
  3661. #else
  3662. last_freq[i][j*2+3]=last_record_freq[i][j];
  3663. #endif
  3664. }
  3665. }
  3666. }
  3667. }
  3668. }
  3669. void writeInitLogFile(char *logstr)
  3670. {
  3671. FILE *fd;
  3672. pthread_mutex_lock(&init_log_mutex);
  3673. fd=fopen("/tmp/freq","a+");
  3674. if(fd)
  3675. {
  3676. fwrite(logstr,1,strlen(logstr),fd);
  3677. fclose(fd);
  3678. }
  3679. pthread_mutex_unlock(&init_log_mutex);
  3680. }
  3681. void clearInitLogFile()
  3682. {
  3683. FILE *fd;
  3684. pthread_mutex_lock(&init_log_mutex);
  3685. fd=fopen("/tmp/freq","w");
  3686. if(fd)
  3687. fclose(fd);
  3688. if(isFixedFreqMode())
  3689. {
  3690. fd=fopen("/tmp/search","w");
  3691. if(fd)
  3692. fclose(fd);
  3693. fd=fopen("/tmp/freq","w");
  3694. if(fd)
  3695. fclose(fd);
  3696. fd=fopen("/tmp/lasttemp","w");
  3697. if(fd)
  3698. fclose(fd);
  3699. }
  3700. pthread_mutex_unlock(&init_log_mutex);
  3701. }
  3702. void set_frequency(unsigned short int frequency)
  3703. {
  3704. unsigned char buf[9] = {0,0,0,0,0,0,0,0,0};
  3705. unsigned int cmd_buf[3] = {0,0,0};
  3706. int i,j,k,max_freq_index = 0,step_down = 0;
  3707. unsigned int ret, value;
  3708. uint32_t reg_data_pll = 0;
  3709. uint16_t reg_data_pll2 = 0;
  3710. uint32_t reg_data_vil = 0;
  3711. int chain_max_freq,chain_min_freq;
  3712. unsigned char vol_pic;
  3713. int vol_value;
  3714. int default_freq_index=get_pll_index(frequency);
  3715. char logstr[1024];
  3716. applog(LOG_DEBUG,"\n--- %s\n", __FUNCTION__);
  3717. get_plldata(1385, frequency, &reg_data_pll, &reg_data_pll2, &reg_data_vil);
  3718. applog(LOG_DEBUG,"%s: frequency = %d\n", __FUNCTION__, frequency);
  3719. //////////////// set default freq when no freq in PIC //////////////////////////////
  3720. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  3721. {
  3722. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i]>0)
  3723. {
  3724. #ifdef T9_18
  3725. if (isFixedFreqMode() || getChainPICMagicNumber(i) != FREQ_MAGIC)
  3726. #else
  3727. if (isFixedFreqMode() || last_freq[i][1] != FREQ_MAGIC)
  3728. #endif
  3729. {
  3730. isUseDefaultFreq=true;
  3731. #ifdef T9_18
  3732. if(fpga_version>=0xE)
  3733. {
  3734. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3735. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3736. chain_pic_buf[new_T9_PLUS_chainIndex][0]=FREQ_MAGIC;
  3737. for(k=0; k<3; k++) // if chain[1] has no magic number, then we need set chain[1] [8] [9] 's freq to default freq!!!
  3738. {
  3739. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3740. {
  3741. chain_pic_buf[new_T9_PLUS_chainIndex][7+k*31+4+j]=default_freq_index; // default is config file's freq
  3742. }
  3743. }
  3744. }
  3745. else
  3746. {
  3747. chain_pic_buf[((i/3)*3)][0]=FREQ_MAGIC;
  3748. for(k=i; k<i+3; k++) // if chain[0] has no magic number, then we need set chain[0] [1] [2] 's freq to default freq!!!
  3749. {
  3750. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3751. {
  3752. chain_pic_buf[((k/3)*3)][7+(k%3)*31+4+j]=default_freq_index; // default is config file's freq
  3753. }
  3754. }
  3755. }
  3756. #else
  3757. last_freq[i][0]=0;
  3758. last_freq[i][1]=FREQ_MAGIC;
  3759. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3760. {
  3761. last_freq[i][j*2+2]=0;
  3762. last_freq[i][j*2+3]=default_freq_index; // default is config file's freq
  3763. }
  3764. #endif
  3765. sprintf(logstr,"Chain[J%d] has no freq in PIC, set default freq=%dM\n",i+1,frequency);
  3766. writeInitLogFile(logstr);
  3767. }
  3768. #ifdef T9_18
  3769. if(fpga_version>=0xE)
  3770. {
  3771. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3772. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3773. if(chain_pic_buf[new_T9_PLUS_chainIndex][0]==FREQ_MAGIC && (!isUseDefaultFreq))
  3774. {
  3775. sprintf(logstr,"Chain[J%d] has core num in PIC\n",i+1);
  3776. writeInitLogFile(logstr);
  3777. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3778. {
  3779. if(j%2)
  3780. chain_badcore_num[i][j]=(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+22+(j/2)]&0x0f);
  3781. else chain_badcore_num[i][j]=(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+22+(j/2)]>>4)&0x0f;
  3782. if(chain_badcore_num[i][j]>0)
  3783. {
  3784. sprintf(logstr,"Chain[J%d] ASIC[%d] has core num=%d\n",i+1,j,chain_badcore_num[i][j]);
  3785. writeInitLogFile(logstr);
  3786. }
  3787. }
  3788. }
  3789. else
  3790. {
  3791. sprintf(logstr,"Chain[J%d] has no core num in PIC\n",i+1);
  3792. writeInitLogFile(logstr);
  3793. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3794. chain_badcore_num[i][j]=0; // fixed to 0
  3795. }
  3796. }
  3797. else
  3798. {
  3799. if(chain_pic_buf[((i/3)*3)][0]==FREQ_MAGIC && (!isUseDefaultFreq))
  3800. {
  3801. sprintf(logstr,"Chain[J%d] has core num in PIC\n",i+1);
  3802. writeInitLogFile(logstr);
  3803. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3804. {
  3805. if(j%2)
  3806. chain_badcore_num[i][j]=(chain_pic_buf[((i/3)*3)][7+(i%3)*31+22+(j/2)]&0x0f);
  3807. else chain_badcore_num[i][j]=(chain_pic_buf[((i/3)*3)][7+(i%3)*31+22+(j/2)]>>4)&0x0f;
  3808. if(chain_badcore_num[i][j]>0)
  3809. {
  3810. sprintf(logstr,"Chain[J%d] ASIC[%d] has core num=%d\n",i+1,j,chain_badcore_num[i][j]);
  3811. writeInitLogFile(logstr);
  3812. }
  3813. }
  3814. }
  3815. else
  3816. {
  3817. sprintf(logstr,"Chain[J%d] has no core num in PIC\n",i+1);
  3818. writeInitLogFile(logstr);
  3819. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3820. chain_badcore_num[i][j]=0; // fixed to 0
  3821. }
  3822. }
  3823. #else
  3824. if(badcore_num_buf[i][0]==BADCORE_MAGIC && (!isUseDefaultFreq))
  3825. {
  3826. sprintf(logstr,"Chain[J%d] has core num in PIC\n",i+1);
  3827. writeInitLogFile(logstr);
  3828. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3829. {
  3830. if(j%2)
  3831. chain_badcore_num[i][j]=(badcore_num_buf[i][(j/2)*2+1]&0x0f);
  3832. else chain_badcore_num[i][j]=(badcore_num_buf[i][(j/2)*2+1]>>4)&0x0f;
  3833. if(chain_badcore_num[i][j]>0)
  3834. {
  3835. sprintf(logstr,"Chain[J%d] ASIC[%d] has core num=%d\n",i+1,j,chain_badcore_num[i][j]);
  3836. writeInitLogFile(logstr);
  3837. }
  3838. }
  3839. }
  3840. else
  3841. {
  3842. sprintf(logstr,"Chain[J%d] has no core num in PIC\n",i+1);
  3843. writeInitLogFile(logstr);
  3844. for(j = 0; j < CHAIN_ASIC_NUM; j++)
  3845. chain_badcore_num[i][j]=0; // fixed to 0
  3846. }
  3847. #endif
  3848. }
  3849. }
  3850. //////////////////////set default freq END////////////////////////////////////////
  3851. if(!isUseDefaultFreq)
  3852. {
  3853. sprintf(logstr,"miner total rate=%dGH/s fixed rate=%dGH/s\n",GetTotalRate(),ConvirtTotalRate(GetTotalRate()));
  3854. writeInitLogFile(logstr);
  3855. }
  3856. if(!isFixedFreqMode())
  3857. {
  3858. //////////////////////// just print freq record, but do not set freq, we need fix freq /////////////////////////
  3859. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  3860. {
  3861. chain_max_freq=0;
  3862. chain_min_freq=100;
  3863. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i]>0)
  3864. {
  3865. vol_pic=get_pic_voltage(i);
  3866. vol_value = getVolValueFromPICvoltage(vol_pic);
  3867. sprintf(logstr,"read PIC voltage=%d on chain[%d]\n",vol_value,i);
  3868. writeInitLogFile(logstr);
  3869. sprintf(logstr,"Chain:%d chipnum=%d\n",i,dev->chain_asic_num[i]);
  3870. writeInitLogFile(logstr);
  3871. #ifdef T9_18
  3872. if(fpga_version>=0xE)
  3873. {
  3874. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3875. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3876. sprintf(logstr,"Chain[J%d] voltage added=0.%dV\n",i+1,chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+2]);
  3877. }
  3878. else
  3879. {
  3880. sprintf(logstr,"Chain[J%d] voltage added=0.%dV\n",i+1,chain_pic_buf[((i/3)*3)][7+(i%3)*31+2]);
  3881. }
  3882. #else
  3883. sprintf(logstr,"Chain[J%d] voltage added=0.%dV\n",i+1,last_freq[i][10]&0x3f);
  3884. #endif
  3885. writeInitLogFile(logstr);
  3886. #ifdef T9_18
  3887. if(fpga_version>=0xE)
  3888. {
  3889. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  3890. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  3891. if(isUseDefaultFreq)
  3892. base_freq_index[i]=default_freq_index;
  3893. else base_freq_index[i]=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31];
  3894. sprintf(logstr,"Chain:%d base freq=%s\n",i,freq_pll_1385[base_freq_index[i]].freq);
  3895. writeInitLogFile(logstr);
  3896. for(j = 0; j < dev->chain_asic_num[i]; j ++)
  3897. {
  3898. applog(LOG_DEBUG,"%s: freq index=%d\n", __FUNCTION__,chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]);
  3899. if(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]<MIN_FREQ)
  3900. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]=MIN_FREQ;// error index, set to index of 300M as min
  3901. if(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j] > MAX_FREQ)
  3902. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]=MAX_FREQ;// error index, set to index of 850M as max
  3903. if(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j] > max_freq_index)
  3904. max_freq_index = chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  3905. if(chain_max_freq<chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j])
  3906. chain_max_freq=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  3907. if(chain_min_freq>chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j])
  3908. chain_min_freq=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  3909. // set_frequency_with_addr_plldatai(last_freq[i][j*2+3],0, j * dev->addrInterval,i);
  3910. sprintf(logstr,"Asic[%2d]:%s ",j,freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq);
  3911. writeInitLogFile(logstr);
  3912. if ((j % 8) == 0)
  3913. {
  3914. sprintf(logstr,"\n");
  3915. writeInitLogFile(logstr);
  3916. }
  3917. }
  3918. }
  3919. else
  3920. {
  3921. if(isUseDefaultFreq)
  3922. base_freq_index[i]=default_freq_index;
  3923. else base_freq_index[i]=chain_pic_buf[((i/3)*3)][7+(i%3)*31];
  3924. sprintf(logstr,"Chain:%d base freq=%s\n",i,freq_pll_1385[base_freq_index[i]].freq);
  3925. writeInitLogFile(logstr);
  3926. for(j = 0; j < dev->chain_asic_num[i]; j ++)
  3927. {
  3928. applog(LOG_DEBUG,"%s: freq index=%d\n", __FUNCTION__,chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]);
  3929. if(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]<MIN_FREQ)
  3930. chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]=MIN_FREQ;// error index, set to index of 300M as min
  3931. if(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j] > MAX_FREQ)
  3932. chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]=MAX_FREQ;// error index, set to index of 850M as max
  3933. if(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j] > max_freq_index)
  3934. max_freq_index = chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  3935. if(chain_max_freq<chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j])
  3936. chain_max_freq=chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  3937. if(chain_min_freq>chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j])
  3938. chain_min_freq=chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  3939. // set_frequency_with_addr_plldatai(last_freq[i][j*2+3],0, j * dev->addrInterval,i);
  3940. sprintf(logstr,"Asic[%2d]:%s ",j,freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq);
  3941. writeInitLogFile(logstr);
  3942. if ((j % 8) == 0)
  3943. {
  3944. sprintf(logstr,"\n");
  3945. writeInitLogFile(logstr);
  3946. }
  3947. }
  3948. }
  3949. #else
  3950. pic_temp_offset[i]=((last_freq[i][2]&0x0f)<<4)+(last_freq[i][4]&0x0f);
  3951. sprintf(logstr,"Chain:%d temp offset=%d\n",i,(signed char)pic_temp_offset[i]);
  3952. writeInitLogFile(logstr);
  3953. if(isUseDefaultFreq)
  3954. base_freq_index[i]=default_freq_index;
  3955. else base_freq_index[i]=((last_freq[i][6]&0x0f)<<4)+(last_freq[i][8]&0x0f);
  3956. sprintf(logstr,"Chain:%d base freq=%s\n",i,freq_pll_1385[base_freq_index[i]].freq);
  3957. writeInitLogFile(logstr);
  3958. for(j = 0; j < dev->chain_asic_num[i]; j ++)
  3959. {
  3960. step_down = last_freq[i][0] & 0x3f;
  3961. if(step_down == 0x3f)
  3962. step_down = 0;
  3963. last_freq[i][j*2+3] -=step_down; // down steps based on the PIC's freq
  3964. applog(LOG_DEBUG,"%s: freq index=%d\n", __FUNCTION__,last_freq[i][j*2+3]);
  3965. if(last_freq[i][j*2+3]<MIN_FREQ)
  3966. last_freq[i][j*2+3]=MIN_FREQ;// error index, set to index of 300M as min
  3967. if(last_freq[i][j*2+3] > MAX_FREQ)
  3968. last_freq[i][j*2+3]=MAX_FREQ;// error index, set to index of 850M as max
  3969. if(last_freq[i][j*2+3] > max_freq_index)
  3970. max_freq_index = last_freq[i][j*2+3];
  3971. if(chain_max_freq<last_freq[i][j*2+3])
  3972. chain_max_freq=last_freq[i][j*2+3];
  3973. if(chain_min_freq>last_freq[i][j*2+3])
  3974. chain_min_freq=last_freq[i][j*2+3];
  3975. // set_frequency_with_addr_plldatai(last_freq[i][j*2+3],0, j * dev->addrInterval,i);
  3976. sprintf(logstr,"Asic[%2d]:%s ",j,freq_pll_1385[last_freq[i][j*2+3]].freq);
  3977. writeInitLogFile(logstr);
  3978. if ((j % 8) == 0)
  3979. {
  3980. sprintf(logstr,"\n");
  3981. writeInitLogFile(logstr);
  3982. }
  3983. }
  3984. #endif
  3985. sprintf(logstr,"\nChain:%d max freq=%s\n",i,freq_pll_1385[chain_max_freq].freq);
  3986. writeInitLogFile(logstr);
  3987. sprintf(logstr,"Chain:%d min freq=%s\n",i,freq_pll_1385[chain_min_freq].freq);
  3988. writeInitLogFile(logstr);
  3989. sprintf(logstr,"\n");
  3990. writeInitLogFile(logstr);
  3991. }
  3992. }
  3993. }
  3994. /////////////////// fix freq and set freq //////////////////////////////////////////
  3995. max_freq_index = 0;
  3996. sprintf(logstr,"\nMiner fix freq ...\n");
  3997. writeInitLogFile(logstr);
  3998. if((!isUseDefaultFreq) && isChainEnough())
  3999. {
  4000. ProcessFixFreqForChips(); //do the real freq fix at first, because this freq is higher than freq showd to users.
  4001. }
  4002. // always save real freq into chip_last_freq
  4003. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  4004. {
  4005. if(dev->chain_exist[i] == 1)
  4006. {
  4007. #ifdef T9_18
  4008. memcpy(chip_last_freq[i],chain_pic_buf[i],128);
  4009. #else
  4010. memcpy(chip_last_freq[i],last_freq[i],256);
  4011. #endif
  4012. }
  4013. }
  4014. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  4015. {
  4016. chain_max_freq=0;
  4017. chain_min_freq=100;
  4018. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i]>0)
  4019. {
  4020. vol_pic=get_pic_voltage(i);
  4021. vol_value = getVolValueFromPICvoltage(vol_pic);
  4022. sprintf(logstr,"read PIC voltage=%d on chain[%d]\n",vol_value,i);
  4023. writeInitLogFile(logstr);
  4024. sprintf(logstr,"Chain:%d chipnum=%d\n",i,dev->chain_asic_num[i]);
  4025. writeInitLogFile(logstr);
  4026. if(!isFixedFreqMode())
  4027. {
  4028. #ifdef T9_18
  4029. if(fpga_version>=0xE)
  4030. {
  4031. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  4032. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  4033. sprintf(logstr,"Chain[J%d] voltage added=0.%dV\n",i+1,chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+2]);
  4034. }
  4035. else
  4036. {
  4037. sprintf(logstr,"Chain[J%d] voltage added=0.%dV\n",i+1,chain_pic_buf[((i/3)*3)][7+(i%3)*31+2]);
  4038. }
  4039. #else
  4040. sprintf(logstr,"Chain[J%d] voltage added=0.%dV\n",i+1,last_freq[i][10]&0x3f);
  4041. #endif
  4042. writeInitLogFile(logstr);
  4043. }
  4044. #ifdef T9_18
  4045. if(fpga_version>=0xE)
  4046. {
  4047. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  4048. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  4049. if(!isFixedFreqMode())
  4050. {
  4051. if(isUseDefaultFreq)
  4052. base_freq_index[i]=default_freq_index;
  4053. else base_freq_index[i]=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31];
  4054. sprintf(logstr,"Chain:%d base freq=%s\n",i,freq_pll_1385[base_freq_index[i]].freq);
  4055. writeInitLogFile(logstr);
  4056. }
  4057. for(j = 0; j < dev->chain_asic_num[i]; j ++)
  4058. {
  4059. applog(LOG_DEBUG,"%s: freq index=%d\n", __FUNCTION__,chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]);
  4060. if(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]<MIN_FREQ)
  4061. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]=MIN_FREQ;// error index, set to index of 300M as min
  4062. if(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j] > MAX_FREQ)
  4063. chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]=MAX_FREQ;// error index, set to index of 850M as max
  4064. if(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j] > max_freq_index)
  4065. max_freq_index = chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  4066. if(chain_max_freq<chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j])
  4067. chain_max_freq=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  4068. if(chain_min_freq>chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j])
  4069. chain_min_freq=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j];
  4070. if(isFixedFreqMode()) // when use fixed freq, we add more 1 step on freq index
  4071. //set_frequency_with_addr_plldatai(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]+1, 0, j * dev->addrInterval, i);
  4072. set_frequency_with_addr_plldatai(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j], 0, j * dev->addrInterval, i);
  4073. else
  4074. set_frequency_with_addr_plldatai(chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j], 0, j * dev->addrInterval, i);
  4075. sprintf(logstr,"Asic[%2d]:%s ",j,freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq);
  4076. writeInitLogFile(logstr);
  4077. if ((j % 8) == 0)
  4078. {
  4079. sprintf(logstr,"\n");
  4080. writeInitLogFile(logstr);
  4081. }
  4082. }
  4083. }
  4084. else
  4085. {
  4086. if(!isFixedFreqMode())
  4087. {
  4088. if(isUseDefaultFreq)
  4089. base_freq_index[i]=default_freq_index;
  4090. else base_freq_index[i]=chain_pic_buf[((i/3)*3)][7+(i%3)*31];
  4091. sprintf(logstr,"Chain:%d base freq=%s\n",i,freq_pll_1385[base_freq_index[i]].freq);
  4092. writeInitLogFile(logstr);
  4093. }
  4094. for(j = 0; j < dev->chain_asic_num[i]; j ++)
  4095. {
  4096. applog(LOG_DEBUG,"%s: freq index=%d\n", __FUNCTION__,chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]);
  4097. if(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]<MIN_FREQ)
  4098. chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]=MIN_FREQ;// error index, set to index of 300M as min
  4099. if(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j] > MAX_FREQ)
  4100. chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]=MAX_FREQ;// error index, set to index of 850M as max
  4101. if(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j] > max_freq_index)
  4102. max_freq_index = chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  4103. if(chain_max_freq<chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j])
  4104. chain_max_freq=chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  4105. if(chain_min_freq>chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j])
  4106. chain_min_freq=chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j];
  4107. if(isFixedFreqMode()) // when use fixed freq, we add more 1 step on freq index
  4108. //set_frequency_with_addr_plldatai(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]+1, 0, j * dev->addrInterval, i);
  4109. set_frequency_with_addr_plldatai(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j], 0, j * dev->addrInterval, i);
  4110. else
  4111. set_frequency_with_addr_plldatai(chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j], 0, j * dev->addrInterval, i);
  4112. sprintf(logstr,"Asic[%2d]:%s ",j,freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq);
  4113. writeInitLogFile(logstr);
  4114. if ((j % 8) == 0)
  4115. {
  4116. sprintf(logstr,"\n");
  4117. writeInitLogFile(logstr);
  4118. }
  4119. }
  4120. }
  4121. #else
  4122. if(!isFixedFreqMode())
  4123. {
  4124. pic_temp_offset[i]=((last_freq[i][2]&0x0f)<<4)+(last_freq[i][4]&0x0f);
  4125. sprintf(logstr,"Chain:%d temp offset=%d\n",i,(signed char)pic_temp_offset[i]);
  4126. writeInitLogFile(logstr);
  4127. if(isUseDefaultFreq)
  4128. base_freq_index[i]=default_freq_index;
  4129. else base_freq_index[i]=((last_freq[i][6]&0x0f)<<4)+(last_freq[i][8]&0x0f);
  4130. sprintf(logstr,"Chain:%d base freq=%s\n",i,freq_pll_1385[base_freq_index[i]].freq);
  4131. writeInitLogFile(logstr);
  4132. }
  4133. for(j = 0; j < dev->chain_asic_num[i]; j ++)
  4134. {
  4135. if(!isFixedFreqMode())
  4136. {
  4137. step_down = last_freq[i][0] & 0x3f;
  4138. if(step_down == 0x3f)
  4139. step_down = 0;
  4140. last_freq[i][j*2+3] -=step_down; // down some steps based on the PIC's freq, now NOT USED!!!
  4141. }
  4142. applog(LOG_DEBUG,"%s: freq index=%d\n", __FUNCTION__,last_freq[i][j*2+3]);
  4143. if(last_freq[i][j*2+3]<MIN_FREQ)
  4144. last_freq[i][j*2+3]=MIN_FREQ;// error index, set to index of 300M as min
  4145. if(last_freq[i][j*2+3] > MAX_FREQ)
  4146. last_freq[i][j*2+3]=MAX_FREQ;// error index, set to index of 850M as max
  4147. if(last_freq[i][j*2+3] > max_freq_index)
  4148. max_freq_index = last_freq[i][j*2+3];
  4149. if(chain_max_freq<last_freq[i][j*2+3])
  4150. chain_max_freq=last_freq[i][j*2+3];
  4151. if(chain_min_freq>last_freq[i][j*2+3])
  4152. chain_min_freq=last_freq[i][j*2+3];
  4153. if(isFixedFreqMode()) // when use fixed freq, we add more 1 step on freq index
  4154. //set_frequency_with_addr_plldatai(last_freq[i][j*2+3]+1,0, j * dev->addrInterval,i);
  4155. set_frequency_with_addr_plldatai(last_freq[i][j*2+3],0, j * dev->addrInterval,i);
  4156. else
  4157. set_frequency_with_addr_plldatai(last_freq[i][j*2+3],0, j * dev->addrInterval,i);
  4158. sprintf(logstr,"Asic[%2d]:%s ",j,freq_pll_1385[last_freq[i][j*2+3]].freq);
  4159. writeInitLogFile(logstr);
  4160. if ((j % 8) == 0)
  4161. {
  4162. sprintf(logstr,"\n");
  4163. writeInitLogFile(logstr);
  4164. }
  4165. }
  4166. #endif
  4167. sprintf(logstr,"\nChain:%d max freq=%s\n",i,freq_pll_1385[chain_max_freq].freq);
  4168. writeInitLogFile(logstr);
  4169. sprintf(logstr,"Chain:%d min freq=%s\n",i,freq_pll_1385[chain_min_freq].freq);
  4170. writeInitLogFile(logstr);
  4171. sprintf(logstr,"\n");
  4172. writeInitLogFile(logstr);
  4173. }
  4174. }
  4175. value = atoi(freq_pll_1385[max_freq_index].freq);
  4176. dev->frequency = value;
  4177. sprintf(logstr,"max freq = %d\n",dev->frequency);
  4178. writeInitLogFile(logstr);
  4179. if(!isUseDefaultFreq)
  4180. {
  4181. ProcessFixFreq();
  4182. }
  4183. // always save freq showd to user, into show_last_freq, because the real freq is up 5% when in user mode.
  4184. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  4185. {
  4186. if(dev->chain_exist[i] == 1)
  4187. {
  4188. #ifdef T9_18
  4189. memcpy(show_last_freq[i],chain_pic_buf[i],128);
  4190. #else
  4191. memcpy(show_last_freq[i],last_freq[i],256);
  4192. #endif
  4193. }
  4194. }
  4195. #ifdef DISABLE_TEMP_PROTECT
  4196. sprintf(logstr,"\nAttention: Temp Protect Disabled! Debug Mode\n\n");
  4197. writeInitLogFile(logstr);
  4198. #endif
  4199. }
  4200. void set_frequency_with_addr(unsigned short int frequency,unsigned char mode,unsigned char addr, unsigned char chain)
  4201. {
  4202. unsigned char buf[9] = {0,0,0,0,0,0,0,0,0};
  4203. unsigned int cmd_buf[3] = {0,0,0};
  4204. int i;
  4205. unsigned int ret, value;
  4206. uint32_t reg_data_pll = 0;
  4207. uint16_t reg_data_pll2 = 0;
  4208. uint32_t reg_data_vil = 0;
  4209. i = chain;
  4210. applog(LOG_DEBUG,"\n--- %s\n", __FUNCTION__);
  4211. get_plldata(1385, frequency, &reg_data_pll, &reg_data_pll2, &reg_data_vil);
  4212. applog(LOG_DEBUG,"%s: frequency = %d\n", __FUNCTION__, frequency);
  4213. //applog(LOG_DEBUG,"%s: i = %d\n", __FUNCTION__, i);
  4214. if(!opt_multi_version) // fil mode
  4215. {
  4216. memset(buf,0,sizeof(buf));
  4217. memset(cmd_buf,0,sizeof(cmd_buf));
  4218. buf[0] = 0;
  4219. buf[0] |= SET_PLL_DIVIDER1;
  4220. buf[1] = (reg_data_pll >> 16) & 0xff;
  4221. buf[2] = (reg_data_pll >> 8) & 0xff;
  4222. buf[3] = (reg_data_pll >> 0) & 0xff;
  4223. buf[3] |= CRC5(buf, 4*8 - 5);
  4224. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  4225. set_BC_command_buffer(cmd_buf);
  4226. ret = get_BC_write_command();
  4227. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  4228. set_BC_write_command(value);
  4229. cgsleep_us(3000);
  4230. memset(buf,0,sizeof(buf));
  4231. memset(cmd_buf,0,sizeof(cmd_buf));
  4232. buf[0] = SET_PLL_DIVIDER2;
  4233. buf[0] |= COMMAND_FOR_ALL;
  4234. buf[1] = 0; //addr
  4235. buf[2] = reg_data_pll2 >> 8;
  4236. buf[3] = reg_data_pll2& 0x0ff;
  4237. buf[3] |= CRC5(buf, 4*8 - 5);
  4238. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  4239. set_BC_command_buffer(cmd_buf);
  4240. ret = get_BC_write_command();
  4241. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  4242. set_BC_write_command(value);
  4243. dev->freq[i] = frequency;
  4244. cgsleep_us(5000);
  4245. }
  4246. else // vil
  4247. {
  4248. memset(buf,0,9);
  4249. memset(cmd_buf,0,3*sizeof(int));
  4250. if(mode)
  4251. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  4252. else
  4253. buf[0] = VIL_COMMAND_TYPE | SET_CONFIG;
  4254. buf[1] = 0x09;
  4255. buf[2] = addr;
  4256. buf[3] = PLL_PARAMETER;
  4257. buf[4] = (reg_data_vil >> 24) & 0xff;
  4258. buf[5] = (reg_data_vil >> 16) & 0xff;
  4259. buf[6] = (reg_data_vil >> 8) & 0xff;
  4260. buf[7] = (reg_data_vil >> 0) & 0xff;
  4261. buf[8] = CRC5(buf, 8*8);
  4262. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  4263. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];;
  4264. cmd_buf[2] = buf[8]<<24;
  4265. set_BC_command_buffer(cmd_buf);
  4266. ret = get_BC_write_command();
  4267. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  4268. set_BC_write_command(value);
  4269. dev->freq[i] = frequency;
  4270. cgsleep_us(10000);
  4271. }
  4272. }
  4273. void clear_nonce_fifo()
  4274. {
  4275. unsigned int buf[2] = {0};
  4276. pthread_mutex_lock(&nonce_mutex);
  4277. nonce_read_out.p_wr = 0;
  4278. nonce_read_out.p_rd = 0;
  4279. nonce_read_out.nonce_num = 0;
  4280. pthread_mutex_unlock(&nonce_mutex);
  4281. }
  4282. void clear_register_value_buf()
  4283. {
  4284. pthread_mutex_lock(&reg_mutex);
  4285. reg_value_buf.p_wr = 0;
  4286. reg_value_buf.p_rd = 0;
  4287. reg_value_buf.reg_value_num = 0;
  4288. //memset(reg_value_buf.reg_buffer, 0, sizeof(struct reg_content)*MAX_NONCE_NUMBER_IN_FIFO);
  4289. pthread_mutex_unlock(&reg_mutex);
  4290. }
  4291. void read_asic_register(unsigned char chain, unsigned char mode, unsigned char chip_addr, unsigned char reg_addr)
  4292. {
  4293. unsigned char buf[5] = {0,0,0,0,0};
  4294. unsigned char buf_vil[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
  4295. unsigned int cmd_buf[3] = {0,0,0};
  4296. unsigned int ret, value;
  4297. char logstr[1024];
  4298. int wait_count=0;
  4299. if(!opt_multi_version) // fil mode
  4300. {
  4301. buf[0] = GET_STATUS;
  4302. buf[1] = chip_addr;
  4303. buf[2] = reg_addr;
  4304. if (mode) //all
  4305. buf[0] |= COMMAND_FOR_ALL;
  4306. buf[3] = CRC5(buf, 4*8 - 5);
  4307. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  4308. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  4309. set_BC_command_buffer(cmd_buf);
  4310. ret = get_BC_write_command();
  4311. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (chain << 16) | (ret & 0xfff0ffff);
  4312. set_BC_write_command(value);
  4313. }
  4314. else // vil mode
  4315. {
  4316. buf[0] = VIL_COMMAND_TYPE | GET_STATUS;
  4317. if(mode)
  4318. buf[0] |= VIL_ALL;
  4319. buf[1] = 0x05;
  4320. buf[2] = chip_addr;
  4321. buf[3] = reg_addr;
  4322. buf[4] = CRC5(buf, 4*8);
  4323. applog(LOG_DEBUG,"%s:VIL buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x, buf[4]=0x%x", __FUNCTION__, buf[0], buf[1], buf[2], buf[3], buf[4]);
  4324. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  4325. cmd_buf[1] = buf[4]<<24;
  4326. while (1)
  4327. {
  4328. if (((ret = get_BC_write_command()) & 0x80000000) == 0)
  4329. break;
  4330. cgsleep_ms(1);
  4331. wait_count++;
  4332. if(wait_count>3000)
  4333. {
  4334. sprintf(logstr,"Error: clement debug: wait BC ready timeout, PLUG ON=0x%08x..\n",(unsigned int)get_hash_on_plug());
  4335. writeInitLogFile(logstr);
  4336. break;
  4337. }
  4338. }
  4339. set_BC_command_buffer(cmd_buf);
  4340. ret = get_BC_write_command();
  4341. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (chain << 16) | (ret & 0xfff0ffff);
  4342. set_BC_write_command(value);
  4343. }
  4344. }
  4345. void read_temp(unsigned char device,unsigned reg,unsigned char data,unsigned char write,unsigned char chip_addr,int chain)
  4346. {
  4347. unsigned char buf[9] = {0,0,0,0,0,0,0,0,0};
  4348. unsigned int cmd_buf[3] = {0,0,0};
  4349. unsigned int ret, value,i;
  4350. i = chain;
  4351. if(!opt_multi_version)
  4352. {
  4353. //printf("fil mode do not support temp reading");
  4354. }
  4355. else
  4356. {
  4357. buf[0] = VIL_COMMAND_TYPE | SET_CONFIG;
  4358. buf[1] = 0x09;
  4359. buf[2] = chip_addr;
  4360. buf[3] = GENERAL_I2C_COMMAND;
  4361. buf[4] = 0x01;
  4362. buf[5] = device | write;
  4363. buf[6] = reg;
  4364. buf[7] = data;
  4365. buf[8] = CRC5(buf, 8*8);
  4366. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  4367. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
  4368. cmd_buf[2] = buf[8]<<24;
  4369. while (1)
  4370. {
  4371. ret = get_BC_write_command();
  4372. if ((ret & 0x80000000) == 0)
  4373. break;
  4374. cgsleep_ms(1);
  4375. }
  4376. set_BC_command_buffer(cmd_buf);
  4377. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  4378. set_BC_write_command(value);
  4379. }
  4380. }
  4381. static void suffix_string_soc(uint64_t val, char *buf, size_t bufsiz, int sigdigits,bool display)
  4382. {
  4383. const double dkilo = 1000.0;
  4384. const uint64_t kilo = 1000ull;
  4385. const uint64_t mega = 1000000ull;
  4386. const uint64_t giga = 1000000000ull;
  4387. const uint64_t tera = 1000000000000ull;
  4388. const uint64_t peta = 1000000000000000ull;
  4389. const uint64_t exa = 1000000000000000000ull;
  4390. char suffix[2] = "";
  4391. bool decimal = true;
  4392. double dval;
  4393. /*
  4394. if (val >= exa)
  4395. {
  4396. val /= peta;
  4397. dval = (double)val / dkilo;
  4398. strcpy(suffix, "E");
  4399. }
  4400. else if (val >= peta)
  4401. {
  4402. val /= tera;
  4403. dval = (double)val / dkilo;
  4404. strcpy(suffix, "P");
  4405. }
  4406. else if (val >= tera)
  4407. {
  4408. val /= giga;
  4409. dval = (double)val / dkilo;
  4410. strcpy(suffix, "T");
  4411. }
  4412. else */if (val >= giga)
  4413. {
  4414. val /= mega;
  4415. dval = (double)val / dkilo;
  4416. strcpy(suffix, "G");
  4417. }
  4418. else if (val >= mega)
  4419. {
  4420. val /= kilo;
  4421. dval = (double)val / dkilo;
  4422. strcpy(suffix, "M");
  4423. }
  4424. else if (val >= kilo)
  4425. {
  4426. dval = (double)val / dkilo;
  4427. strcpy(suffix, "K");
  4428. }
  4429. else
  4430. {
  4431. dval = val;
  4432. decimal = false;
  4433. }
  4434. if (!sigdigits)
  4435. {
  4436. if (decimal)
  4437. snprintf(buf, bufsiz, "%.3g%s", dval, suffix);
  4438. else
  4439. snprintf(buf, bufsiz, "%d%s", (unsigned int)dval, suffix);
  4440. }
  4441. else
  4442. {
  4443. /* Always show sigdigits + 1, padded on right with zeroes
  4444. * followed by suffix */
  4445. int ndigits = sigdigits - 1 - (dval > 0.0 ? floor(log10(dval)) : 0);
  4446. if(display)
  4447. snprintf(buf, bufsiz, "%*.*f%s", sigdigits + 1, ndigits, dval, suffix);
  4448. else
  4449. snprintf(buf, bufsiz, "%*.*f", sigdigits + 1, ndigits, dval);
  4450. }
  4451. }
  4452. void showAllBadRTInfo()
  4453. {
  4454. int i,j;
  4455. char logstr[1024];
  4456. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  4457. {
  4458. if(dev->chain_exist[i] == 1
  4459. #ifdef DEBUG_XILINX_NONCE_NOTENOUGH
  4460. && i!=DISABLE_REG_CHAIN_INDEX
  4461. #endif
  4462. )
  4463. {
  4464. sprintf(logstr,"Check Chain[J%d] ASIC RT error: (asic index start from 1-%d)\n",i+1,CHAIN_ASIC_NUM);
  4465. writeLogFile(logstr);
  4466. for(j=0; j<CHAIN_ASIC_NUM; j++)
  4467. {
  4468. if(chain_asic_RT[i][j]>100)
  4469. {
  4470. sprintf(logstr,"Asic[%02d]=%f\n",j+1,chain_asic_RT[i][j]);
  4471. writeLogFile(logstr);
  4472. }
  4473. }
  4474. }
  4475. }
  4476. }
  4477. bool check_asic_reg(unsigned int reg)
  4478. {
  4479. int i, j, not_reg_data_time=0;
  4480. int nonce_number = 0;
  4481. unsigned int buf[2] = {0};
  4482. unsigned int reg_value_num=0;
  4483. unsigned int temp_nonce = 0;
  4484. unsigned char reg_buf[5] = {0,0,0,0,0};
  4485. int read_num = 0;
  4486. uint64_t tmp_rate = 0;
  4487. int reg_processed_counter=0;
  4488. char logstr[1024];
  4489. rerun_all:
  4490. clear_register_value_buf();
  4491. tmp_rate = 0;
  4492. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  4493. {
  4494. reg_processed_counter=0;
  4495. read_num = 0;
  4496. if(dev->chain_exist[i] == 1
  4497. #ifdef DEBUG_XILINX_NONCE_NOTENOUGH
  4498. && i!=DISABLE_REG_CHAIN_INDEX
  4499. #endif
  4500. )
  4501. {
  4502. tmp_rate = 0;
  4503. // sprintf(logstr,"do read_asic_register on Chain[%d]...\n",i);
  4504. // writeLogFile(logstr);
  4505. read_asic_register(i, 1, 0, reg);
  4506. // sprintf(logstr,"Done read_asic_register on Chain[%d]\n",i);
  4507. // writeLogFile(logstr);
  4508. if (reg ==CHIP_ADDRESS)
  4509. dev->chain_asic_num[i] = 0;
  4510. if(reg == 0x08)
  4511. {
  4512. sprintf(logstr,"\nget RT hashrate from Chain[%d]: (asic index start from 1-%d)\n",i,CHAIN_ASIC_NUM);
  4513. writeLogFile(logstr);
  4514. }
  4515. while(not_reg_data_time < 3) //if there is no register value for 3 times, we can think all asic return their address
  4516. {
  4517. cgsleep_ms(300);
  4518. pthread_mutex_lock(&reg_mutex);
  4519. reg_value_num = reg_value_buf.reg_value_num;
  4520. if((reg_value_num >= MAX_NONCE_NUMBER_IN_FIFO || reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO) && not_reg_data_time <3)
  4521. {
  4522. not_reg_data_time ++;
  4523. pthread_mutex_unlock(&reg_mutex);
  4524. goto rerun_all;
  4525. }
  4526. if(not_reg_data_time == 3)
  4527. {
  4528. pthread_mutex_unlock(&reg_mutex);
  4529. return true;
  4530. }
  4531. if(reg_value_num > 0)
  4532. {
  4533. reg_processed_counter+=reg_value_num;
  4534. if(reg_processed_counter>600)
  4535. {
  4536. // sprintf(logstr,"read asic reg Error on Chain[%d]\n",i);
  4537. // writeInitLogFile(logstr);
  4538. pthread_mutex_unlock(&reg_mutex);
  4539. return false;
  4540. }
  4541. // sprintf(logstr,"process reg_value_num=%d on Chain[%d]\n",reg_value_num,i);
  4542. // writeLogFile(logstr);
  4543. not_reg_data_time = 0;
  4544. for(j = 0; j < reg_value_num; j++)
  4545. {
  4546. if(reg_value_buf.reg_buffer[reg_value_buf.p_rd].chain_number != i)
  4547. {
  4548. // sprintf(logstr,"read asic reg Error: wrong chain number=%d on Chain[%d]\n",i);
  4549. // writeInitLogFile(logstr);
  4550. reg_value_buf.p_rd++;
  4551. reg_value_buf.reg_value_num--;
  4552. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4553. {
  4554. reg_value_buf.p_rd = 0;
  4555. }
  4556. continue;
  4557. }
  4558. reg_buf[3] = (unsigned char)(reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value & 0xff);
  4559. reg_buf[2] = (unsigned char)((reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value >> 8) & 0xff);
  4560. reg_buf[1] = (unsigned char)((reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value >> 16)& 0xff);
  4561. reg_buf[0] = (unsigned char)((reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value >> 24)& 0xff);
  4562. #ifdef ENABLE_REGISTER_CRC_CHECK
  4563. if(CRC5(reg_buf, (REGISTER_DATA_LENGTH+3)*8-5) != reg_value_buf.reg_buffer[reg_value_buf.p_rd].crc)
  4564. {
  4565. sprintf(logstr,"%s: crc is 0x%x, but it should be 0x%x\n", __FUNCTION__, CRC5(reg_buf, (REGISTER_DATA_LENGTH+1)*8-5), reg_value_buf.reg_buffer[reg_value_buf.p_rd].crc);
  4566. writeInitLogFile(logstr);
  4567. reg_value_buf.p_rd++;
  4568. reg_value_buf.reg_value_num--;
  4569. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4570. {
  4571. reg_value_buf.p_rd = 0;
  4572. }
  4573. continue;
  4574. }
  4575. #endif
  4576. reg_value_buf.p_rd++;
  4577. reg_value_buf.reg_value_num--;
  4578. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4579. {
  4580. reg_value_buf.p_rd = 0;
  4581. }
  4582. if(reg == CHIP_ADDRESS)
  4583. {
  4584. dev->chain_asic_num[i]++;
  4585. }
  4586. if(reg == PLL_PARAMETER)
  4587. {
  4588. sprintf(logstr,"chain[%d]: the asic freq is 0x%x\n", i, reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value);
  4589. writeInitLogFile(logstr);
  4590. }
  4591. if(reg == TICKET_MASK)
  4592. {
  4593. sprintf(logstr,"chain[%d]: the asic TICKET_MASK is 0x%x\n", i, reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value);
  4594. writeInitLogFile(logstr);
  4595. }
  4596. if(reg == 0x08)
  4597. {
  4598. int ii;
  4599. char displayed_rate_asic[32];
  4600. uint64_t temp_hash_rate = 0;
  4601. uint8_t rate_buf[10];
  4602. uint8_t displayed_rate[16];
  4603. read_num ++;
  4604. if(read_num<=CHAIN_ASIC_NUM)
  4605. {
  4606. for(ii = 0; ii < 4; ii++)
  4607. {
  4608. sprintf(rate_buf + 2*ii,"%02x",reg_buf[ii]);
  4609. }
  4610. // sprintf(logstr,"%s: hashrate is %s\n", __FUNCTION__, rate_buf);
  4611. // writeInitLogFile(logstr);
  4612. temp_hash_rate = strtol(rate_buf,NULL,16);
  4613. temp_hash_rate = (temp_hash_rate << 24);
  4614. tmp_rate += temp_hash_rate;
  4615. suffix_string_soc(temp_hash_rate, displayed_rate_asic, sizeof(displayed_rate_asic), 6,false);
  4616. sprintf(logstr,"Asic[%02d]=%s ",read_num,displayed_rate_asic);
  4617. writeLogFile(logstr);
  4618. chain_asic_RT[i][read_num-1]=atof(displayed_rate_asic);
  4619. if(read_num%8 == 0 || read_num==CHAIN_ASIC_NUM)
  4620. {
  4621. sprintf(logstr,"\n");
  4622. writeLogFile(logstr);
  4623. }
  4624. }
  4625. }
  4626. }
  4627. if(reg == CHIP_ADDRESS)
  4628. {
  4629. if (dev->chain_asic_num[i] == CHAIN_ASIC_NUM)
  4630. {
  4631. pthread_mutex_unlock(&reg_mutex);
  4632. break;
  4633. }
  4634. }
  4635. // sprintf(logstr,"Done reg_value_num=%d on Chain[%d]\n",reg_value_num,i);
  4636. // writeLogFile(logstr);
  4637. }
  4638. else
  4639. {
  4640. cgsleep_ms(100);
  4641. not_reg_data_time++;
  4642. // sprintf(logstr,"not_reg_data_time=%d on Chain[%d]\n",not_reg_data_time,i);
  4643. // writeLogFile(logstr);
  4644. }
  4645. pthread_mutex_unlock(&reg_mutex);
  4646. }
  4647. not_reg_data_time = 0;
  4648. if(reg == CHIP_ADDRESS)
  4649. {
  4650. if(dev->chain_asic_num[i] > dev->max_asic_num_in_one_chain)
  4651. {
  4652. dev->max_asic_num_in_one_chain = dev->chain_asic_num[i];
  4653. }
  4654. }
  4655. if(read_num == dev->chain_asic_num[i])
  4656. {
  4657. rate[i] = tmp_rate;
  4658. suffix_string_soc(rate[i], (char * )displayed_rate[i], sizeof(displayed_rate[i]), 6,false);
  4659. rate_error[i] = 0;
  4660. // sprintf(logstr,"%s: chain %d hashrate is %s\n", __FUNCTION__, i, displayed_rate[i]);
  4661. // writeInitLogFile(logstr);
  4662. }
  4663. if(read_num == 0 || status_error )
  4664. {
  4665. rate_error[i]++;
  4666. if(rate_error[i] > 3 || status_error)
  4667. {
  4668. rate[i] = 0;
  4669. suffix_string_soc(rate[i], (char * )displayed_rate[i], sizeof(displayed_rate[i]), 6,false);
  4670. }
  4671. }
  4672. //set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() & ~(FLUSH_NONCE3_FIFO));
  4673. clear_register_value_buf();
  4674. }
  4675. }
  4676. return true;
  4677. }
  4678. void reset_one_hashboard(int chainIndex)
  4679. {
  4680. set_QN_write_data_command(RESET_HASH_BOARD | CHAIN_ID(chainIndex) | RESET_TIME(RESET_HASHBOARD_TIME));
  4681. while(get_QN_write_data_command() & RESET_HASH_BOARD)
  4682. {
  4683. usleep(10000);
  4684. }
  4685. sleep(1);
  4686. }
  4687. bool check_asic_reg_oneChain(int chainIndex, unsigned int reg)
  4688. {
  4689. int i, j, not_reg_data_time=0;
  4690. int nonce_number = 0;
  4691. unsigned int buf[2] = {0};
  4692. unsigned int reg_value_num=0;
  4693. unsigned int temp_nonce = 0;
  4694. unsigned char reg_buf[5] = {0,0,0,0,0};
  4695. int read_num = 0;
  4696. uint64_t tmp_rate = 0;
  4697. int reg_processed_counter=0;
  4698. char logstr[1024];
  4699. rerun_all:
  4700. clear_register_value_buf();
  4701. tmp_rate = 0;
  4702. //for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  4703. i=chainIndex;
  4704. {
  4705. reg_processed_counter=0;
  4706. read_num = 0;
  4707. if(dev->chain_exist[i] == 1)
  4708. {
  4709. tmp_rate = 0;
  4710. // sprintf(logstr,"do read_asic_register on Chain[%d]...\n",i);
  4711. // writeLogFile(logstr);
  4712. read_asic_register(i, 1, 0, reg);
  4713. // sprintf(logstr,"Done read_asic_register on Chain[%d]\n",i);
  4714. // writeLogFile(logstr);
  4715. if (reg ==CHIP_ADDRESS)
  4716. dev->chain_asic_num[i] = 0;
  4717. while(not_reg_data_time < 3) //if there is no register value for 3 times, we can think all asic return their address
  4718. {
  4719. cgsleep_ms(300);
  4720. pthread_mutex_lock(&reg_mutex);
  4721. reg_value_num = reg_value_buf.reg_value_num;
  4722. if((reg_value_num >= MAX_NONCE_NUMBER_IN_FIFO || reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO) && not_reg_data_time <3)
  4723. {
  4724. not_reg_data_time ++;
  4725. pthread_mutex_unlock(&reg_mutex);
  4726. goto rerun_all;
  4727. }
  4728. if(not_reg_data_time == 3)
  4729. {
  4730. pthread_mutex_unlock(&reg_mutex);
  4731. return true;
  4732. }
  4733. if(reg_value_num > 0)
  4734. {
  4735. reg_processed_counter+=reg_value_num;
  4736. if(reg_processed_counter>600)
  4737. {
  4738. // sprintf(logstr,"read asic reg Error on Chain[%d]\n",i);
  4739. // writeInitLogFile(logstr);
  4740. pthread_mutex_unlock(&reg_mutex);
  4741. return false;
  4742. }
  4743. // sprintf(logstr,"process reg_value_num=%d on Chain[%d]\n",reg_value_num,i);
  4744. // writeLogFile(logstr);
  4745. not_reg_data_time = 0;
  4746. for(j = 0; j < reg_value_num; j++)
  4747. {
  4748. if(reg_value_buf.reg_buffer[reg_value_buf.p_rd].chain_number != i)
  4749. {
  4750. reg_value_buf.p_rd++;
  4751. reg_value_buf.reg_value_num--;
  4752. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4753. {
  4754. reg_value_buf.p_rd = 0;
  4755. }
  4756. continue;
  4757. }
  4758. reg_buf[3] = (unsigned char)(reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value & 0xff);
  4759. reg_buf[2] = (unsigned char)((reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value >> 8) & 0xff);
  4760. reg_buf[1] = (unsigned char)((reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value >> 16)& 0xff);
  4761. reg_buf[0] = (unsigned char)((reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value >> 24)& 0xff);
  4762. #ifdef ENABLE_REGISTER_CRC_CHECK
  4763. if(CRC5(reg_buf, (REGISTER_DATA_LENGTH+3)*8-5) != reg_value_buf.reg_buffer[reg_value_buf.p_rd].crc)
  4764. {
  4765. sprintf(logstr,"%s: crc is 0x%x, but it should be 0x%x\n", __FUNCTION__, CRC5(reg_buf, (REGISTER_DATA_LENGTH+1)*8-5), reg_value_buf.reg_buffer[reg_value_buf.p_rd].crc);
  4766. writeInitLogFile(logstr);
  4767. reg_value_buf.p_rd++;
  4768. reg_value_buf.reg_value_num--;
  4769. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4770. {
  4771. reg_value_buf.p_rd = 0;
  4772. }
  4773. continue;
  4774. }
  4775. #endif
  4776. reg_value_buf.p_rd++;
  4777. reg_value_buf.reg_value_num--;
  4778. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4779. {
  4780. reg_value_buf.p_rd = 0;
  4781. }
  4782. if(reg == CHIP_ADDRESS)
  4783. {
  4784. dev->chain_asic_num[i]++;
  4785. }
  4786. if(reg == PLL_PARAMETER)
  4787. {
  4788. applog(LOG_DEBUG,"%s: the asic freq is 0x%x\n", __FUNCTION__, reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value);
  4789. }
  4790. if(reg == 0x08)
  4791. {
  4792. int i;
  4793. uint64_t temp_hash_rate = 0;
  4794. uint8_t rate_buf[10];
  4795. uint8_t displayed_rate[16];
  4796. for(i = 0; i < 4; i++)
  4797. {
  4798. sprintf(rate_buf + 2*i,"%02x",reg_buf[i]);
  4799. }
  4800. applog(LOG_DEBUG,"%s: hashrate is %s\n", __FUNCTION__, rate_buf);
  4801. temp_hash_rate = strtol(rate_buf,NULL,16);
  4802. temp_hash_rate = (temp_hash_rate << 24);
  4803. tmp_rate += temp_hash_rate;
  4804. read_num ++;
  4805. }
  4806. }
  4807. if(reg == CHIP_ADDRESS)
  4808. {
  4809. if (dev->chain_asic_num[i] == CHAIN_ASIC_NUM)
  4810. {
  4811. pthread_mutex_unlock(&reg_mutex);
  4812. break;
  4813. }
  4814. }
  4815. // sprintf(logstr,"Done reg_value_num=%d on Chain[%d]\n",reg_value_num,i);
  4816. // writeLogFile(logstr);
  4817. }
  4818. else
  4819. {
  4820. cgsleep_ms(100);
  4821. not_reg_data_time++;
  4822. // sprintf(logstr,"not_reg_data_time=%d on Chain[%d]\n",not_reg_data_time,i);
  4823. // writeLogFile(logstr);
  4824. }
  4825. pthread_mutex_unlock(&reg_mutex);
  4826. }
  4827. not_reg_data_time = 0;
  4828. if(reg == CHIP_ADDRESS)
  4829. {
  4830. if(dev->chain_asic_num[i] > dev->max_asic_num_in_one_chain)
  4831. {
  4832. dev->max_asic_num_in_one_chain = dev->chain_asic_num[i];
  4833. }
  4834. }
  4835. if(read_num == dev->chain_asic_num[i])
  4836. {
  4837. rate[i] = tmp_rate;
  4838. suffix_string_soc(rate[i], (char * )displayed_rate[i], sizeof(displayed_rate[i]), 6,false);
  4839. rate_error[i] = 0;
  4840. applog(LOG_DEBUG,"%s: chain %d hashrate is %s\n", __FUNCTION__, i, displayed_rate[i]);
  4841. }
  4842. if(read_num == 0 || status_error )
  4843. {
  4844. rate_error[i]++;
  4845. if(rate_error[i] > 3 || status_error)
  4846. {
  4847. rate[i] = 0;
  4848. suffix_string_soc(rate[i], (char * )displayed_rate[i], sizeof(displayed_rate[i]), 6,false);
  4849. }
  4850. }
  4851. //set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() & ~(FLUSH_NONCE3_FIFO));
  4852. clear_register_value_buf();
  4853. }
  4854. }
  4855. return true;
  4856. }
  4857. #define RETRY_NUM 5
  4858. unsigned int check_asic_reg_with_addr(unsigned int reg,unsigned int chip_addr,unsigned int chain, int check_num)
  4859. {
  4860. int i, j, not_reg_data_time=0;
  4861. int nonce_number = 0;
  4862. unsigned int reg_value_num=0;
  4863. unsigned int reg_buf = 0;
  4864. i = chain;
  4865. rerun:
  4866. clear_register_value_buf();
  4867. read_asic_register(i, 0, chip_addr, reg);
  4868. cgsleep_ms(80);
  4869. while(not_reg_data_time < RETRY_NUM) //if there is no register value for 3 times, we can think all asic return their address
  4870. {
  4871. pthread_mutex_lock(&reg_mutex);
  4872. reg_value_num = reg_value_buf.reg_value_num;
  4873. //applog(LOG_NOTICE,"%s: p_wr = %d reg_value_num = %d\n", __FUNCTION__,reg_value_buf.p_wr,reg_value_buf.reg_value_num);
  4874. pthread_mutex_unlock(&reg_mutex);
  4875. applog(LOG_DEBUG,"%s: reg_value_num %d", __FUNCTION__, reg_value_num);
  4876. if((reg_value_num >= MAX_NONCE_NUMBER_IN_FIFO || reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO ||reg_value_num ==0 ) && not_reg_data_time <RETRY_NUM)
  4877. {
  4878. not_reg_data_time ++;
  4879. goto rerun;
  4880. }
  4881. if(not_reg_data_time >= RETRY_NUM)
  4882. {
  4883. return 0;
  4884. }
  4885. pthread_mutex_lock(&reg_mutex);
  4886. for(i = 0; i < reg_value_num; i++)
  4887. {
  4888. reg_buf = reg_value_buf.reg_buffer[reg_value_buf.p_rd].reg_value;
  4889. applog(LOG_DEBUG,"%s: chip %x reg %x reg_buff %x", __FUNCTION__, chip_addr,reg,reg_buf);
  4890. reg_value_buf.p_rd++;
  4891. reg_value_buf.reg_value_num--;
  4892. // clement: why ? I need change it
  4893. #if 0
  4894. if(reg_value_buf.p_rd < MAX_NONCE_NUMBER_IN_FIFO)
  4895. {
  4896. reg_value_buf.p_rd = 0;
  4897. }
  4898. #else
  4899. if(reg_value_buf.p_rd >= MAX_NONCE_NUMBER_IN_FIFO)
  4900. {
  4901. reg_value_buf.p_rd = 0;
  4902. }
  4903. #endif
  4904. if(reg == GENERAL_I2C_COMMAND)
  4905. {
  4906. if((reg_buf & 0xc0000000) == 0x0)
  4907. {
  4908. pthread_mutex_unlock(&reg_mutex);
  4909. clear_register_value_buf();
  4910. return reg_buf;
  4911. }
  4912. else
  4913. {
  4914. pthread_mutex_unlock(&reg_mutex);
  4915. clear_register_value_buf();
  4916. return 0;
  4917. }
  4918. }
  4919. }
  4920. pthread_mutex_unlock(&reg_mutex);
  4921. }
  4922. //set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() & ~(FLUSH_NONCE3_FIFO));
  4923. clear_register_value_buf();
  4924. return 0;
  4925. }
  4926. unsigned int wait_iic_ok(unsigned int chip_addr,unsigned int chain,bool update)
  4927. {
  4928. int fail_time = 0;
  4929. unsigned int ret = 0;
  4930. while(fail_time < 2)
  4931. {
  4932. ret = check_asic_reg_with_addr(GENERAL_I2C_COMMAND,chip_addr,chain,1);
  4933. if (ret != 0)
  4934. {
  4935. return ret;
  4936. }
  4937. else
  4938. {
  4939. fail_time++;
  4940. cgsleep_ms(1);
  4941. }
  4942. }
  4943. return 0;
  4944. }
  4945. unsigned int check_reg_temp(unsigned char device,unsigned reg,unsigned char data,unsigned char write,unsigned char chip_addr,int chain)
  4946. {
  4947. int fail_time =0;
  4948. unsigned int ret;
  4949. if(!write)
  4950. {
  4951. do
  4952. {
  4953. wait_iic_ok(chip_addr,chain,0);
  4954. read_temp(device, reg, data, write,chip_addr,chain);
  4955. cgsleep_ms(1);
  4956. ret = wait_iic_ok(chip_addr,chain,1);
  4957. cgsleep_ms(1);
  4958. fail_time++;
  4959. }
  4960. while (((ret & 0xff00) >>8 != reg || (ret & 0xff) == 0xff || (ret & 0xff) == 0x7f ) && fail_time < 2);
  4961. }
  4962. else
  4963. {
  4964. do
  4965. {
  4966. wait_iic_ok(chip_addr,chain,0);
  4967. read_temp(device, reg, data, write,chip_addr,chain);
  4968. wait_iic_ok(chip_addr,chain,1);
  4969. cgsleep_ms(1);
  4970. wait_iic_ok(chip_addr,chain,0);
  4971. read_temp(device, reg, 0, 0,chip_addr,chain);
  4972. ret = wait_iic_ok(chip_addr,chain,1);
  4973. cgsleep_ms(1);
  4974. fail_time++;
  4975. }
  4976. while (((ret & 0xff00) >>8 != reg && (ret & 0xff) != data )&& fail_time < 2);
  4977. }
  4978. if (fail_time == 2)
  4979. return 0;
  4980. else
  4981. return ret;
  4982. }
  4983. #ifdef USE_N_OFFSET_FIX_TEMP
  4984. int8_t calc_offset(int remote, int local)
  4985. {
  4986. float t_noise;
  4987. t_noise = remote - ((1.11-1.008)/1.008)*(273.15 + local) - local;
  4988. return (int8_t)(0 - t_noise);
  4989. }
  4990. int16_t get_remote(int16_t remote)
  4991. {
  4992. float t_re_re;
  4993. #ifdef EXTEND_TEMP_MODE
  4994. remote = remote - 64;
  4995. #endif
  4996. t_re_re = (1.008 * (remote) - (1.11 - 1.008) * 273.15) / 1.11;
  4997. applog(LOG_DEBUG,"remote : %"PRId16" temp : %f",remote,t_re_re);
  4998. return (int16_t)(t_re_re);
  4999. }
  5000. int16_t get_local(int16_t local)
  5001. {
  5002. #ifdef EXTEND_TEMP_MODE
  5003. local = local - 64;
  5004. #endif
  5005. return local;
  5006. }
  5007. int8_t do_calibration_sensor_offset(unsigned char device,unsigned char chip_addr,int chain, int temp_chip_index)
  5008. {
  5009. int8_t offset = 0;
  5010. int8_t middle,local = 0;
  5011. int16_t ret = 0;
  5012. int8_t error_Limit = 0;
  5013. int8_t retry_Time_Count = 0;
  5014. int get_value_once=0;
  5015. int check_ok_counter=0;
  5016. char logstr[1024];
  5017. ret = check_reg_temp(device, 0xfe, 0x0, 0, chip_addr, chain); // Read Local Temp, Without Any Exception?
  5018. dev->TempChipType[chain][temp_chip_index]= ret & 0xff;
  5019. if(dev->TempChipType[chain][temp_chip_index]==0x1a) //debug for 218
  5020. is218_Temp=true;
  5021. #ifdef EXTEND_TEMP_MODE
  5022. check_reg_temp(device, 0x9, 0x04, 1, chip_addr, chain);
  5023. #endif
  5024. check_reg_temp(device, 0x11, offset, 1, chip_addr, chain); // Set offset
  5025. ret = check_reg_temp(device, 0x0, 0x0, 0, chip_addr, chain); // Read Local Temp, Without Any Exception?
  5026. local = get_local(ret & 0xff);
  5027. ret = check_reg_temp(device, 0x1, 0x0, 0, chip_addr, chain); // Read Remote Temp
  5028. #ifdef EXTEND_TEMP_MODE
  5029. middle = ret & 0xff - 64;
  5030. #else
  5031. middle = ret & 0xff;
  5032. #endif
  5033. offset = calc_offset(middle,local);
  5034. ret = check_reg_temp(device, 0x11, offset, 1, chip_addr, chain); //set offset
  5035. sprintf(logstr,"New offset Chain[%d] chip[%d] local:%hhd remote:%hhd offset:%hhd \n",chain,chip_addr,local,middle,offset);
  5036. writeInitLogFile(logstr);
  5037. return offset;
  5038. }
  5039. #else
  5040. int16_t get_remote(int16_t remote)
  5041. {
  5042. #ifdef EXTEND_TEMP_MODE
  5043. remote = remote - 64;
  5044. #endif
  5045. return remote;
  5046. }
  5047. int16_t get_local(int16_t local)
  5048. {
  5049. #ifdef EXTEND_TEMP_MODE
  5050. local = local - 64;
  5051. #endif
  5052. return local;
  5053. }
  5054. int8_t do_calibration_sensor_offset(unsigned char device,unsigned char chip_addr,int chain, int temp_chip_index)
  5055. {
  5056. int8_t offset = 0xba;
  5057. int8_t middle,local = 0;
  5058. unsigned int ret = 0;
  5059. int8_t error_Limit = 0;
  5060. int8_t retry_Time_Count = 0;
  5061. int get_value_once=0;
  5062. int check_ok_counter=0;
  5063. char logstr[1024];
  5064. ret = check_reg_temp(device, 0xfe, 0x0, 0, chip_addr, chain); // Read Local Temp, Without Any Exception?
  5065. dev->TempChipType[chain][temp_chip_index]= ret & 0xff;
  5066. if(dev->TempChipType[chain][temp_chip_index]==0x1a) //debug for 218
  5067. is218_Temp=true;
  5068. // applog(LOG_NOTICE,"chain %d local:%hhd remote:%hhd offset:%hhd",chain,local,middle,offset);
  5069. do
  5070. {
  5071. ret = check_reg_temp(device, 0x0, 0x0, 0, chip_addr, chain); // Read Local Temp, Without Any Exception?
  5072. local = ret & 0xff;
  5073. if ( retry_Time_Count++ > MAX_RETRY_COUNT )
  5074. {
  5075. // TODO: What To Do?
  5076. applog(LOG_WARNING,"calibration for %d times",retry_Time_Count);
  5077. break;
  5078. }
  5079. ret = check_reg_temp(device, 0x11, offset, 1, chip_addr, chain); // Set offset
  5080. ret = check_reg_temp(device, 0x1, 0x0, 0, chip_addr, chain); // Read Remote Temp
  5081. middle = ret & 0xff;
  5082. if(middle==0 && get_value_once==0)
  5083. {
  5084. error_Limit=MAX_ERROR_LIMIT_ABS+1; //force to do again
  5085. offset = offset + 30; // -70 default is out of temp value.
  5086. }
  5087. else
  5088. {
  5089. get_value_once=1; // sometime, if temp is 0, there is a chance , local=0 , middle=0, but this is not error, middle really is 0, so if we get one value for the first time, then we will not check middle==0 again!!!
  5090. error_Limit = middle - local;
  5091. offset = offset + (local - middle);
  5092. }
  5093. // applog(LOG_NOTICE,"%s chain %d local:%hhd remote:%hhd offset:%hhd", __FUNCTION__,chain,local,middle,offset);
  5094. sprintf(logstr,"Chain[%d] chip[%d] local:%hhd remote:%hhd offset:%hhd \n",chain,chip_addr,local,middle,offset);
  5095. writeInitLogFile(logstr);
  5096. if(abs(error_Limit) <= MAX_ERROR_LIMIT_ABS)
  5097. check_ok_counter++;
  5098. else
  5099. check_ok_counter=0;
  5100. }
  5101. while (check_ok_counter<3);
  5102. return offset;
  5103. }
  5104. #endif
  5105. void set_baud_with_addr(unsigned char bauddiv,int mode,unsigned char chip_addr,int chain,int iic,int open_core,int bottom_or_mid)
  5106. {
  5107. unsigned char buf[9] = {0,0,0,0,0,0,0,0,0};
  5108. unsigned int cmd_buf[3] = {0,0,0};
  5109. unsigned int ret, value,i;
  5110. i = chain;
  5111. //first step: send new bauddiv to ASIC, but FPGA doesn't change its bauddiv, it uses old bauddiv to send BC command to ASIC
  5112. if(!opt_multi_version) // fil mode
  5113. {
  5114. buf[0] = SET_BAUD_OPS;
  5115. buf[1] = 0x10;
  5116. buf[2] = bauddiv & 0x1f;
  5117. buf[0] |= COMMAND_FOR_ALL;
  5118. buf[3] = CRC5(buf, 4*8 - 5);
  5119. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  5120. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  5121. set_BC_command_buffer(cmd_buf);
  5122. ret = get_BC_write_command();
  5123. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  5124. set_BC_write_command(value);
  5125. }
  5126. else // vil mode
  5127. {
  5128. buf[0] = VIL_COMMAND_TYPE | SET_CONFIG;
  5129. if(mode)
  5130. buf[0] = VIL_COMMAND_TYPE | SET_CONFIG |VIL_ALL;
  5131. buf[1] = 0x09;
  5132. buf[2] = chip_addr;
  5133. buf[3] = MISC_CONTROL;
  5134. buf[4] = 0x40;
  5135. if(bottom_or_mid)
  5136. buf[5] = 0x20;
  5137. else
  5138. buf[5] = 0x21;
  5139. if(iic)
  5140. {
  5141. buf[6] = (bauddiv & 0x1f) | 0x40;
  5142. buf[7] = 0x60;
  5143. }
  5144. else
  5145. {
  5146. buf[6] = (bauddiv & 0x1f);
  5147. buf[7] = 0x00;
  5148. }
  5149. if(open_core)
  5150. buf[6] = buf[6]| GATEBCLK;
  5151. buf[8] = 0;
  5152. buf[8] = CRC5(buf, 8*8);
  5153. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  5154. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
  5155. cmd_buf[2] = buf[8]<<24;
  5156. while (1)
  5157. {
  5158. if (((ret = get_BC_write_command()) & 0x80000000) == 0)
  5159. break;
  5160. cgsleep_ms(1);
  5161. }
  5162. set_BC_command_buffer(cmd_buf);
  5163. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  5164. set_BC_write_command(value);
  5165. }
  5166. }
  5167. int8_t calibration_sensor_offset(unsigned char device, int chain)
  5168. {
  5169. int i;
  5170. signed char temp_offset[8];
  5171. unsigned int ret = 0;
  5172. char logstr[1024];
  5173. int8_t middle,local = 0;
  5174. #ifdef T9_18
  5175. if(fpga_version>=0xE)
  5176. {
  5177. if(chain!=8 && chain!=10 && chain!=12) // only chain 8, 10, 12... has temp sensor!!!
  5178. return 0;
  5179. }
  5180. else
  5181. {
  5182. if(chain%3 != 1) // only chain 1, 4, 7... has temp sensor!!!
  5183. return 0;
  5184. }
  5185. #endif
  5186. #ifndef TWO_CHIP_TEMP_S9
  5187. get_temperature_offset_value(chain,temp_offset);
  5188. sprintf(logstr,"Chain[J%d] PIC temp offset=%d,%d,%d,%d,%d,%d,%d,%d\n",chain+1,temp_offset[0],temp_offset[1],temp_offset[2],temp_offset[3],temp_offset[4],temp_offset[5],temp_offset[6],temp_offset[7]);
  5189. writeInitLogFile(logstr);
  5190. dev->chain_asic_temp_num[chain]=0;
  5191. for(i=0; i<4; i++)
  5192. {
  5193. if(temp_offset[2*i]>0)
  5194. {
  5195. dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]]=(temp_offset[2*i]-1)*4;
  5196. middle_Offset[chain][dev->chain_asic_temp_num[chain]] = temp_offset[2*i+1];
  5197. set_baud_with_addr(dev->baud, 0, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]], chain, 1, 0, (int) TEMP_MIDDLE);
  5198. check_asic_reg_with_addr(MISC_CONTROL,dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]],chain,1);
  5199. ret = check_reg_temp(device, 0xfe, 0x0, 0, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]], chain); // Read Local Temp, Without Any Exception?
  5200. dev->TempChipType[chain][dev->chain_asic_temp_num[chain]]= ret & 0xff;
  5201. sprintf(logstr,"Chain[J%d] chip[%d] use PIC middle temp offset=%d typeID=%02x\n",chain+1,dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]],middle_Offset[chain][dev->chain_asic_temp_num[chain]],dev->TempChipType[chain][dev->chain_asic_temp_num[chain]]);
  5202. writeInitLogFile(logstr);
  5203. if(dev->TempChipType[chain][dev->chain_asic_temp_num[chain]]!=0x1a && dev->TempChipType[chain][dev->chain_asic_temp_num[chain]]!=0x55)
  5204. {
  5205. dev->chain_asic_temp_num[chain]=0;
  5206. break; // error, we just jump out
  5207. }
  5208. if(dev->TempChipType[chain][dev->chain_asic_temp_num[chain]]==0x1a) //debug for 218
  5209. is218_Temp=true;
  5210. #ifndef USE_N_OFFSET_FIX_TEMP
  5211. #ifdef EXTEND_TEMP_MODE
  5212. check_reg_temp(device, 0x9, 0x04, 1, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]], chain);
  5213. #endif
  5214. ret = check_reg_temp(device, 0x0, 0x0, 0, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]], chain); // Read Local Temp, Without Any Exception?
  5215. #ifdef EXTEND_TEMP_MODE
  5216. local = ret & 0xff - 64;
  5217. #else
  5218. local = ret & 0xff;
  5219. #endif
  5220. ret = check_reg_temp(device, 0x11, middle_Offset[chain][dev->chain_asic_temp_num[chain]], 1, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]], chain); // Set offset
  5221. ret = check_reg_temp(device, 0x1, 0x0, 0, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]], chain); // Read Remote Temp
  5222. #ifdef EXTEND_TEMP_MODE
  5223. middle = ret & 0xff - 64;
  5224. #else
  5225. middle = ret & 0xff;
  5226. #endif
  5227. if(local-middle > 2 || local-middle < -2) // we allow 2 degree diff
  5228. {
  5229. sprintf(logstr,"Warning: Chain[J%d] use PIC temp offset local=%d > middle=%d, need calculate offset ...!\n",chain+1,local,middle);
  5230. writeInitLogFile(logstr);
  5231. #endif
  5232. middle_Offset[chain][dev->chain_asic_temp_num[chain]] = do_calibration_sensor_offset(device, dev->TempChipAddr[chain][dev->chain_asic_temp_num[chain]],chain,dev->chain_asic_temp_num[chain]);
  5233. sprintf(logstr,"Chain[J%d] chip[%d] get middle temp offset=%d typeID=%02x\n",chain+1,dev->TempChipAddr[chain][i],middle_Offset[chain][dev->chain_asic_temp_num[chain]],dev->TempChipType[chain][dev->chain_asic_temp_num[chain]]);
  5234. writeInitLogFile(logstr);
  5235. #ifndef USE_N_OFFSET_FIX_TEMP
  5236. }
  5237. #endif
  5238. dev->chain_asic_temp_num[chain]++;
  5239. }
  5240. }
  5241. if(dev->chain_asic_temp_num[chain]<=0)
  5242. {
  5243. sprintf(logstr,"Warning: Chain[J%d] has no temp offset in PIC! will fix it\n",chain+1);
  5244. writeInitLogFile(logstr);
  5245. #ifdef R4
  5246. dev->chain_asic_temp_num[chain]=2;
  5247. dev->TempChipAddr[chain][0]=0x8;
  5248. dev->TempChipAddr[chain][1]=0xC;
  5249. #endif
  5250. #ifdef S9_PLUS
  5251. dev->chain_asic_temp_num[chain]=2;
  5252. dev->TempChipAddr[chain][0]=112; // (29-1)* 4
  5253. dev->TempChipAddr[chain][1]=4; // (2-1)* 4
  5254. #endif
  5255. #ifdef S9_63
  5256. dev->chain_asic_temp_num[chain]=1;
  5257. dev->TempChipAddr[chain][0]=0xF4;
  5258. #endif
  5259. #ifdef T9_18
  5260. dev->chain_asic_temp_num[chain]=2;
  5261. dev->TempChipAddr[chain][0]=0; // (1-1)* 4
  5262. dev->TempChipAddr[chain][1]=32; // (9-1)* 4
  5263. #endif
  5264. // Set Each Temp Offset
  5265. // 0. Switch To Middle
  5266. // 1. Calibration
  5267. for(i=0; i<dev->chain_asic_temp_num[chain]; i++)
  5268. {
  5269. set_baud_with_addr(dev->baud, 0, dev->TempChipAddr[chain][i], chain, 1, 0, (int) TEMP_MIDDLE);
  5270. check_asic_reg_with_addr(MISC_CONTROL,dev->TempChipAddr[chain][i],chain,1);
  5271. middle_Offset[chain][i] = do_calibration_sensor_offset(device, dev->TempChipAddr[chain][i],chain,i);
  5272. sprintf(logstr,"Chain[J%d] chip[%d] get middle temp offset=%d typeID=%02x\n",chain+1,dev->TempChipAddr[chain][i],middle_Offset[chain][i],dev->TempChipType[chain][i]);
  5273. writeInitLogFile(logstr);
  5274. }
  5275. }
  5276. #else
  5277. dev->chain_asic_temp_num[chain]=2;
  5278. dev->TempChipAddr[chain][0]=0xF4;
  5279. dev->TempChipAddr[chain][1]=0x60;
  5280. // Set Each Temp Offset
  5281. // 0. Switch To Middle
  5282. // 1. Calibration
  5283. for(i=0; i<dev->chain_asic_temp_num[chain]; i++)
  5284. {
  5285. set_baud_with_addr(dev->baud, 0, dev->TempChipAddr[chain][i], chain, 1, 0, (int) TEMP_MIDDLE);
  5286. check_asic_reg_with_addr(MISC_CONTROL,dev->TempChipAddr[chain][i],chain,1);
  5287. middle_Offset[chain][i] = do_calibration_sensor_offset(device, dev->TempChipAddr[chain][i],chain,i);
  5288. sprintf(logstr,"Chain[J%d] chip[%d] get middle temp offset=%d typeID=%02x\n",chain+1,dev->TempChipAddr[chain][i],middle_Offset[chain][i],dev->TempChipType[chain][i]);
  5289. writeInitLogFile(logstr);
  5290. }
  5291. #endif
  5292. #ifdef SHOW_BOTTOM_TEMP
  5293. // 2. Switch To Bottom
  5294. for(i=0; i<dev->chain_asic_temp_num[chain]; i++)
  5295. {
  5296. set_baud_with_addr(dev->baud, 0, dev->TempChipAddr[chain][i], chain, 1, 0, (int) TEMP_BOTTOM);
  5297. check_asic_reg_with_addr(MISC_CONTROL,dev->TempChipAddr[chain][i],chain,1);
  5298. bottom_Offset[chain][i] = do_calibration_sensor_offset(device, dev->TempChipAddr[chain][i],chain,i);
  5299. sprintf(logstr,"Chain[J%d] chip[%d] get bottom temp offset=%d\n",chain+1,dev->TempChipAddr[chain][i],bottom_Offset[chain][i]);
  5300. writeInitLogFile(logstr);
  5301. }
  5302. #endif
  5303. return 0;
  5304. }
  5305. void clearTempLogFile()
  5306. {
  5307. FILE *fd;
  5308. fd=fopen("/tmp/temp","w");
  5309. if(fd)
  5310. {
  5311. fclose(fd);
  5312. }
  5313. }
  5314. void writeLogFile(char *logstr)
  5315. {
  5316. FILE *fd;
  5317. fd=fopen("/tmp/temp","a+");
  5318. if(fd)
  5319. {
  5320. fwrite(logstr,1,strlen(logstr),fd);
  5321. fclose(fd);
  5322. }
  5323. }
  5324. void updateLogFile()
  5325. {
  5326. system("cp /tmp/temp /tmp/lasttemp");
  5327. }
  5328. void saveTestID(int testID)
  5329. {
  5330. FILE *fd;
  5331. char testnumStr[32];
  5332. fd=fopen("/etc/config/testID","wb");
  5333. if(fd)
  5334. {
  5335. memset(testnumStr,'\0',sizeof(testnumStr));
  5336. sprintf(testnumStr,"%d",testID);
  5337. fwrite(testnumStr,1,sizeof(testnumStr),fd);
  5338. fclose(fd);
  5339. }
  5340. }
  5341. int readTestID()
  5342. {
  5343. FILE *fd;
  5344. char testnumStr[32];
  5345. fd=fopen("/etc/config/testID","rb");
  5346. if(fd)
  5347. {
  5348. memset(testnumStr,'\0',sizeof(testnumStr));
  5349. fread(testnumStr,1,sizeof(testnumStr),fd);
  5350. fclose(fd);
  5351. return atoi(testnumStr);
  5352. }
  5353. return 0;
  5354. }
  5355. void do8xPattenTest()
  5356. {
  5357. int i=0;
  5358. doTestPatten=true;
  5359. startCheckNetworkJob=false;
  5360. pthread_mutex_lock(&reinit_mutex);
  5361. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  5362. sleep(3);
  5363. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  5364. sleep(2);
  5365. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5366. {
  5367. if(dev->chain_exist[i] == 1)
  5368. {
  5369. #ifdef T9_18
  5370. memcpy(chain_pic_buf[i],chip_last_freq[i],128); // restore the real freq for chips
  5371. #else
  5372. memcpy(last_freq[i],chip_last_freq[i],256); // restore the real freq for chips
  5373. #endif
  5374. }
  5375. }
  5376. set_asic_ticket_mask(0);
  5377. clement_doTestBoardOnce(true);
  5378. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5379. {
  5380. if(dev->chain_exist[i] == 1)
  5381. {
  5382. #ifdef T9_18
  5383. memcpy(chain_pic_buf[i],show_last_freq[i],128); // restore the user freq for showed on web for users
  5384. #else
  5385. memcpy(last_freq[i],show_last_freq[i],256); // restore the user freq for showed on web for users
  5386. #endif
  5387. }
  5388. }
  5389. // must re-set these two address to FPGA
  5390. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  5391. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  5392. #ifndef CAPTURE_PATTEN
  5393. set_asic_ticket_mask(63); // clement
  5394. cgsleep_ms(10);
  5395. #endif
  5396. set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() | FLUSH_NONCE3_FIFO);
  5397. clear_nonce_fifo();
  5398. //set real timeout back
  5399. if(opt_multi_version)
  5400. set_time_out_control(((dev->timeout * opt_multi_version) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  5401. else
  5402. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  5403. doTestPatten=false;
  5404. pthread_mutex_unlock(&reinit_mutex);
  5405. re_send_last_job();
  5406. cgtime(&tv_send_job);
  5407. cgtime(&tv_send);
  5408. startCheckNetworkJob=true;
  5409. }
  5410. void bitmain_reinit_test()
  5411. {
  5412. char ret=0,j;
  5413. uint16_t crc = 0;
  5414. int i=0,x = 0,y = 0;
  5415. int hardware_version;
  5416. unsigned int data = 0;
  5417. bool testRet;
  5418. int testCounter=0;
  5419. char logstr[1024];
  5420. pthread_mutex_lock(&iic_mutex);
  5421. // clear all dev values
  5422. memset(dev,0x00,sizeof(struct all_parameters));
  5423. dev->current_job_start_address = job_start_address_1;
  5424. #ifdef USE_NEW_RESET_FPGA
  5425. set_reset_allhashboard(1);
  5426. sleep(RESET_KEEP_TIME);
  5427. set_reset_allhashboard(0);
  5428. sleep(1);
  5429. set_reset_allhashboard(1);
  5430. #endif
  5431. //reset FPGA & HASH board
  5432. {
  5433. set_QN_write_data_command(RESET_HASH_BOARD | RESET_ALL | RESET_FPGA | RESET_TIME(RESET_HASHBOARD_TIME));
  5434. #ifdef USE_NEW_RESET_FPGA
  5435. sleep(2);
  5436. #else
  5437. while(get_QN_write_data_command() & RESET_HASH_BOARD)
  5438. {
  5439. cgsleep_ms(30);
  5440. }
  5441. cgsleep_ms(500);
  5442. #endif
  5443. set_PWM(MAX_PWM_PERCENT);
  5444. }
  5445. #ifdef T9_18
  5446. // config fpga into T9+ mode
  5447. set_Hardware_version(0x80000000);
  5448. #endif
  5449. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  5450. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  5451. //check chain
  5452. check_chain();
  5453. #ifdef USE_NEW_RESET_FPGA
  5454. set_reset_allhashboard(1);
  5455. #endif
  5456. #ifdef T9_18
  5457. // close DC, T9_18 only can reset PIC to close DC, because of enable_pic_dAc FUNC can not be called for more than 1 time. it cause PIC error with IIC communication
  5458. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5459. {
  5460. if(dev->chain_exist[i] == 1)
  5461. {
  5462. reset_iic_pic(i);
  5463. jump_to_app_CheckAndRestorePIC_T9_18(i);
  5464. }
  5465. }
  5466. sleep(1);
  5467. #endif
  5468. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5469. {
  5470. if(dev->chain_exist[i] == 1)
  5471. {
  5472. #ifdef ENABLE_HIGH_VOLTAGE_OPENCORE
  5473. unsigned char vol_pic;
  5474. chain_voltage_pic[i] = get_pic_voltage(i); // read orignal voltage at first!
  5475. vol_pic=getPICvoltageFromValue(HIGHEST_VOLTAGE_LIMITED_HW);
  5476. // sprintf(logstr,"Chain[J%d] will use highest voltage=%d [%d] to open core\n",i+1,HIGHEST_VOLTAGE_LIMITED_HW,vol_pic);
  5477. // writeInitLogFile(logstr);
  5478. #ifdef T9_18
  5479. if(fpga_version>=0xE)
  5480. {
  5481. if(i>=1 && i<=3)
  5482. set_voltage_T9_18_into_PIC(i, vol_pic);
  5483. }
  5484. else
  5485. {
  5486. if(i%3==0)
  5487. set_voltage_T9_18_into_PIC(i, vol_pic);
  5488. }
  5489. #else
  5490. set_pic_voltage(i, vol_pic);
  5491. #endif
  5492. #endif
  5493. #ifndef T9_18
  5494. disable_pic_dac(i);
  5495. #endif
  5496. }
  5497. }
  5498. cgsleep_ms(1000);
  5499. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5500. {
  5501. if(dev->chain_exist[i] == 1)
  5502. {
  5503. int vol_value;
  5504. unsigned char vol_pic;
  5505. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  5506. chain_voltage_pic[i] = get_pic_voltage(i);
  5507. #endif
  5508. vol_value = getVolValueFromPICvoltage(chain_voltage_pic[i]);
  5509. sprintf(logstr,"Chain[J%d] working voltage=%d value=%d\n",i+1,chain_voltage_pic[i],vol_value);
  5510. writeInitLogFile(logstr);
  5511. #ifdef T9_18
  5512. if(getChainPICMagicNumber(i) == FREQ_MAGIC)
  5513. #else
  5514. if(last_freq[i][1] == FREQ_MAGIC && last_freq[i][40] == 0x23) //0x23 is backup voltage magic number
  5515. #endif
  5516. {
  5517. if(vol_value < chain_voltage_value[i])
  5518. {
  5519. vol_pic=getPICvoltageFromValue(chain_voltage_value[i]);
  5520. sprintf(logstr,"Chain[J%d] will use backup chain_voltage_pic=%d [%d]\n",i+1,chain_voltage_value[i],vol_pic);
  5521. writeInitLogFile(logstr);
  5522. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  5523. set_pic_voltage(i, vol_pic);
  5524. chain_voltage_pic[i] = get_pic_voltage(i);
  5525. #else
  5526. chain_voltage_pic[i] = vol_pic;
  5527. #endif
  5528. sprintf(logstr,"Chain[J%d] get working voltage=%d\n",i+1,chain_voltage_pic[i]);
  5529. writeInitLogFile(logstr);
  5530. }
  5531. }
  5532. }
  5533. }
  5534. #ifdef T9_18
  5535. // set voltage and enable it for only once!!!
  5536. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5537. {
  5538. if(dev->chain_exist[i] == 1)
  5539. {
  5540. set_pic_voltage(i, 0); // the second parameter is not used, we set 0 for T9_18
  5541. enable_pic_dac(i);
  5542. }
  5543. }
  5544. sleep(5); // wait for sometime , voltage need time to prepare!!!
  5545. #else
  5546. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5547. {
  5548. if(dev->chain_exist[i] == 1)
  5549. {
  5550. enable_pic_dac(i);
  5551. }
  5552. }
  5553. #endif
  5554. pthread_mutex_unlock(&iic_mutex);
  5555. cgsleep_ms(2000);
  5556. #ifdef USE_NEW_RESET_FPGA
  5557. set_reset_allhashboard(1);
  5558. sleep(RESET_KEEP_TIME);
  5559. set_reset_allhashboard(0);
  5560. sleep(1);
  5561. #else
  5562. set_QN_write_data_command(RESET_HASH_BOARD | RESET_ALL | RESET_TIME(RESET_HASHBOARD_TIME));
  5563. while(get_QN_write_data_command() & RESET_HASH_BOARD)
  5564. {
  5565. cgsleep_ms(30);
  5566. }
  5567. cgsleep_ms(1000);
  5568. #endif
  5569. if(opt_multi_version)
  5570. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE) | VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version) & (~NEW_BLOCK) & (~RUN_BIT));
  5571. cgsleep_ms(10);
  5572. //set core number
  5573. dev->corenum = BM1387_CORE_NUM;
  5574. #ifdef USE_PREINIT_OPENCORE
  5575. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5576. {
  5577. if(dev->chain_exist[i] == 1)
  5578. {
  5579. getAsicNum_preOpenCore(i);
  5580. sprintf(logstr,"Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  5581. writeInitLogFile(logstr);
  5582. if(dev->chain_asic_num[i] != CHAIN_ASIC_NUM && readRebootTestNum()>0)
  5583. {
  5584. char error_info[256];
  5585. sprintf(error_info,"J%d:3",i+1);
  5586. saveSearchFailedFlagInfo(error_info);
  5587. system("reboot");
  5588. }
  5589. if(dev->chain_asic_num[i]<=0)
  5590. {
  5591. dev->chain_exist[i]=0;
  5592. }
  5593. }
  5594. }
  5595. #else
  5596. //check ASIC number for every chain
  5597. check_asic_reg(CHIP_ADDRESS);
  5598. cgsleep_ms(10);
  5599. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5600. {
  5601. if(dev->chain_exist[i] == 1)
  5602. {
  5603. int retry_count=0;
  5604. sprintf(logstr,"Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  5605. writeInitLogFile(logstr);
  5606. #ifndef T9_18
  5607. while(dev->chain_asic_num[i] != CHAIN_ASIC_NUM && retry_count<6)
  5608. {
  5609. dev->chain_asic_num[i]=0;
  5610. #ifdef USE_NEW_RESET_FPGA
  5611. set_reset_hashboard(i,1);
  5612. #endif
  5613. pthread_mutex_lock(&iic_mutex);
  5614. disable_pic_dac(i);
  5615. pthread_mutex_unlock(&iic_mutex);
  5616. sleep(1);
  5617. pthread_mutex_lock(&iic_mutex);
  5618. enable_pic_dac(i);
  5619. pthread_mutex_unlock(&iic_mutex);
  5620. sleep(2);
  5621. #ifdef USE_NEW_RESET_FPGA
  5622. set_reset_hashboard(i,0);
  5623. sleep(1);
  5624. #else
  5625. reset_one_hashboard(i);
  5626. #endif
  5627. check_asic_reg_oneChain(i,CHIP_ADDRESS);
  5628. retry_count++;
  5629. }
  5630. #endif
  5631. if(dev->chain_asic_num[i]<=0)
  5632. dev->chain_exist[i]=0;
  5633. sprintf(logstr,"retry Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  5634. writeInitLogFile(logstr);
  5635. }
  5636. }
  5637. #endif
  5638. software_set_address();
  5639. cgsleep_ms(10);
  5640. if(config_parameter.frequency_eft)
  5641. {
  5642. dev->frequency = config_parameter.frequency;
  5643. set_frequency(dev->frequency);
  5644. sprintf(dev->frequency_t,"%u",dev->frequency);
  5645. }
  5646. cgsleep_ms(10);
  5647. //check who control fan
  5648. dev->fan_eft = config_parameter.fan_eft;
  5649. dev->fan_pwm= config_parameter.fan_pwm_percent;
  5650. applog(LOG_DEBUG,"%s: fan_eft : %d fan_pwm : %d\n", __FUNCTION__,dev->fan_eft,dev->fan_pwm);
  5651. if(config_parameter.fan_eft)
  5652. {
  5653. if((config_parameter.fan_pwm_percent >= 0) && (config_parameter.fan_pwm_percent <= 100))
  5654. {
  5655. set_PWM(config_parameter.fan_pwm_percent);
  5656. }
  5657. else
  5658. {
  5659. set_PWM_according_to_temperature();
  5660. }
  5661. }
  5662. else
  5663. {
  5664. set_PWM_according_to_temperature();
  5665. }
  5666. //calculate real timeout
  5667. if(config_parameter.timeout_eft)
  5668. {
  5669. if(config_parameter.timeout_data_integer == 0 && config_parameter.timeout_data_fractions == 0) //driver calculate out timeout value
  5670. {
  5671. // clement change to 70/100 org: 90/100
  5672. #ifdef CAPTURE_PATTEN
  5673. dev->timeout = 0x1000000/calculate_core_number(dev->corenum)*dev->addrInterval/(dev->frequency)*30/100;
  5674. #else
  5675. dev->timeout = 0x1000000/calculate_core_number(dev->corenum)*dev->addrInterval/(dev->frequency)*90/100;
  5676. #endif
  5677. // for set_freq_auto test,set timeout when frequency equals 700M
  5678. // dev->timeout = 0x1000000/calculate_core_number(dev->corenum)*dev->addrInterval/700*90/100;
  5679. applog(LOG_DEBUG,"dev->timeout = %d\n", dev->timeout);
  5680. }
  5681. else
  5682. {
  5683. dev->timeout = config_parameter.timeout_data_integer * 1000 + config_parameter.timeout_data_fractions;
  5684. }
  5685. if(dev->timeout > MAX_TIMEOUT_VALUE)
  5686. {
  5687. dev->timeout = MAX_TIMEOUT_VALUE;
  5688. }
  5689. }
  5690. //set baud
  5691. init_uart_baud();
  5692. cgsleep_ms(10);
  5693. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5694. {
  5695. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i] == CHAIN_ASIC_NUM)
  5696. {
  5697. calibration_sensor_offset(0x98,i);
  5698. cgsleep_ms(10);
  5699. }
  5700. }
  5701. #ifdef T9_18
  5702. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5703. {
  5704. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i] == CHAIN_ASIC_NUM)
  5705. {
  5706. if(fpga_version>=0xE)
  5707. {
  5708. switch(i) // only chain 8, 10, 12... has temp sensor!!! we just copy to other chains.
  5709. {
  5710. case 1:
  5711. case 9:
  5712. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[8];
  5713. dev->TempChipAddr[i][0]=dev->TempChipAddr[8][0];
  5714. dev->TempChipAddr[i][1]=dev->TempChipAddr[8][1];
  5715. break;
  5716. case 2:
  5717. case 11:
  5718. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[10];
  5719. dev->TempChipAddr[i][0]=dev->TempChipAddr[10][0];
  5720. dev->TempChipAddr[i][1]=dev->TempChipAddr[10][1];
  5721. break;
  5722. case 3:
  5723. case 13:
  5724. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[12];
  5725. dev->TempChipAddr[i][0]=dev->TempChipAddr[12][0];
  5726. dev->TempChipAddr[i][1]=dev->TempChipAddr[12][1];
  5727. break;
  5728. }
  5729. }
  5730. else
  5731. {
  5732. if(i%3 != 1) // only chain 1, 4, 7... has temp sensor!!! we just copy chain[1] temp info into chain[0] and chain[2]
  5733. {
  5734. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[((i/3)*3)+1];
  5735. dev->TempChipAddr[i][0]=dev->TempChipAddr[((i/3)*3)+1][0];
  5736. dev->TempChipAddr[i][1]=dev->TempChipAddr[((i/3)*3)+1][1];
  5737. }
  5738. }
  5739. }
  5740. }
  5741. #endif
  5742. //set big timeout value for open core
  5743. //set_time_out_control((MAX_TIMEOUT_VALUE - 100) | TIME_OUT_VALID);
  5744. set_time_out_control(0xc350 | TIME_OUT_VALID);
  5745. #ifdef ENABLE_HIGH_VOLTAGE_OPENCORE
  5746. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5747. {
  5748. if(dev->chain_exist[i] == 1)
  5749. {
  5750. #ifdef USE_OPENCORE_ONEBYONE
  5751. opencore_onebyone_onChain(i);
  5752. #else
  5753. open_core_one_chain(i,true);
  5754. #endif
  5755. sleep(1);
  5756. #ifdef T9_18
  5757. if(fpga_version>=0xE)
  5758. {
  5759. if(i==1)
  5760. {
  5761. // we must try open core on chain [8] and chain[9] ...
  5762. #ifdef USE_OPENCORE_ONEBYONE
  5763. opencore_onebyone_onChain(8);
  5764. sleep(1);
  5765. opencore_onebyone_onChain(9);
  5766. sleep(1);
  5767. #else
  5768. open_core_one_chain(8,true);
  5769. sleep(1);
  5770. open_core_one_chain(9,true);
  5771. sleep(1);
  5772. #endif
  5773. }
  5774. else if(i==2)
  5775. {
  5776. #ifdef USE_OPENCORE_ONEBYONE
  5777. opencore_onebyone_onChain(10);
  5778. sleep(1);
  5779. opencore_onebyone_onChain(11);
  5780. sleep(1);
  5781. #else
  5782. open_core_one_chain(10,true);
  5783. sleep(1);
  5784. open_core_one_chain(11,true);
  5785. sleep(1);
  5786. #endif
  5787. }
  5788. else if(i==3)
  5789. {
  5790. #ifdef USE_OPENCORE_ONEBYONE
  5791. opencore_onebyone_onChain(12);
  5792. sleep(1);
  5793. opencore_onebyone_onChain(13);
  5794. sleep(1);
  5795. #else
  5796. open_core_one_chain(12,true);
  5797. sleep(1);
  5798. open_core_one_chain(13,true);
  5799. sleep(1);
  5800. #endif
  5801. }
  5802. else break; // we jump out of open core loop. because we have done open core above!!!
  5803. pthread_mutex_lock(&iic_mutex);
  5804. set_pic_voltage(i, chain_voltage_pic[i]);
  5805. pthread_mutex_unlock(&iic_mutex);
  5806. sprintf(logstr,"Chain[J%d] set working voltage=%d [%d]\n",i+1,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  5807. writeInitLogFile(logstr);
  5808. }
  5809. else
  5810. {
  5811. if(i%3==2) // only set working voltage when open core done on last chain of 3 chains!!!
  5812. {
  5813. pthread_mutex_lock(&iic_mutex);
  5814. set_pic_voltage(i, chain_voltage_pic[i]);
  5815. pthread_mutex_unlock(&iic_mutex);
  5816. }
  5817. sprintf(logstr,"Chain[J%d] set working voltage=%d [%d]\n",i+1,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  5818. writeInitLogFile(logstr);
  5819. }
  5820. #else
  5821. pthread_mutex_lock(&iic_mutex);
  5822. // restore the normal voltage
  5823. set_pic_voltage(i, chain_voltage_pic[i]);
  5824. pthread_mutex_unlock(&iic_mutex);
  5825. sprintf(logstr,"Chain[J%d] set working voltage=%d [%d]\n",i+1,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  5826. writeInitLogFile(logstr);
  5827. #endif
  5828. }
  5829. }
  5830. #else
  5831. open_core(true);
  5832. #endif
  5833. //set real timeout back
  5834. if(opt_multi_version)
  5835. set_time_out_control(((dev->timeout * opt_multi_version) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  5836. else
  5837. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  5838. }
  5839. void doReInitTest()
  5840. {
  5841. int i;
  5842. doTestPatten=true;
  5843. startCheckNetworkJob=false;
  5844. pthread_mutex_lock(&reinit_mutex);
  5845. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  5846. sleep(3);
  5847. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  5848. sleep(2);
  5849. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5850. {
  5851. if(dev->chain_exist[i] == 1)
  5852. {
  5853. #ifdef T9_18
  5854. memcpy(chain_pic_buf[i],show_last_freq[i],128); // restore the user freq for showed on web for users
  5855. #else
  5856. memcpy(last_freq[i],show_last_freq[i],256); // restore the user freq for showed on web for users
  5857. #endif
  5858. }
  5859. }
  5860. // must re-set these two address to FPGA
  5861. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  5862. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  5863. doTestPatten=false;
  5864. bitmain_reinit_test();
  5865. doTestPatten=true;
  5866. // must re-set these two address to FPGA
  5867. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  5868. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  5869. #ifndef CAPTURE_PATTEN
  5870. set_asic_ticket_mask(63); // clement
  5871. cgsleep_ms(10);
  5872. #endif
  5873. set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() | FLUSH_NONCE3_FIFO);
  5874. clear_nonce_fifo();
  5875. //set real timeout back
  5876. if(opt_multi_version)
  5877. set_time_out_control(((dev->timeout * opt_multi_version) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  5878. else
  5879. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  5880. doTestPatten=false;
  5881. pthread_mutex_unlock(&reinit_mutex);
  5882. re_send_last_job();
  5883. cgtime(&tv_send_job);
  5884. cgtime(&tv_send);
  5885. startCheckNetworkJob=true;
  5886. }
  5887. void processTEST()
  5888. {
  5889. char logstr[1024];
  5890. int testID=readTestID();
  5891. int chainIndex;
  5892. int i;
  5893. int cur_vol_pic,cur_vol_value,set_vol_value;
  5894. if(testID==11)
  5895. {
  5896. // test : do 8xPatten test
  5897. saveTestID(0);
  5898. sprintf(logstr,"get TEST ID=%d do 8xPatten test\n",testID);
  5899. writeInitLogFile(logstr);
  5900. do8xPattenTest();
  5901. }
  5902. else if(testID==12)
  5903. {
  5904. // test : do open core
  5905. saveTestID(0);
  5906. sprintf(logstr,"get TEST ID=%d do bitmain_core_reInit test\n",testID);
  5907. writeInitLogFile(logstr);
  5908. bitmain_core_reInit();
  5909. reCalculateAVG();
  5910. sprintf(logstr,"Done bitmain_core_reInit test\n");
  5911. writeInitLogFile(logstr);
  5912. }
  5913. else if(testID==13)
  5914. {
  5915. // test : do re-init
  5916. saveTestID(0);
  5917. sprintf(logstr,"get TEST ID=%d do doReInitTest test\n",testID);
  5918. writeInitLogFile(logstr);
  5919. doReInitTest();
  5920. reCalculateAVG();
  5921. sprintf(logstr,"Done doReInitTest test\n");
  5922. writeInitLogFile(logstr);
  5923. }
  5924. else if(testID==14)
  5925. {
  5926. // test : do get asicnum
  5927. saveTestID(0);
  5928. sprintf(logstr,"get TEST ID=%d do do get asicnum\n",testID);
  5929. writeInitLogFile(logstr);
  5930. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  5931. {
  5932. if(dev->chain_exist[i] == 1)
  5933. {
  5934. dev->chain_asic_num[i]=0;
  5935. check_asic_reg_oneChain(i,CHIP_ADDRESS);
  5936. sprintf(logstr,"Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  5937. writeInitLogFile(logstr);
  5938. }
  5939. }
  5940. sprintf(logstr,"Done do get asicnum\n");
  5941. writeInitLogFile(logstr);
  5942. }
  5943. else if(testID>=101 && testID<=116)
  5944. {
  5945. // force to add voltage 0.1V on one board
  5946. saveTestID(0);
  5947. chainIndex=(testID%100)-1;
  5948. sprintf(logstr,"get TEST ID=%d up voltage 0.1V on Chain[J%d]\n",testID,chainIndex+1);
  5949. writeInitLogFile(logstr);
  5950. i=chainIndex;
  5951. {
  5952. if(dev->chain_exist[i] == 1)
  5953. {
  5954. cur_vol_pic = get_pic_voltage(i);
  5955. cur_vol_value = getVolValueFromPICvoltage(cur_vol_pic);
  5956. if(cur_vol_value+10>940)
  5957. {
  5958. sprintf(logstr,"Chain[J%d] current vol=%d , too high! will set to 940\n",i+1,cur_vol_value);
  5959. writeInitLogFile(logstr);
  5960. set_vol_value=940;
  5961. }
  5962. else set_vol_value=cur_vol_value+10;
  5963. sprintf(logstr,"Try to up 0.1V on chain[J%d] from vol=%d to %d...\n",i+1,cur_vol_value,set_vol_value);
  5964. writeInitLogFile(logstr);
  5965. cur_vol_pic=getPICvoltageFromValue(set_vol_value);
  5966. sprintf(logstr,"now set pic voltage=%d on chain[J%d]\n",cur_vol_pic,i+1);
  5967. writeInitLogFile(logstr);
  5968. set_pic_voltage(i, cur_vol_pic);
  5969. }
  5970. else
  5971. {
  5972. sprintf(logstr,"There is hashboard on Chain[J%d]\n",i+1);
  5973. writeInitLogFile(logstr);
  5974. }
  5975. }
  5976. }
  5977. else if(testID>=201 && testID<=216)
  5978. {
  5979. // force to decrease voltage 0.1V on one board
  5980. saveTestID(0);
  5981. chainIndex=(testID%100)-1;
  5982. sprintf(logstr,"get TEST ID=%d down voltage 0.1V on Chain[J%d]\n",testID, chainIndex+1);
  5983. writeInitLogFile(logstr);
  5984. i=chainIndex;
  5985. {
  5986. if(dev->chain_exist[i] == 1)
  5987. {
  5988. cur_vol_pic = get_pic_voltage(i);
  5989. cur_vol_value = getVolValueFromPICvoltage(cur_vol_pic);
  5990. if(cur_vol_value-10<860)
  5991. {
  5992. sprintf(logstr,"Chain[J%d] current vol=%d , too low! will set to 860\n",i+1,cur_vol_value);
  5993. writeInitLogFile(logstr);
  5994. set_vol_value=860;
  5995. }
  5996. else set_vol_value=cur_vol_value-10;
  5997. sprintf(logstr,"Try to down 0.1V on chain[J%d] from vol=%d to %d...\n",i+1,cur_vol_value,set_vol_value);
  5998. writeInitLogFile(logstr);
  5999. cur_vol_pic=getPICvoltageFromValue(set_vol_value);
  6000. sprintf(logstr,"now set pic voltage=%d on chain[J%d]\n",cur_vol_pic,i+1);
  6001. writeInitLogFile(logstr);
  6002. set_pic_voltage(i, cur_vol_pic);
  6003. }
  6004. else
  6005. {
  6006. sprintf(logstr,"There is hashboard on Chain[J%d]\n",i+1);
  6007. writeInitLogFile(logstr);
  6008. }
  6009. }
  6010. }
  6011. }
  6012. int fakeMiddleTempFromPCB(int local_temp)
  6013. {
  6014. if(local_temp<=0)
  6015. return 0;
  6016. else if(local_temp<=50)
  6017. return local_temp+25;
  6018. else if(local_temp<=60)
  6019. return local_temp+30;
  6020. else
  6021. return local_temp+35;
  6022. }
  6023. #define OFFSIDE_TOP 125
  6024. #define OFFSIDE_LOW 75
  6025. void * read_temp_func()
  6026. {
  6027. char logstr[1024];
  6028. char error_info[256];
  6029. struct timeval diff;
  6030. int i,j;
  6031. unsigned int ret = 0;
  6032. unsigned int ret0 = 0;
  6033. unsigned int ret1 = 0;
  6034. unsigned int ret2 = 0;
  6035. unsigned int ret3 = 0;
  6036. int chain_asic_temp_error[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM]= {0};
  6037. int16_t temp_top[TEMP_POS_NUM];
  6038. int16_t temp_low[TEMP_POS_NUM];
  6039. bool already_offside = false;
  6040. Temp_Type_E temp_Type = TEMP_MIDDLE;
  6041. int maxtemp[TEMP_POS_NUM];
  6042. int mintemp[TEMP_POS_NUM];
  6043. int cur_fan_num=0;
  6044. int fatal_error_counter=0;
  6045. clearTempLogFile();
  6046. // sprintf(logstr,"start read_temp_func ...\n");
  6047. // writeLogFile(logstr);
  6048. while(1)
  6049. {
  6050. #if 0 //def DEBUG_XILINX_NONCE_NOTENOUGH
  6051. // only change fan speed after read temp value!!!
  6052. check_fan();
  6053. set_PWM(MAX_PWM_PERCENT);
  6054. dev->fan_pwm = MAX_PWM_PERCENT;
  6055. sleep(10);
  6056. continue;
  6057. #endif
  6058. #ifndef KEEP_TEMPFAN_LOG
  6059. clearTempLogFile();
  6060. #endif
  6061. sprintf(logstr,"do read_temp_func once...\n");
  6062. writeLogFile(logstr);
  6063. pthread_mutex_lock(&opencore_readtemp_mutex);
  6064. sprintf(logstr,"do check_asic_reg 0x08\n");
  6065. writeLogFile(logstr);
  6066. // read hashrate RT
  6067. if(doTestPatten)
  6068. usleep(100000);
  6069. else
  6070. {
  6071. // sprintf(logstr,"call check_asic_reg(0x08)\n");
  6072. // writeLogFile(logstr);
  6073. if(!check_asic_reg(0x08))
  6074. {
  6075. sprintf(logstr,"Error: check_asic_reg 0x08 timeout\n");
  6076. writeInitLogFile(logstr);
  6077. }
  6078. else showAllBadRTInfo();
  6079. }
  6080. sprintf(logstr,"Done check_asic_reg\n");
  6081. writeLogFile(logstr);
  6082. memset(temp_top,0x00,sizeof(temp_top));
  6083. memset(temp_low,0x00,sizeof(temp_low));
  6084. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  6085. {
  6086. if(dev->chain_exist[i] == 1
  6087. #ifdef DEBUG_XILINX_NONCE_NOTENOUGH
  6088. && i!=DISABLE_REG_CHAIN_INDEX
  6089. #endif
  6090. ) //clement for debug
  6091. {
  6092. #ifdef T9_18
  6093. if(fpga_version>=0xE)
  6094. {
  6095. if(i!=8 && i!=10 && i!=12) // only 8,10,12... has temp sensor!!! we just copy temp value to other chains!
  6096. continue;
  6097. }
  6098. else
  6099. {
  6100. if(i%3 != 1) // only 1,4,7... has temp sensor!!! we just copy temp value to other chains!
  6101. continue;
  6102. }
  6103. #endif
  6104. sprintf(logstr,"do read temp on Chain[%d]\n",i);
  6105. writeLogFile(logstr);
  6106. maxtemp[TEMP_POS_LOCAL]=0;
  6107. maxtemp[TEMP_POS_MIDDLE]=0;
  6108. maxtemp[TEMP_POS_BOTTOM]=0;
  6109. mintemp[TEMP_POS_LOCAL]=1000;
  6110. mintemp[TEMP_POS_MIDDLE]=1000;
  6111. mintemp[TEMP_POS_BOTTOM]=1000; // set 1000, as init value
  6112. for(j=0; j<dev->chain_asic_temp_num[i]; j++)
  6113. {
  6114. // sprintf(logstr,"do read temp chip[%d] addr=%d on Chain[%d]\n",j,TempChipAddr[j],i);
  6115. // writeLogFile(logstr);
  6116. sprintf(logstr,"Chain[%d] Chip[%d] TempTypeID=%02x middle offset=%d\n",i, (dev->TempChipAddr[i][j]/4)+1, dev->TempChipType[i][j],middle_Offset[i][j]);
  6117. writeLogFile(logstr);
  6118. ret = check_reg_temp(0x98, 0x00, 0x0, 0x0, dev->TempChipAddr[i][j], i);
  6119. if (ret != 0)
  6120. {
  6121. dev->chain_asic_temp[i][j][TEMP_POS_LOCAL] = get_local(ret & 0xff);
  6122. sprintf(logstr,"Chain[%d] Chip[%d] local Temp=%d\n",i, (dev->TempChipAddr[i][j]/4)+1, dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]);
  6123. writeLogFile(logstr);
  6124. }
  6125. else
  6126. {
  6127. sprintf(logstr,"read failed, old value: Chain[%d] Chip[%d] local Temp=%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]);
  6128. writeLogFile(logstr);
  6129. }
  6130. // 0. Switch To Middle
  6131. set_baud_with_addr(dev->baud, 0, dev->TempChipAddr[i][j], i, 1, 0, (int) TEMP_MIDDLE);
  6132. check_asic_reg_with_addr(MISC_CONTROL,dev->TempChipAddr[i][j],i,1);
  6133. #if ((!defined USE_N_OFFSET_FIX_TEMP) && (!defined EXTEND_TEMP_MODE))
  6134. if(dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]<125)
  6135. {
  6136. middle_Offset_sw[i][j]=0;
  6137. }
  6138. // set middle offset
  6139. if(middle_Offset_sw[i][j]!=0)
  6140. {
  6141. ret = check_reg_temp(0x98, 0x11, middle_Offset_sw[i][j], 1, dev->TempChipAddr[i][j], i); // Set offset
  6142. }
  6143. else
  6144. ret = check_reg_temp(0x98, 0x11, middle_Offset[i][j], 1, dev->TempChipAddr[i][j], i); // Set offset
  6145. #endif
  6146. ret = check_reg_temp(0x98, 0x01, 0x0, 0x0, dev->TempChipAddr[i][j], i);
  6147. if (ret != 0)
  6148. {
  6149. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE] = get_remote(ret & 0xff);
  6150. #if ((!defined USE_N_OFFSET_FIX_TEMP) && (!defined EXTEND_TEMP_MODE))
  6151. if(middle_Offset_sw[i][j]!=0)
  6152. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE] - middle_Offset_sw[i][j];
  6153. #endif
  6154. sprintf(logstr,"Chain[%d] Chip[%d] middle Temp=%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]);
  6155. writeLogFile(logstr);
  6156. }
  6157. else
  6158. {
  6159. #if ((!defined USE_N_OFFSET_FIX_TEMP) && (!defined EXTEND_TEMP_MODE))
  6160. if(middle_Offset_sw[i][j]!=0)
  6161. {
  6162. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=0; // set 0, if read failed, when use sw offset.
  6163. sprintf(logstr,"read failed on Chain[%d] Chip[%d] middle Temp\n",i,(dev->TempChipAddr[i][j]/4)+1);
  6164. writeLogFile(logstr);
  6165. }
  6166. else
  6167. {
  6168. middle_Offset_sw[i][j] += MAX_SW_TEMP_OFFSET;
  6169. if(((int)middle_Offset[i][j] + (int)middle_Offset_sw[i][j]) > 127)
  6170. middle_Offset_sw[i][j] = 127 - middle_Offset[i][j];
  6171. else if(((int)middle_Offset[i][j] + (int)middle_Offset_sw[i][j]) < -128)
  6172. middle_Offset_sw[i][j] = -128 - middle_Offset[i][j];
  6173. ret = check_reg_temp(0x98, 0x11, middle_Offset[i][j] + middle_Offset_sw[i][j], 1, dev->TempChipAddr[i][j], i); // Set offset
  6174. sprintf(logstr,"overflow start using sw offset, old value: Chain[%d] Chip[%d] middle Temp=%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]);
  6175. writeLogFile(logstr);
  6176. }
  6177. #else
  6178. sprintf(logstr,"read failed on Chain[%d] Chip[%d] middle Temp old value:%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]);
  6179. writeLogFile(logstr);
  6180. #endif
  6181. }
  6182. #ifdef SHOW_BOTTOM_TEMP
  6183. if(!doTestPatten)
  6184. {
  6185. ////////////////////////// BOTTOM TEMP /////////////////////////////////
  6186. // 0. Switch To Bottom
  6187. set_baud_with_addr(dev->baud, 0, dev->TempChipAddr[i][j], i, 1, 0, (int) TEMP_BOTTOM);
  6188. check_asic_reg_with_addr(MISC_CONTROL,dev->TempChipAddr[i][j],i,1);
  6189. #if ((!defined USE_N_OFFSET_FIX_TEMP) && (!defined EXTEND_TEMP_MODE))
  6190. if(dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]<125)
  6191. {
  6192. bottom_Offset_sw[i][j]=0;
  6193. }
  6194. // set Bottom offset
  6195. if(bottom_Offset_sw[i][j]!=0)
  6196. ret = check_reg_temp(0x98, 0x11, bottom_Offset_sw[i][j], 1, dev->TempChipAddr[i][j], i); // Set offset
  6197. else
  6198. ret = check_reg_temp(0x98, 0x11, bottom_Offset[i][j], 1, dev->TempChipAddr[i][j], i); // Set offset
  6199. #endif
  6200. ret = check_reg_temp(0x98, 0x01, 0x0, 0x0, dev->TempChipAddr[i][j], i);
  6201. if (ret != 0)
  6202. {
  6203. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM] = get_remote(ret & 0xff);
  6204. #if ((!defined USE_N_OFFSET_FIX_TEMP) && (!defined EXTEND_TEMP_MODE))
  6205. if(bottom_Offset_sw[i][j]!=0)
  6206. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]=dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]+(bottom_Offset[i][j]-bottom_Offset_sw[i][j]);
  6207. #endif
  6208. sprintf(logstr,"Chain[%d] Chip[%d] bottom Temp=%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]);
  6209. writeLogFile(logstr);
  6210. }
  6211. else
  6212. {
  6213. #if ((!defined USE_N_OFFSET_FIX_TEMP) && (!defined EXTEND_TEMP_MODE))
  6214. if(bottom_Offset_sw[i][j]!=0)
  6215. {
  6216. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]=0; // if already set sw offset, still failed, then we set temp to 0
  6217. sprintf(logstr,"read failed on Chain[%d] Chip[%d] bottom Temp\n",i,(dev->TempChipAddr[i][j]/4)+1);
  6218. writeLogFile(logstr);
  6219. }
  6220. else
  6221. {
  6222. bottom_Offset_sw[i][j]=MAX_SW_TEMP_OFFSET;
  6223. ret = check_reg_temp(0x98, 0x11, bottom_Offset_sw[i][j], 1, dev->TempChipAddr[i][j], i); // Set offset
  6224. sprintf(logstr,"overflow start using sw offset, old value: Chain[%d] Chip[%d] bottom Temp=%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]);
  6225. writeLogFile(logstr);
  6226. }
  6227. #else
  6228. sprintf(logstr,"read failed on Chain[%d] Chip[%d] bottom Temp old value:%d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]);
  6229. writeLogFile(logstr);
  6230. #endif
  6231. }
  6232. }
  6233. #endif
  6234. if(is218_Temp)
  6235. {
  6236. //set fake middle temp!!!
  6237. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=fakeMiddleTempFromPCB(dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]);
  6238. sprintf(logstr,"218 fix Chain[%d] Chip[%d] middle Temp = %d\n",i,(dev->TempChipAddr[i][j]/4)+1,dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]);
  6239. writeLogFile(logstr);
  6240. }
  6241. // below is used to check the temp chip is OK or not! only in reboot test mode, to save ERROR CODE
  6242. if(readRebootTestNum()>0)
  6243. {
  6244. if(dev->chain_asic_temp[i][j][TEMP_POS_LOCAL] > dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE])
  6245. {
  6246. chain_asic_temp_error[i][j]++;
  6247. if(chain_asic_temp_error[i][j]>1) // error with twice
  6248. {
  6249. sprintf(error_info,"J%d:6",i+1);
  6250. saveSearchFailedFlagInfo(error_info);
  6251. system("reboot");
  6252. }
  6253. }
  6254. else if(dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]>120)
  6255. {
  6256. chain_asic_temp_error[i][j]++;
  6257. if(chain_asic_temp_error[i][j]>1) // error with twice
  6258. {
  6259. sprintf(error_info,"J%d:6",i+1);
  6260. saveSearchFailedFlagInfo(error_info);
  6261. system("reboot");
  6262. }
  6263. }
  6264. else
  6265. {
  6266. chain_asic_temp_error[i][j]=0;
  6267. }
  6268. }
  6269. if(dev->chain_asic_temp[i][j][TEMP_POS_LOCAL] > maxtemp[TEMP_POS_LOCAL])
  6270. maxtemp[TEMP_POS_LOCAL]=dev->chain_asic_temp[i][j][TEMP_POS_LOCAL];
  6271. if(dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE] > maxtemp[TEMP_POS_MIDDLE])
  6272. maxtemp[TEMP_POS_MIDDLE]=dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE];
  6273. if(dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM] > maxtemp[TEMP_POS_BOTTOM])
  6274. maxtemp[TEMP_POS_BOTTOM]=dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM];
  6275. if(dev->chain_asic_temp[i][j][TEMP_POS_LOCAL] < mintemp[TEMP_POS_LOCAL])
  6276. mintemp[TEMP_POS_LOCAL]=dev->chain_asic_temp[i][j][TEMP_POS_LOCAL];
  6277. if(dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE] < mintemp[TEMP_POS_MIDDLE])
  6278. mintemp[TEMP_POS_MIDDLE]=dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE];
  6279. if(dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM] < mintemp[TEMP_POS_BOTTOM])
  6280. mintemp[TEMP_POS_BOTTOM]=dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM];
  6281. }
  6282. dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL]=maxtemp[TEMP_POS_LOCAL];
  6283. dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE]=maxtemp[TEMP_POS_MIDDLE];
  6284. dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM]=maxtemp[TEMP_POS_BOTTOM];
  6285. dev->chain_asic_mintemp[i][TEMP_POS_LOCAL]=mintemp[TEMP_POS_LOCAL];
  6286. dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE]=mintemp[TEMP_POS_MIDDLE];
  6287. dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM]=mintemp[TEMP_POS_BOTTOM];
  6288. // we use the max temp value of chain, as the PWM control and temp offside check!!!!
  6289. if(check_temp_offside)
  6290. {
  6291. if (dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE] > OFFSIDE_TOP || dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE] < OFFSIDE_LOW)
  6292. {
  6293. if (already_offside == false)
  6294. {
  6295. temp_offside[i]++;
  6296. already_offside = true;
  6297. }
  6298. }
  6299. else
  6300. {
  6301. already_offside = false;
  6302. }
  6303. }
  6304. if (dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL] > temp_top[TEMP_POS_LOCAL])
  6305. temp_top[TEMP_POS_LOCAL] = dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL];
  6306. if (dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE] > temp_top[TEMP_POS_MIDDLE])
  6307. temp_top[TEMP_POS_MIDDLE] = dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE];
  6308. if (dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM] > temp_top[TEMP_POS_BOTTOM])
  6309. temp_top[TEMP_POS_BOTTOM] = dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM];
  6310. if ((dev->chain_asic_mintemp[i][TEMP_POS_LOCAL] < temp_low[TEMP_POS_LOCAL] && dev->chain_asic_mintemp[i][TEMP_POS_LOCAL]>0 && chain_temp_toolow[i]==0) || temp_low[TEMP_POS_LOCAL] == 0)
  6311. temp_low[TEMP_POS_LOCAL] = dev->chain_asic_mintemp[i][TEMP_POS_LOCAL];
  6312. if ((dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE] < temp_low[TEMP_POS_MIDDLE] && dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE]>0 && chain_temp_toolow[i]==0) || temp_low[TEMP_POS_MIDDLE] == 0)
  6313. temp_low[TEMP_POS_MIDDLE] = dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE];
  6314. if ((dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM] < temp_low[TEMP_POS_BOTTOM] && dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM]>0 && chain_temp_toolow[i]==0) || temp_low[TEMP_POS_BOTTOM] == 0)
  6315. temp_low[TEMP_POS_BOTTOM] = dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM];
  6316. sprintf(logstr,"Done read temp on Chain[%d]\n",i);
  6317. writeLogFile(logstr);
  6318. }
  6319. }
  6320. dev->temp_top1[TEMP_POS_LOCAL] = temp_top[TEMP_POS_LOCAL];
  6321. dev->temp_top1[TEMP_POS_MIDDLE] = temp_top[TEMP_POS_MIDDLE];
  6322. dev->temp_top1[TEMP_POS_BOTTOM] = temp_top[TEMP_POS_BOTTOM];
  6323. dev->temp_low1[TEMP_POS_LOCAL] = temp_low[TEMP_POS_LOCAL];
  6324. dev->temp_low1[TEMP_POS_MIDDLE] = temp_low[TEMP_POS_MIDDLE];
  6325. dev->temp_low1[TEMP_POS_BOTTOM] = temp_low[TEMP_POS_BOTTOM];
  6326. #ifdef T9_18
  6327. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  6328. {
  6329. if(dev->chain_exist[i] == 1)
  6330. {
  6331. if(fpga_version>=0xE)
  6332. {
  6333. switch(i) // only 8,10,12... has temp sensor!!! we just copy temp value to other chains!
  6334. {
  6335. case 1:
  6336. case 9:
  6337. for(j=0; j<dev->chain_asic_temp_num[i]; j++)
  6338. {
  6339. dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]=dev->chain_asic_temp[8][j][TEMP_POS_LOCAL];
  6340. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=dev->chain_asic_temp[8][j][TEMP_POS_MIDDLE];
  6341. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]=dev->chain_asic_temp[8][j][TEMP_POS_BOTTOM];
  6342. }
  6343. dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL]=dev->chain_asic_maxtemp[8][TEMP_POS_LOCAL];
  6344. dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_maxtemp[8][TEMP_POS_MIDDLE];
  6345. dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_maxtemp[8][TEMP_POS_BOTTOM];
  6346. dev->chain_asic_mintemp[i][TEMP_POS_LOCAL]=dev->chain_asic_mintemp[8][TEMP_POS_LOCAL];
  6347. dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_mintemp[8][TEMP_POS_MIDDLE];
  6348. dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_mintemp[8][TEMP_POS_BOTTOM];
  6349. break;
  6350. case 2:
  6351. case 11:
  6352. for(j=0; j<dev->chain_asic_temp_num[i]; j++)
  6353. {
  6354. dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]=dev->chain_asic_temp[10][j][TEMP_POS_LOCAL];
  6355. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=dev->chain_asic_temp[10][j][TEMP_POS_MIDDLE];
  6356. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]=dev->chain_asic_temp[10][j][TEMP_POS_BOTTOM];
  6357. }
  6358. dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL]=dev->chain_asic_maxtemp[10][TEMP_POS_LOCAL];
  6359. dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_maxtemp[10][TEMP_POS_MIDDLE];
  6360. dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_maxtemp[10][TEMP_POS_BOTTOM];
  6361. dev->chain_asic_mintemp[i][TEMP_POS_LOCAL]=dev->chain_asic_mintemp[10][TEMP_POS_LOCAL];
  6362. dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_mintemp[10][TEMP_POS_MIDDLE];
  6363. dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_mintemp[10][TEMP_POS_BOTTOM];
  6364. break;
  6365. case 3:
  6366. case 13:
  6367. for(j=0; j<dev->chain_asic_temp_num[i]; j++)
  6368. {
  6369. dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]=dev->chain_asic_temp[12][j][TEMP_POS_LOCAL];
  6370. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=dev->chain_asic_temp[12][j][TEMP_POS_MIDDLE];
  6371. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]=dev->chain_asic_temp[12][j][TEMP_POS_BOTTOM];
  6372. }
  6373. dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL]=dev->chain_asic_maxtemp[12][TEMP_POS_LOCAL];
  6374. dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_maxtemp[12][TEMP_POS_MIDDLE];
  6375. dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_maxtemp[12][TEMP_POS_BOTTOM];
  6376. dev->chain_asic_mintemp[i][TEMP_POS_LOCAL]=dev->chain_asic_mintemp[12][TEMP_POS_LOCAL];
  6377. dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_mintemp[12][TEMP_POS_MIDDLE];
  6378. dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_mintemp[12][TEMP_POS_BOTTOM];
  6379. break;
  6380. }
  6381. }
  6382. else
  6383. {
  6384. if(i%3 != 1) // only 1,4,7... has temp sensor!!! we just copy temp value to other chains!
  6385. {
  6386. for(j=0; j<dev->chain_asic_temp_num[i]; j++)
  6387. {
  6388. dev->chain_asic_temp[i][j][TEMP_POS_LOCAL]=dev->chain_asic_temp[((i/3)*3)+1][j][TEMP_POS_LOCAL];
  6389. dev->chain_asic_temp[i][j][TEMP_POS_MIDDLE]=dev->chain_asic_temp[((i/3)*3)+1][j][TEMP_POS_MIDDLE];
  6390. dev->chain_asic_temp[i][j][TEMP_POS_BOTTOM]=dev->chain_asic_temp[((i/3)*3)+1][j][TEMP_POS_BOTTOM];
  6391. }
  6392. dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL]=dev->chain_asic_maxtemp[((i/3)*3)+1][TEMP_POS_LOCAL];
  6393. dev->chain_asic_maxtemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_maxtemp[((i/3)*3)+1][TEMP_POS_MIDDLE];
  6394. dev->chain_asic_maxtemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_maxtemp[((i/3)*3)+1][TEMP_POS_BOTTOM];
  6395. dev->chain_asic_mintemp[i][TEMP_POS_LOCAL]=dev->chain_asic_mintemp[((i/3)*3)+1][TEMP_POS_LOCAL];
  6396. dev->chain_asic_mintemp[i][TEMP_POS_MIDDLE]=dev->chain_asic_mintemp[((i/3)*3)+1][TEMP_POS_MIDDLE];
  6397. dev->chain_asic_mintemp[i][TEMP_POS_BOTTOM]=dev->chain_asic_mintemp[((i/3)*3)+1][TEMP_POS_BOTTOM];
  6398. }
  6399. }
  6400. }
  6401. }
  6402. #endif
  6403. // only change fan speed after read temp value!!!
  6404. check_fan();
  6405. set_PWM_according_to_temperature();
  6406. if(startCheckNetworkJob)
  6407. {
  6408. cgtime(&tv_send);
  6409. timersub(&tv_send, &tv_send_job, &diff);
  6410. cur_fan_num=dev->fan_num;
  6411. if(diff.tv_sec > 120) // we need print network error to log here, below the process will not print this error msg to log!!!
  6412. {
  6413. sprintf(logstr,"Fatal Error: network connection lost!\n");
  6414. writeInitLogFile(logstr);
  6415. }
  6416. }
  6417. else
  6418. {
  6419. diff.tv_sec=0; // ignore network job error when in test patten mode
  6420. cur_fan_num=MIN_FAN_NUM;
  6421. }
  6422. #ifdef DEBUG_NOT_CHECK_FAN_NUM
  6423. if(cur_fan_num < MIN_FAN_NUM)
  6424. {
  6425. sprintf(logstr,"DEBUG Fatal Error: FAN lost! fan num=%d\n",cur_fan_num);
  6426. writeInitLogFile(logstr);
  6427. // we keep running...
  6428. cur_fan_num=MIN_FAN_NUM;
  6429. }
  6430. #endif
  6431. #ifndef DISABLE_TEMP_PROTECT
  6432. if(diff.tv_sec > 120 || dev->temp_top1[TEMP_POS_LOCAL] > MAX_PCB_TEMP // we use pcb temp to check protect or not
  6433. || cur_fan_num < MIN_FAN_NUM /*|| dev->fan_speed_top1 < (MAX_FAN_SPEED * dev->fan_pwm / 150) */ )
  6434. {
  6435. fatal_error_counter++;
  6436. if(fatal_error_counter>=3)
  6437. {
  6438. global_stop = true; // still counter the chip's x times
  6439. if(dev->temp_top1[TEMP_POS_LOCAL] > MAX_PCB_TEMP)
  6440. FatalErrorValue=ERROR_OVER_MAXTEMP;
  6441. else if(cur_fan_num < MIN_FAN_NUM)
  6442. FatalErrorValue=ERROR_FAN_LOST;
  6443. else if(dev->fan_speed_top1 < (MAX_FAN_SPEED * cur_fan_num / 150))
  6444. FatalErrorValue=ERROR_FAN_SPEED;
  6445. else
  6446. FatalErrorValue=ERROR_UNKOWN_STATUS;
  6447. if(dev->temp_top1[TEMP_POS_LOCAL] > MAX_PCB_TEMP
  6448. || cur_fan_num < MIN_FAN_NUM /* || dev->fan_speed_top1 < (MAX_FAN_SPEED * dev->fan_pwm / 150) */)
  6449. {
  6450. status_error = true; // will stop counter the chip's x times
  6451. once_error = true;
  6452. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  6453. {
  6454. if(dev->chain_exist[i] == 1)
  6455. {
  6456. pthread_mutex_lock(&iic_mutex);
  6457. disable_pic_dac(i);
  6458. pthread_mutex_unlock(&iic_mutex);
  6459. }
  6460. }
  6461. }
  6462. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  6463. }
  6464. else
  6465. {
  6466. global_stop = false;
  6467. if (!once_error)
  6468. status_error = false;
  6469. }
  6470. }
  6471. else
  6472. {
  6473. fatal_error_counter=0;
  6474. global_stop = false;
  6475. if (!once_error)
  6476. status_error = false;
  6477. }
  6478. #endif
  6479. // set_led(global_stop);
  6480. if(status_error)
  6481. {
  6482. char error_info[256];
  6483. switch(FatalErrorValue)
  6484. {
  6485. case ERROR_OVER_MAXTEMP:
  6486. sprintf(logstr,"Fatal Error: Temperature is too high!\n");
  6487. if(readRebootTestNum()>0)
  6488. {
  6489. sprintf(error_info,"P:1");
  6490. saveSearchFailedFlagInfo(error_info);
  6491. system("reboot");
  6492. }
  6493. break;
  6494. case ERROR_FAN_LOST:
  6495. sprintf(logstr,"Fatal Error: Fan lost!\n");
  6496. if(readRebootTestNum()>0)
  6497. {
  6498. sprintf(error_info,"F:1");
  6499. saveSearchFailedFlagInfo(error_info);
  6500. system("reboot");
  6501. }
  6502. break;
  6503. case ERROR_FAN_SPEED:
  6504. sprintf(logstr,"Fatal Error: Fan speed too low!\n");
  6505. break;
  6506. case ERROR_UNKOWN_STATUS:
  6507. sprintf(logstr,"Fatal Error: network connection lost!\n");
  6508. break;
  6509. default:
  6510. sprintf(logstr,"Fatal Error: unkown status.\n");
  6511. break;
  6512. }
  6513. writeInitLogFile(logstr);
  6514. }
  6515. processTEST();
  6516. sprintf(logstr,"FAN PWM: %d\n",dev->fan_pwm);
  6517. writeLogFile(logstr);
  6518. pthread_mutex_unlock(&opencore_readtemp_mutex);
  6519. sprintf(logstr,"read_temp_func Done!\n");
  6520. writeLogFile(logstr);
  6521. sprintf(logstr,"CRC error counter=%d\n",get_crc_count());
  6522. writeLogFile(logstr);
  6523. updateLogFile();
  6524. if(doTestPatten)
  6525. sleep(3);
  6526. else sleep(1);
  6527. }
  6528. }
  6529. void chain_inactive(unsigned char chain)
  6530. {
  6531. unsigned char buf[5] = {0,0,0,0,5};
  6532. unsigned int cmd_buf[3] = {0,0,0};
  6533. unsigned int ret, value;
  6534. if(!opt_multi_version) // fil mode
  6535. {
  6536. buf[0] = CHAIN_INACTIVE | COMMAND_FOR_ALL;
  6537. buf[1] = 0;
  6538. buf[2] = 0;
  6539. buf[3] = CRC5(buf, 4*8 - 5);
  6540. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  6541. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6542. set_BC_command_buffer(cmd_buf);
  6543. ret = get_BC_write_command();
  6544. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (chain << 16) | (ret & 0xfff0ffff);
  6545. set_BC_write_command(value);
  6546. }
  6547. else // vil mode
  6548. {
  6549. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | CHAIN_INACTIVE;
  6550. buf[1] = 0x05;
  6551. buf[2] = 0;
  6552. buf[3] = 0;
  6553. buf[4] = CRC5(buf, 4*8);
  6554. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x, buf[4]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3], buf[4]);
  6555. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6556. cmd_buf[1] = buf[4]<<24;
  6557. while (1)
  6558. {
  6559. ret = get_BC_write_command();
  6560. if ((ret & 0x80000000) == 0)
  6561. break;
  6562. cgsleep_ms(1);
  6563. }
  6564. set_BC_command_buffer(cmd_buf);
  6565. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (chain << 16) | (ret & 0xfff0ffff);
  6566. set_BC_write_command(value);
  6567. }
  6568. }
  6569. void set_address(unsigned char chain, unsigned char mode, unsigned char address)
  6570. {
  6571. unsigned char buf[9] = {0};
  6572. unsigned int cmd_buf[3] = {0,0,0};
  6573. unsigned int ret, value;
  6574. if(!opt_multi_version) // fil mode
  6575. {
  6576. buf[0] = SET_ADDRESS;
  6577. buf[1] = address;
  6578. buf[2] = 0;
  6579. if (mode) //all
  6580. buf[0] |= COMMAND_FOR_ALL;
  6581. buf[3] = CRC5(buf, 4*8 - 5);
  6582. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  6583. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6584. set_BC_command_buffer(cmd_buf);
  6585. ret = get_BC_write_command();
  6586. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (chain << 16) | (ret & 0xfff0ffff);
  6587. set_BC_write_command(value);
  6588. }
  6589. else // vil mode
  6590. {
  6591. buf[0] = VIL_COMMAND_TYPE | SET_ADDRESS;
  6592. buf[1] = 0x05;
  6593. buf[2] = address;
  6594. buf[3] = 0;
  6595. buf[4] = CRC5(buf, 4*8);
  6596. //applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x, buf[4]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3], buf[4]);
  6597. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6598. cmd_buf[1] = buf[4]<<24;
  6599. while (1)
  6600. {
  6601. ret = get_BC_write_command();
  6602. if ((ret & 0x80000000) == 0)
  6603. break;
  6604. cgsleep_ms(1);
  6605. }
  6606. set_BC_command_buffer(cmd_buf);
  6607. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (chain << 16) | (ret & 0xfff0ffff);
  6608. set_BC_write_command(value);
  6609. }
  6610. }
  6611. int calculate_asic_number(unsigned int actual_asic_number)
  6612. {
  6613. int i = 0;
  6614. if(actual_asic_number == 1)
  6615. {
  6616. i = 1;
  6617. }
  6618. else if(actual_asic_number == 2)
  6619. {
  6620. i = 2;
  6621. }
  6622. else if((actual_asic_number > 2) && (actual_asic_number <= 4))
  6623. {
  6624. i = 4;
  6625. }
  6626. else if((actual_asic_number > 4) && (actual_asic_number <= 8))
  6627. {
  6628. i = 8;
  6629. }
  6630. else if((actual_asic_number > 8) && (actual_asic_number <= 16))
  6631. {
  6632. i = 16;
  6633. }
  6634. else if((actual_asic_number > 16) && (actual_asic_number <= 32))
  6635. {
  6636. i = 32;
  6637. }
  6638. else if((actual_asic_number > 32) && (actual_asic_number <= 64))
  6639. {
  6640. i = 64;
  6641. }
  6642. else if((actual_asic_number > 64) && (actual_asic_number <= 128))
  6643. {
  6644. i = 128;
  6645. }
  6646. else
  6647. {
  6648. applog(LOG_DEBUG,"actual_asic_number = %d, but it is error\n", actual_asic_number);
  6649. return -1;
  6650. }
  6651. return i;
  6652. }
  6653. int calculate_core_number(unsigned int actual_core_number)
  6654. {
  6655. int i = 0;
  6656. if(actual_core_number == 1)
  6657. {
  6658. i = 1;
  6659. }
  6660. else if(actual_core_number == 2)
  6661. {
  6662. i = 2;
  6663. }
  6664. else if((actual_core_number > 2) && (actual_core_number <= 4))
  6665. {
  6666. i = 4;
  6667. }
  6668. else if((actual_core_number > 4) && (actual_core_number <= 8))
  6669. {
  6670. i = 8;
  6671. }
  6672. else if((actual_core_number > 8) && (actual_core_number <= 16))
  6673. {
  6674. i = 16;
  6675. }
  6676. else if((actual_core_number > 16) && (actual_core_number <= 32))
  6677. {
  6678. i = 32;
  6679. }
  6680. else if((actual_core_number > 32) && (actual_core_number <= 64))
  6681. {
  6682. i = 64;
  6683. }
  6684. else if((actual_core_number > 64) && (actual_core_number <= 128))
  6685. {
  6686. i = 128;
  6687. }
  6688. else
  6689. {
  6690. applog(LOG_DEBUG,"actual_core_number = %d, but it is error\n", actual_core_number);
  6691. return -1;
  6692. }
  6693. return i;
  6694. }
  6695. void software_set_address_onChain(int chainIndex)
  6696. {
  6697. unsigned int i=chainIndex, j;
  6698. unsigned char chip_addr = 0;
  6699. unsigned char check_bit = 0;
  6700. dev->check_bit=0;
  6701. applog(LOG_DEBUG,"--- %s\n", __FUNCTION__);
  6702. dev->addrInterval = CHIP_ADDR_INTERVAL; // fix chip addr interval
  6703. check_bit = dev->addrInterval - 1;
  6704. while(check_bit)
  6705. {
  6706. check_bit = check_bit >> 1;
  6707. dev->check_bit++;
  6708. }
  6709. //for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  6710. {
  6711. //if(dev->chain_exist[i] == 1)
  6712. {
  6713. chip_addr = 0;
  6714. chain_inactive(i);
  6715. cgsleep_ms(30);
  6716. chain_inactive(i);
  6717. cgsleep_ms(30);
  6718. chain_inactive(i);
  6719. cgsleep_ms(30);
  6720. for(j = 0; j < 0x100/dev->addrInterval; j++)
  6721. {
  6722. set_address(i, 0, chip_addr);
  6723. chip_addr += dev->addrInterval;
  6724. cgsleep_ms(30);
  6725. }
  6726. }
  6727. }
  6728. }
  6729. void software_set_address()
  6730. {
  6731. unsigned int i, j;
  6732. unsigned char chip_addr = 0;
  6733. unsigned char check_bit = 0;
  6734. dev->check_bit=0;
  6735. applog(LOG_DEBUG,"--- %s\n", __FUNCTION__);
  6736. dev->addrInterval = CHIP_ADDR_INTERVAL;
  6737. check_bit = dev->addrInterval - 1;
  6738. while(check_bit)
  6739. {
  6740. check_bit = check_bit >> 1;
  6741. dev->check_bit++;
  6742. }
  6743. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  6744. {
  6745. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i] > 0)
  6746. {
  6747. chip_addr = 0;
  6748. chain_inactive(i);
  6749. cgsleep_ms(30);
  6750. chain_inactive(i);
  6751. cgsleep_ms(30);
  6752. chain_inactive(i);
  6753. cgsleep_ms(30);
  6754. for(j = 0; j < 0x100/dev->addrInterval; j++)
  6755. {
  6756. set_address(i, 0, chip_addr);
  6757. chip_addr += dev->addrInterval;
  6758. cgsleep_ms(30);
  6759. }
  6760. }
  6761. }
  6762. }
  6763. void set_asic_ticket_mask(unsigned int ticket_mask)
  6764. {
  6765. unsigned char buf[9] = {0};
  6766. unsigned int cmd_buf[3] = {0,0,0};
  6767. unsigned int ret, value,i;
  6768. unsigned int tm;
  6769. tm = Swap32(ticket_mask);
  6770. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  6771. {
  6772. if(dev->chain_exist[i] == 1)
  6773. {
  6774. //first step: send new bauddiv to ASIC, but FPGA doesn't change its bauddiv, it uses old bauddiv to send BC command to ASIC
  6775. if(!opt_multi_version) // fil mode
  6776. {
  6777. buf[0] = SET_BAUD_OPS;
  6778. buf[1] = 0x10;
  6779. buf[2] = ticket_mask & 0x1f;
  6780. buf[0] |= COMMAND_FOR_ALL;
  6781. buf[3] = CRC5(buf, 4*8 - 5);
  6782. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  6783. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6784. set_BC_command_buffer(cmd_buf);
  6785. ret = get_BC_write_command();
  6786. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  6787. set_BC_write_command(value);
  6788. }
  6789. else // vil mode
  6790. {
  6791. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  6792. buf[1] = 0x09;
  6793. buf[2] = 0;
  6794. buf[3] = TICKET_MASK;
  6795. buf[4] = tm & 0xff;
  6796. buf[5] = (tm >> 8) & 0xff;
  6797. buf[6] = (tm >> 16) & 0xff;
  6798. buf[7] = (tm >> 24) & 0xff;
  6799. buf[8] = CRC5(buf, 8*8);
  6800. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6801. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
  6802. cmd_buf[2] = buf[8]<<24;
  6803. set_BC_command_buffer(cmd_buf);
  6804. ret = get_BC_write_command();
  6805. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  6806. set_BC_write_command(value);
  6807. }
  6808. }
  6809. }
  6810. }
  6811. void set_hcnt(unsigned int hcnt)
  6812. {
  6813. unsigned char buf[9] = {0};
  6814. unsigned int cmd_buf[3] = {0,0,0};
  6815. unsigned int ret, value,i;
  6816. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  6817. {
  6818. if(dev->chain_exist[i] == 1)
  6819. {
  6820. if(!opt_multi_version) // fil mode
  6821. {
  6822. ;
  6823. }
  6824. else // vil mode
  6825. {
  6826. /* 20160510
  6827. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  6828. buf[1] = 0x09;
  6829. buf[2] = 0;
  6830. buf[3] = MISC_CONTROL;
  6831. buf[4] = 0x40;
  6832. buf[5] = INV_CLKO | cgpu.temp_sel;
  6833. buf[6] = (bauddiv & 0x1f) | (cgpu.rfs << 6);
  6834. buf[7] = cgpu.tfs << 5;
  6835. buf[8] = CRC5(buf, 8*8);
  6836. */
  6837. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  6838. buf[1] = 0x09;
  6839. buf[2] = 0;
  6840. buf[3] = HASH_COUNTING_NUMBER;
  6841. buf[4] = hcnt>>24;
  6842. buf[5] = hcnt>>16;
  6843. buf[6] = hcnt>>8;
  6844. buf[7] = hcnt;
  6845. buf[8] = CRC5(buf, 8*8);
  6846. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6847. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
  6848. cmd_buf[2] = buf[8]<<24;
  6849. set_BC_command_buffer(cmd_buf);
  6850. ret = get_BC_write_command();
  6851. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xFFF0FFFF);
  6852. set_BC_write_command(value);
  6853. }
  6854. }
  6855. }
  6856. }
  6857. #if 1
  6858. void set_baud(unsigned char bauddiv,int no_use)
  6859. {
  6860. unsigned char buf[9] = {0};
  6861. unsigned int cmd_buf[3] = {0,0,0};
  6862. unsigned int ret, value,i;
  6863. if(dev->baud == bauddiv)
  6864. {
  6865. applog(LOG_DEBUG,"%s: the setting bauddiv(%d) is the same as before\n", __FUNCTION__, bauddiv);
  6866. return;
  6867. }
  6868. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  6869. {
  6870. if(dev->chain_exist[i] == 1)
  6871. {
  6872. //first step: send new bauddiv to ASIC, but FPGA doesn't change its bauddiv, it uses old bauddiv to send BC command to ASIC
  6873. if(!opt_multi_version) // fil mode
  6874. {
  6875. buf[0] = SET_BAUD_OPS;
  6876. buf[1] = 0x10;
  6877. buf[2] = bauddiv & 0x1f;
  6878. buf[0] |= COMMAND_FOR_ALL;
  6879. buf[3] = CRC5(buf, 4*8 - 5);
  6880. applog(LOG_DEBUG,"%s: buf[0]=0x%x, buf[1]=0x%x, buf[2]=0x%x, buf[3]=0x%x\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  6881. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6882. set_BC_command_buffer(cmd_buf);
  6883. ret = get_BC_write_command();
  6884. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  6885. set_BC_write_command(value);
  6886. }
  6887. else // vil mode
  6888. {
  6889. buf[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  6890. buf[1] = 0x09;
  6891. buf[2] = 0;
  6892. buf[3] = MISC_CONTROL;
  6893. buf[4] = 0;
  6894. buf[5] = INV_CLKO;
  6895. buf[6] = bauddiv & 0x1f;
  6896. buf[7] = 0;
  6897. buf[8] = CRC5(buf, 8*8);
  6898. cmd_buf[0] = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
  6899. cmd_buf[1] = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
  6900. cmd_buf[2] = buf[8]<<24;
  6901. applog(LOG_DEBUG,"%s: cmd_buf[0]=0x%x, cmd_buf[1]=0x%x, cmd_buf[2]=0x%x\n", __FUNCTION__, cmd_buf[0], cmd_buf[1], cmd_buf[2]);
  6902. set_BC_command_buffer(cmd_buf);
  6903. ret = get_BC_write_command();
  6904. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID| (i << 16) | (ret & 0xfff0ffff);
  6905. set_BC_write_command(value);
  6906. }
  6907. }
  6908. }
  6909. // second step: change FPGA's bauddiv
  6910. cgsleep_us(50000);
  6911. ret = get_BC_write_command();
  6912. value = (ret & 0xffffffe0) | (bauddiv & 0x1f);
  6913. set_BC_write_command(value);
  6914. dev->baud = bauddiv;
  6915. }
  6916. #endif
  6917. void init_uart_baud()
  6918. {
  6919. unsigned int rBaudrate = 0, baud = 0;
  6920. unsigned char bauddiv = 0;
  6921. int i =0;
  6922. char logstr[1024];
  6923. rBaudrate = 1000000 * 5/3 / dev->timeout * (64*8); //64*8 need send bit, ratio=2/3
  6924. baud = 25000000/rBaudrate/8 - 1;
  6925. if(baud > MAX_BAUD_DIVIDER)
  6926. {
  6927. bauddiv = MAX_BAUD_DIVIDER;
  6928. }
  6929. else
  6930. {
  6931. bauddiv = baud;
  6932. }
  6933. #ifdef FIX_BAUD_VALUE
  6934. baud = FIX_BAUD_VALUE;
  6935. #endif
  6936. sprintf(logstr,"set baud=%d\n",bauddiv);
  6937. writeInitLogFile(logstr);
  6938. set_baud(bauddiv,1);
  6939. }
  6940. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  6941. void set_red_led(bool flag)
  6942. {
  6943. char cmd[100];
  6944. if(isC5_CtrlBoard)
  6945. {
  6946. if(flag)
  6947. {
  6948. sprintf(cmd,"echo %d > %s", 1,RED_LED_DEV_C5);
  6949. system(cmd);
  6950. }
  6951. else
  6952. {
  6953. sprintf(cmd,"echo %d > %s", 0,RED_LED_DEV_C5);
  6954. system(cmd);
  6955. }
  6956. }
  6957. else
  6958. {
  6959. if(flag)
  6960. {
  6961. sprintf(cmd,"echo %d > %s", 1,RED_LED_DEV_XILINX);
  6962. system(cmd);
  6963. }
  6964. else
  6965. {
  6966. sprintf(cmd,"echo %d > %s", 0,RED_LED_DEV_XILINX);
  6967. system(cmd);
  6968. }
  6969. }
  6970. }
  6971. #endif
  6972. void set_led(bool stop)
  6973. {
  6974. static bool blink = true;
  6975. char cmd[100];
  6976. blink = !blink;
  6977. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  6978. return; // disable LED, we use it to debug
  6979. #endif
  6980. if(isC5_CtrlBoard)
  6981. {
  6982. if(stop)
  6983. {
  6984. sprintf(cmd,"echo %d > %s", 0,GREEN_LED_DEV_C5);
  6985. system(cmd);
  6986. sprintf(cmd,"echo %d > %s", (blink)?1:0,RED_LED_DEV_C5);
  6987. system(cmd);
  6988. }
  6989. else
  6990. {
  6991. sprintf(cmd,"echo %d > %s", 0,RED_LED_DEV_C5);
  6992. system(cmd);
  6993. sprintf(cmd,"echo %d > %s", (blink)?1:0,GREEN_LED_DEV_C5);
  6994. system(cmd);
  6995. }
  6996. }
  6997. else
  6998. {
  6999. if(stop)
  7000. {
  7001. sprintf(cmd,"echo %d > %s", 0,GREEN_LED_DEV_XILINX);
  7002. system(cmd);
  7003. sprintf(cmd,"echo %d > %s", (blink)?1:0,RED_LED_DEV_XILINX);
  7004. system(cmd);
  7005. }
  7006. else
  7007. {
  7008. sprintf(cmd,"echo %d > %s", 0,RED_LED_DEV_XILINX);
  7009. system(cmd);
  7010. sprintf(cmd,"echo %d > %s", (blink)?1:0,GREEN_LED_DEV_XILINX);
  7011. system(cmd);
  7012. }
  7013. }
  7014. }
  7015. void * pic_heart_beat_func()
  7016. {
  7017. int i;
  7018. while(1)
  7019. {
  7020. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  7021. {
  7022. if(dev->chain_exist[i])
  7023. {
  7024. pthread_mutex_lock(&iic_mutex);
  7025. pic_heart_beat_each_chain(i);
  7026. pthread_mutex_unlock(&iic_mutex);
  7027. cgsleep_ms(10);
  7028. }
  7029. }
  7030. sleep(HEART_BEAT_TIME_GAP);
  7031. }
  7032. return 0;
  7033. }
  7034. void change_pic_voltage_old()
  7035. {
  7036. int i;
  7037. sleep(300);
  7038. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  7039. {
  7040. uint8_t tmp_vol = de_voltage;
  7041. if(dev->chain_exist[i])
  7042. {
  7043. while(1)
  7044. {
  7045. if(tmp_vol > chain_voltage_pic[i])
  7046. break;
  7047. tmp_vol += 5;
  7048. if(tmp_vol > chain_voltage_pic[i])
  7049. tmp_vol = chain_voltage_pic[i];
  7050. pthread_mutex_lock(&iic_mutex);
  7051. set_pic_voltage(i,tmp_vol);
  7052. pthread_mutex_unlock(&iic_mutex);
  7053. pthread_mutex_lock(&iic_mutex);
  7054. get_pic_voltage(i);
  7055. pthread_mutex_unlock(&iic_mutex);
  7056. if(tmp_vol == chain_voltage_pic[i])
  7057. break;
  7058. cgsleep_ms(100);
  7059. }
  7060. }
  7061. }
  7062. }
  7063. int get_asic_nonce_num(int chain, int asic, int timeslice)
  7064. {
  7065. int i = timeslice;
  7066. int index = 0;
  7067. int nonce = 0;
  7068. for (i = 1; i <= timeslice; i++)
  7069. {
  7070. if(nonce_times%TIMESLICE - i >= 0)
  7071. index = nonce_times%TIMESLICE - i;
  7072. else
  7073. index = nonce_times%TIMESLICE - i + TIMESLICE;
  7074. nonce += nonce_num[chain][asic][index];
  7075. }
  7076. return nonce;
  7077. }
  7078. void get_lastn_nonce_num(char * dest,int n)
  7079. {
  7080. int i = 0;
  7081. int j = 0;
  7082. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  7083. {
  7084. if(dev->chain_exist[i])
  7085. {
  7086. char xtime[2048] = "{";
  7087. char tmp[20] = "";
  7088. sprintf(tmp,"Chain%d:{",i+1);
  7089. strcat(xtime,tmp);
  7090. sprintf(tmp,"N%d=%d",0,get_asic_nonce_num(i,0,n));
  7091. strcat(xtime,tmp);
  7092. for (j = 1; j < dev->max_asic_num_in_one_chain; j++)
  7093. {
  7094. sprintf(tmp,",N%d=%d",j,get_asic_nonce_num(i,j,n));
  7095. strcat(xtime,tmp);
  7096. }
  7097. strcat(xtime,"},");
  7098. strcat(dest,xtime);
  7099. }
  7100. }
  7101. dest[(strlen(dest)-1) >= 0 ? (strlen(dest)-1) : 0]='\0';
  7102. // printf("%s\n",dest);
  7103. }
  7104. bool if_hashrate_ok()
  7105. {
  7106. double avg_rate = total_mhashes_done / 1000 / total_secs;
  7107. if( avg_rate > ((double)GetTotalRate()) * 0.98)
  7108. {
  7109. return true;
  7110. }
  7111. else
  7112. {
  7113. return false;
  7114. }
  7115. }
  7116. bool check_hashrate_maybe_ok(double level)
  7117. {
  7118. double avg_rate = total_mhashes_done / 1000 / total_secs;
  7119. if( avg_rate > ((double)GetTotalRate()) * level)
  7120. {
  7121. return true;
  7122. }
  7123. else
  7124. {
  7125. return false;
  7126. }
  7127. }
  7128. void saveRebootTestNum(int num)
  7129. {
  7130. FILE *fd;
  7131. char testnumStr[32];
  7132. fd=fopen("/etc/config/rebootTest","wb");
  7133. if(fd)
  7134. {
  7135. memset(testnumStr,'\0',sizeof(testnumStr));
  7136. sprintf(testnumStr,"%d",num);
  7137. fwrite(testnumStr,1,sizeof(testnumStr),fd);
  7138. fclose(fd);
  7139. }
  7140. system("sync");
  7141. }
  7142. int readRebootTestNum()
  7143. {
  7144. #ifdef DISABLE_FINAL_TEST
  7145. return 0; // fixed to 0, means miner fw is not in test mode!!! (if in test mode, will stop mining when get some error)
  7146. #else
  7147. FILE *fd;
  7148. char testnumStr[32];
  7149. fd=fopen("/etc/config/rebootTest","rb");
  7150. if(fd)
  7151. {
  7152. memset(testnumStr,'\0',sizeof(testnumStr));
  7153. fread(testnumStr,1,sizeof(testnumStr),fd);
  7154. fclose(fd);
  7155. return atoi(testnumStr);
  7156. }
  7157. return 0;
  7158. #endif
  7159. }
  7160. void saveRestartNum(int num)
  7161. {
  7162. FILE *fd;
  7163. char testnumStr[32];
  7164. fd=fopen("/etc/config/restartTest","wb");
  7165. if(fd)
  7166. {
  7167. memset(testnumStr,'\0',sizeof(testnumStr));
  7168. sprintf(testnumStr,"%d",num);
  7169. fwrite(testnumStr,1,sizeof(testnumStr),fd);
  7170. fclose(fd);
  7171. }
  7172. system("sync");
  7173. }
  7174. int readRestartNum()
  7175. {
  7176. #ifdef DISABLE_FINAL_TEST
  7177. return 2; // fixed to 2, means miner fw is in user mode, normal working mode...
  7178. #else
  7179. FILE *fd;
  7180. char testnumStr[32];
  7181. fd=fopen("/etc/config/restartTest","rb");
  7182. if(fd)
  7183. {
  7184. memset(testnumStr,'\0',sizeof(testnumStr));
  7185. fread(testnumStr,1,sizeof(testnumStr),fd);
  7186. fclose(fd);
  7187. return atoi(testnumStr);
  7188. }
  7189. return 0;
  7190. #endif
  7191. }
  7192. #ifdef ENABLE_REINIT_WHEN_TESTFAILED
  7193. void saveRetryFlag(int retry_flag)
  7194. {
  7195. FILE *fd;
  7196. char testnumStr[32];
  7197. fd=fopen("/etc/config/retryFlag","wb");
  7198. if(fd)
  7199. {
  7200. memset(testnumStr,'\0',sizeof(testnumStr));
  7201. sprintf(testnumStr,"%d",retry_flag);
  7202. fwrite(testnumStr,1,sizeof(testnumStr),fd);
  7203. fclose(fd);
  7204. }
  7205. system("sync");
  7206. }
  7207. int readRetryFlag()
  7208. {
  7209. FILE *fd;
  7210. char testnumStr[32];
  7211. fd=fopen("/etc/config/retryFlag","rb");
  7212. if(fd)
  7213. {
  7214. memset(testnumStr,'\0',sizeof(testnumStr));
  7215. fread(testnumStr,1,sizeof(testnumStr),fd);
  7216. fclose(fd);
  7217. return atoi(testnumStr);
  7218. }
  7219. return 0;
  7220. }
  7221. #endif
  7222. void * check_system_work()
  7223. {
  7224. struct timeval tv_start, tv_end,tv_reboot,tv_reboot_start;
  7225. int i = 0, j = 0;
  7226. cgtime(&tv_end);
  7227. cgtime(&tv_reboot);
  7228. copy_time(&tv_start, &tv_end);
  7229. copy_time(&tv_reboot_start, &tv_reboot);
  7230. int asic_num = 0, error_asic = 0, avg_num = 0;
  7231. int run_counter=0;
  7232. int rebootTestNum=readRebootTestNum();
  7233. double rt_board_rate;
  7234. double ideal_board_rate;
  7235. char logstr[1024];
  7236. int restartNum=readRestartNum();
  7237. #ifdef DEBUG_REBOOT
  7238. static int print_once=1;
  7239. sprintf(logstr,"DEBUG_REBOOT mode: will check hashrate >= 98% after 3000 seconds, then reboot if hashrate OK or stop hashrate low...\n");
  7240. writeInitLogFile(logstr);
  7241. #endif
  7242. if(restartNum>0)
  7243. {
  7244. sprintf(logstr,"restartNum = %d , auto-reinit enabled...\n",restartNum);
  7245. writeInitLogFile(logstr);
  7246. }
  7247. while(1)
  7248. {
  7249. struct timeval diff;
  7250. #ifdef DEBUG_REINIT
  7251. static int debug_counter=0;
  7252. #endif
  7253. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  7254. // must set led in 1 seconds, must be faster, in read temp func ,will be too slow
  7255. set_led(global_stop);
  7256. #endif
  7257. if(doTestPatten)
  7258. {
  7259. cgsleep_ms(100);
  7260. continue;
  7261. }
  7262. #ifdef ENABLE_REINIT_MINING
  7263. reinit_counter++;
  7264. #endif
  7265. #ifdef DEBUG_REINIT
  7266. debug_counter++;
  7267. if(debug_counter>120)
  7268. {
  7269. debug_counter=0;
  7270. sprintf(logstr,"Debug to Re-init %d...\n",debug_counter);
  7271. writeInitLogFile(logstr);
  7272. bitmain_core_reInit();
  7273. sprintf(logstr,"Re-init Done : %d\n",debug_counter);
  7274. writeInitLogFile(logstr);
  7275. reinit_counter=0;// wait for 10 min again for next retry init check
  7276. }
  7277. #endif
  7278. cgtime(&tv_end);
  7279. cgtime(&tv_reboot);
  7280. timersub(&tv_end, &tv_start, &diff);
  7281. #ifdef ENABLE_REINIT_MINING
  7282. if(restartNum>0 && (!global_stop) && reinit_counter>600) // normal mining status, we can check hashrate
  7283. {
  7284. #if 0
  7285. if(reinit_counter>660) // 10min + 60s
  7286. {
  7287. reinit_counter=600; // check after 60s next time
  7288. // check RT value with ideal value,
  7289. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7290. {
  7291. if(dev->chain_exist[i] == 1)
  7292. {
  7293. rt_board_rate=atof(displayed_rate[i]);
  7294. ideal_board_rate=GetBoardRate(i);
  7295. if(rt_board_rate*100/ideal_board_rate < CHECK_RT_IDEAL_RATE_PERCENT)
  7296. sprintf(logstr,"Chain[%d] RT=%f ideal=%f need re-init\n",i,rt_board_rate,ideal_board_rate);
  7297. else sprintf(logstr,"Chain[%d] RT=%f ideal=%f OK\n",i,rt_board_rate,ideal_board_rate);
  7298. writeInitLogFile(logstr);
  7299. }
  7300. }
  7301. }
  7302. #endif
  7303. if(reinit_counter>600)
  7304. {
  7305. reinit_counter=666;
  7306. // check RT value with ideal value,
  7307. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7308. {
  7309. if(dev->chain_exist[i] == 1)
  7310. {
  7311. rt_board_rate=atof(displayed_rate[i]);
  7312. ideal_board_rate=GetBoardRate(i);
  7313. if(rt_board_rate*100/ideal_board_rate < CHECK_RT_IDEAL_RATE_PERCENT)
  7314. {
  7315. sprintf(logstr,"Chain[%d] RT=%f ideal=%f need re-init\n",i,rt_board_rate,ideal_board_rate);
  7316. writeInitLogFile(logstr);
  7317. bitmain_core_reInit();
  7318. sprintf(logstr,"Re-init Done\n");
  7319. writeInitLogFile(logstr);
  7320. reinit_counter=0; // will wait for 10mins to start check to reinit again
  7321. break;
  7322. }
  7323. }
  7324. }
  7325. }
  7326. }
  7327. #endif
  7328. #ifdef DEBUG_REBOOT
  7329. if(total_secs > 3000 && print_once>0)
  7330. {
  7331. if(if_hashrate_ok())
  7332. {
  7333. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7334. {
  7335. if(dev->chain_exist[i] == 1)
  7336. {
  7337. disable_pic_dac(i);
  7338. }
  7339. }
  7340. set_PWM(1);
  7341. system("reboot");
  7342. }
  7343. else
  7344. {
  7345. double avg_rate = total_mhashes_done / 1000 / total_secs;
  7346. if(print_once>0)
  7347. {
  7348. print_once--;
  7349. sprintf(logstr,"Failed: avg hashrate=%f is low! will not reboot!!!\n",avg_rate);
  7350. writeInitLogFile(logstr);
  7351. }
  7352. }
  7353. }
  7354. #endif
  7355. #ifdef REBOOT_TEST_ONCE_1HOUR
  7356. if(total_secs > 3000 && rebootTestNum>=1 && rebootTestNum<=3) // not larger than 1 hour, avoid someone treat it as good miner
  7357. #else
  7358. if(total_secs > 1800 && rebootTestNum>=1 && rebootTestNum<=3)
  7359. #endif
  7360. {
  7361. #ifdef DEBUG_KEEP_REBOOT_EVERY_ONE_HOUR
  7362. // we do not set rebootTestNum = 0, so we can debug and reboot every one hour!
  7363. system("sync");
  7364. system("reboot");
  7365. #else
  7366. //TODO...
  7367. if(if_hashrate_ok())
  7368. {
  7369. #ifdef REBOOT_TEST_ONCE_1HOUR
  7370. saveRebootTestNum(0);
  7371. saveRestartNum(2);
  7372. #else
  7373. saveRebootTestNum((rebootTestNum-1));
  7374. // reboot test over, will enable re-init by set restart num = 2
  7375. if((rebootTestNum-1)==0)
  7376. saveRestartNum(2);
  7377. #endif
  7378. #if ((defined ENABLE_FINAL_TEST_WITHOUT_REBOOT) && (defined REBOOT_TEST_ONCE_1HOUR))
  7379. rebootTestNum=0;
  7380. restartNum=2;
  7381. #else
  7382. system("reboot");
  7383. #endif
  7384. }
  7385. else
  7386. {
  7387. char error_info[256];
  7388. // keep the log , we can check it
  7389. system("cp /tmp/search /etc/config/lastlog -f");
  7390. saveRebootTestNum(3333); // 3333 is magic number inform that this miner failed on test !
  7391. rebootTestNum=3333;
  7392. sprintf(error_info,"R:1");
  7393. saveSearchFailedFlagInfo(error_info);
  7394. system("reboot");
  7395. }
  7396. #endif
  7397. }
  7398. if (diff.tv_sec > 300)
  7399. check_temp_offside = true;
  7400. if (diff.tv_sec > 60 || (global_stop == true && diff.tv_sec > 30))
  7401. {
  7402. run_counter++; // for check asic o or x
  7403. #ifdef ENABLE_REINIT_MINING
  7404. if(restartNum>0 && (!global_stop) && reinit_counter>600)
  7405. {
  7406. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  7407. {
  7408. if(dev->chain_exist[i])
  7409. {
  7410. for(j=0; j<dev->chain_asic_num[i]; j++)
  7411. {
  7412. if(dev->chain_asic_nonce[i][j]>0)
  7413. break;
  7414. }
  7415. if(j>=dev->chain_asic_num[i] && dev->chain_asic_num[i]>0)
  7416. {
  7417. // all chips get 0 nonce
  7418. sprintf(logstr,"Chain[%d] get 0 nonce in 1 min\n",i);
  7419. writeInitLogFile(logstr);
  7420. bitmain_core_reInit();
  7421. sprintf(logstr,"Re-init Done\n");
  7422. writeInitLogFile(logstr);
  7423. reinit_counter=0; // will wait for 10mins to start check to reinit again
  7424. break;
  7425. }
  7426. }
  7427. }
  7428. }
  7429. #endif
  7430. asic_num = 0, error_asic = 0, avg_num = 0;
  7431. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  7432. {
  7433. if(dev->chain_exist[i])
  7434. {
  7435. asic_num += dev->chain_asic_num[i];
  7436. for(j=0; j<dev->chain_asic_num[i]; j++)
  7437. {
  7438. nonce_num[i][j][nonce_times % TIMESLICE] = dev->chain_asic_nonce[i][j];
  7439. avg_num += dev->chain_asic_nonce[i][j];
  7440. applog(LOG_DEBUG,"%s: chain %d asic %d asic_nonce_num %d", __FUNCTION__, i,j,dev->chain_asic_nonce[i][j]);
  7441. }
  7442. }
  7443. }
  7444. nonce_times ++;
  7445. memset(nonce_num10_string,0,NONCE_BUFF);
  7446. memset(nonce_num30_string,0,NONCE_BUFF);
  7447. memset(nonce_num60_string,0,NONCE_BUFF);
  7448. get_lastn_nonce_num(nonce_num10_string,10);
  7449. get_lastn_nonce_num(nonce_num30_string,30);
  7450. get_lastn_nonce_num(nonce_num60_string,60);
  7451. if (asic_num != 0)
  7452. {
  7453. applog(LOG_DEBUG,"%s: avg_num %d asic_num %d", __FUNCTION__, avg_num,asic_num);
  7454. avg_num = avg_num / asic_num / 8;
  7455. avg_num = 10;
  7456. }
  7457. else
  7458. {
  7459. avg_num = 1;
  7460. }
  7461. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  7462. {
  7463. if(dev->chain_exist[i])
  7464. {
  7465. int offset = 0;
  7466. for(j=0; j<dev->chain_asic_num[i]; j++)
  7467. {
  7468. if(j%8 == 0)
  7469. {
  7470. dev->chain_asic_status_string[i][j+offset] = ' ';
  7471. offset++;
  7472. }
  7473. #ifdef DISABLE_SHOWX_ENABLE_XTIMES
  7474. if(get_asic_nonce_num(i, j, 1) > 1) // 1 mins check nonce counter
  7475. {
  7476. dev->chain_asic_status_string[i][j+offset] = 'o';
  7477. }
  7478. else
  7479. {
  7480. dev->chain_asic_status_string[i][j+offset] = 'o'; // still show o, not x
  7481. if(!status_error)
  7482. x_time[i][j]++;
  7483. }
  7484. #else
  7485. if(get_asic_nonce_num(i, j, 1) > 1) // 1 mins check nonce counter
  7486. {
  7487. dev->chain_asic_status_string[i][j+offset] = 'o';
  7488. }
  7489. else
  7490. {
  7491. dev->chain_asic_status_string[i][j+offset] = 'x'; // still show o, not x
  7492. if(!status_error)
  7493. x_time[i][j]++;
  7494. }
  7495. #endif
  7496. dev->chain_asic_nonce[i][j] = 0;
  7497. }
  7498. dev->chain_asic_status_string[i][j+offset] = '\0';
  7499. }
  7500. }
  7501. if(run_counter>60)
  7502. run_counter=0;
  7503. copy_time(&tv_start, &tv_end);
  7504. }
  7505. cgsleep_ms(1000);
  7506. }
  7507. }
  7508. #ifdef DEBUG_OPENCORE_TWICE
  7509. static int debug_once=1; // clement for debug
  7510. #endif
  7511. void open_core(bool nullwork_enable)
  7512. {
  7513. unsigned int i = 0, j = 0, k, m, work_id = 0, ret = 0, value = 0, work_fifo_ready = 0, loop=0;
  7514. unsigned char gateblk[4] = {0,0,0,0};
  7515. unsigned int cmd_buf[3] = {0,0,0}, buf[TW_WRITE_COMMAND_LEN/sizeof(unsigned int)]= {0};
  7516. unsigned int buf_vil_tw[TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int)]= {0};
  7517. unsigned char data[TW_WRITE_COMMAND_LEN] = {0xff};
  7518. unsigned char buf_vil[9] = {0,0,0,0,0,0,0,0,0};
  7519. struct vil_work work_vil;
  7520. struct vil_work_1387 work_vil_1387;
  7521. char logstr[1024];
  7522. int wati_count=0;
  7523. #ifdef DEBUG_OPENCORE_TWICE
  7524. loop = BM1387_CORE_NUM-debug_once;
  7525. debug_once=0;
  7526. #else
  7527. loop = BM1387_CORE_NUM;
  7528. #endif
  7529. if(!opt_multi_version) // fil mode
  7530. {
  7531. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE));
  7532. set_hash_counting_number(0);
  7533. gateblk[0] = SET_BAUD_OPS;
  7534. gateblk[1] = 0;//0x10; //16-23
  7535. gateblk[2] = dev->baud | 0x80; //8-15 gateblk=1
  7536. gateblk[0] |= 0x80;
  7537. //gateblk[3] = CRC5(gateblk, 4*8 - 5);
  7538. gateblk[3] = 0x80; // MMEN=1
  7539. gateblk[3] = 0x80 | (0x1f & CRC5(gateblk, 4*8 - 5));
  7540. applog(LOG_DEBUG,"%s: gateblk[0]=0x%x, gateblk[1]=0x%x, gateblk[2]=0x%x, gateblk[3]=0x%x\n", __FUNCTION__, gateblk[0], gateblk[1], gateblk[2], gateblk[3]);
  7541. cmd_buf[0] = gateblk[0]<<24 | gateblk[1]<<16 | gateblk[2]<<8 | gateblk[3];
  7542. memset(data, 0x00, TW_WRITE_COMMAND_LEN);
  7543. data[TW_WRITE_COMMAND_LEN - 1] = 0xff;
  7544. data[TW_WRITE_COMMAND_LEN - 12] = 0xff;
  7545. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7546. {
  7547. if(dev->chain_exist[i] == 1)
  7548. {
  7549. set_BC_command_buffer(cmd_buf);
  7550. ret = get_BC_write_command();
  7551. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7552. set_BC_write_command(value);
  7553. cgsleep_us(10000);
  7554. for(m=0; m<loop; m++)
  7555. {
  7556. do
  7557. {
  7558. work_fifo_ready = get_buffer_space();
  7559. if(work_fifo_ready & (0x1 << i))
  7560. {
  7561. break;
  7562. }
  7563. else //work fifo is full, wait for 50ms
  7564. {
  7565. //applog(LOG_DEBUG,"%s: chain%d work fifo not ready: 0x%x\n", __FUNCTION__, i, work_fifo_ready);
  7566. cgsleep_us(1000);
  7567. }
  7568. }
  7569. while(1);
  7570. if(m==0) //new block
  7571. {
  7572. data[0] = NEW_BLOCK_MARKER;
  7573. }
  7574. else
  7575. {
  7576. data[0] = NORMAL_BLOCK_MARKER;
  7577. }
  7578. data[1] = i | 0x80; //set chain id and enable it
  7579. if(m==0)
  7580. {
  7581. ret = get_BC_write_command(); //disable null work
  7582. ret &= ~BC_COMMAND_EN_NULL_WORK;
  7583. set_BC_write_command(ret);
  7584. }
  7585. if(m==loop - 1 && nullwork_enable)
  7586. {
  7587. ret = get_BC_write_command(); //enable null work
  7588. ret &= (BC_COMMAND_EN_CHAIN_ID | BC_COMMAND_EN_NULL_WORK | ((i & 0xf) << 16));
  7589. set_BC_write_command(ret);
  7590. }
  7591. memset(buf, 0, TW_WRITE_COMMAND_LEN/sizeof(unsigned int));
  7592. for(j=0; j<TW_WRITE_COMMAND_LEN/sizeof(unsigned int); j++)
  7593. {
  7594. buf[j] = (data[4*j + 0] << 24) | (data[4*j + 1] << 16) | (data[4*j + 2] << 8) | data[4*j + 3];
  7595. if(j==9)
  7596. {
  7597. buf[j] = work_id++;
  7598. }
  7599. //applog(LOG_DEBUG,"buf[%d] = 0x%x\n", j, buf[i]);
  7600. }
  7601. set_TW_write_command(buf);
  7602. }
  7603. }
  7604. }
  7605. set_dhash_acc_control(get_dhash_acc_control() | OPERATION_MODE);
  7606. }
  7607. else // vil mode
  7608. {
  7609. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE) | VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version));
  7610. set_hash_counting_number(0);
  7611. // prepare gateblk
  7612. buf_vil[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  7613. buf_vil[1] = 0x09;
  7614. buf_vil[2] = 0;
  7615. buf_vil[3] = MISC_CONTROL;
  7616. buf_vil[4] = 0x40;
  7617. buf_vil[5] = INV_CLKO; // enable INV_CLKO
  7618. buf_vil[6] = (dev->baud & 0x1f) | GATEBCLK; // enable gateblk
  7619. buf_vil[7] = MMEN; // MMEN=1
  7620. buf_vil[8] = CRC5(buf_vil, 8*8);
  7621. cmd_buf[0] = buf_vil[0]<<24 | buf_vil[1]<<16 | buf_vil[2]<<8 | buf_vil[3];
  7622. cmd_buf[1] = buf_vil[4]<<24 | buf_vil[5]<<16 | buf_vil[6]<<8 | buf_vil[7];
  7623. cmd_buf[2] = buf_vil[8]<<24;
  7624. // prepare special work for openning core
  7625. memset(buf_vil_tw, 0x00, TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int));
  7626. memset(&work_vil_1387, 0xff, sizeof(struct vil_work_1387));
  7627. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7628. {
  7629. if(dev->chain_exist[i] == 1)
  7630. {
  7631. ret = get_BC_write_command(); //disable null work
  7632. ret = BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7633. ret &= ~BC_COMMAND_EN_NULL_WORK;
  7634. set_BC_write_command(ret);
  7635. cgsleep_us(1000);
  7636. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  7637. work_vil_1387.chain_id = 0x80 | i;
  7638. work_vil_1387.reserved1[0]= 0;
  7639. work_vil_1387.reserved1[1]= 0;
  7640. work_vil_1387.work_count = 0;
  7641. work_vil_1387.data[0] = 0xff;
  7642. work_vil_1387.data[11] = 0xff;
  7643. set_BC_command_buffer(cmd_buf);
  7644. ret = get_BC_write_command();
  7645. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7646. //ret &= ~BC_COMMAND_EN_NULL_WORK;
  7647. set_BC_write_command(value);
  7648. cgsleep_us(10000);
  7649. for(m=0; m<loop; m++)
  7650. {
  7651. wati_count=0;
  7652. //applog(LOG_DEBUG,"%s: m = %d\n", __FUNCTION__, m);
  7653. do
  7654. {
  7655. work_fifo_ready = get_buffer_space();
  7656. if(work_fifo_ready & (0x1 << i))
  7657. {
  7658. wati_count=0;
  7659. break;
  7660. }
  7661. else //work fifo is full, wait for 50ms
  7662. {
  7663. //applog(LOG_DEBUG,"%s: chain%d work fifo not ready: 0x%x\n", __FUNCTION__, i, work_fifo_ready);
  7664. cgsleep_us(1000);
  7665. wati_count++;
  7666. if(wati_count>3000)
  7667. {
  7668. sprintf(logstr,"Error: send open core work Failed on Chain[%d]!\n",i);
  7669. writeInitLogFile(logstr);
  7670. break;
  7671. }
  7672. }
  7673. }
  7674. while(1);
  7675. if(wati_count>3000) // failed on send open core work
  7676. break;
  7677. if(m==0) //new block
  7678. {
  7679. work_vil_1387.work_type = NEW_BLOCK_MARKER;
  7680. }
  7681. else
  7682. {
  7683. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  7684. }
  7685. work_vil_1387.chain_id = i | 0x80; //set chain id and enable it
  7686. buf_vil_tw[0] = (work_vil_1387.work_type<< 24) | (work_vil_1387.chain_id << 16) | (work_vil_1387.reserved1[0] << 8) | work_vil_1387.reserved1[1];
  7687. buf_vil_tw[1] = work_vil_1387.work_count;
  7688. for(j=2; j<DATA2_LEN/sizeof(unsigned int)+2; j++)
  7689. {
  7690. buf_vil_tw[j] = (work_vil_1387.data[4*(j-2) + 0] << 24) | (work_vil_1387.data[4*(j-2) + 1] << 16) | (work_vil_1387.data[4*(j-2) + 2] << 8) | work_vil_1387.data[4*(j-2) + 3];
  7691. }
  7692. for(j=5; j<MIDSTATE_LEN/sizeof(unsigned int)+5; j++)
  7693. {
  7694. buf_vil_tw[j] = 0;
  7695. }
  7696. set_TW_write_command_vil(buf_vil_tw);
  7697. if(m==loop - 1 && nullwork_enable)
  7698. {
  7699. ret = get_BC_write_command(); //enable null work
  7700. ret |= BC_COMMAND_EN_NULL_WORK;
  7701. set_BC_write_command(ret);
  7702. }
  7703. }
  7704. }
  7705. }
  7706. set_dhash_acc_control(get_dhash_acc_control()| VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version));
  7707. }
  7708. }
  7709. void open_core_one_chain(int chainIndex, bool nullwork_enable)
  7710. {
  7711. unsigned int i = 0, j = 0, k, m, work_id = 0, ret = 0, value = 0, work_fifo_ready = 0, loop=0;
  7712. unsigned char gateblk[4] = {0,0,0,0};
  7713. unsigned int cmd_buf[3] = {0,0,0}, buf[TW_WRITE_COMMAND_LEN/sizeof(unsigned int)]= {0};
  7714. unsigned int buf_vil_tw[TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int)]= {0};
  7715. unsigned char data[TW_WRITE_COMMAND_LEN] = {0xff};
  7716. unsigned char buf_vil[9] = {0,0,0,0,0,0,0,0,0};
  7717. struct vil_work work_vil;
  7718. struct vil_work_1387 work_vil_1387;
  7719. char logstr[1024];
  7720. int wati_count=0;
  7721. #ifdef DEBUG_OPENCORE_TWICE
  7722. loop = BM1387_CORE_NUM-debug_once;
  7723. debug_once=0;
  7724. #else
  7725. loop = BM1387_CORE_NUM;
  7726. #endif
  7727. if(!opt_multi_version) // fil mode
  7728. {
  7729. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE));
  7730. set_hash_counting_number(0);
  7731. gateblk[0] = SET_BAUD_OPS;
  7732. gateblk[1] = 0;//0x10; //16-23
  7733. gateblk[2] = dev->baud | 0x80; //8-15 gateblk=1
  7734. gateblk[0] |= 0x80;
  7735. //gateblk[3] = CRC5(gateblk, 4*8 - 5);
  7736. gateblk[3] = 0x80; // MMEN=1
  7737. gateblk[3] = 0x80 | (0x1f & CRC5(gateblk, 4*8 - 5));
  7738. applog(LOG_DEBUG,"%s: gateblk[0]=0x%x, gateblk[1]=0x%x, gateblk[2]=0x%x, gateblk[3]=0x%x\n", __FUNCTION__, gateblk[0], gateblk[1], gateblk[2], gateblk[3]);
  7739. cmd_buf[0] = gateblk[0]<<24 | gateblk[1]<<16 | gateblk[2]<<8 | gateblk[3];
  7740. memset(data, 0x00, TW_WRITE_COMMAND_LEN);
  7741. data[TW_WRITE_COMMAND_LEN - 1] = 0xff;
  7742. data[TW_WRITE_COMMAND_LEN - 12] = 0xff;
  7743. i=chainIndex;
  7744. //for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7745. {
  7746. if(dev->chain_exist[i] == 1)
  7747. {
  7748. set_BC_command_buffer(cmd_buf);
  7749. ret = get_BC_write_command();
  7750. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7751. set_BC_write_command(value);
  7752. cgsleep_us(10000);
  7753. for(m=0; m<loop; m++)
  7754. {
  7755. do
  7756. {
  7757. work_fifo_ready = get_buffer_space();
  7758. if(work_fifo_ready & (0x1 << i))
  7759. {
  7760. break;
  7761. }
  7762. else //work fifo is full, wait for 50ms
  7763. {
  7764. //applog(LOG_DEBUG,"%s: chain%d work fifo not ready: 0x%x\n", __FUNCTION__, i, work_fifo_ready);
  7765. cgsleep_us(1000);
  7766. }
  7767. }
  7768. while(1);
  7769. if(m==0) //new block
  7770. {
  7771. data[0] = NEW_BLOCK_MARKER;
  7772. }
  7773. else
  7774. {
  7775. data[0] = NORMAL_BLOCK_MARKER;
  7776. }
  7777. data[1] = i | 0x80; //set chain id and enable it
  7778. if(m==0)
  7779. {
  7780. ret = get_BC_write_command(); //disable null work
  7781. ret &= ~BC_COMMAND_EN_NULL_WORK;
  7782. set_BC_write_command(ret);
  7783. }
  7784. if(m==loop - 1 && nullwork_enable)
  7785. {
  7786. ret = get_BC_write_command(); //enable null work
  7787. ret &= (BC_COMMAND_EN_CHAIN_ID | BC_COMMAND_EN_NULL_WORK | ((i & 0xf) << 16));
  7788. set_BC_write_command(ret);
  7789. }
  7790. memset(buf, 0, TW_WRITE_COMMAND_LEN/sizeof(unsigned int));
  7791. for(j=0; j<TW_WRITE_COMMAND_LEN/sizeof(unsigned int); j++)
  7792. {
  7793. buf[j] = (data[4*j + 0] << 24) | (data[4*j + 1] << 16) | (data[4*j + 2] << 8) | data[4*j + 3];
  7794. if(j==9)
  7795. {
  7796. buf[j] = work_id++;
  7797. }
  7798. //applog(LOG_DEBUG,"buf[%d] = 0x%x\n", j, buf[i]);
  7799. }
  7800. set_TW_write_command(buf);
  7801. }
  7802. }
  7803. }
  7804. set_dhash_acc_control(get_dhash_acc_control() | OPERATION_MODE);
  7805. }
  7806. else // vil mode
  7807. {
  7808. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE) | VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version));
  7809. set_hash_counting_number(0);
  7810. // prepare gateblk
  7811. buf_vil[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  7812. buf_vil[1] = 0x09;
  7813. buf_vil[2] = 0;
  7814. buf_vil[3] = MISC_CONTROL;
  7815. buf_vil[4] = 0x40;
  7816. buf_vil[5] = INV_CLKO; // enable INV_CLKO
  7817. buf_vil[6] = (dev->baud & 0x1f) | GATEBCLK; // enable gateblk
  7818. buf_vil[7] = MMEN; // MMEN=1
  7819. buf_vil[8] = CRC5(buf_vil, 8*8);
  7820. cmd_buf[0] = buf_vil[0]<<24 | buf_vil[1]<<16 | buf_vil[2]<<8 | buf_vil[3];
  7821. cmd_buf[1] = buf_vil[4]<<24 | buf_vil[5]<<16 | buf_vil[6]<<8 | buf_vil[7];
  7822. cmd_buf[2] = buf_vil[8]<<24;
  7823. // prepare special work for openning core
  7824. memset(buf_vil_tw, 0x00, TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int));
  7825. memset(&work_vil_1387, 0xff, sizeof(struct vil_work_1387));
  7826. i=chainIndex;
  7827. //for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7828. {
  7829. if(dev->chain_exist[i] == 1)
  7830. {
  7831. ret = get_BC_write_command(); //disable null work
  7832. ret = BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7833. ret &= ~BC_COMMAND_EN_NULL_WORK;
  7834. set_BC_write_command(ret);
  7835. cgsleep_us(1000);
  7836. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  7837. work_vil_1387.chain_id = 0x80 | i;
  7838. work_vil_1387.reserved1[0]= 0;
  7839. work_vil_1387.reserved1[1]= 0;
  7840. work_vil_1387.work_count = 0;
  7841. work_vil_1387.data[0] = 0xff;
  7842. work_vil_1387.data[11] = 0xff;
  7843. set_BC_command_buffer(cmd_buf);
  7844. ret = get_BC_write_command();
  7845. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7846. //ret &= ~BC_COMMAND_EN_NULL_WORK;
  7847. set_BC_write_command(value);
  7848. cgsleep_us(10000);
  7849. for(m=0; m<loop; m++)
  7850. {
  7851. wati_count=0;
  7852. //applog(LOG_DEBUG,"%s: m = %d\n", __FUNCTION__, m);
  7853. do
  7854. {
  7855. work_fifo_ready = get_buffer_space();
  7856. if(work_fifo_ready & (0x1 << i))
  7857. {
  7858. wati_count=0;
  7859. break;
  7860. }
  7861. else //work fifo is full, wait for 50ms
  7862. {
  7863. //applog(LOG_DEBUG,"%s: chain%d work fifo not ready: 0x%x\n", __FUNCTION__, i, work_fifo_ready);
  7864. cgsleep_us(1000);
  7865. wati_count++;
  7866. if(wati_count>3000)
  7867. {
  7868. sprintf(logstr,"Error: send open core work Failed on Chain[%d]!\n",i);
  7869. writeInitLogFile(logstr);
  7870. break;
  7871. }
  7872. }
  7873. }
  7874. while(1);
  7875. if(wati_count>3000) // failed on send open core work
  7876. break;
  7877. if(m==0) //new block
  7878. {
  7879. work_vil_1387.work_type = NEW_BLOCK_MARKER;
  7880. }
  7881. else
  7882. {
  7883. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  7884. }
  7885. work_vil_1387.chain_id = i | 0x80; //set chain id and enable it
  7886. buf_vil_tw[0] = (work_vil_1387.work_type<< 24) | (work_vil_1387.chain_id << 16) | (work_vil_1387.reserved1[0] << 8) | work_vil_1387.reserved1[1];
  7887. buf_vil_tw[1] = work_vil_1387.work_count;
  7888. for(j=2; j<DATA2_LEN/sizeof(unsigned int)+2; j++)
  7889. {
  7890. buf_vil_tw[j] = (work_vil_1387.data[4*(j-2) + 0] << 24) | (work_vil_1387.data[4*(j-2) + 1] << 16) | (work_vil_1387.data[4*(j-2) + 2] << 8) | work_vil_1387.data[4*(j-2) + 3];
  7891. }
  7892. for(j=5; j<MIDSTATE_LEN/sizeof(unsigned int)+5; j++)
  7893. {
  7894. buf_vil_tw[j] = 0;
  7895. }
  7896. set_TW_write_command_vil(buf_vil_tw);
  7897. if(m==loop - 1 && nullwork_enable)
  7898. {
  7899. ret = get_BC_write_command(); //enable null work
  7900. ret |= BC_COMMAND_EN_NULL_WORK;
  7901. set_BC_write_command(ret);
  7902. }
  7903. }
  7904. }
  7905. }
  7906. set_dhash_acc_control(get_dhash_acc_control()| VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version));
  7907. }
  7908. }
  7909. void open_core_onChain(int chainIndex, int coreNum, int opencore_num, bool nullwork_enable)
  7910. {
  7911. unsigned int i = 0, j = 0, k, m, work_id = 0, ret = 0, value = 0, work_fifo_ready = 0, loop=0;
  7912. unsigned char gateblk[4] = {0,0,0,0};
  7913. unsigned int cmd_buf[3] = {0,0,0}, buf[TW_WRITE_COMMAND_LEN/sizeof(unsigned int)]= {0};
  7914. unsigned int buf_vil_tw[TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int)]= {0};
  7915. unsigned char data[TW_WRITE_COMMAND_LEN] = {0xff};
  7916. unsigned char buf_vil[9] = {0,0,0,0,0,0,0,0,0};
  7917. struct vil_work work_vil;
  7918. struct vil_work_1387 work_vil_1387;
  7919. char logstr[1024];
  7920. int wati_count=0;
  7921. loop = coreNum;
  7922. if(!opt_multi_version) // fil mode
  7923. {
  7924. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE));
  7925. set_hash_counting_number(0);
  7926. gateblk[0] = SET_BAUD_OPS;
  7927. gateblk[1] = 0;//0x10; //16-23
  7928. gateblk[2] = dev->baud | 0x80; //8-15 gateblk=1
  7929. gateblk[0] |= 0x80;
  7930. //gateblk[3] = CRC5(gateblk, 4*8 - 5);
  7931. gateblk[3] = 0x80; // MMEN=1
  7932. gateblk[3] = 0x80 | (0x1f & CRC5(gateblk, 4*8 - 5));
  7933. applog(LOG_DEBUG,"%s: gateblk[0]=0x%x, gateblk[1]=0x%x, gateblk[2]=0x%x, gateblk[3]=0x%x\n", __FUNCTION__, gateblk[0], gateblk[1], gateblk[2], gateblk[3]);
  7934. cmd_buf[0] = gateblk[0]<<24 | gateblk[1]<<16 | gateblk[2]<<8 | gateblk[3];
  7935. memset(data, 0x00, TW_WRITE_COMMAND_LEN);
  7936. data[TW_WRITE_COMMAND_LEN - 1] = 0xff;
  7937. data[TW_WRITE_COMMAND_LEN - 12] = 0xff;
  7938. i=chainIndex;
  7939. //for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  7940. {
  7941. if(dev->chain_exist[i] == 1)
  7942. {
  7943. set_BC_command_buffer(cmd_buf);
  7944. ret = get_BC_write_command();
  7945. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  7946. set_BC_write_command(value);
  7947. cgsleep_us(10000);
  7948. for(m=0; m<loop; m++)
  7949. {
  7950. do
  7951. {
  7952. work_fifo_ready = get_buffer_space();
  7953. if(work_fifo_ready & (0x1 << i))
  7954. {
  7955. break;
  7956. }
  7957. else //work fifo is full, wait for 50ms
  7958. {
  7959. //applog(LOG_DEBUG,"%s: chain%d work fifo not ready: 0x%x\n", __FUNCTION__, i, work_fifo_ready);
  7960. cgsleep_us(1000);
  7961. }
  7962. }
  7963. while(1);
  7964. if(m==0) //new block
  7965. {
  7966. data[0] = NEW_BLOCK_MARKER;
  7967. }
  7968. else
  7969. {
  7970. data[0] = NORMAL_BLOCK_MARKER;
  7971. }
  7972. data[1] = i | 0x80; //set chain id and enable it
  7973. if(m==0)
  7974. {
  7975. ret = get_BC_write_command(); //disable null work
  7976. ret &= ~BC_COMMAND_EN_NULL_WORK;
  7977. set_BC_write_command(ret);
  7978. }
  7979. if(m==loop - 1 && nullwork_enable)
  7980. {
  7981. ret = get_BC_write_command(); //enable null work
  7982. ret &= (BC_COMMAND_EN_CHAIN_ID | BC_COMMAND_EN_NULL_WORK | ((i & 0xf) << 16));
  7983. set_BC_write_command(ret);
  7984. }
  7985. memset(buf, 0, TW_WRITE_COMMAND_LEN/sizeof(unsigned int));
  7986. for(j=0; j<TW_WRITE_COMMAND_LEN/sizeof(unsigned int); j++)
  7987. {
  7988. buf[j] = (data[4*j + 0] << 24) | (data[4*j + 1] << 16) | (data[4*j + 2] << 8) | data[4*j + 3];
  7989. if(j==9)
  7990. {
  7991. buf[j] = work_id++;
  7992. }
  7993. //applog(LOG_DEBUG,"buf[%d] = 0x%x\n", j, buf[i]);
  7994. }
  7995. set_TW_write_command(buf);
  7996. }
  7997. }
  7998. }
  7999. set_dhash_acc_control(get_dhash_acc_control() | OPERATION_MODE);
  8000. }
  8001. else // vil mode
  8002. {
  8003. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE) | VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version));
  8004. set_hash_counting_number(0);
  8005. // prepare gateblk
  8006. buf_vil[0] = VIL_COMMAND_TYPE | VIL_ALL | SET_CONFIG;
  8007. buf_vil[1] = 0x09;
  8008. buf_vil[2] = 0;
  8009. buf_vil[3] = MISC_CONTROL;
  8010. buf_vil[4] = 0x40;
  8011. buf_vil[5] = INV_CLKO; // enable INV_CLKO
  8012. buf_vil[6] = (dev->baud & 0x1f) | GATEBCLK; // enable gateblk
  8013. buf_vil[7] = MMEN; // MMEN=1
  8014. buf_vil[8] = CRC5(buf_vil, 8*8);
  8015. cmd_buf[0] = buf_vil[0]<<24 | buf_vil[1]<<16 | buf_vil[2]<<8 | buf_vil[3];
  8016. cmd_buf[1] = buf_vil[4]<<24 | buf_vil[5]<<16 | buf_vil[6]<<8 | buf_vil[7];
  8017. cmd_buf[2] = buf_vil[8]<<24;
  8018. // prepare special work for openning core
  8019. memset(buf_vil_tw, 0x00, TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int));
  8020. memset(&work_vil_1387, 0xff, sizeof(struct vil_work_1387));
  8021. i=chainIndex;
  8022. //for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8023. {
  8024. if(dev->chain_exist[i] == 1)
  8025. {
  8026. ret = get_BC_write_command(); //disable null work
  8027. ret = BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  8028. ret &= ~BC_COMMAND_EN_NULL_WORK;
  8029. set_BC_write_command(ret);
  8030. cgsleep_us(1000);
  8031. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  8032. work_vil_1387.chain_id = 0x80 | i;
  8033. work_vil_1387.reserved1[0]= 0;
  8034. work_vil_1387.reserved1[1]= 0;
  8035. work_vil_1387.work_count = 0;
  8036. work_vil_1387.data[0] = 0xff;
  8037. work_vil_1387.data[11] = 0xff;
  8038. set_BC_command_buffer(cmd_buf);
  8039. ret = get_BC_write_command();
  8040. value = BC_COMMAND_BUFFER_READY | BC_COMMAND_EN_CHAIN_ID | (i << 16) | (ret & 0xfff0ffff);
  8041. //ret &= ~BC_COMMAND_EN_NULL_WORK;
  8042. set_BC_write_command(value);
  8043. cgsleep_us(10000);
  8044. for(m=0; m<loop; m++)
  8045. {
  8046. if(m>=opencore_num)
  8047. {
  8048. work_vil_1387.data[0] = 0x0;
  8049. work_vil_1387.data[11] = 0x0;
  8050. }
  8051. else
  8052. {
  8053. work_vil_1387.data[0] = 0xff;
  8054. work_vil_1387.data[11] = 0xff;
  8055. }
  8056. wati_count=0;
  8057. //applog(LOG_DEBUG,"%s: m = %d\n", __FUNCTION__, m);
  8058. do
  8059. {
  8060. work_fifo_ready = get_buffer_space();
  8061. if(work_fifo_ready & (0x1 << i))
  8062. {
  8063. wati_count=0;
  8064. break;
  8065. }
  8066. else //work fifo is full, wait for 50ms
  8067. {
  8068. //applog(LOG_DEBUG,"%s: chain%d work fifo not ready: 0x%x\n", __FUNCTION__, i, work_fifo_ready);
  8069. cgsleep_us(1000);
  8070. wati_count++;
  8071. if(wati_count>3000)
  8072. {
  8073. sprintf(logstr,"Error: send open core work Failed on Chain[%d]!\n",i);
  8074. writeInitLogFile(logstr);
  8075. break;
  8076. }
  8077. }
  8078. }
  8079. while(1);
  8080. if(wati_count>3000) // failed on send open core work
  8081. break;
  8082. if(m==0) //new block
  8083. {
  8084. work_vil_1387.work_type = NEW_BLOCK_MARKER;
  8085. }
  8086. else
  8087. {
  8088. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  8089. }
  8090. work_vil_1387.chain_id = i | 0x80; //set chain id and enable it
  8091. buf_vil_tw[0] = (work_vil_1387.work_type<< 24) | (work_vil_1387.chain_id << 16) | (work_vil_1387.reserved1[0] << 8) | work_vil_1387.reserved1[1];
  8092. buf_vil_tw[1] = work_vil_1387.work_count;
  8093. for(j=2; j<DATA2_LEN/sizeof(unsigned int)+2; j++)
  8094. {
  8095. buf_vil_tw[j] = (work_vil_1387.data[4*(j-2) + 0] << 24) | (work_vil_1387.data[4*(j-2) + 1] << 16) | (work_vil_1387.data[4*(j-2) + 2] << 8) | work_vil_1387.data[4*(j-2) + 3];
  8096. }
  8097. for(j=5; j<MIDSTATE_LEN/sizeof(unsigned int)+5; j++)
  8098. {
  8099. buf_vil_tw[j] = 0;
  8100. }
  8101. set_TW_write_command_vil(buf_vil_tw);
  8102. if(m==loop - 1 && nullwork_enable)
  8103. {
  8104. ret = get_BC_write_command(); //enable null work
  8105. ret |= BC_COMMAND_EN_NULL_WORK;
  8106. set_BC_write_command(ret);
  8107. }
  8108. }
  8109. }
  8110. }
  8111. set_dhash_acc_control(get_dhash_acc_control()| VIL_MODE | VIL_MIDSTATE_NUMBER(opt_multi_version));
  8112. }
  8113. }
  8114. void opencore_onebyone_onChain(int chainIndex)
  8115. {
  8116. int i;
  8117. for(i=0; i<BM1387_CORE_NUM; i++)
  8118. {
  8119. open_core_onChain(chainIndex,BM1387_CORE_NUM,i+1,true);
  8120. usleep(100000);
  8121. }
  8122. }
  8123. #ifdef USE_PREINIT_OPENCORE
  8124. #define PRE_OPENCORE_FREQ 8
  8125. void getAsicNum_preOpenCore(int chainIndex)
  8126. {
  8127. char logstr[1024];
  8128. int i,j;
  8129. int each_asic_freq;
  8130. int freq_test=PRE_OPENCORE_FREQ; //300M
  8131. int freq_value=atoi(freq_pll_1385[freq_test].freq);
  8132. for(j=0; j<2; j++)
  8133. {
  8134. for(i=0; i<6; i++)
  8135. {
  8136. software_set_address_onChain(chainIndex);
  8137. for(each_asic_freq = 0; each_asic_freq < CHAIN_ASIC_NUM; each_asic_freq ++)
  8138. {
  8139. set_frequency_with_addr_plldatai(freq_test, 0, each_asic_freq * dev->addrInterval, chainIndex);
  8140. }
  8141. dev->timeout = 0x1000000/calculate_core_number(BM1387_CORE_NUM)*dev->addrInterval/freq_value*10/100; // 10% timeout
  8142. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  8143. //open core : inside of this function, set baud into FPGA, so we need set default before here.
  8144. open_core_onChain(chainIndex,BM1387_CORE_NUM,i+1,true); // open 1 cores
  8145. sprintf(logstr,"PreOpenCore: open %d cores Done!\n",i+1);
  8146. writeInitLogFile(logstr);
  8147. dev->chain_asic_num[chainIndex]=0;
  8148. check_asic_reg_oneChain(chainIndex,CHIP_ADDRESS);
  8149. sprintf(logstr,"PreOpenCore: chain[%d] get asicNum = %d\n",chainIndex, dev->chain_asic_num[chainIndex]);
  8150. writeInitLogFile(logstr);
  8151. if(dev->chain_asic_num[chainIndex]==CHAIN_ASIC_NUM)
  8152. return;
  8153. }
  8154. #ifdef USE_NEW_RESET_FPGA
  8155. set_reset_hashboard(chainIndex,1);
  8156. sleep(RESET_KEEP_TIME);
  8157. set_reset_hashboard(chainIndex,0);
  8158. sleep(1);
  8159. #else
  8160. reset_one_hashboard(chainIndex);
  8161. #endif
  8162. }
  8163. }
  8164. #endif
  8165. void insert_reg_data(unsigned int *buf)
  8166. {
  8167. char logstr[1024];
  8168. if(reg_value_buf.reg_value_num >= MAX_NONCE_NUMBER_IN_FIFO || reg_value_buf.p_wr >= MAX_NONCE_NUMBER_IN_FIFO)
  8169. {
  8170. clear_register_value_buf();
  8171. return;
  8172. }
  8173. pthread_mutex_lock(&reg_mutex);
  8174. reg_value_buf.reg_buffer[reg_value_buf.p_wr].reg_value = buf[1];
  8175. reg_value_buf.reg_buffer[reg_value_buf.p_wr].crc = (buf[0] >> 24) & 0x1f;
  8176. reg_value_buf.reg_buffer[reg_value_buf.p_wr].chain_number = CHAIN_NUMBER(buf[0]);
  8177. #ifdef DEBUG_XILINX_NONCE_NOTENOUGH
  8178. if(doTestPatten)
  8179. {
  8180. if(CHAIN_NUMBER(buf[0])==DISABLE_REG_CHAIN_INDEX)
  8181. {
  8182. sprintf(logstr,"Fatal Debug Error: get reg data on Chain[%d]!\n",CHAIN_NUMBER(buf[0]));
  8183. writeInitLogFile(logstr);
  8184. }
  8185. }
  8186. #endif
  8187. if(reg_value_buf.p_wr < MAX_NONCE_NUMBER_IN_FIFO - 1)
  8188. {
  8189. reg_value_buf.p_wr++;
  8190. }
  8191. else
  8192. {
  8193. reg_value_buf.p_wr = 0;
  8194. }
  8195. if(reg_value_buf.reg_value_num < MAX_NONCE_NUMBER_IN_FIFO)
  8196. {
  8197. reg_value_buf.reg_value_num++;
  8198. }
  8199. else
  8200. {
  8201. reg_value_buf.reg_value_num = MAX_NONCE_NUMBER_IN_FIFO;
  8202. }
  8203. //applog(LOG_NOTICE,"%s: p_wr = %d reg_value_num = %d\n", __FUNCTION__,reg_value_buf.p_wr,reg_value_buf.reg_value_num);
  8204. pthread_mutex_unlock(&reg_mutex);
  8205. }
  8206. void * get_nonce_and_register()
  8207. {
  8208. unsigned int work_id=0, *data_addr=NULL;
  8209. unsigned int i=0, j=0, m=0, nonce_number = 0, read_loop=0;
  8210. unsigned int buf[2] = {0,0};
  8211. uint64_t n2h = 0, n2l = 0;
  8212. char ret = 0;
  8213. unsigned int nonce_p_wr=0, nonce_p_rd=0, nonce_nonce_num=0, nonce_loop_back=0;
  8214. unsigned int reg_p_wr=0, reg_p_rd=0, reg_reg_value_num=0, reg_loop_back=0;
  8215. char *buf_hex = NULL;
  8216. while(1)
  8217. {
  8218. cgsleep_ms(1);
  8219. if(doTestPatten)
  8220. {
  8221. cgsleep_ms(100);
  8222. continue;
  8223. }
  8224. i = 0;
  8225. read_loop = 0;
  8226. nonce_number = get_nonce_number_in_fifo() & MAX_NONCE_NUMBER_IN_FIFO;
  8227. if(nonce_number)
  8228. {
  8229. read_loop = nonce_number;
  8230. applog(LOG_DEBUG,"%s: read_loop = %d\n", __FUNCTION__, read_loop);
  8231. for(j=0; j<read_loop; j++)
  8232. {
  8233. get_return_nonce(buf);
  8234. if(buf[0] & WORK_ID_OR_CRC) //nonce
  8235. {
  8236. if(gBegin_get_nonce)
  8237. {
  8238. if(buf[0] & NONCE_INDICATOR)
  8239. {
  8240. pthread_mutex_lock(&nonce_mutex);
  8241. work_id = WORK_ID_OR_CRC_VALUE(buf[0]);
  8242. data_addr = (unsigned int *)((unsigned char *)nonce2_jobid_address + work_id*64);
  8243. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].work_id = work_id;
  8244. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].nonce3 = buf[1];
  8245. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].chain_num = buf[0] & 0x0000000f;
  8246. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].job_id = *(data_addr + JOB_ID_OFFSET);
  8247. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].header_version = *(data_addr + HEADER_VERSION_OFFSET);
  8248. n2h = *(data_addr + NONCE2_H_OFFSET);
  8249. n2l = *(data_addr + NONCE2_L_OFFSET);
  8250. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].nonce2 = (n2h << 32) | (n2l);
  8251. for(m=0; m<MIDSTATE_LEN; m++)
  8252. {
  8253. nonce_read_out.nonce_buffer[nonce_read_out.p_wr].midstate[m] = *((unsigned char *)data_addr + MIDSTATE_OFFSET + m);
  8254. }
  8255. //applog(LOG_DEBUG,"%s: buf[0] = 0x%x\n", __FUNCTION__, buf[0]);
  8256. //applog(LOG_DEBUG,"%s: work_id = 0x%x\n", __FUNCTION__, work_id);
  8257. //applog(LOG_DEBUG,"%s: nonce2_jobid_address = 0x%x\n", __FUNCTION__, nonce2_jobid_address);
  8258. //applog(LOG_DEBUG,"%s: data_addr = 0x%x\n", __FUNCTION__, data_addr);
  8259. //applog(LOG_DEBUG,"%s: nonce3 = 0x%x\n", __FUNCTION__, nonce_read_out.nonce_buffer[nonce_read_out.p_wr].nonce3);
  8260. //applog(LOG_DEBUG,"%s: job_id = 0x%x\n", __FUNCTION__, nonce_read_out.nonce_buffer[nonce_read_out.p_wr].job_id);
  8261. //applog(LOG_DEBUG,"%s: header_version = 0x%x\n", __FUNCTION__, nonce_read_out.nonce_buffer[nonce_read_out.p_wr].header_version);
  8262. //applog(LOG_DEBUG,"%s: nonce2 = 0x%x\n", __FUNCTION__, nonce_read_out.nonce_buffer[nonce_read_out.p_wr].nonce2);
  8263. //buf_hex = bin2hex(nonce_read_out.nonce_buffer[nonce_read_out.p_wr].midstate,32);
  8264. //applog(LOG_DEBUG,"%s: midstate: %s\n", __FUNCTION__, buf_hex);
  8265. //free(buf_hex);
  8266. if(nonce_read_out.p_wr < MAX_NONCE_NUMBER_IN_FIFO - 1)
  8267. {
  8268. nonce_read_out.p_wr++;
  8269. }
  8270. else
  8271. {
  8272. nonce_read_out.p_wr = 0;
  8273. }
  8274. if(nonce_read_out.nonce_num < MAX_NONCE_NUMBER_IN_FIFO)
  8275. {
  8276. nonce_read_out.nonce_num++;
  8277. }
  8278. else
  8279. {
  8280. nonce_read_out.nonce_num = MAX_NONCE_NUMBER_IN_FIFO;
  8281. }
  8282. pthread_mutex_unlock(&nonce_mutex);
  8283. }
  8284. }
  8285. }
  8286. else //reg value
  8287. {
  8288. if(reg_value_buf.reg_value_num >= MAX_NONCE_NUMBER_IN_FIFO || reg_value_buf.p_wr >= MAX_NONCE_NUMBER_IN_FIFO)
  8289. {
  8290. clear_register_value_buf();
  8291. continue;
  8292. }
  8293. pthread_mutex_lock(&reg_mutex);
  8294. reg_value_buf.reg_buffer[reg_value_buf.p_wr].reg_value = buf[1];
  8295. reg_value_buf.reg_buffer[reg_value_buf.p_wr].crc = (buf[0] >> 24) & 0x1f;
  8296. reg_value_buf.reg_buffer[reg_value_buf.p_wr].chain_number = CHAIN_NUMBER(buf[0]);
  8297. if(reg_value_buf.p_wr < MAX_NONCE_NUMBER_IN_FIFO - 1)
  8298. {
  8299. reg_value_buf.p_wr++;
  8300. }
  8301. else
  8302. {
  8303. reg_value_buf.p_wr = 0;
  8304. }
  8305. if(reg_value_buf.reg_value_num < MAX_NONCE_NUMBER_IN_FIFO)
  8306. {
  8307. reg_value_buf.reg_value_num++;
  8308. }
  8309. else
  8310. {
  8311. reg_value_buf.reg_value_num = MAX_NONCE_NUMBER_IN_FIFO;
  8312. }
  8313. //applog(LOG_NOTICE,"%s: p_wr = %d reg_value_num = %d\n", __FUNCTION__,reg_value_buf.p_wr,reg_value_buf.reg_value_num);
  8314. pthread_mutex_unlock(&reg_mutex);
  8315. }
  8316. }
  8317. }
  8318. }
  8319. }
  8320. int getChainAsicNum(int chainIndex)
  8321. {
  8322. return dev->chain_asic_num[chainIndex];
  8323. }
  8324. int getChainExistFlag(int chainIndex)
  8325. {
  8326. return dev->chain_exist[chainIndex];
  8327. }
  8328. void saveSearchFailedFlagInfo(char *search_failed_info)
  8329. {
  8330. FILE *fd;
  8331. fd=fopen("/tmp/searcherror","wb");
  8332. if(fd)
  8333. {
  8334. fwrite(search_failed_info,1,strlen(search_failed_info)+1,fd);
  8335. fclose(fd);
  8336. }
  8337. system("cp /tmp/search /tmp/err1.log -f");
  8338. system("cp /tmp/freq /tmp/err2.log -f");
  8339. system("cp /tmp/lasttemp /tmp/err3.log -f");
  8340. system("sync");
  8341. }
  8342. int bitmain_soc_init(struct init_config config)
  8343. {
  8344. char ret=0,j;
  8345. uint16_t crc = 0;
  8346. bool test_result;
  8347. int i=0,x = 0,y = 0;
  8348. int hardware_version;
  8349. unsigned int data = 0;
  8350. bool testRet;
  8351. int testCounter=0;
  8352. struct sysinfo si;
  8353. char logstr[1024];
  8354. #ifdef DISABLE_FINAL_TEST // if disable test mode, we need set two value and save into files on flash
  8355. saveRestartNum(2);
  8356. saveRebootTestNum(0);
  8357. #endif
  8358. clearInitLogFile();
  8359. isC5_CtrlBoard=isC5_Board();
  8360. if(isC5_CtrlBoard)
  8361. {
  8362. #ifdef R4
  8363. MIN_PWM_PERCENT=MIN_PWM_PERCENT_C5;
  8364. MID_PWM_PERCENT=MID_PWM_PERCENT_C5;
  8365. MAX_PWM_PERCENT=MAX_PWM_PERCENT_C5;
  8366. MAX_TEMP=MAX_TEMP_C5;
  8367. MAX_FAN_TEMP=MAX_FAN_TEMP_C5;
  8368. MID_FAN_TEMP=MID_FAN_TEMP_C5;
  8369. MIN_FAN_TEMP=MIN_FAN_TEMP_C5;
  8370. MAX_PCB_TEMP=MAX_PCB_TEMP_C5;
  8371. MAX_FAN_PCB_TEMP=MAX_FAN_PCB_TEMP_C5;
  8372. #endif
  8373. PHY_MEM_NONCE2_JOBID_ADDRESS=PHY_MEM_NONCE2_JOBID_ADDRESS_C5;
  8374. sprintf(logstr,"This is C5 board.\n");
  8375. }
  8376. else
  8377. {
  8378. #ifdef R4
  8379. MIN_PWM_PERCENT=MIN_PWM_PERCENT_XILINX;
  8380. MID_PWM_PERCENT=MID_PWM_PERCENT_XILINX;
  8381. MAX_PWM_PERCENT=MAX_PWM_PERCENT_XILINX;
  8382. MAX_TEMP=MAX_TEMP_XILINX;
  8383. MAX_FAN_TEMP=MAX_FAN_TEMP_XILINX;
  8384. MID_FAN_TEMP=MID_FAN_TEMP_XILINX;
  8385. MIN_FAN_TEMP=MIN_FAN_TEMP_XILINX;
  8386. MAX_PCB_TEMP=MAX_PCB_TEMP_XILINX;
  8387. MAX_FAN_PCB_TEMP=MAX_FAN_PCB_TEMP_XILINX;
  8388. #endif
  8389. sysinfo(&si);
  8390. if(si.totalram > 1000000000)
  8391. {
  8392. PHY_MEM_NONCE2_JOBID_ADDRESS=PHY_MEM_NONCE2_JOBID_ADDRESS_XILINX_1GB;
  8393. sprintf(logstr, "Detect 1GB control board of XILINX\n");
  8394. }
  8395. else if(si.totalram > 500000000)
  8396. {
  8397. PHY_MEM_NONCE2_JOBID_ADDRESS=PHY_MEM_NONCE2_JOBID_ADDRESS_XILINX_512MB;
  8398. sprintf(logstr, "Detect 512MB control board of XILINX\n");
  8399. }
  8400. else
  8401. {
  8402. PHY_MEM_NONCE2_JOBID_ADDRESS=PHY_MEM_NONCE2_JOBID_ADDRESS_XILINX_256MB;
  8403. sprintf(logstr, "Detect 256MB control board of XILINX\n");
  8404. }
  8405. }
  8406. writeInitLogFile(logstr);
  8407. #ifdef R4 // if defined , for R4 63 chips
  8408. sprintf(logstr,"Miner Type = R4\n");
  8409. writeInitLogFile(logstr);
  8410. #endif
  8411. #ifdef S9_PLUS // if defined , for T9 57 chips
  8412. sprintf(logstr,"Miner Type = T9\n");
  8413. writeInitLogFile(logstr);
  8414. #endif
  8415. #ifdef S9_63 // if defined , for S9 63 chips
  8416. sprintf(logstr,"Miner Type = S9\n");
  8417. writeInitLogFile(logstr);
  8418. #endif
  8419. #ifdef T9_18 // if defined , for T9+ 18 chips
  8420. sprintf(logstr,"Miner Type = T9+\n");
  8421. writeInitLogFile(logstr);
  8422. #endif
  8423. memcpy(&config_parameter, &config, sizeof(struct init_config));
  8424. if(config_parameter.token_type != INIT_CONFIG_TYPE)
  8425. {
  8426. applog(LOG_DEBUG,"%s: config_parameter.token_type != 0x%x, it is 0x%x\n", __FUNCTION__, INIT_CONFIG_TYPE, config_parameter.token_type);
  8427. return -1;
  8428. }
  8429. crc = CRC16((uint8_t*)(&config_parameter), sizeof(struct init_config) - sizeof(uint16_t));
  8430. if(crc != config_parameter.crc)
  8431. {
  8432. applog(LOG_DEBUG,"%s: config_parameter.crc = 0x%x, but we calculate it as 0x%x\n", __FUNCTION__, config_parameter.crc, crc);
  8433. return -2;
  8434. }
  8435. read_nonce_reg_id = calloc(1,sizeof(struct thr_info));
  8436. if(thr_info_create(read_nonce_reg_id, NULL, get_nonce_and_register, read_nonce_reg_id))
  8437. {
  8438. applog(LOG_DEBUG,"%s: create thread for get nonce and register from FPGA failed\n", __FUNCTION__);
  8439. return -5;
  8440. }
  8441. pthread_detach(read_nonce_reg_id->pth);
  8442. //init axi
  8443. bitmain_axi_init();
  8444. #ifdef USE_NEW_RESET_FPGA
  8445. set_reset_allhashboard(1);
  8446. sleep(RESET_KEEP_TIME);
  8447. set_reset_allhashboard(0);
  8448. sleep(1);
  8449. set_reset_allhashboard(1);
  8450. #endif
  8451. //reset FPGA & HASH board
  8452. if(config_parameter.reset)
  8453. {
  8454. set_QN_write_data_command(RESET_HASH_BOARD | RESET_ALL | RESET_FPGA | RESET_TIME(RESET_HASHBOARD_TIME));
  8455. #ifdef USE_NEW_RESET_FPGA
  8456. sleep(2);
  8457. #else
  8458. while(get_QN_write_data_command() & RESET_HASH_BOARD)
  8459. {
  8460. cgsleep_ms(30);
  8461. }
  8462. cgsleep_ms(500);
  8463. #endif
  8464. set_PWM(MAX_PWM_PERCENT);
  8465. }
  8466. #ifdef T9_18
  8467. // config fpga into T9+ mode
  8468. set_Hardware_version(0x80000000);
  8469. #endif
  8470. #ifdef DEBUG_PRINT_T9_PLUS_PIC_HEART_INFO
  8471. set_red_led(false); // close red led
  8472. #endif
  8473. // need read FPGA version at first, used in T9+
  8474. hardware_version = get_hardware_version();
  8475. pcb_version = (hardware_version >> 16) & 0x00007fff; // for T9+ the highest bit is used as config for S9 or T9+ mode, so use 7fff
  8476. fpga_version = hardware_version & 0x000000ff;
  8477. sprintf(g_miner_version, "%d.%d.%d.%d", fpga_version, pcb_version, SOC_VERSION, BMMINER_VERSION);
  8478. #ifdef USE_NEW_RESET_FPGA
  8479. set_reset_allhashboard(1);
  8480. #endif
  8481. dev->baud=DEFAULT_BAUD_VALUE; // need set default value as init value
  8482. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  8483. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  8484. //check chain
  8485. check_chain();
  8486. #ifdef T9_18
  8487. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8488. {
  8489. if(dev->chain_exist[i] == 1)
  8490. {
  8491. if(fpga_version>=0xE)
  8492. {
  8493. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  8494. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  8495. pthread_mutex_lock(&iic_mutex);
  8496. if(i>=1 && i<=3)
  8497. {
  8498. reset_iic_pic(i);
  8499. jump_to_app_CheckAndRestorePIC_T9_18(i);
  8500. if(!isFixedFreqMode())
  8501. {
  8502. read_freq_badcores(i,chain_pic_buf[i]);
  8503. sprintf(logstr,"Chain[%d] read_freq_badcores : ",i);
  8504. writeInitLogFile(logstr);
  8505. for(j=0; j<128; j++)
  8506. {
  8507. sprintf(logstr,"0x%02x ",chain_pic_buf[i][j]);
  8508. writeInitLogFile(logstr);
  8509. }
  8510. sprintf(logstr,"\n");
  8511. writeInitLogFile(logstr);
  8512. }
  8513. }
  8514. if(!isFixedFreqMode())
  8515. {
  8516. if(chain_pic_buf[new_T9_PLUS_chainIndex][0] == FREQ_MAGIC)
  8517. {
  8518. chain_voltage_value[i]=chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+1]*10;
  8519. sprintf(logstr,"Chain[J%d] has backup chain_voltage=%d\n",i+1,chain_voltage_value[i]);
  8520. writeInitLogFile(logstr);
  8521. }
  8522. lowest_testOK_temp[i]=(signed char)chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+3];
  8523. sprintf(logstr,"Chain[J%d] test patten OK temp=%d\n",i+1,lowest_testOK_temp[i]);
  8524. writeInitLogFile(logstr);
  8525. #ifdef DISABLE_TEMP_PROTECT // for debug
  8526. if(lowest_testOK_temp[i]<MIN_TEMP_CONTINUE_DOWN_FAN)
  8527. lowest_testOK_temp[i]=MIN_TEMP_CONTINUE_DOWN_FAN; // if too low, we just set MIN_TEMP_CONTINUE_DOWN_FAN
  8528. #endif
  8529. }
  8530. pthread_mutex_unlock(&iic_mutex);
  8531. }
  8532. else
  8533. {
  8534. pthread_mutex_lock(&iic_mutex);
  8535. // if(i%3==0) read into chain_pic_buf
  8536. if(i%3==0)
  8537. {
  8538. reset_iic_pic(i);
  8539. sleep(1);
  8540. jump_to_app_CheckAndRestorePIC_T9_18(i);
  8541. if(!isFixedFreqMode())
  8542. read_freq_badcores(i,chain_pic_buf[((i/3)*3)]);
  8543. }
  8544. if(!isFixedFreqMode())
  8545. {
  8546. if(chain_pic_buf[((i/3)*3)][0] == FREQ_MAGIC)
  8547. {
  8548. chain_voltage_value[i]=chain_pic_buf[((i/3)*3)][7+(i%3)*31+1]*10;
  8549. sprintf(logstr,"Chain[J%d] has backup chain_voltage=%d\n",i+1,chain_voltage_value[i]);
  8550. writeInitLogFile(logstr);
  8551. }
  8552. lowest_testOK_temp[i]=(signed char)chain_pic_buf[((i/3)*3)][7+(i%3)*31+3];
  8553. sprintf(logstr,"Chain[J%d] test patten OK temp=%d\n",i+1,lowest_testOK_temp[i]);
  8554. writeInitLogFile(logstr);
  8555. #ifdef DISABLE_TEMP_PROTECT // for debug
  8556. if(lowest_testOK_temp[i]<MIN_TEMP_CONTINUE_DOWN_FAN)
  8557. lowest_testOK_temp[i]=MIN_TEMP_CONTINUE_DOWN_FAN; // if too low, we just set MIN_TEMP_CONTINUE_DOWN_FAN
  8558. #endif
  8559. }
  8560. pthread_mutex_unlock(&iic_mutex);
  8561. }
  8562. }
  8563. }
  8564. #else
  8565. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8566. {
  8567. if(dev->chain_exist[i] == 1)
  8568. {
  8569. pthread_mutex_lock(&iic_mutex);
  8570. reset_iic_pic(i);
  8571. cgsleep_ms(500);
  8572. if(!isFixedFreqMode())
  8573. {
  8574. set_pic_iic_flash_addr_pointer(i, PIC_FLASH_POINTER_FREQ_START_ADDRESS_H, PIC_FLASH_POINTER_FREQ_START_ADDRESS_L);
  8575. get_data_from_pic_flash(i, last_freq[i]);
  8576. get_data_from_pic_flash(i, last_freq[i]+16);
  8577. get_data_from_pic_flash(i, last_freq[i]+32);
  8578. get_data_from_pic_flash(i, last_freq[i]+48);
  8579. get_data_from_pic_flash(i, last_freq[i]+64);
  8580. get_data_from_pic_flash(i, last_freq[i]+80);
  8581. get_data_from_pic_flash(i, last_freq[i]+96);
  8582. get_data_from_pic_flash(i, last_freq[i]+112);
  8583. set_pic_iic_flash_addr_pointer(i, PIC_FLASH_POINTER_BADCORE_START_ADDRESS_H, PIC_FLASH_POINTER_BADCORE_START_ADDRESS_L);
  8584. get_data_from_pic_flash(i, badcore_num_buf[i]);
  8585. get_data_from_pic_flash(i, badcore_num_buf[i]+16);
  8586. get_data_from_pic_flash(i, badcore_num_buf[i]+32);
  8587. get_data_from_pic_flash(i, badcore_num_buf[i]+48);
  8588. if(last_freq[i][1] == FREQ_MAGIC && last_freq[i][40] == 0x23) //0x23 is backup voltage magic number
  8589. {
  8590. chain_voltage_value[i]=(((last_freq[i][36]&0x0f)<<4)+(last_freq[i][38]&0x0f))*10;
  8591. sprintf(logstr,"Chain[J%d] has backup chain_voltage=%d\n",i+1,chain_voltage_value[i]);
  8592. writeInitLogFile(logstr);
  8593. }
  8594. if(last_freq[i][1] == FREQ_MAGIC && last_freq[i][46] == 0x23) //0x23 is board temp magic number
  8595. {
  8596. lowest_testOK_temp[i]=(signed char)(((last_freq[i][42]&0x0f)<<4)+(last_freq[i][44]&0x0f));
  8597. sprintf(logstr,"Chain[J%d] test patten OK temp=%d\n",i+1,lowest_testOK_temp[i]);
  8598. writeInitLogFile(logstr);
  8599. #ifdef DISABLE_TEMP_PROTECT // for debug
  8600. if(lowest_testOK_temp[i]<MIN_TEMP_CONTINUE_DOWN_FAN)
  8601. lowest_testOK_temp[i]=MIN_TEMP_CONTINUE_DOWN_FAN; // if too low, we just set MIN_TEMP_CONTINUE_DOWN_FAN
  8602. #endif
  8603. }
  8604. }
  8605. jump_to_app_CheckAndRestorePIC(i);
  8606. pthread_mutex_unlock(&iic_mutex);
  8607. }
  8608. }
  8609. #endif
  8610. pic_heart_beat = calloc(1,sizeof(struct thr_info));
  8611. if(thr_info_create(pic_heart_beat, NULL, pic_heart_beat_func, pic_heart_beat))
  8612. {
  8613. applog(LOG_DEBUG,"%s: create thread error for pic_heart_beat_func\n", __FUNCTION__);
  8614. return -6;
  8615. }
  8616. pthread_detach(pic_heart_beat->pth);
  8617. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8618. {
  8619. if(dev->chain_exist[i] == 1)
  8620. {
  8621. unsigned char vol_pic;
  8622. pthread_mutex_lock(&iic_mutex);
  8623. #ifdef ENABLE_HIGH_VOLTAGE_OPENCORE
  8624. chain_voltage_pic[i] = get_pic_voltage(i); // read orignal voltage at first!
  8625. vol_pic=getPICvoltageFromValue(HIGHEST_VOLTAGE_LIMITED_HW);
  8626. #ifdef T9_18
  8627. sprintf(logstr,"Chain[J%d] will use voltage=%d [%d] to open core\n",i+1,HIGHEST_VOLTAGE_LIMITED_HW,vol_pic);
  8628. writeInitLogFile(logstr);
  8629. if(fpga_version>=0xE)
  8630. {
  8631. if(i>=1 && i<=3)
  8632. set_voltage_T9_18_into_PIC(i, vol_pic);
  8633. }
  8634. else
  8635. {
  8636. if(i%3==0)
  8637. set_voltage_T9_18_into_PIC(i, vol_pic);
  8638. }
  8639. #else
  8640. set_pic_voltage(i, vol_pic);
  8641. #endif
  8642. #endif
  8643. #ifndef T9_18
  8644. enable_pic_dac(i);
  8645. #endif
  8646. pthread_mutex_unlock(&iic_mutex);
  8647. }
  8648. }
  8649. if(isFixedFreqMode())
  8650. {
  8651. // we must set voltage value according to the freq of config file!
  8652. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8653. {
  8654. if(dev->chain_exist[i] == 1)
  8655. {
  8656. chain_voltage_value[i] = getFixedFreqVoltageValue(config_parameter.frequency);
  8657. chain_voltage_pic[i] = getPICvoltageFromValue(chain_voltage_value[i]);
  8658. sprintf(logstr,"Fix freq=%d Chain[%d] voltage_pic=%d value=%d\n",config_parameter.frequency,i,chain_voltage_pic[i],chain_voltage_value[i]);
  8659. writeInitLogFile(logstr);
  8660. }
  8661. }
  8662. }
  8663. if(!isFixedFreqMode())
  8664. {
  8665. cgsleep_ms(100);
  8666. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8667. {
  8668. if(dev->chain_exist[i] == 1)
  8669. {
  8670. int vol_value;
  8671. unsigned char vol_pic;
  8672. pthread_mutex_lock(&iic_mutex);
  8673. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  8674. chain_voltage_pic[i] = get_pic_voltage(i);
  8675. #endif
  8676. vol_value = getVolValueFromPICvoltage(chain_voltage_pic[i]);
  8677. sprintf(logstr,"Chain[J%d] orignal chain_voltage_pic=%d value=%d\n",i+1,chain_voltage_pic[i],vol_value);
  8678. writeInitLogFile(logstr);
  8679. #ifdef T9_18
  8680. if(getChainPICMagicNumber(i)== FREQ_MAGIC)
  8681. #else
  8682. if(last_freq[i][1] == FREQ_MAGIC && last_freq[i][40] == 0x23) //0x23 is backup voltage magic number
  8683. #endif
  8684. {
  8685. #ifndef DEBUG_KEEP_USE_PIC_VOLTAGE_WITHOUT_CHECKING_VOLTAGE_OF_SEARCHFREQ
  8686. if(vol_value != chain_voltage_value[i]) // if not equal, we need force to set backup voltage to hashbaord!!!
  8687. {
  8688. vol_pic=getPICvoltageFromValue(chain_voltage_value[i]);
  8689. sprintf(logstr,"Chain[J%d] will use backup chain_voltage_pic=%d [%d]\n",i+1,chain_voltage_value[i],vol_pic);
  8690. writeInitLogFile(logstr);
  8691. chain_voltage_pic[i] = vol_pic;
  8692. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  8693. #ifndef T9_18 // T9_18 only can call enable_pic_dac once after jump to app!!!
  8694. set_pic_voltage(i, vol_pic);
  8695. enable_pic_dac(i);
  8696. #endif
  8697. #endif
  8698. sprintf(logstr,"Chain[J%d] get working chain_voltage_pic=%d\n",i+1,chain_voltage_pic[i]);
  8699. writeInitLogFile(logstr);
  8700. }
  8701. #endif
  8702. }
  8703. pthread_mutex_unlock(&iic_mutex);
  8704. }
  8705. }
  8706. cgsleep_ms(1000);
  8707. }
  8708. #ifdef T9_18
  8709. // set voltage and enable it for only once!!!
  8710. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8711. {
  8712. if(dev->chain_exist[i] == 1)
  8713. {
  8714. pthread_mutex_lock(&iic_mutex);
  8715. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  8716. set_pic_voltage(i, 0); // the second parameter is not used, we set 0 for T9_18
  8717. #endif
  8718. enable_pic_dac(i);
  8719. pthread_mutex_unlock(&iic_mutex);
  8720. }
  8721. }
  8722. sleep(5); // wait for sometime , voltage need time to prepare!!!
  8723. #endif
  8724. #ifdef USE_NEW_RESET_FPGA
  8725. set_reset_allhashboard(1);
  8726. sleep(RESET_KEEP_TIME);
  8727. set_reset_allhashboard(0);
  8728. sleep(1);
  8729. #else
  8730. set_QN_write_data_command(RESET_HASH_BOARD | RESET_ALL | RESET_TIME(RESET_HASHBOARD_TIME));
  8731. while(get_QN_write_data_command() & RESET_HASH_BOARD)
  8732. {
  8733. cgsleep_ms(30);
  8734. }
  8735. cgsleep_ms(1000);
  8736. #endif
  8737. if(opt_multi_version)
  8738. set_dhash_acc_control(get_dhash_acc_control() & (~OPERATION_MODE) & (~ VIL_MIDSTATE_NUMBER(0xf)) | VIL_MIDSTATE_NUMBER(1) | VIL_MODE & (~NEW_BLOCK) & (~RUN_BIT));
  8739. cgsleep_ms(10);
  8740. //set core number
  8741. dev->corenum = BM1387_CORE_NUM;
  8742. #ifdef USE_PREINIT_OPENCORE
  8743. // must set default baud here !!! because maybe some chip can not receive signal, we must keep default baud, and let those chip has chance to get command on next time!!!
  8744. // but if set default baud, it is too slow ,will cause chips work timeout, voltage will be changing always!!!
  8745. //set_baud(DEFAULT_BAUD_VALUE,1); // so we do not set it, and only try pre-open core in S9+
  8746. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8747. {
  8748. if(dev->chain_exist[i] == 1)
  8749. {
  8750. getAsicNum_preOpenCore(i); // need set default baud 26 at first!!!
  8751. sprintf(logstr,"Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  8752. writeInitLogFile(logstr);
  8753. if(dev->chain_asic_num[i] != CHAIN_ASIC_NUM && readRebootTestNum()>0)
  8754. {
  8755. char error_info[256];
  8756. sprintf(error_info,"J%d:3",i+1);
  8757. saveSearchFailedFlagInfo(error_info);
  8758. system("reboot");
  8759. }
  8760. if(dev->chain_asic_num[i]<=0)
  8761. {
  8762. dev->chain_exist[i]=0;
  8763. }
  8764. }
  8765. }
  8766. #else
  8767. //check ASIC number for every chain
  8768. check_asic_reg(CHIP_ADDRESS);
  8769. cgsleep_ms(10);
  8770. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8771. {
  8772. if(dev->chain_exist[i] == 1)
  8773. {
  8774. int retry_count=0;
  8775. sprintf(logstr,"Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  8776. writeInitLogFile(logstr);
  8777. #ifndef T9_18
  8778. while(dev->chain_asic_num[i] != CHAIN_ASIC_NUM && retry_count<6)
  8779. {
  8780. dev->chain_asic_num[i]=0;
  8781. #ifdef USE_NEW_RESET_FPGA
  8782. set_reset_hashboard(i,1);
  8783. #endif
  8784. pthread_mutex_lock(&iic_mutex);
  8785. disable_pic_dac(i);
  8786. pthread_mutex_unlock(&iic_mutex);
  8787. sleep(1);
  8788. pthread_mutex_lock(&iic_mutex);
  8789. enable_pic_dac(i);
  8790. pthread_mutex_unlock(&iic_mutex);
  8791. sleep(2);
  8792. #ifdef USE_NEW_RESET_FPGA
  8793. set_reset_hashboard(i,0);
  8794. sleep(1);
  8795. #else
  8796. reset_one_hashboard(i);
  8797. #endif
  8798. check_asic_reg_oneChain(i,CHIP_ADDRESS);
  8799. sprintf(logstr,"retry Chain[J%d] has %d asic\n",i+1,dev->chain_asic_num[i]);
  8800. writeInitLogFile(logstr);
  8801. retry_count++;
  8802. }
  8803. #endif
  8804. if(dev->chain_asic_num[i] != CHAIN_ASIC_NUM && readRebootTestNum()>0)
  8805. {
  8806. char error_info[256];
  8807. sprintf(error_info,"J%d:3",i+1);
  8808. saveSearchFailedFlagInfo(error_info);
  8809. system("reboot");
  8810. }
  8811. if(dev->chain_asic_num[i]<=0)
  8812. {
  8813. dev->chain_exist[i]=0;
  8814. }
  8815. }
  8816. }
  8817. #endif
  8818. // clement for debug
  8819. // check_asic_reg(TICKET_MASK);
  8820. software_set_address();
  8821. cgsleep_ms(10);
  8822. // check_asic_reg(CHIP_ADDRESS);
  8823. // cgsleep_ms(10);
  8824. if(config_parameter.frequency_eft)
  8825. {
  8826. dev->frequency = config_parameter.frequency;
  8827. set_frequency(dev->frequency);
  8828. sprintf(dev->frequency_t,"%u",dev->frequency);
  8829. }
  8830. cgsleep_ms(10);
  8831. //check who control fan
  8832. dev->fan_eft = config_parameter.fan_eft;
  8833. dev->fan_pwm= config_parameter.fan_pwm_percent;
  8834. applog(LOG_DEBUG,"%s: fan_eft : %d fan_pwm : %d\n", __FUNCTION__,dev->fan_eft,dev->fan_pwm);
  8835. if(config_parameter.fan_eft)
  8836. {
  8837. if((config_parameter.fan_pwm_percent >= 0) && (config_parameter.fan_pwm_percent <= 100))
  8838. {
  8839. set_PWM(config_parameter.fan_pwm_percent);
  8840. }
  8841. else
  8842. {
  8843. set_PWM_according_to_temperature();
  8844. }
  8845. }
  8846. else
  8847. {
  8848. set_PWM_according_to_temperature();
  8849. }
  8850. //calculate real timeout
  8851. if(config_parameter.timeout_eft)
  8852. {
  8853. if(config_parameter.timeout_data_integer == 0 && config_parameter.timeout_data_fractions == 0) //driver calculate out timeout value
  8854. {
  8855. // clement change to 70/100 org: 90/100
  8856. #ifdef CAPTURE_PATTEN
  8857. dev->timeout = 0x1000000/calculate_core_number(dev->corenum)*dev->addrInterval/(dev->frequency)*10/100;
  8858. #else
  8859. dev->timeout = 0x1000000/calculate_core_number(dev->corenum)*dev->addrInterval/(dev->frequency)*90/100;
  8860. #endif
  8861. // for set_freq_auto test,set timeout when frequency equals 700M
  8862. // dev->timeout = 0x1000000/calculate_core_number(dev->corenum)*dev->addrInterval/700*90/100;
  8863. applog(LOG_DEBUG,"dev->timeout = %d\n", dev->timeout);
  8864. }
  8865. else
  8866. {
  8867. dev->timeout = config_parameter.timeout_data_integer * 1000 + config_parameter.timeout_data_fractions;
  8868. }
  8869. if(dev->timeout > MAX_TIMEOUT_VALUE)
  8870. {
  8871. dev->timeout = MAX_TIMEOUT_VALUE;
  8872. }
  8873. }
  8874. //set baud
  8875. init_uart_baud();
  8876. cgsleep_ms(10);
  8877. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8878. {
  8879. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i] == CHAIN_ASIC_NUM)
  8880. {
  8881. calibration_sensor_offset(0x98,i);
  8882. cgsleep_ms(10);
  8883. }
  8884. }
  8885. #ifdef T9_18
  8886. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8887. {
  8888. if(dev->chain_exist[i] == 1 && dev->chain_asic_num[i] == CHAIN_ASIC_NUM)
  8889. {
  8890. if(fpga_version>=0xE)
  8891. {
  8892. switch(i) // only chain 8, 10, 12... has temp sensor!!! we just copy to other chains
  8893. {
  8894. case 1:
  8895. case 9:
  8896. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[8];
  8897. dev->TempChipAddr[i][0]=dev->TempChipAddr[8][0];
  8898. dev->TempChipAddr[i][1]=dev->TempChipAddr[8][1];
  8899. break;
  8900. case 2:
  8901. case 11:
  8902. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[10];
  8903. dev->TempChipAddr[i][0]=dev->TempChipAddr[10][0];
  8904. dev->TempChipAddr[i][1]=dev->TempChipAddr[10][1];
  8905. break;
  8906. case 3:
  8907. case 13:
  8908. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[12];
  8909. dev->TempChipAddr[i][0]=dev->TempChipAddr[12][0];
  8910. dev->TempChipAddr[i][1]=dev->TempChipAddr[12][1];
  8911. break;
  8912. }
  8913. }
  8914. else
  8915. {
  8916. if(i%3 != 1) // only chain 1, 4, 7... has temp sensor!!! we just copy chain[1] temp info into chain[0] and chain[2]
  8917. {
  8918. dev->chain_asic_temp_num[i]=dev->chain_asic_temp_num[((i/3)*3)+1];
  8919. dev->TempChipAddr[i][0]=dev->TempChipAddr[((i/3)*3)+1][0];
  8920. dev->TempChipAddr[i][1]=dev->TempChipAddr[((i/3)*3)+1][1];
  8921. }
  8922. }
  8923. }
  8924. }
  8925. #endif
  8926. #if 0
  8927. //set big timeout value for open core
  8928. //set_time_out_control((MAX_TIMEOUT_VALUE - 100) | TIME_OUT_VALID);
  8929. set_time_out_control(0xc350 | TIME_OUT_VALID);
  8930. #else
  8931. //set testpatten timeout , dev->timeout*0.1
  8932. if(opt_multi_version)
  8933. set_time_out_control((((dev->timeout/10) * 1 /*opt_multi_version*/) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  8934. else
  8935. set_time_out_control(((dev->timeout/10) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  8936. #endif
  8937. set_PWM(MAX_PWM_PERCENT);
  8938. if(!isFixedFreqMode())
  8939. {
  8940. // below process is check voltage and make sure not too high, must can be processed after set_frequency, because of GetTotalRate function need init to support fixed freq old miner S9
  8941. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++) // here must use i from 0 in for loop, because we use j to get the index as config file's voltage value
  8942. {
  8943. int vol_value_limited,vol_value;
  8944. unsigned char vol_pic;
  8945. if(dev->chain_exist[i] == 0)
  8946. continue;
  8947. if(isChainEnough())
  8948. {
  8949. vol_pic=chain_voltage_pic[i];
  8950. vol_value = getVolValueFromPICvoltage(vol_pic);
  8951. vol_value_limited=getVoltageLimitedFromHashrate(GetTotalRate());
  8952. #ifdef DEBUG_KEEP_USE_PIC_VOLTAGE_WITHOUT_CHECKING_VOLTAGE_OF_SEARCHFREQ
  8953. if(vol_value>=HIGHEST_VOLTAGE_LIMITED_HW) // we keep use pic voltage, but we still need check voltage can not be the highest voltage!
  8954. #endif
  8955. {
  8956. sprintf(logstr,"get PIC voltage=%d on chain[%d], check: must be < %d\n",vol_value,i,vol_value_limited);
  8957. writeInitLogFile(logstr);
  8958. #ifdef ENABLE_REINIT_WHEN_TESTFAILED
  8959. if(readRetryFlag()>0)
  8960. {
  8961. // we need use higher voltage , but meet the limit rules of power and hashrate
  8962. sprintf(logstr,"retryFlag=1 will set the voltage limited on chain[%d], change voltage=%d\n",i,vol_value_limited);
  8963. writeInitLogFile(logstr);
  8964. vol_pic=getPICvoltageFromValue(vol_value_limited);
  8965. sprintf(logstr,"now set pic voltage=%d on chain[%d]\n",vol_pic,i);
  8966. writeInitLogFile(logstr);
  8967. chain_voltage_pic[i]=vol_pic;
  8968. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  8969. pthread_mutex_lock(&iic_mutex);
  8970. set_pic_voltage(i, vol_pic);
  8971. pthread_mutex_unlock(&iic_mutex);
  8972. #endif
  8973. }
  8974. else
  8975. #endif
  8976. if(vol_value > vol_value_limited) // we will set voltage to the highest voltage for the last chance on test patten
  8977. {
  8978. sprintf(logstr,"will set the voltage limited on chain[%d], change voltage=%d\n",i,vol_value_limited);
  8979. writeInitLogFile(logstr);
  8980. vol_pic=getPICvoltageFromValue(vol_value_limited);
  8981. sprintf(logstr,"now set pic voltage=%d on chain[%d]\n",vol_pic,i);
  8982. writeInitLogFile(logstr);
  8983. chain_voltage_pic[i]=vol_pic;
  8984. #ifndef ENABLE_HIGH_VOLTAGE_OPENCORE
  8985. pthread_mutex_lock(&iic_mutex);
  8986. set_pic_voltage(i, vol_pic);
  8987. pthread_mutex_unlock(&iic_mutex);
  8988. #endif
  8989. }
  8990. }
  8991. }
  8992. }
  8993. }
  8994. #ifdef ENABLE_HIGH_VOLTAGE_OPENCORE
  8995. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  8996. {
  8997. int vol_value;
  8998. unsigned char vol_pic;
  8999. if(dev->chain_exist[i] == 1)
  9000. {
  9001. #ifdef USE_OPENCORE_ONEBYONE
  9002. opencore_onebyone_onChain(i);
  9003. #else
  9004. open_core_one_chain(i,true);
  9005. #endif
  9006. sleep(1);
  9007. #ifdef DEBUG_DOWN_VOLTAGE_TEST
  9008. vol_value=getVolValueFromPICvoltage(chain_voltage_pic[i])-DEBUG_DOWN_VOLTAGE_VALUE;
  9009. vol_pic=getPICvoltageFromValue(vol_value);
  9010. pthread_mutex_lock(&iic_mutex);
  9011. // restore the normal voltage
  9012. set_pic_voltage(i, vol_pic);
  9013. pthread_mutex_unlock(&iic_mutex);
  9014. sprintf(logstr,"DEBUG MODE Chain[J%d] set working voltage=%d [%d] orignal voltage=%d [%d]\n",i+1,vol_value,vol_pic,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  9015. writeInitLogFile(logstr);
  9016. #else
  9017. #ifdef T9_18
  9018. if(fpga_version>=0xE)
  9019. {
  9020. if(i==1)
  9021. {
  9022. sprintf(logstr,"open core on chain[1] [8] [9]...\n");
  9023. writeInitLogFile(logstr);
  9024. // we must try open core on chain [8] and chain[9] ...
  9025. #ifdef USE_OPENCORE_ONEBYONE
  9026. opencore_onebyone_onChain(8);
  9027. sleep(1);
  9028. opencore_onebyone_onChain(9);
  9029. sleep(1);
  9030. #else
  9031. open_core_one_chain(8,true);
  9032. sleep(1);
  9033. open_core_one_chain(9,true);
  9034. sleep(1);
  9035. #endif
  9036. }
  9037. else if(i==2)
  9038. {
  9039. sprintf(logstr,"open core on chain[2] [10] [11]...\n");
  9040. writeInitLogFile(logstr);
  9041. #ifdef USE_OPENCORE_ONEBYONE
  9042. opencore_onebyone_onChain(10);
  9043. sleep(1);
  9044. opencore_onebyone_onChain(11);
  9045. sleep(1);
  9046. #else
  9047. open_core_one_chain(10,true);
  9048. sleep(1);
  9049. open_core_one_chain(11,true);
  9050. sleep(1);
  9051. #endif
  9052. }
  9053. else if(i==3)
  9054. {
  9055. sprintf(logstr,"open core on chain[3] [12] [13]...\n");
  9056. writeInitLogFile(logstr);
  9057. #ifdef USE_OPENCORE_ONEBYONE
  9058. opencore_onebyone_onChain(12);
  9059. sleep(1);
  9060. opencore_onebyone_onChain(13);
  9061. sleep(1);
  9062. #else
  9063. open_core_one_chain(12,true);
  9064. sleep(1);
  9065. open_core_one_chain(13,true);
  9066. sleep(1);
  9067. #endif
  9068. }
  9069. else break; // we jump out of open core loop. because we have done open core above!!!
  9070. pthread_mutex_lock(&iic_mutex);
  9071. set_pic_voltage(i, chain_voltage_pic[i]);
  9072. pthread_mutex_unlock(&iic_mutex);
  9073. sprintf(logstr,"Chain[J%d] set working voltage=%d [%d]\n",i+1,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  9074. writeInitLogFile(logstr);
  9075. }
  9076. else
  9077. {
  9078. if(i%3==2) // only set working voltage when open core done on last chain of 3 chains!!!
  9079. {
  9080. pthread_mutex_lock(&iic_mutex);
  9081. set_pic_voltage((i/3)*3, chain_voltage_pic[i]);
  9082. pthread_mutex_unlock(&iic_mutex);
  9083. }
  9084. sprintf(logstr,"Chain[J%d] set working voltage=%d [%d]\n",i+1,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  9085. writeInitLogFile(logstr);
  9086. }
  9087. #else
  9088. pthread_mutex_lock(&iic_mutex);
  9089. // restore the normal voltage
  9090. set_pic_voltage(i, chain_voltage_pic[i]);
  9091. pthread_mutex_unlock(&iic_mutex);
  9092. sprintf(logstr,"Chain[J%d] set working voltage=%d [%d]\n",i+1,getVolValueFromPICvoltage(chain_voltage_pic[i]),chain_voltage_pic[i]);
  9093. writeInitLogFile(logstr);
  9094. #endif
  9095. #endif
  9096. }
  9097. }
  9098. #else
  9099. open_core(true);
  9100. #endif
  9101. #ifdef USE_OPENCORE_TWICE
  9102. sleep(5);
  9103. open_core(true);
  9104. #endif
  9105. // clement for debug
  9106. // check_asic_reg(TICKET_MASK);
  9107. #ifdef ENABLE_SET_TICKETMASK_BEFORE_TESTPATTEN
  9108. // must call it , because when software restart (not system reboot), it cause test patten failed !!!
  9109. sleep(5);
  9110. set_asic_ticket_mask(0);
  9111. set_hcnt(0);
  9112. sleep(5);
  9113. #endif
  9114. // clement for debug
  9115. // check_asic_reg(TICKET_MASK);
  9116. #ifdef FASTER_TESTPATTEN
  9117. //set real timeout back
  9118. if(opt_multi_version)
  9119. set_time_out_control((((dev->timeout/10) * 1 /*opt_multi_version*/) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9120. else
  9121. set_time_out_control(((dev->timeout/10) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9122. #else
  9123. //set real timeout back
  9124. if(opt_multi_version)
  9125. set_time_out_control(((dev->timeout * 1 /*opt_multi_version*/) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9126. else
  9127. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9128. #endif
  9129. read_temp_id = calloc(1,sizeof(struct thr_info));
  9130. if(thr_info_create(read_temp_id, NULL, read_temp_func, read_temp_id))
  9131. {
  9132. applog(LOG_DEBUG,"%s: create thread for read temp\n", __FUNCTION__);
  9133. return -7;
  9134. }
  9135. pthread_detach(read_temp_id->pth);
  9136. #ifndef CAPTURE_PATTEN
  9137. if(!isFixedFreqMode())
  9138. {
  9139. //#ifdef ENABLE_PREHEAT
  9140. if(opt_pre_heat)
  9141. {
  9142. #ifdef DEBUG_XILINX_NONCE_NOTENOUGH
  9143. set_bmc_counter(0);
  9144. sprintf(logstr,"reset bmc counter=0x%08x\n",read_bmc_counter());
  9145. writeInitLogFile(logstr);
  9146. #endif
  9147. //if(readRebootTestNum()<=0) // we will do preheat every time, even in reboot test mode.
  9148. {
  9149. doTestPatten=true;
  9150. sleep(3);
  9151. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  9152. {
  9153. if(dev->chain_exist[i] == 1)
  9154. {
  9155. #ifdef T9_18
  9156. memcpy(chain_pic_buf[i],chip_last_freq[i],128); // restore the real freq for chips
  9157. #else
  9158. memcpy(last_freq[i],chip_last_freq[i],256); // restore the real freq for chips
  9159. #endif
  9160. }
  9161. }
  9162. someBoardUpVoltage=false;
  9163. test_result=clement_doTestBoard(true);
  9164. #ifdef ENABLE_REINIT_WHEN_TESTFAILED
  9165. #ifdef DEBUG_FORCE_REINIT
  9166. test_result=false;
  9167. #endif
  9168. if((!test_result) && (readRetryFlag()==0))
  9169. {
  9170. // save a flag file to indicate that we need use the higher voltage after open core, but still meet the limit rules of power and hashrate
  9171. saveRetryFlag(1);
  9172. // call system to restart bmminer
  9173. exit(0);
  9174. // while(1)sleep(1);
  9175. }
  9176. //if(readRetryFlag()>0)
  9177. // saveRetryFlag(0); // clear retry flag if success on test patten!
  9178. #endif
  9179. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  9180. {
  9181. if(dev->chain_exist[i] == 1)
  9182. {
  9183. #ifdef T9_18
  9184. memcpy(chain_pic_buf[i],show_last_freq[i],128); // restore the user freq for showed on web for users
  9185. #else
  9186. memcpy(last_freq[i],show_last_freq[i],256); // restore the user freq for showed on web for users
  9187. #endif
  9188. }
  9189. }
  9190. // must re-set these two address to FPGA
  9191. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  9192. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  9193. doTestPatten=false;
  9194. }
  9195. #ifdef DEBUG_XILINX_NONCE_NOTENOUGH
  9196. sprintf(logstr,"After TEST bmc counter=0x%08x\n",read_bmc_counter());
  9197. writeInitLogFile(logstr);
  9198. #endif
  9199. }
  9200. //#endif
  9201. }
  9202. #endif
  9203. //clear_nonce_fifo();
  9204. #ifndef CAPTURE_PATTEN
  9205. set_asic_ticket_mask(63); // clement
  9206. set_hcnt(0);
  9207. cgsleep_ms(10);
  9208. #endif
  9209. #ifdef FASTER_TESTPATTEN
  9210. //set real timeout back
  9211. if(opt_multi_version)
  9212. set_time_out_control(((dev->timeout * opt_multi_version) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9213. else
  9214. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9215. #endif
  9216. check_system_work_id = calloc(1,sizeof(struct thr_info));
  9217. if(thr_info_create(check_system_work_id, NULL, check_system_work, check_system_work_id))
  9218. {
  9219. applog(LOG_DEBUG,"%s: create thread for check system\n", __FUNCTION__);
  9220. return -6;
  9221. }
  9222. pthread_detach(check_system_work_id->pth);
  9223. for(x=0; x<BITMAIN_MAX_CHAIN_NUM; x++)
  9224. {
  9225. if(dev->chain_exist[x])
  9226. {
  9227. int offset = 0;
  9228. for(y=0; y<dev->chain_asic_num[x]; y++)
  9229. {
  9230. if(y%8 == 0)
  9231. {
  9232. dev->chain_asic_status_string[x][y+offset] = ' ';
  9233. offset++;
  9234. }
  9235. dev->chain_asic_status_string[x][y+offset] = 'o';
  9236. dev->chain_asic_nonce[x][y] = 0;
  9237. }
  9238. dev->chain_asic_status_string[x][y+offset] = '\0';
  9239. }
  9240. }
  9241. cgtime(&tv_send_job);
  9242. cgtime(&tv_send);
  9243. startCheckNetworkJob=true;
  9244. setStartTimePoint();
  9245. return 0;
  9246. }
  9247. void bitmain_core_reInit()
  9248. {
  9249. int i,j;
  9250. int vol_value,vol_pic,cur_vol_value,cur_vol_pic;
  9251. char logstr[1024];
  9252. doTestPatten=true;
  9253. pthread_mutex_lock(&reinit_mutex);
  9254. startCheckNetworkJob=false;
  9255. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  9256. sleep(3);
  9257. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  9258. sleep(2);
  9259. open_core(true);
  9260. sprintf(logstr,"bitmain_core_reInit open_core over\n");
  9261. writeInitLogFile(logstr);
  9262. // must re-set these two address to FPGA
  9263. set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
  9264. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  9265. //clear_nonce_fifo();
  9266. #ifndef CAPTURE_PATTEN
  9267. set_asic_ticket_mask(63); // clement
  9268. set_hcnt(0);
  9269. cgsleep_ms(10);
  9270. #endif
  9271. set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() | FLUSH_NONCE3_FIFO);
  9272. #if 0
  9273. //set real timeout back
  9274. if(opt_multi_version)
  9275. set_time_out_control(((dev->timeout * opt_multi_version) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9276. else
  9277. set_time_out_control(((dev->timeout) & MAX_TIMEOUT_VALUE) | TIME_OUT_VALID);
  9278. #endif
  9279. doTestPatten=false;
  9280. pthread_mutex_unlock(&reinit_mutex);
  9281. re_send_last_job();
  9282. cgtime(&tv_send_job);
  9283. cgtime(&tv_send);
  9284. startCheckNetworkJob=true;
  9285. }
  9286. int parse_job_to_soc(unsigned char **buf,struct pool *pool,uint32_t id)
  9287. {
  9288. uint16_t crc = 0;
  9289. uint32_t buf_len = 0;
  9290. uint64_t nonce2 = 0;
  9291. unsigned char * tmp_buf;
  9292. int i;
  9293. static uint64_t pool_send_nu = 0;
  9294. struct part_of_job part_job;
  9295. char *buf_hex = NULL;
  9296. part_job.token_type = SEND_JOB_TYPE;
  9297. part_job.version = 0x00;
  9298. part_job.pool_nu = pool_send_nu;
  9299. part_job.new_block = pool->swork.clean ?1:0;
  9300. part_job.asic_diff_valid = 1;
  9301. part_job.asic_diff = 15;
  9302. part_job.job_id = id;
  9303. hex2bin((unsigned char *)&part_job.bbversion, pool->bbversion, 4);
  9304. hex2bin(part_job.prev_hash, pool->prev_hash, 32);
  9305. hex2bin((unsigned char *)&part_job.nbit, pool->nbit, 4);
  9306. hex2bin((unsigned char *)&part_job.ntime, pool->ntime, 4);
  9307. part_job.coinbase_len = pool->coinbase_len;
  9308. part_job.nonce2_offset = pool->nonce2_offset;
  9309. part_job.nonce2_bytes_num = pool->n2size;
  9310. nonce2 = htole64(pool->nonce2);
  9311. memcpy(&(part_job.nonce2_start_value), pool->coinbase + pool->nonce2_offset,8);
  9312. memcpy(&(part_job.nonce2_start_value), &nonce2,pool->n2size);
  9313. part_job.merkles_num = pool->merkles;
  9314. buf_len = sizeof(struct part_of_job) + pool->coinbase_len + pool->merkles * 32 + 2;
  9315. tmp_buf = (unsigned char *)malloc(buf_len);
  9316. if (unlikely(!tmp_buf))
  9317. quit(1, "Failed to malloc tmp_buf");
  9318. part_job.length = buf_len -8;
  9319. memset(tmp_buf,0,buf_len);
  9320. memcpy(tmp_buf,&part_job,sizeof(struct part_of_job));
  9321. memcpy(tmp_buf + sizeof(struct part_of_job), pool->coinbase, pool->coinbase_len);
  9322. /*
  9323. buf_hex = bin2hex(pool->coinbase,pool->coinbase_len);
  9324. printf("coinbase:%s offset:%d n2size:%d nonce2%lld\n",buf_hex,pool->nonce2_offset,pool->n2size,pool->nonce2);
  9325. free(buf_hex);
  9326. */
  9327. for (i = 0; i < pool->merkles; i++)
  9328. {
  9329. memcpy(tmp_buf + sizeof(struct part_of_job) + pool->coinbase_len + i * 32, pool->swork.merkle_bin[i], 32);
  9330. }
  9331. crc = CRC16((uint8_t *)tmp_buf, buf_len-2);
  9332. memcpy(tmp_buf + (buf_len - 2), &crc, 2);
  9333. pool_send_nu++;
  9334. *buf = (unsigned char *)malloc(buf_len);
  9335. if (unlikely(!tmp_buf))
  9336. quit(1, "Failed to malloc buf");
  9337. memcpy(*buf,tmp_buf,buf_len);
  9338. memcpy(last_job_buffer,tmp_buf,buf_len);
  9339. free(tmp_buf);
  9340. return buf_len;
  9341. }
  9342. static void show_status(int if_quit)
  9343. {
  9344. char * buf_hex = NULL;
  9345. unsigned int *l_job_start_address = NULL;
  9346. unsigned int buf[2] = {0};
  9347. int i = 0;
  9348. get_work_nonce2(buf);
  9349. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  9350. while((unsigned int)get_dhash_acc_control() & RUN_BIT)
  9351. {
  9352. cgsleep_ms(1);
  9353. applog(LOG_DEBUG,"%s: run bit is 1 after set it to 0", __FUNCTION__);
  9354. }
  9355. buf_hex = bin2hex((unsigned char *)dev->current_job_start_address,c_coinbase_padding);
  9356. free(buf_hex);
  9357. for(i=0; i<c_merkles_num; i++)
  9358. {
  9359. buf_hex = bin2hex((unsigned char *)dev->current_job_start_address + c_coinbase_padding+ i*MERKLE_BIN_LEN,32);
  9360. free(buf_hex);
  9361. }
  9362. if(dev->current_job_start_address == job_start_address_1)
  9363. {
  9364. l_job_start_address = job_start_address_2;
  9365. }
  9366. else if(dev->current_job_start_address == job_start_address_2)
  9367. {
  9368. l_job_start_address = job_start_address_1;
  9369. }
  9370. buf_hex = bin2hex((unsigned char *)l_job_start_address,l_coinbase_padding);
  9371. free(buf_hex);
  9372. for(i=0; i<l_merkles_num; i++)
  9373. {
  9374. buf_hex = bin2hex((unsigned char *)l_job_start_address + l_coinbase_padding+ i*MERKLE_BIN_LEN,32);
  9375. free(buf_hex);
  9376. }
  9377. if(if_quit)
  9378. quit(1, "HW is more than 5!!");
  9379. }
  9380. static void show_pool_status(struct pool *pool,uint64_t nonce2)
  9381. {
  9382. char * buf_hex = NULL;
  9383. int i = 0;
  9384. buf_hex = bin2hex(pool->coinbase,pool->coinbase_len);
  9385. printf("%s: nonce2 0x%x\n", __FUNCTION__, nonce2);
  9386. printf("%s: coinbase : %s\n", __FUNCTION__, buf_hex);
  9387. free(buf_hex);
  9388. for(i=0; i<pool->merkles; i++)
  9389. {
  9390. buf_hex = bin2hex(pool->swork.merkle_bin[i],32);
  9391. printf("%s: merkle_bin %d : %s\n", __FUNCTION__, i, buf_hex);
  9392. free(buf_hex);
  9393. }
  9394. }
  9395. void re_send_last_job()
  9396. {
  9397. if(last_job_buffer[0]!=23)
  9398. {
  9399. pthread_mutex_lock(&reinit_mutex);
  9400. send_job(last_job_buffer);
  9401. pthread_mutex_unlock(&reinit_mutex);
  9402. }
  9403. }
  9404. int send_job(unsigned char *buf)
  9405. {
  9406. unsigned int len = 0, i=0, j=0, coinbase_padding_len = 0;
  9407. unsigned short int crc = 0, job_length = 0;
  9408. unsigned char *temp_buf = NULL, *coinbase_padding = NULL, *merkles_bin = NULL;
  9409. unsigned char buf1[PREV_HASH_LEN] = {0};
  9410. unsigned int buf2[PREV_HASH_LEN] = {0};
  9411. int times = 0;
  9412. struct part_of_job *part_job = NULL;
  9413. if(doTestPatten) // do patten , do not send job
  9414. return 0;
  9415. if(*(buf + 0) != SEND_JOB_TYPE)
  9416. {
  9417. applog(LOG_DEBUG,"%s: SEND_JOB_TYPE is wrong : 0x%x\n", __FUNCTION__, *(buf + 0));
  9418. return -1;
  9419. }
  9420. len = *((unsigned int *)buf + 4/sizeof(int));
  9421. applog(LOG_DEBUG,"%s: len = 0x%x\n", __FUNCTION__, len);
  9422. temp_buf = malloc(len + 8*sizeof(unsigned char));
  9423. if(!temp_buf)
  9424. {
  9425. applog(LOG_DEBUG,"%s: malloc buffer failed.\n", __FUNCTION__);
  9426. return -2;
  9427. }
  9428. else
  9429. {
  9430. memset(temp_buf, 0, len + 8*sizeof(unsigned char));
  9431. memcpy(temp_buf, buf, len + 8*sizeof(unsigned char));
  9432. part_job = (struct part_of_job *)temp_buf;
  9433. }
  9434. //write new job data into dev->current_job_start_address
  9435. if(dev->current_job_start_address == job_start_address_1)
  9436. {
  9437. dev->current_job_start_address = job_start_address_2;
  9438. }
  9439. else if(dev->current_job_start_address == job_start_address_2)
  9440. {
  9441. dev->current_job_start_address = job_start_address_1;
  9442. }
  9443. else
  9444. {
  9445. applog(LOG_DEBUG,"%s: dev->current_job_start_address = 0x%x, but job_start_address_1 = 0x%x, job_start_address_2 = 0x%x\n", __FUNCTION__, dev->current_job_start_address, job_start_address_1, job_start_address_2);
  9446. return -3;
  9447. }
  9448. if((part_job->coinbase_len % 64) > 55)
  9449. {
  9450. coinbase_padding_len = (part_job->coinbase_len/64 + 2) * 64;
  9451. }
  9452. else
  9453. {
  9454. coinbase_padding_len = (part_job->coinbase_len/64 + 1) * 64;
  9455. }
  9456. coinbase_padding = malloc(coinbase_padding_len);
  9457. if(!coinbase_padding)
  9458. {
  9459. applog(LOG_DEBUG,"%s: malloc coinbase_padding failed.\n", __FUNCTION__);
  9460. return -4;
  9461. }
  9462. else
  9463. {
  9464. applog(LOG_DEBUG,"%s: coinbase_padding = 0x%x", __FUNCTION__, (unsigned int)coinbase_padding);
  9465. }
  9466. if(part_job->merkles_num)
  9467. {
  9468. merkles_bin = malloc(part_job->merkles_num * MERKLE_BIN_LEN);
  9469. if(!merkles_bin)
  9470. {
  9471. applog(LOG_DEBUG,"%s: malloc merkles_bin failed.\n", __FUNCTION__);
  9472. return -5;
  9473. }
  9474. else
  9475. {
  9476. applog(LOG_DEBUG,"%s: merkles_bin = 0x%x", __FUNCTION__, (unsigned int)merkles_bin);
  9477. }
  9478. }
  9479. //applog(LOG_DEBUG,"%s: copy coinbase into memory ...\n", __FUNCTION__);
  9480. memset(coinbase_padding, 0, coinbase_padding_len);
  9481. memcpy(coinbase_padding, buf + sizeof(struct part_of_job), part_job->coinbase_len);
  9482. *(coinbase_padding + part_job->coinbase_len) = 0x80;
  9483. *((unsigned int *)coinbase_padding + (coinbase_padding_len - 4)/sizeof(int)) = Swap32((unsigned int)((unsigned long long int)(part_job->coinbase_len * sizeof(char) * 8) & 0x00000000ffffffff)); // 8 means 8 bits
  9484. *((unsigned int *)coinbase_padding + (coinbase_padding_len - 8)/sizeof(int)) = Swap32((unsigned int)(((unsigned long long int)(part_job->coinbase_len * sizeof(char) * 8) & 0xffffffff00000000) >> 32)); // 8 means 8 bits
  9485. l_coinbase_padding = c_coinbase_padding;
  9486. c_coinbase_padding = coinbase_padding_len;
  9487. for(i=0; i<coinbase_padding_len; i++)
  9488. {
  9489. *((unsigned char *)dev->current_job_start_address + i) = *(coinbase_padding + i);
  9490. //applog(LOG_DEBUG,"%s: coinbase_padding_in_ddr[%d] = 0x%x", __FUNCTION__, i, *(((unsigned char *)dev->current_job_start_address + i)));
  9491. }
  9492. /* check coinbase & padding in ddr */
  9493. for(i=0; i<coinbase_padding_len; i++)
  9494. {
  9495. if(*((unsigned char *)dev->current_job_start_address + i) != *(coinbase_padding + i))
  9496. {
  9497. applog(LOG_DEBUG,"%s: coinbase_padding_in_ddr[%d] = 0x%x, but *(coinbase_padding + %d) = 0x%x", __FUNCTION__, i, *(((unsigned char *)dev->current_job_start_address + i)), i, *(coinbase_padding + i));
  9498. }
  9499. }
  9500. l_merkles_num = c_merkles_num;
  9501. c_merkles_num = part_job->merkles_num;
  9502. if(part_job->merkles_num)
  9503. {
  9504. applog(LOG_DEBUG,"%s: copy merkle bin into memory ...\n", __FUNCTION__);
  9505. memset(merkles_bin, 0, part_job->merkles_num * MERKLE_BIN_LEN);
  9506. memcpy(merkles_bin, buf + sizeof(struct part_of_job) + part_job->coinbase_len , part_job->merkles_num * MERKLE_BIN_LEN);
  9507. for(i=0; i<(part_job->merkles_num * MERKLE_BIN_LEN); i++)
  9508. {
  9509. *((unsigned char *)dev->current_job_start_address + coinbase_padding_len + i) = *(merkles_bin + i);
  9510. //applog(LOG_DEBUG,"%s: merkles_in_ddr[%d] = 0x%x", __FUNCTION__, i, *(((unsigned char *)dev->current_job_start_address + coinbase_padding_len + i)));
  9511. }
  9512. for(i=0; i<(part_job->merkles_num * MERKLE_BIN_LEN); i++)
  9513. {
  9514. if(*((unsigned char *)dev->current_job_start_address + coinbase_padding_len + i) != *(merkles_bin + i))
  9515. {
  9516. applog(LOG_DEBUG,"%s: merkles_in_ddr[%d] = 0x%x, but *(merkles_bin + %d) =0x%x", __FUNCTION__, i, *(((unsigned char *)dev->current_job_start_address + coinbase_padding_len + i)), i, *(merkles_bin + i));
  9517. }
  9518. }
  9519. }
  9520. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  9521. while((unsigned int)get_dhash_acc_control() & RUN_BIT)
  9522. {
  9523. cgsleep_ms(1);
  9524. applog(LOG_DEBUG,"%s: run bit is 1 after set it to 0\n", __FUNCTION__);
  9525. times++;
  9526. }
  9527. cgsleep_ms(1);
  9528. //write new job data into dev->current_job_start_address
  9529. if(dev->current_job_start_address == job_start_address_1)
  9530. {
  9531. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
  9532. //applog(LOG_DEBUG,"%s: dev->current_job_start_address = 0x%x\n", __FUNCTION__, (unsigned int)job_start_address_2);
  9533. }
  9534. else if(dev->current_job_start_address == job_start_address_2)
  9535. {
  9536. set_job_start_address(PHY_MEM_JOB_START_ADDRESS_2);
  9537. //applog(LOG_DEBUG,"%s: dev->current_job_start_address = 0x%x\n", __FUNCTION__, (unsigned int)job_start_address_1);
  9538. }
  9539. if(part_job->asic_diff_valid)
  9540. {
  9541. #ifndef CAPTURE_PATTEN
  9542. set_ticket_mask((unsigned int)(part_job->asic_diff & 0x000000ff)); // clement disable it
  9543. #endif
  9544. dev->diff = part_job->asic_diff & 0xff;
  9545. }
  9546. set_job_id(part_job->job_id);
  9547. set_block_header_version(part_job->bbversion);
  9548. memset(buf2, 0, PREV_HASH_LEN*sizeof(unsigned int));
  9549. for(i=0; i<(PREV_HASH_LEN/sizeof(unsigned int)); i++)
  9550. {
  9551. buf2[i] = ((part_job->prev_hash[4*i + 3]) << 24) | ((part_job->prev_hash[4*i + 2]) << 16) | ((part_job->prev_hash[4*i + 1]) << 8) | (part_job->prev_hash[4*i + 0]);
  9552. }
  9553. set_pre_header_hash(buf2);
  9554. set_time_stamp(part_job->ntime);
  9555. set_target_bits(part_job->nbit);
  9556. j = (part_job->nonce2_offset << 16) | ((unsigned char)(part_job->nonce2_bytes_num & 0x00ff)) << 8 | (unsigned char)((coinbase_padding_len/64) & 0x000000ff);
  9557. set_coinbase_length_and_nonce2_length(j);
  9558. //memset(buf2, 0, PREV_HASH_LEN*sizeof(unsigned int));
  9559. buf2[0] = 0;
  9560. buf2[1] = 0;
  9561. buf2[0] = ((unsigned long long )(part_job->nonce2_start_value)) & 0xffffffff;
  9562. buf2[1] = ((unsigned long long )(part_job->nonce2_start_value) >> 32) & 0xffffffff;
  9563. set_work_nonce2(buf2);
  9564. set_merkle_bin_number(part_job->merkles_num);
  9565. job_length = coinbase_padding_len + part_job->merkles_num*MERKLE_BIN_LEN;
  9566. set_job_length((unsigned int)job_length & 0x0000ffff);
  9567. cgsleep_ms(1);
  9568. if(!gBegin_get_nonce)
  9569. {
  9570. set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() | FLUSH_NONCE3_FIFO);
  9571. gBegin_get_nonce = true;
  9572. }
  9573. #if 1
  9574. //start FPGA generating works
  9575. if(part_job->new_block)
  9576. {
  9577. if(!opt_multi_version)
  9578. {
  9579. set_dhash_acc_control((unsigned int)get_dhash_acc_control() | NEW_BLOCK );
  9580. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & (~ VIL_MIDSTATE_NUMBER(0xf)) | VIL_MIDSTATE_NUMBER(opt_multi_version)| RUN_BIT | OPERATION_MODE);
  9581. }
  9582. else
  9583. {
  9584. set_dhash_acc_control((unsigned int)get_dhash_acc_control() | NEW_BLOCK );
  9585. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & (~ VIL_MIDSTATE_NUMBER(0xf)) | VIL_MIDSTATE_NUMBER(opt_multi_version)| RUN_BIT | OPERATION_MODE |VIL_MODE);
  9586. }
  9587. }
  9588. else
  9589. {
  9590. if(!opt_multi_version)
  9591. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & (~ VIL_MIDSTATE_NUMBER(0xf)) | VIL_MIDSTATE_NUMBER(opt_multi_version)| RUN_BIT| OPERATION_MODE );
  9592. else
  9593. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & (~ VIL_MIDSTATE_NUMBER(0xf)) | VIL_MIDSTATE_NUMBER(opt_multi_version)| RUN_BIT| OPERATION_MODE |VIL_MODE);
  9594. }
  9595. #endif
  9596. free(temp_buf);
  9597. free((unsigned char *)coinbase_padding);
  9598. if(part_job->merkles_num)
  9599. {
  9600. free((unsigned char *)merkles_bin);
  9601. }
  9602. applog(LOG_DEBUG,"--- %s end\n", __FUNCTION__);
  9603. cgtime(&tv_send_job);
  9604. return 0;
  9605. }
  9606. static void copy_pool_stratum(struct pool *pool_stratum, struct pool *pool)
  9607. {
  9608. int i;
  9609. int merkles = pool->merkles, job_id_len;
  9610. size_t coinbase_len = pool->coinbase_len;
  9611. unsigned short crc;
  9612. if (!pool->swork.job_id)
  9613. return;
  9614. if (pool_stratum->swork.job_id)
  9615. {
  9616. job_id_len = strlen(pool->swork.job_id);
  9617. crc = CRC16((unsigned char *)pool->swork.job_id, job_id_len);
  9618. job_id_len = strlen(pool_stratum->swork.job_id);
  9619. if (CRC16((unsigned char *)pool_stratum->swork.job_id, job_id_len) == crc)
  9620. return;
  9621. }
  9622. cg_wlock(&pool_stratum->data_lock);
  9623. free(pool_stratum->swork.job_id);
  9624. free(pool_stratum->nonce1);
  9625. free(pool_stratum->coinbase);
  9626. pool_stratum->coinbase = cgcalloc(coinbase_len, 1);
  9627. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  9628. for (i = 0; i < pool_stratum->merkles; i++)
  9629. free(pool_stratum->swork.merkle_bin[i]);
  9630. if (merkles)
  9631. {
  9632. pool_stratum->swork.merkle_bin = cgrealloc(pool_stratum->swork.merkle_bin,
  9633. sizeof(char *) * merkles + 1);
  9634. for (i = 0; i < merkles; i++)
  9635. {
  9636. pool_stratum->swork.merkle_bin[i] = cgmalloc(32);
  9637. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  9638. }
  9639. }
  9640. pool_stratum->sdiff = pool->sdiff;
  9641. pool_stratum->coinbase_len = pool->coinbase_len;
  9642. pool_stratum->nonce2_offset = pool->nonce2_offset;
  9643. pool_stratum->n2size = pool->n2size;
  9644. pool_stratum->merkles = pool->merkles;
  9645. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  9646. pool_stratum->nonce1 = strdup(pool->nonce1);
  9647. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  9648. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  9649. cg_wunlock(&pool_stratum->data_lock);
  9650. }
  9651. static bool bitmain_soc_prepare(struct thr_info *thr)
  9652. {
  9653. struct cgpu_info *bitmain_soc = thr->cgpu;
  9654. struct bitmain_soc_info *info = bitmain_soc->device_data;
  9655. info->thr = thr;
  9656. mutex_init(&info->lock);
  9657. cglock_init(&info->update_lock);
  9658. cglock_init(&info->pool0.data_lock);
  9659. cglock_init(&info->pool1.data_lock);
  9660. cglock_init(&info->pool2.data_lock);
  9661. struct init_config soc_config =
  9662. {
  9663. .token_type = 0x51,
  9664. .version = 0,
  9665. .length = 26,
  9666. .reset = 1,
  9667. .fan_eft = opt_bitmain_fan_ctrl,
  9668. .timeout_eft = 1,
  9669. .frequency_eft = 1,
  9670. .voltage_eft = 1,
  9671. .chain_check_time_eft = 1,
  9672. .chip_config_eft = 1,
  9673. .hw_error_eft = 1,
  9674. .beeper_ctrl = 1,
  9675. .temp_ctrl = 1,
  9676. .chain_freq_eft = 1,
  9677. .reserved1 = 0,
  9678. .reserved2 = {0},
  9679. .chain_num = 9,
  9680. .asic_num = 54,
  9681. .fan_pwm_percent = opt_bitmain_fan_pwm,
  9682. .temperature = 80,
  9683. .frequency = opt_bitmain_soc_freq,
  9684. .voltage = {0x07,0x25},
  9685. .chain_check_time_integer = 10,
  9686. .chain_check_time_fractions = 10,
  9687. .timeout_data_integer = 0,
  9688. .timeout_data_fractions = 0,
  9689. .reg_data = 0,
  9690. .chip_address = 0x04,
  9691. .reg_address = 0,
  9692. .chain_min_freq = 400,
  9693. .chain_max_freq = 600,
  9694. };
  9695. soc_config.crc = CRC16((uint8_t *)(&soc_config), sizeof(soc_config)-2);
  9696. bitmain_soc_init(soc_config);
  9697. return true;
  9698. }
  9699. static void bitmain_soc_reinit_device(struct cgpu_info *bitmain)
  9700. {
  9701. if(!status_error)
  9702. {
  9703. //system("/etc/init.d/bmminer.sh restart > /dev/null 2>&1 &");
  9704. exit(0);
  9705. }
  9706. }
  9707. static void bitmain_soc_detect(__maybe_unused bool hotplug)
  9708. {
  9709. struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
  9710. struct device_drv *drv = &bitmain_soc_drv;
  9711. struct bitmain_soc_info *a;
  9712. assert(cgpu);
  9713. cgpu->drv = drv;
  9714. cgpu->deven = DEV_ENABLED;
  9715. cgpu->threads = 1;
  9716. cgpu->device_data = calloc(sizeof(struct bitmain_soc_info), 1);
  9717. if (unlikely(!(cgpu->device_data)))
  9718. quit(1, "Failed to calloc cgpu_info data");
  9719. a = cgpu->device_data;
  9720. a->pool0_given_id = 0;
  9721. a->pool1_given_id = 1;
  9722. a->pool2_given_id = 2;
  9723. assert(add_cgpu(cgpu));
  9724. }
  9725. static __inline void flip_swab(void *dest_p, const void *src_p, unsigned int length)
  9726. {
  9727. uint32_t *dest = dest_p;
  9728. const uint32_t *src = src_p;
  9729. int i;
  9730. for (i = 0; i < length/4; i++)
  9731. dest[i] = swab32(src[i]);
  9732. }
  9733. static uint64_t hashtest_submit(struct thr_info *thr, struct work *work, uint32_t nonce, uint8_t *midstate,struct pool *pool,uint64_t nonce2,uint32_t chain_id )
  9734. {
  9735. unsigned char hash1[32];
  9736. unsigned char hash2[32];
  9737. int i,j;
  9738. unsigned char which_asic_nonce, which_core_nonce;
  9739. uint64_t hashes = 0;
  9740. static uint64_t pool_diff = 0, net_diff = 0;
  9741. static uint64_t pool_diff_bit = 0, net_diff_bit = 0;
  9742. if(pool_diff != (uint64_t)work->sdiff)
  9743. {
  9744. pool_diff = (uint64_t)work->sdiff;
  9745. pool_diff_bit = 0;
  9746. uint64_t tmp_pool_diff = pool_diff;
  9747. while(tmp_pool_diff > 0)
  9748. {
  9749. tmp_pool_diff = tmp_pool_diff >> 1;
  9750. pool_diff_bit++;
  9751. }
  9752. pool_diff_bit--;
  9753. applog(LOG_DEBUG,"%s: pool_diff:%d work_diff:%d pool_diff_bit:%d ...\n", __FUNCTION__,pool_diff,work->sdiff,pool_diff_bit);
  9754. }
  9755. if(net_diff != (uint64_t)current_diff)
  9756. {
  9757. net_diff = (uint64_t)current_diff;
  9758. net_diff_bit = 0;
  9759. uint64_t tmp_net_diff = net_diff;
  9760. while(tmp_net_diff > 0)
  9761. {
  9762. tmp_net_diff = tmp_net_diff >> 1;
  9763. net_diff_bit++;
  9764. }
  9765. net_diff_bit--;
  9766. applog(LOG_DEBUG,"%s:net_diff:%d current_diff:%d net_diff_bit %d ...\n", __FUNCTION__,net_diff,current_diff,net_diff_bit);
  9767. }
  9768. uint32_t *hash2_32 = (uint32_t *)hash1;
  9769. __attribute__ ((aligned (4))) sha2_context ctx;
  9770. memcpy(ctx.state, (void*)work->midstate, 32);
  9771. #if TEST_DHASH
  9772. rev((unsigned char*)ctx.state, sizeof(ctx.state));
  9773. #endif
  9774. ctx.total[0] = 80;
  9775. ctx.total[1] = 00;
  9776. memcpy(hash1, (void*)work->data + 64, 12);
  9777. #if TEST_DHASH
  9778. rev(hash1, 12);
  9779. #endif
  9780. flip_swab(ctx.buffer, hash1, 12);
  9781. memcpy(hash1, &nonce, 4);
  9782. #if TEST_DHASH
  9783. rev(hash1, 4);
  9784. #endif
  9785. flip_swab(ctx.buffer + 12, hash1, 4);
  9786. sha2_finish(&ctx, hash1);
  9787. memset( &ctx, 0, sizeof( sha2_context ) );
  9788. sha2(hash1, 32, hash2);
  9789. flip32(hash1, hash2);
  9790. if (hash2_32[7] != 0)
  9791. {
  9792. if(dev->chain_exist[chain_id] == 1)
  9793. {
  9794. inc_hw_errors(thr);
  9795. dev->chain_hw[chain_id]++;
  9796. }
  9797. //inc_hw_errors_with_diff(thr,(0x01UL << DEVICE_DIFF));
  9798. //dev->chain_hw[chain_id]+=(0x01UL << DEVICE_DIFF);
  9799. applog(LOG_DEBUG,"%s: HASH2_32[7] != 0", __FUNCTION__);
  9800. return 0;
  9801. }
  9802. for(i=0; i < 7; i++)
  9803. {
  9804. if(be32toh(hash2_32[6 - i]) != 0)
  9805. break;
  9806. }
  9807. #ifdef CAPTURE_PATTEN
  9808. // clement change below:
  9809. savelog_nonce(work, nonce);
  9810. #endif
  9811. if(i >= pool_diff_bit/32)
  9812. {
  9813. which_asic_nonce = (nonce >> (24 + dev->check_bit)) & 0xff;
  9814. which_core_nonce = (nonce & 0x7f);
  9815. applog(LOG_DEBUG,"%s: chain %d which_asic_nonce %d which_core_nonce %d", __FUNCTION__, chain_id, which_asic_nonce, which_core_nonce);
  9816. dev->chain_asic_nonce[chain_id][which_asic_nonce]++;
  9817. if(be32toh(hash2_32[6 - pool_diff_bit/32]) < ((uint32_t)0xffffffff >> (pool_diff_bit%32)))
  9818. {
  9819. hashes += (0x01UL << DEVICE_DIFF);
  9820. if(current_diff != 0)
  9821. {
  9822. for(i=0; i < net_diff_bit/32; i++)
  9823. {
  9824. if(be32toh(hash2_32[6 - i]) != 0)
  9825. break;
  9826. }
  9827. if(i == net_diff_bit/32)
  9828. {
  9829. if(be32toh(hash2_32[6 - net_diff_bit/32]) < ((uint32_t)0xffffffff >> (net_diff_bit%32)))
  9830. {
  9831. // to do found block!!!
  9832. }
  9833. }
  9834. }
  9835. #ifndef CAPTURE_PATTEN
  9836. submit_nonce(thr, work, nonce); // clement disable it , do not submit to pool
  9837. #endif
  9838. }
  9839. else if(be32toh(hash2_32[6 - DEVICE_DIFF/32]) < ((uint32_t)0xffffffff >> (DEVICE_DIFF%32)))
  9840. {
  9841. hashes += (0x01UL << DEVICE_DIFF);
  9842. }
  9843. }
  9844. return hashes;
  9845. }
  9846. void * bitmain_scanhash(void *arg)
  9847. {
  9848. struct thr_info *thr = (struct thr_info *)arg;
  9849. struct cgpu_info *bitmain_soc = thr->cgpu;
  9850. struct bitmain_soc_info *info = bitmain_soc->device_data;
  9851. double device_tdiff, hwp;
  9852. uint32_t a = 0, b = 0;
  9853. static uint32_t last_nonce3 = 0;
  9854. static uint32_t last_workid = 0;
  9855. int i, j;
  9856. h = 0;
  9857. pthread_mutex_lock(&nonce_mutex);
  9858. cg_rlock(&info->update_lock);
  9859. while(nonce_read_out.nonce_num>0)
  9860. {
  9861. uint32_t nonce3 = nonce_read_out.nonce_buffer[nonce_read_out.p_rd].nonce3;
  9862. uint32_t job_id = nonce_read_out.nonce_buffer[nonce_read_out.p_rd].job_id;
  9863. uint64_t nonce2 = nonce_read_out.nonce_buffer[nonce_read_out.p_rd].nonce2;
  9864. uint32_t chain_id = nonce_read_out.nonce_buffer[nonce_read_out.p_rd].chain_num;
  9865. uint32_t work_id = nonce_read_out.nonce_buffer[nonce_read_out.p_rd].work_id;
  9866. uint32_t version = Swap32(nonce_read_out.nonce_buffer[nonce_read_out.p_rd].header_version);
  9867. uint8_t midstate[32] = {0};
  9868. int i = 0;
  9869. for(i=0; i<32; i++)
  9870. {
  9871. midstate[(7-(i/4))*4 + (i%4)] = nonce_read_out.nonce_buffer[nonce_read_out.p_rd].midstate[i];
  9872. }
  9873. applog(LOG_DEBUG,"%s: job_id:0x%x work_id:0x%x nonce2:0x%llx nonce3:0x%x version:0x%x\n", __FUNCTION__,job_id, work_id,nonce2, nonce3,version);
  9874. struct work * work;
  9875. struct pool *pool, *c_pool;
  9876. struct pool *pool_stratum0 = &info->pool0;
  9877. struct pool *pool_stratum1 = &info->pool1;
  9878. struct pool *pool_stratum2 = &info->pool2;
  9879. if(nonce_read_out.p_rd< MAX_NONCE_NUMBER_IN_FIFO-1)
  9880. {
  9881. nonce_read_out.p_rd++;
  9882. }
  9883. else
  9884. {
  9885. nonce_read_out.p_rd = 0;
  9886. }
  9887. nonce_read_out.nonce_num--;
  9888. if(nonce3 != last_nonce3 || work_id != last_workid )
  9889. {
  9890. last_nonce3 = nonce3;
  9891. last_workid = work_id;
  9892. }
  9893. else
  9894. {
  9895. if(dev->chain_exist[chain_id] == 1)
  9896. {
  9897. inc_hw_errors(thr);
  9898. dev->chain_hw[chain_id]++;
  9899. }
  9900. continue;
  9901. }
  9902. applog(LOG_DEBUG,"%s: Chain ID J%d ...\n", __FUNCTION__, chain_id + 1);
  9903. if( (given_id -2)> job_id && given_id < job_id)
  9904. {
  9905. applog(LOG_DEBUG,"%s: job_id error ...\n", __FUNCTION__);
  9906. if(dev->chain_exist[chain_id] == 1)
  9907. {
  9908. inc_hw_errors(thr);
  9909. dev->chain_hw[chain_id]++;
  9910. }
  9911. continue;
  9912. }
  9913. applog(LOG_DEBUG,"%s: given_id:%d job_id:%d switch:%d ...\n", __FUNCTION__,given_id,job_id,given_id - job_id);
  9914. switch (given_id - job_id)
  9915. {
  9916. case 0:
  9917. pool = pool_stratum0;
  9918. break;
  9919. case 1:
  9920. pool = pool_stratum1;
  9921. break;
  9922. case 2:
  9923. pool = pool_stratum2;
  9924. break;
  9925. default:
  9926. applog(LOG_DEBUG,"%s: job_id non't found ...\n", __FUNCTION__);
  9927. if(dev->chain_exist[chain_id] == 1)
  9928. {
  9929. inc_hw_errors(thr);
  9930. dev->chain_hw[chain_id]++;
  9931. }
  9932. continue;
  9933. }
  9934. c_pool = pools[pool->pool_no];
  9935. get_work_by_nonce2(thr,&work,pool,c_pool,nonce2,version);
  9936. h += hashtest_submit(thr,work,nonce3,midstate,pool,nonce2,chain_id);
  9937. free_work(work);
  9938. }
  9939. cg_runlock(&info->update_lock);
  9940. pthread_mutex_unlock(&nonce_mutex);
  9941. cgsleep_ms(1);
  9942. if(h != 0)
  9943. {
  9944. applog(LOG_DEBUG,"%s: hashes %u ...\n", __FUNCTION__,h * 0xffffffffull);
  9945. }
  9946. h = h * 0xffffffffull;
  9947. }
  9948. static int64_t bitmain_soc_scanhash(struct thr_info *thr)
  9949. {
  9950. h = 0;
  9951. pthread_t send_id;
  9952. pthread_create(&send_id, NULL, bitmain_scanhash, thr);
  9953. pthread_join(send_id, NULL);
  9954. return h;
  9955. }
  9956. static void bitmain_soc_update(struct cgpu_info *bitmain_soc)
  9957. {
  9958. struct bitmain_soc_info *info = bitmain_soc->device_data;
  9959. struct thr_info *thr = bitmain_soc->thr[0];
  9960. struct work *work;
  9961. struct pool *pool;
  9962. int i, count = 0;
  9963. mutex_lock(&info->lock);
  9964. static char *last_job = NULL;
  9965. bool same_job = true;
  9966. unsigned char *buf = NULL;
  9967. thr->work_update = false;
  9968. thr->work_restart = false;
  9969. /* Step 1: Make sure pool is ready */
  9970. work = get_work(thr, thr->id);
  9971. discard_work(work); /* Don't leak memory */
  9972. /* Step 2: Protocol check */
  9973. pool = current_pool();
  9974. if (!pool->has_stratum)
  9975. quit(1, "Bitmain S9 has to use stratum pools");
  9976. /* Step 3: Parse job to c5 formart */
  9977. cg_wlock(&info->update_lock);
  9978. cg_rlock(&pool->data_lock);
  9979. info->pool_no = pool->pool_no;
  9980. copy_pool_stratum(&info->pool2, &info->pool1);
  9981. info->pool2_given_id = info->pool1_given_id;
  9982. copy_pool_stratum(&info->pool1, &info->pool0);
  9983. info->pool1_given_id = info->pool0_given_id;
  9984. copy_pool_stratum(&info->pool0, pool);
  9985. info->pool0_given_id = ++given_id;
  9986. parse_job_to_soc(&buf, pool, info->pool0_given_id);
  9987. /* Step 4: Send out buf */
  9988. if(!status_error)
  9989. {
  9990. pthread_mutex_lock(&reinit_mutex);
  9991. send_job(buf);
  9992. pthread_mutex_unlock(&reinit_mutex);
  9993. }
  9994. cg_runlock(&pool->data_lock);
  9995. cg_wunlock(&info->update_lock);
  9996. free(buf);
  9997. mutex_unlock(&info->lock);
  9998. }
  9999. static void get_bitmain_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bitmain_soc)
  10000. {
  10001. struct bitmain_soc_info *info = bitmain_soc->device_data;
  10002. }
  10003. void remove_dot_char(char *number)
  10004. {
  10005. char tempStr[64];
  10006. int i,j=0;
  10007. for(i=0; i<strlen(number); i++)
  10008. {
  10009. if(number[i]!=',')
  10010. tempStr[j++]=number[i];
  10011. }
  10012. tempStr[j]='\0';
  10013. strcpy(number,tempStr);
  10014. }
  10015. void add_dot_number(char *number)
  10016. {
  10017. char *pstr;
  10018. char tempStr[32];
  10019. remove_dot_char(number);
  10020. strcpy(tempStr,number);
  10021. pstr=strstr(number,".");
  10022. if(pstr)
  10023. {
  10024. if((unsigned int)pstr-(unsigned int)number>3)
  10025. {
  10026. memcpy(tempStr,number,(unsigned int)pstr-(unsigned int)number-3);
  10027. tempStr[(unsigned int)pstr-(unsigned int)number-3]=',';
  10028. strcpy(tempStr+((unsigned int)pstr-(unsigned int)number-3+1),number+((unsigned int)pstr-(unsigned int)number-3));
  10029. }
  10030. }
  10031. strcpy(number,tempStr);
  10032. }
  10033. static struct api_data *bitmain_api_stats(struct cgpu_info *cgpu)
  10034. {
  10035. struct api_data *root = NULL;
  10036. struct bitmain_soc_info *info = cgpu->device_data;
  10037. char buf[64];
  10038. int i = 0;
  10039. uint64_t hash_rate_all = 0;
  10040. char displayed_rate_all[16];
  10041. bool copy_data = true;
  10042. root = api_add_uint8(root, "miner_count", &(dev->chain_num), copy_data);
  10043. root = api_add_string(root, "frequency", dev->frequency_t, copy_data);
  10044. root = api_add_uint8(root, "fan_num", &(dev->fan_num), copy_data);
  10045. for(i = 0; i < BITMAIN_MAX_FAN_NUM; i++)
  10046. {
  10047. char fan_name[12];
  10048. sprintf(fan_name,"fan%d", i+1);
  10049. root = api_add_uint(root, fan_name, &(dev->fan_speed_value[i]), copy_data);
  10050. }
  10051. root = api_add_uint8(root, "temp_num", &(dev->chain_num), copy_data);
  10052. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10053. {
  10054. char temp_name[12];
  10055. sprintf(temp_name,"temp%d", i+1);
  10056. root = api_add_int16(root, temp_name, &(dev->chain_asic_maxtemp[i][TEMP_POS_LOCAL]), copy_data);
  10057. }
  10058. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10059. {
  10060. char temp2_name[12];
  10061. sprintf(temp2_name,"temp2_%d", i+1);
  10062. root = api_add_int16(root, temp2_name, &(dev->chain_asic_temp[i][0][TEMP_POS_MIDDLE]), copy_data);
  10063. }
  10064. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10065. {
  10066. char temp3_name[12];
  10067. sprintf(temp3_name,"temp3_%d", i+1);
  10068. root = api_add_int16(root, temp3_name, &(dev->chain_asic_temp[i][1][TEMP_POS_MIDDLE]), copy_data);
  10069. }
  10070. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10071. {
  10072. char freq_sum[12];
  10073. int j = 0;
  10074. int temp;
  10075. double dev_sum_freq=0;
  10076. sprintf(freq_sum,"freq_avg%d",i+1);
  10077. if(dev->chain_exist[i] == 1)
  10078. {
  10079. #ifdef T9_18
  10080. if(getChainPICMagicNumber(i)== FREQ_MAGIC)
  10081. {
  10082. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10083. {
  10084. if(fpga_version>=0xE)
  10085. {
  10086. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  10087. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  10088. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq);
  10089. }
  10090. else
  10091. {
  10092. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq);
  10093. }
  10094. }
  10095. if(dev->chain_asic_num[i]>0)
  10096. dev_sum_freq=dev_sum_freq/dev->chain_asic_num[i];
  10097. temp=(int)(dev_sum_freq*100);
  10098. dev_sum_freq=((temp*1.0)/100);
  10099. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10100. }
  10101. else
  10102. {
  10103. temp=(int)(dev_sum_freq*100);
  10104. dev_sum_freq=((temp*1.0)/100);
  10105. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10106. }
  10107. #else
  10108. if(last_freq[i][1] == FREQ_MAGIC)
  10109. {
  10110. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10111. {
  10112. dev_sum_freq += atoi(freq_pll_1385[last_freq[i][j*2+3]].freq);
  10113. }
  10114. if(dev->chain_asic_num[i]>0)
  10115. dev_sum_freq=dev_sum_freq/dev->chain_asic_num[i];
  10116. temp=(int)(dev_sum_freq*100);
  10117. dev_sum_freq=((temp*1.0)/100);
  10118. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10119. }
  10120. else
  10121. {
  10122. temp=(int)(dev_sum_freq*100);
  10123. dev_sum_freq=((temp*1.0)/100);
  10124. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10125. }
  10126. #endif
  10127. }
  10128. else
  10129. {
  10130. temp=(int)(dev_sum_freq*100);
  10131. dev_sum_freq=((temp*1.0)/100);
  10132. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10133. }
  10134. }
  10135. if(1)
  10136. {
  10137. char freq_sum[32];
  10138. int j = 0;
  10139. int temp;
  10140. double dev_sum_freq=0;
  10141. sprintf(freq_sum,"total_rateideal");
  10142. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10143. {
  10144. if(dev->chain_exist[i] == 1)
  10145. {
  10146. #ifdef T9_18
  10147. if(getChainPICMagicNumber(i)== FREQ_MAGIC)
  10148. {
  10149. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10150. {
  10151. if(fpga_version>=0xE)
  10152. {
  10153. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  10154. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  10155. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  10156. }
  10157. else
  10158. {
  10159. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  10160. }
  10161. }
  10162. }
  10163. #else
  10164. if(last_freq[i][1] == FREQ_MAGIC)
  10165. {
  10166. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10167. {
  10168. dev_sum_freq += atoi(freq_pll_1385[last_freq[i][j*2+3]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  10169. }
  10170. }
  10171. #endif
  10172. }
  10173. }
  10174. dev_sum_freq=((dev_sum_freq*1.0)/1000);
  10175. temp=(int)(dev_sum_freq*100);
  10176. dev_sum_freq=((temp*1.0)/100);
  10177. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10178. }
  10179. if(1)
  10180. {
  10181. char freq_sum[32];
  10182. int j = 0;
  10183. int temp;
  10184. int total_acn_num=0;
  10185. double dev_sum_freq=0;
  10186. sprintf(freq_sum,"total_freqavg");
  10187. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10188. {
  10189. if(dev->chain_exist[i] == 1)
  10190. {
  10191. #ifdef T9_18
  10192. if(getChainPICMagicNumber(i)== FREQ_MAGIC)
  10193. {
  10194. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10195. {
  10196. if(fpga_version>=0xE)
  10197. {
  10198. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  10199. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  10200. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq);
  10201. }
  10202. else
  10203. {
  10204. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq);
  10205. }
  10206. total_acn_num++;
  10207. }
  10208. }
  10209. #else
  10210. if(last_freq[i][1] == FREQ_MAGIC)
  10211. {
  10212. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10213. {
  10214. dev_sum_freq += atoi(freq_pll_1385[last_freq[i][j*2+3]].freq);
  10215. total_acn_num++;
  10216. }
  10217. }
  10218. #endif
  10219. }
  10220. }
  10221. dev_sum_freq=(dev_sum_freq*1.0/total_acn_num);
  10222. temp=(int)(dev_sum_freq*100);
  10223. dev_sum_freq=((temp*1.0)/100);
  10224. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10225. }
  10226. if(1)
  10227. {
  10228. char freq_sum[32];
  10229. int16_t asic_num_total=0;
  10230. sprintf(freq_sum,"total_acn");
  10231. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10232. {
  10233. if(dev->chain_exist[i] == 1)
  10234. {
  10235. asic_num_total+=dev->chain_asic_num[i];
  10236. }
  10237. }
  10238. root = api_add_int16(root, freq_sum, &asic_num_total, true);
  10239. }
  10240. if(1)
  10241. {
  10242. double total_rate=0;
  10243. char freq_sum[32];
  10244. int temp;
  10245. sprintf(freq_sum,"total_rate");
  10246. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10247. {
  10248. if(dev->chain_exist[i] == 1 && strlen(displayed_rate[i])>0)
  10249. {
  10250. total_rate+=atof(displayed_rate[i]);
  10251. }
  10252. }
  10253. temp=(int)(total_rate*100);
  10254. total_rate=((temp*1.0)/100);
  10255. root = api_add_mhs(root, freq_sum, &total_rate, true);
  10256. }
  10257. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10258. {
  10259. char freq_sum[32];
  10260. int j = 0;
  10261. int temp;
  10262. double dev_sum_freq=0;
  10263. sprintf(freq_sum,"chain_rateideal%d",i+1);
  10264. if(dev->chain_exist[i] == 1)
  10265. {
  10266. #ifdef T9_18
  10267. if(getChainPICMagicNumber(i)== FREQ_MAGIC)
  10268. {
  10269. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10270. {
  10271. if(fpga_version>=0xE)
  10272. {
  10273. int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
  10274. getPICChainIndexOffset(i,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
  10275. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  10276. }
  10277. else
  10278. {
  10279. dev_sum_freq += atoi(freq_pll_1385[chain_pic_buf[((i/3)*3)][7+(i%3)*31+4+j]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  10280. }
  10281. }
  10282. dev_sum_freq=((dev_sum_freq*1.0)/1000);
  10283. temp=(int)(dev_sum_freq*100);
  10284. dev_sum_freq=((temp*1.0)/100);
  10285. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10286. }
  10287. else
  10288. {
  10289. dev_sum_freq=((dev_sum_freq*1.0)/1000);
  10290. temp=(int)(dev_sum_freq*100);
  10291. dev_sum_freq=((temp*1.0)/100);
  10292. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10293. }
  10294. #else
  10295. if(last_freq[i][1] == FREQ_MAGIC)
  10296. {
  10297. for(j = 0; j < dev->chain_asic_num[i]; j++)
  10298. {
  10299. dev_sum_freq += atoi(freq_pll_1385[last_freq[i][j*2+3]].freq)*(BM1387_CORE_NUM-chain_badcore_num[i][j]);
  10300. }
  10301. dev_sum_freq=((dev_sum_freq*1.0)/1000);
  10302. temp=(int)(dev_sum_freq*100);
  10303. dev_sum_freq=((temp*1.0)/100);
  10304. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10305. }
  10306. else
  10307. {
  10308. dev_sum_freq=((dev_sum_freq*1.0)/1000);
  10309. temp=(int)(dev_sum_freq*100);
  10310. dev_sum_freq=((temp*1.0)/100);
  10311. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10312. }
  10313. #endif
  10314. }
  10315. else
  10316. {
  10317. dev_sum_freq=((dev_sum_freq*1.0)/1000);
  10318. temp=(int)(dev_sum_freq*100);
  10319. dev_sum_freq=((temp*1.0)/100);
  10320. root = api_add_mhs(root, freq_sum, &dev_sum_freq, true);
  10321. }
  10322. }
  10323. root = api_add_int(root, "temp_max", &(dev->temp_top1[PWM_T]), copy_data);
  10324. total_diff1 = total_diff_accepted + total_diff_rejected + total_diff_stale;
  10325. double dev_hwp = (hw_errors + total_diff1) ?
  10326. (double)(hw_errors) / (double)(hw_errors + total_diff1) : 0;
  10327. root = api_add_percent(root, "Device Hardware%", &(dev_hwp), true);
  10328. root = api_add_int(root, "no_matching_work", &hw_errors, copy_data);
  10329. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10330. {
  10331. char chain_name[12];
  10332. sprintf(chain_name,"chain_acn%d",i+1);
  10333. root = api_add_uint8(root, chain_name, &(dev->chain_asic_num[i]), copy_data);
  10334. }
  10335. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10336. {
  10337. char chain_asic_name[12];
  10338. sprintf(chain_asic_name,"chain_acs%d",i+1);
  10339. root = api_add_string(root, chain_asic_name, dev->chain_asic_status_string[i], copy_data);
  10340. }
  10341. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10342. {
  10343. char chain_hw[16];
  10344. sprintf(chain_hw,"chain_hw%d",i+1);
  10345. root = api_add_uint32(root, chain_hw, &(dev->chain_hw[i]), copy_data);
  10346. }
  10347. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10348. {
  10349. char chain_rate[16];
  10350. sprintf(chain_rate,"chain_rate%d",i+1);
  10351. root = api_add_string(root, chain_rate, displayed_rate[i], copy_data);
  10352. }
  10353. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10354. {
  10355. if(dev->chain_exist[i] == 1)
  10356. {
  10357. bool first = true;
  10358. int j = 0;
  10359. char chain_xtime[16];
  10360. char xtime[2048] = "{";
  10361. char tmp[20] = "";
  10362. sprintf(chain_xtime,"chain_xtime%d",i+1);
  10363. if(x_time[i][0] != 0)
  10364. {
  10365. sprintf(tmp,"X%d=%d",0,x_time[i][0]);
  10366. strcat(xtime,tmp);
  10367. first = false;
  10368. }
  10369. for (j = 1; j < dev->chain_asic_num[i]; j++)
  10370. {
  10371. if(x_time[i][j] != 0)
  10372. {
  10373. if (first)
  10374. {
  10375. sprintf(tmp,"X%d=%d",j,x_time[i][j]);
  10376. first = false;
  10377. }
  10378. else
  10379. {
  10380. sprintf(tmp,",X%d=%d",j,x_time[i][j]);
  10381. }
  10382. strcat(xtime,tmp);
  10383. }
  10384. }
  10385. strcat(xtime,"}");
  10386. root = api_add_string(root, chain_xtime, xtime, copy_data);
  10387. }
  10388. }
  10389. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10390. {
  10391. if(dev->chain_exist[i] == 1)
  10392. {
  10393. int j = 0;
  10394. char chain_offside[20];
  10395. char tmp[20];
  10396. sprintf(chain_offside,"chain_offside_%d",i+1);
  10397. sprintf(tmp,"%d",temp_offside[i]);
  10398. root = api_add_string(root, chain_offside, tmp, copy_data);
  10399. }
  10400. }
  10401. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10402. {
  10403. if(dev->chain_exist[i] == 1)
  10404. {
  10405. int j = 0;
  10406. char chain_opencore[20];
  10407. char tmp[20];
  10408. sprintf(chain_opencore,"chain_opencore_%d",i+1);
  10409. if(isChainAllCoresOpened[i])
  10410. sprintf(tmp,"1");
  10411. else sprintf(tmp,"0");
  10412. root = api_add_string(root, chain_opencore, tmp, copy_data);
  10413. }
  10414. }
  10415. for(i = 0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  10416. {
  10417. if(dev->chain_exist[i] == 1)
  10418. {
  10419. hash_rate_all += rate[i];
  10420. }
  10421. }
  10422. suffix_string_soc(hash_rate_all, (char * )displayed_hash_rate, sizeof(displayed_hash_rate), 7,false);
  10423. if(1)
  10424. {
  10425. char param_name[32];
  10426. sprintf(param_name,"miner_version");
  10427. root = api_add_string(root, param_name, g_miner_version, copy_data);
  10428. }
  10429. return root;
  10430. }
  10431. static void bitmain_soc_shutdown(struct thr_info *thr)
  10432. {
  10433. unsigned int ret;
  10434. thr_info_cancel(check_system_work_id);
  10435. thr_info_cancel(read_nonce_reg_id);
  10436. thr_info_cancel(read_temp_id);
  10437. thr_info_cancel(pic_heart_beat);
  10438. ret = get_BC_write_command(); //disable null work
  10439. ret &= ~BC_COMMAND_EN_NULL_WORK;
  10440. set_BC_write_command(ret);
  10441. set_dhash_acc_control((unsigned int)get_dhash_acc_control() & ~RUN_BIT);
  10442. }
  10443. struct device_drv bitmain_soc_drv =
  10444. {
  10445. .drv_id = DRIVER_bitmain_soc,
  10446. .dname = "Bitmain_SOC",
  10447. .name = "BTM_SOC",
  10448. .drv_detect = bitmain_soc_detect,
  10449. .thread_prepare = bitmain_soc_prepare,
  10450. .hash_work = hash_driver_work,
  10451. .scanwork = bitmain_soc_scanhash,
  10452. .flush_work = bitmain_soc_update,
  10453. .update_work = bitmain_soc_update,
  10454. .get_api_stats = bitmain_api_stats,
  10455. .reinit_device = bitmain_soc_reinit_device,
  10456. .get_statline_before = get_bitmain_statline_before,
  10457. .thread_shutdown = bitmain_soc_shutdown,
  10458. };