mtk_nand.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <string.h>
  32. #include <config.h>
  33. #include <malloc.h>
  34. #include <printf.h>
  35. #include <platform/mt_typedefs.h>
  36. #include <platform/mtk_nand.h>
  37. #include <partition.h>
  38. #include <platform/bmt.h>
  39. #include "partition_define.h"
  40. #include "cust_nand.h"
  41. #include <arch/ops.h>
  42. #include <kernel/event.h>
  43. #include <platform/mt_irq.h>
  44. #include <platform/mtk_nand_ops.h>
  45. #if defined(MTK_COMBO_NAND_SUPPORT)
  46. // BMT_POOL_SIZE is not used anymore
  47. #else
  48. #ifndef PART_SIZE_BMTPOOL
  49. #define BMT_POOL_SIZE (80)
  50. #else
  51. #define BMT_POOL_SIZE (PART_SIZE_BMTPOOL)
  52. #endif
  53. #endif
  54. #define PMT_POOL_SIZE (2)
  55. #define STATUS_READY (0x40)
  56. #define STATUS_FAIL (0x01)
  57. #define STATUS_WR_ALLOW (0x80)
  58. static bool nand_reset(void);
  59. static bool nand_set_command(u16 command);
  60. static bool nand_device_reset(void);
  61. bool mtk_nand_SetFeature(u16 cmd, u32 addr, u8 *value, u8 bytes);
  62. bool mtk_nand_GetFeature(u16 cmd, u32 addr, u8 *value, u8 bytes);
  63. static int mtk_nand_interface_async();
  64. #if defined(CONFIG_CMD_NAND)
  65. extern int mt_part_register_device(part_dev_t * dev);
  66. struct nand_ecclayout nand_oob_16 = {
  67. .eccbytes = 8,
  68. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  69. .oobfree = {{1, 6}, {0, 0}}
  70. };
  71. struct nand_ecclayout nand_oob_64 = {
  72. .eccbytes = 32,
  73. .eccpos = {
  74. 32, 33, 34, 35, 36, 37, 38, 39,
  75. 40, 41, 42, 43, 44, 45, 46, 47,
  76. 48, 49, 50, 51, 52, 53, 54, 55,
  77. 56, 57, 58, 59, 60, 61, 62, 63
  78. },
  79. .oobfree = {{1, 7}, {9, 7}, {17, 7}, {25, 6}, {0, 0}}
  80. };
  81. struct nand_ecclayout nand_oob_128 = {
  82. .eccbytes = 64,
  83. .eccpos = {
  84. 64, 65, 66, 67, 68, 69, 70, 71,
  85. 72, 73, 74, 75, 76, 77, 78, 79,
  86. 80, 81, 82, 83, 84, 85, 86, 86,
  87. 88, 89, 90, 91, 92, 93, 94, 95,
  88. 96, 97, 98, 99, 100, 101, 102, 103,
  89. 104, 105, 106, 107, 108, 109, 110, 111,
  90. 112, 113, 114, 115, 116, 117, 118, 119,
  91. 120, 121, 122, 123, 124, 125, 126, 127
  92. },
  93. .oobfree = {{1, 7}, {9, 7}, {17, 7}, {25, 7}, {33, 7}, {41, 7}, {49, 7},
  94. {57, 6}
  95. }
  96. };
  97. static bmt_struct *g_bmt = NULL;
  98. struct nand_chip g_nand_chip;
  99. static int en_interrupt = 0;
  100. static event_t nand_int_event;
  101. #if defined(MTK_TLC_NAND_SUPPORT)
  102. bool tlc_lg_left_plane = TRUE; //logical left plane of tlc nand. used to do page program
  103. NFI_TLC_PG_CYCLE tlc_program_cycle;
  104. bool tlc_not_keep_erase_lvl = FALSE;//not keep erase level
  105. u32 slc_ratio = 6; //slc mode block ration in FS partition. means slc_ration %
  106. u32 sys_slc_ratio = 6;
  107. u32 usr_slc_ratio = 6;
  108. #endif
  109. #define dprintf MSG
  110. #define INFO INIT
  111. #if defined(MTK_TLC_NAND_SUPPORT)
  112. extern void mtk_pmt_reset(void);
  113. extern void mtk_slc_blk_addr(u64 addr, u32* blk_num, u32* page_in_block);
  114. extern bool mtk_block_istlc(u64 addr);
  115. extern bool mtk_nand_IsBMTPOOL(u64 logical_address);
  116. int nand_part_erase(int dev_num, u64 offset, u64 size, int part_id);
  117. #endif
  118. //extern unsigned int flash_number;
  119. //flashdev_info gen_FlashTable_p[MAX_FLASH];
  120. #if CFG_2CS_NAND
  121. static bool g_bTricky_CS = FALSE; // for nandbase.c
  122. static bool g_b2Die_CS = FALSE;
  123. static u32 g_nanddie_pages = 0;
  124. #endif
  125. #define ERR_RTN_SUCCESS 1
  126. #define ERR_RTN_FAIL 0
  127. #define ERR_RTN_BCH_FAIL -1
  128. u32 BLOCK_SIZE;
  129. static u32 PAGES_PER_BLOCK = 255;
  130. #define NFI_ISSUE_COMMAND(cmd, col_addr, row_addr, col_num, row_num) \
  131. do { \
  132. DRV_WriteReg(NFI_CMD_REG16,cmd);\
  133. while (DRV_Reg32(NFI_STA_REG32) & STA_CMD_STATE);\
  134. DRV_WriteReg32(NFI_COLADDR_REG32, col_addr);\
  135. DRV_WriteReg32(NFI_ROWADDR_REG32, row_addr);\
  136. DRV_WriteReg(NFI_ADDRNOB_REG16, col_num | (row_num<<ADDR_ROW_NOB_SHIFT));\
  137. while (DRV_Reg32(NFI_STA_REG32) & STA_ADDR_STATE);\
  138. }while(0);
  139. flashdev_info devinfo;
  140. #define CHIPVER_ECO_1 (0x8a00)
  141. #define CHIPVER_ECO_2 (0x8a01)
  142. #define RAND_TYPE_SAMSUNG 0
  143. #define RAND_TYPE_TOSHIBA 1
  144. #define RAND_TYPE_NONE 2
  145. extern u64 part_get_startaddress(u64 byte_address, u32 *idx);
  146. extern bool raw_partition(u32 index);
  147. bool __nand_erase (u64 logical_addr);
  148. bool mark_block_bad (u64 logical_addr);
  149. bool nand_erase_hw (u64 offset);
  150. int check_data_empty(void *data, unsigned size);
  151. struct NAND_CMD g_kCMD;
  152. static u32 g_i4ErrNum;
  153. static bool g_bInitDone;
  154. u64 total_size;
  155. u64 g_nand_size = 0;
  156. static bool DDR_INTERFACE = FALSE;
  157. #define LPAGE 16384
  158. #define LSPARE 2048
  159. __attribute__ ((aligned(64))) static unsigned char g_data_buf[LPAGE+LSPARE];
  160. __attribute__ ((aligned(64))) static struct nand_buffers nBuf;
  161. __attribute__ ((aligned(64))) static unsigned char data_buf_temp[LPAGE];
  162. __attribute__ ((aligned(64))) static unsigned char oob_buf_temp[LSPARE];
  163. #if defined(MTK_TLC_NAND_SUPPORT)
  164. __attribute__ ((aligned(64))) static unsigned char local_tlc_wl_buffer[LPAGE];
  165. #endif
  166. enum flashdev_vendor gVendor;
  167. #if defined(MTK_TLC_NAND_SUPPORT)
  168. unsigned char g_spare_buf[LSPARE];
  169. #else
  170. static unsigned char g_spare_buf[LSPARE];
  171. #endif
  172. static u64 download_size = 0;
  173. static bool use_randomizer = FALSE;
  174. u32 MICRON_TRANSFER(u32 pageNo);
  175. u32 SANDISK_TRANSFER(u32 pageNo);
  176. u32 HYNIX_TRANSFER(u32 pageNo);
  177. typedef u32 (*GetLowPageNumber)(u32 pageNo);
  178. GetLowPageNumber functArray[]= {
  179. MICRON_TRANSFER,
  180. HYNIX_TRANSFER,
  181. SANDISK_TRANSFER,
  182. };
  183. u32 SANDISK_TRANSFER(u32 pageNo)
  184. {
  185. if (0 == pageNo) {
  186. return pageNo;
  187. } else {
  188. return pageNo+pageNo-1;
  189. }
  190. }
  191. u32 HYNIX_TRANSFER(u32 pageNo)
  192. {
  193. u32 temp;
  194. if (pageNo < 4)
  195. return pageNo;
  196. temp = pageNo+(pageNo&0xFFFFFFFE)-2;
  197. return temp;
  198. }
  199. u32 MICRON_TRANSFER(u32 pageNo)
  200. {
  201. u32 temp;
  202. if (pageNo < 4)
  203. return pageNo;
  204. temp = (pageNo - 4) & 0xFFFFFFFE;
  205. if (pageNo<=130)
  206. return (pageNo+temp);
  207. else
  208. return (pageNo+temp-2);
  209. }
  210. static bool mtk_nand_read_status(void);
  211. static inline unsigned int uffs(unsigned int x)
  212. {
  213. unsigned int r = 1;
  214. if (!x)
  215. return 0;
  216. if (!(x & 0xffff)) {
  217. x >>= 16;
  218. r += 16;
  219. }
  220. if (!(x & 0xff)) {
  221. x >>= 8;
  222. r += 8;
  223. }
  224. if (!(x & 0xf)) {
  225. x >>= 4;
  226. r += 4;
  227. }
  228. if (!(x & 3)) {
  229. x >>= 2;
  230. r += 2;
  231. }
  232. if (!(x & 1)) {
  233. x >>= 1;
  234. r += 1;
  235. }
  236. return r;
  237. }
  238. void dump_nfi(void)
  239. {
  240. dprintf(INFO,"~~~~Dump NFI Register in LK~~~~\n");
  241. dprintf(INFO,"NFI_CNFG_REG16: 0x%x\n", DRV_Reg16(NFI_CNFG_REG16));
  242. dprintf(INFO,"NFI_PAGEFMT_REG32: 0x%x\n", DRV_Reg32(NFI_PAGEFMT_REG32));
  243. dprintf(INFO,"NFI_CON_REG32: 0x%x\n", DRV_Reg32(NFI_CON_REG32));
  244. dprintf(INFO,"NFI_ACCCON_REG32: 0x%x\n", DRV_Reg32(NFI_ACCCON_REG32));
  245. dprintf(INFO,"NFI_INTR_EN_REG32: 0x%x\n", DRV_Reg32(NFI_INTR_EN_REG32));
  246. dprintf(INFO,"NFI_INTR_REG16: 0x%x\n", DRV_Reg16(NFI_INTR_REG16));
  247. dprintf(INFO,"NFI_CMD_REG16: 0x%x\n", DRV_Reg16(NFI_CMD_REG16));
  248. dprintf(INFO,"NFI_ADDRNOB_REG16: 0x%x\n", DRV_Reg16(NFI_ADDRNOB_REG16));
  249. dprintf(INFO,"NFI_COLADDR_REG32: 0x%x\n", DRV_Reg32(NFI_COLADDR_REG32));
  250. dprintf(INFO,"NFI_ROWADDR_REG32: 0x%x\n", DRV_Reg32(NFI_ROWADDR_REG32));
  251. dprintf(INFO,"NFI_STRDATA_REG16: 0x%x\n", DRV_Reg16(NFI_STRDATA_REG16));
  252. dprintf(INFO,"NFI_DATAW_REG32: 0x%x\n", DRV_Reg32(NFI_DATAW_REG32));
  253. dprintf(INFO,"NFI_DATAR_REG32: 0x%x\n", DRV_Reg32(NFI_DATAR_REG32));
  254. dprintf(INFO,"NFI_PIO_DIRDY_REG16: 0x%x\n", DRV_Reg16(NFI_PIO_DIRDY_REG16));
  255. dprintf(INFO,"NFI_STA_REG32: 0x%x\n", DRV_Reg32(NFI_STA_REG32));
  256. dprintf(INFO,"NFI_FIFOSTA_REG16: 0x%x\n", DRV_Reg16(NFI_FIFOSTA_REG16));
  257. // dprintf(INFO,"NFI_LOCKSTA_REG16: 0x%x\n", DRV_Reg16(NFI_LOCKSTA_REG16));
  258. dprintf(INFO,"NFI_ADDRCNTR_REG32: 0x%x\n", DRV_Reg32(NFI_ADDRCNTR_REG32));
  259. dprintf(INFO,"NFI_STRADDR_REG32: 0x%x\n", DRV_Reg32(NFI_STRADDR_REG32));
  260. dprintf(INFO,"NFI_BYTELEN_REG32: 0x%x\n", DRV_Reg32(NFI_BYTELEN_REG32));
  261. dprintf(INFO,"NFI_CSEL_REG16: 0x%x\n", DRV_Reg16(NFI_CSEL_REG16));
  262. dprintf(INFO,"NFI_IOCON_REG16: 0x%x\n", DRV_Reg16(NFI_IOCON_REG16));
  263. dprintf(INFO,"NFI_FDM0L_REG32: 0x%x\n", DRV_Reg32(NFI_FDM0L_REG32));
  264. dprintf(INFO,"NFI_FDM0M_REG32: 0x%x\n", DRV_Reg32(NFI_FDM0M_REG32));
  265. dprintf(INFO,"NFI_FIFODATA0_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA0_REG32));
  266. dprintf(INFO,"NFI_FIFODATA1_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA1_REG32));
  267. dprintf(INFO,"NFI_FIFODATA2_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA2_REG32));
  268. dprintf(INFO,"NFI_FIFODATA3_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA3_REG32));
  269. dprintf(INFO,"NFI_DEBUG_CON1_REG16: 0x%x\n", DRV_Reg32(NFI_DEBUG_CON1_REG16));
  270. dprintf(INFO,"NFI_MASTERSTA_REG32: 0x%x\n", DRV_Reg32(NFI_MASTERSTA_REG32));
  271. dprintf(INFO,"NFI_MASTERRST_REG32: 0x%x\n", DRV_Reg32(NFI_MASTERRST_REG32));
  272. dprintf(INFO,"NFI_RANDOM_CNFG_REG32: 0x%x\n", DRV_Reg32(NFI_RANDOM_CNFG_REG32));
  273. dprintf(INFO,"NFI_NAND_TYPE_CNFG_REG32: 0x%x\n", DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32));
  274. dprintf(INFO,"NFI_ACCCON1_REG32: 0x%x\n", DRV_Reg32(NFI_ACCCON1_REG32));
  275. dprintf(INFO,"NFI_DLYCTRL_REG32: 0x%x\n", DRV_Reg32(NFI_DLYCTRL_REG32));
  276. dprintf(INFO,"ECC_ENCCON_REG16: 0x%x\n", DRV_Reg32(ECC_ENCCON_REG16));
  277. dprintf(INFO,"ECC_ENCCNFG_REG32: 0x%x\n", DRV_Reg32(ECC_ENCCNFG_REG32));
  278. dprintf(INFO,"ECC_ENCDIADDR_REG32: 0x%x\n", DRV_Reg32(ECC_ENCDIADDR_REG32));
  279. dprintf(INFO,"ECC_ENCIDLE_REG32: 0x%x\n", DRV_Reg32(ECC_ENCIDLE_REG32));
  280. dprintf(INFO,"ECC_ENCPAR0_REG32: 0x%x\n", DRV_Reg32(ECC_ENCPAR0_REG32));
  281. dprintf(INFO,"ECC_ENCPAR1_REG32: 0x%x\n", DRV_Reg32(ECC_ENCPAR1_REG32));
  282. dprintf(INFO,"ECC_ENCSTA_REG32: 0x%x\n", DRV_Reg32(ECC_ENCSTA_REG32));
  283. dprintf(INFO,"ECC_DECCON_REG16: 0x%x\n", DRV_Reg32(ECC_DECCON_REG16));
  284. dprintf(INFO,"ECC_DECCNFG_REG32: 0x%x\n", DRV_Reg32(ECC_DECCNFG_REG32));
  285. dprintf(INFO,"ECC_DECDIADDR_REG32: 0x%x\n", DRV_Reg32(ECC_DECDIADDR_REG32));
  286. dprintf(INFO,"ECC_DECIDLE_REG16: 0x%x\n", DRV_Reg32(ECC_DECIDLE_REG16));
  287. dprintf(INFO,"ECC_DECFER_REG16: 0x%x\n", DRV_Reg32(ECC_DECFER_REG16));
  288. dprintf(INFO,"ECC_DECENUM0_REG32: 0x%x\n", DRV_Reg32(ECC_DECENUM0_REG32));
  289. dprintf(INFO,"ECC_DECENUM1_REG32: 0x%x\n", DRV_Reg32(ECC_DECENUM1_REG32));
  290. dprintf(INFO,"ECC_DECDONE_REG16: 0x%x\n", DRV_Reg32(ECC_DECDONE_REG16));
  291. dprintf(INFO,"ECC_DECEL0_REG32: 0x%x\n", DRV_Reg32(ECC_DECEL0_REG32));
  292. dprintf(INFO,"ECC_DECEL1_REG32: 0x%x\n", DRV_Reg32(ECC_DECEL1_REG32));
  293. dprintf(INFO,"ECC_BYPASS_REG32: 0x%x\n", DRV_Reg32(ECC_BYPASS_REG32));
  294. //dprintf(INFO,"NFI clock register: 0x%x: %s\n",(PERI_CON_BASE+0x18), (DRV_Reg32((volatile u32 *)(PERI_CON_BASE+0x18)) & (0x1)) ? "Clock Disabled" : "Clock Enabled");
  295. //dprintf(INFO,"NFI clock SEL (MT65XX):0x%x: %s\n",(PERI_CON_BASE+0x5C), (DRV_Reg32((volatile u32 *)(PERI_CON_BASE+0x5C)) & (0x1)) ? "Half clock" : "Quarter clock");
  296. }
  297. extern bool init_pmt_done;
  298. u32 mtk_nand_page_transform(u64 logical_address, u32* blk, u32* map_blk)
  299. {
  300. u64 start_address;
  301. u32 index = 0;
  302. u32 block;
  303. u32 page_in_block;
  304. u32 mapped_block;
  305. #if defined(MTK_TLC_NAND_SUPPORT)
  306. devinfo.tlcControl.slcopmodeEn = FALSE;
  307. #endif
  308. if (VEND_NONE != gVendor && init_pmt_done == TRUE) {
  309. start_address = part_get_startaddress((u64)logical_address, &index);
  310. //MSG(ERR, "start_address(0x%llx), logical_address(0x%x) index(%d)\n",start_address,logical_address,index);
  311. if ((0xFFFFFFFF != index) && (raw_partition(index))) {
  312. // if(start_address == 0xFFFFFFFF)
  313. // while(1);
  314. //MSG(ERR, "raw_partition(%d)\n",index);
  315. #if defined(MTK_TLC_NAND_SUPPORT)
  316. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  317. if (devinfo.tlcControl.normaltlc) {
  318. block = (u32)((u32)(start_address / BLOCK_SIZE) + (u32)((logical_address-start_address) / (BLOCK_SIZE / 3)));
  319. page_in_block = ((u32)((logical_address-start_address) >> g_nand_chip.page_shift) % ((BLOCK_SIZE/g_nand_chip.page_size)/3));
  320. page_in_block *= 3;
  321. devinfo.tlcControl.slcopmodeEn = TRUE;
  322. } else {
  323. block = (u32)((u32)(start_address / BLOCK_SIZE) + (u32)((logical_address-start_address)/ (BLOCK_SIZE / 3)));
  324. page_in_block = ((u32)((logical_address-start_address) >> g_nand_chip.page_shift) % ((BLOCK_SIZE/g_nand_chip.page_size)/3));
  325. //MSG(INIT , "[LOW]0x%x, 0x%x\n",block,page_in_block);
  326. if (devinfo.vendor != VEND_NONE) {
  327. //page_in_block = devinfo.feature_set.PairPage[page_in_block];
  328. page_in_block = functArray[devinfo.feature_set.ptbl_idx](page_in_block);
  329. }
  330. }
  331. } else
  332. #endif
  333. {
  334. block = (u32)((start_address/BLOCK_SIZE) + (logical_address-start_address) / g_nand_chip.erasesize);
  335. page_in_block = (u32)(((logical_address-start_address) /g_nand_chip.page_size)% ((1 << (g_nand_chip.phys_erase_shift-g_nand_chip.page_shift))));
  336. if (devinfo.vendor != VEND_NONE) {
  337. // page_in_block = devinfo.feature_set.PairPage[page_in_block];
  338. page_in_block = functArray[devinfo.feature_set.ptbl_idx](page_in_block);
  339. }
  340. }
  341. mapped_block = get_mapping_block_index(block);
  342. //MSG(ERR, "transform_address(0x%x)\n",mapped_block*(BLOCK_SIZE/(g_nand_chip.page_size))+page_in_block);
  343. } else {
  344. #if defined(MTK_TLC_NAND_SUPPORT)
  345. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) && (!mtk_block_istlc(logical_address))) {
  346. mtk_slc_blk_addr(logical_address, &block, &page_in_block);
  347. devinfo.tlcControl.slcopmodeEn = TRUE;
  348. } else
  349. #endif
  350. {
  351. block = (u32)(logical_address/BLOCK_SIZE);
  352. page_in_block = (u32)((logical_address/g_nand_chip.page_size) % (BLOCK_SIZE >> g_nand_chip.page_shift));
  353. }
  354. mapped_block = get_mapping_block_index(block);
  355. }
  356. } else {
  357. block = (u32)(logical_address/BLOCK_SIZE);
  358. mapped_block = get_mapping_block_index(block);
  359. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  360. page_in_block = (u32)((logical_address/g_nand_chip.page_size) % ((BLOCK_SIZE/g_nand_chip.page_size)/3));
  361. page_in_block *= 3;
  362. } else
  363. page_in_block = (u32)((logical_address/g_nand_chip.page_size) % (BLOCK_SIZE >> g_nand_chip.page_shift));
  364. devinfo.tlcControl.slcopmodeEn = TRUE;
  365. }
  366. *blk = block;
  367. *map_blk = mapped_block;
  368. return mapped_block*(BLOCK_SIZE/(g_nand_chip.page_size))+page_in_block;
  369. }
  370. bool get_device_info(u8*id, flashdev_info *devinfo)
  371. {
  372. u32 i,m,n,mismatch;
  373. int target=-1;
  374. u8 target_id_len=0;
  375. unsigned int flash_number = sizeof(gen_FlashTable) / sizeof(gen_FlashTable[0]);
  376. for (i = 0; i<flash_number; i++) {
  377. mismatch=0;
  378. for (m=0; m<gen_FlashTable[i].id_length; m++) {
  379. if (id[m]!=gen_FlashTable[i].id[m]) {
  380. mismatch=1;
  381. break;
  382. }
  383. }
  384. if (mismatch == 0 && gen_FlashTable[i].id_length > target_id_len) {
  385. target=i;
  386. target_id_len=gen_FlashTable[i].id_length;
  387. }
  388. }
  389. if (target != -1) {
  390. MSG(INIT, "Recognize NAND: ID [");
  391. for (n=0; n<gen_FlashTable[target].id_length; n++) {
  392. devinfo->id[n] = gen_FlashTable[target].id[n];
  393. MSG(INIT, "%x ",devinfo->id[n]);
  394. }
  395. MSG(INIT, "], Device Name [%s], Page Size [%d]B Spare Size [%d]B Total Size [%d]MB\n",gen_FlashTable[target].devciename,gen_FlashTable[target].pagesize,gen_FlashTable[target].sparesize,gen_FlashTable[target].totalsize);
  396. devinfo->id_length=gen_FlashTable[i].id_length;
  397. devinfo->blocksize = gen_FlashTable[target].blocksize;
  398. devinfo->addr_cycle = gen_FlashTable[target].addr_cycle;
  399. devinfo->iowidth = gen_FlashTable[target].iowidth;
  400. devinfo->timmingsetting = gen_FlashTable[target].timmingsetting;
  401. devinfo->advancedmode = gen_FlashTable[target].advancedmode;
  402. devinfo->pagesize = gen_FlashTable[target].pagesize;
  403. devinfo->sparesize = gen_FlashTable[target].sparesize;
  404. devinfo->totalsize = gen_FlashTable[target].totalsize;
  405. devinfo->sectorsize = gen_FlashTable[target].sectorsize;
  406. devinfo->s_acccon= gen_FlashTable[target].s_acccon;
  407. devinfo->s_acccon1= gen_FlashTable[target].s_acccon1;
  408. devinfo->freq= gen_FlashTable[target].freq;
  409. devinfo->vendor = gen_FlashTable[target].vendor;
  410. gVendor = gen_FlashTable[target].vendor;
  411. devinfo->dqs_delay_ctrl = gen_FlashTable[target].dqs_delay_ctrl;
  412. memcpy((u8*)&devinfo->feature_set, (u8*)&gen_FlashTable[target].feature_set, sizeof(struct MLC_feature_set));
  413. memcpy(devinfo->devciename, gen_FlashTable[target].devciename, sizeof(devinfo->devciename));
  414. #if defined(MTK_TLC_NAND_SUPPORT)
  415. devinfo->NAND_FLASH_TYPE = gen_FlashTable[target].NAND_FLASH_TYPE;
  416. memcpy((u8*)&devinfo->tlcControl, (u8*)&gen_FlashTable[target].tlcControl, sizeof(NFI_TLC_CTRL));
  417. #endif
  418. return true;
  419. } else {
  420. MSG(INIT, "Not Found NAND: ID [");
  421. for (n=0; n<NAND_MAX_ID; n++) {
  422. MSG(INIT, "%x ",id[n]);
  423. }
  424. MSG(INIT, "]\n");
  425. return false;
  426. }
  427. }
  428. #if defined(MTK_TLC_NAND_SUPPORT)
  429. bool mtk_is_normal_tlc_nand(void)
  430. {
  431. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  432. && (devinfo.tlcControl.normaltlc))
  433. return TRUE;
  434. else
  435. return FALSE;
  436. }
  437. //tlc releated functions
  438. void NFI_TLC_GetMappedWL(u32 pageidx, NFI_TLC_WL_INFO* WL_Info)
  439. {
  440. //this function is just for normal tlc
  441. WL_Info->word_line_idx = pageidx / 3;
  442. WL_Info->wl_pre = (NFI_TLC_WL_PRE)(pageidx % 3);
  443. }
  444. u32 NFI_TLC_GetRowAddr(u32 rowaddr)
  445. {
  446. u32 real_row;
  447. u32 temp = 0xFF;
  448. int page_per_block = devinfo.blocksize * 1024 / devinfo.pagesize;
  449. if (devinfo.tlcControl.normaltlc) { // normal tlc
  450. temp = page_per_block / 3;
  451. } else {
  452. temp = page_per_block;
  453. }
  454. real_row = ((rowaddr / temp) << devinfo.tlcControl.block_bit) | (rowaddr % temp);
  455. return real_row;
  456. }
  457. u32 NFI_TLC_SetpPlaneAddr(u32 rowaddr, bool left_plane)
  458. {
  459. //this function is just for normal tlc
  460. u32 real_row;
  461. if (devinfo.tlcControl.pPlaneEn) {
  462. if (left_plane)
  463. real_row = (rowaddr & (~(1 << devinfo.tlcControl.pPlane_bit)));
  464. else
  465. real_row = (rowaddr | (1 << devinfo.tlcControl.pPlane_bit));
  466. } else
  467. real_row = rowaddr;
  468. return real_row;
  469. }
  470. u32 NFI_TLC_GetMappedPgAddr(u32 rowaddr) //rowaddr is the real address, the return value is the page increased by degree (pageidx = block no * page per block)
  471. {
  472. u32 page_idx;
  473. u32 page_shift = 0;
  474. u32 real_row;
  475. int page_per_block = devinfo.blocksize * 1024 / devinfo.pagesize;
  476. real_row = rowaddr;
  477. if (devinfo.tlcControl.normaltlc) { // normal tlc
  478. page_shift = devinfo.tlcControl.block_bit;
  479. if (devinfo.tlcControl.pPlaneEn) {
  480. real_row &= (~(1 << devinfo.tlcControl.pPlane_bit));
  481. }
  482. page_idx = ((real_row >> page_shift) * page_per_block) + (((real_row << (32-page_shift)) >> (32-page_shift)) * 3); //always get wl's low page
  483. } else { //micron tlc
  484. page_shift = devinfo.tlcControl.block_bit;
  485. page_idx = ((real_row >> page_shift) * page_per_block) + ((real_row << (32-page_shift)) >> (32-page_shift));
  486. }
  487. return page_idx;
  488. }
  489. #endif
  490. u16 randomizer_seed[128] = {
  491. 0x576A, 0x05E8, 0x629D, 0x45A3,
  492. 0x649C, 0x4BF0, 0x2342, 0x272E,
  493. 0x7358, 0x4FF3, 0x73EC, 0x5F70,
  494. 0x7A60, 0x1AD8, 0x3472, 0x3612,
  495. 0x224F, 0x0454, 0x030E, 0x70A5,
  496. 0x7809, 0x2521, 0x48F4, 0x5A2D,
  497. 0x492A, 0x043D, 0x7F61, 0x3969,
  498. 0x517A, 0x3B42, 0x769D, 0x0647,
  499. 0x7E2A, 0x1383, 0x49D9, 0x07B8,
  500. 0x2578, 0x4EEC, 0x4423, 0x352F,
  501. 0x5B22, 0x72B9, 0x367B, 0x24B6,
  502. 0x7E8E, 0x2318, 0x6BD0, 0x5519,
  503. 0x1783, 0x18A7, 0x7B6E, 0x7602,
  504. 0x4B7F, 0x3648, 0x2C53, 0x6B99,
  505. 0x0C23, 0x67CF, 0x7E0E, 0x4D8C,
  506. 0x5079, 0x209D, 0x244A, 0x747B,
  507. 0x350B, 0x0E4D, 0x7004, 0x6AC3,
  508. 0x7F3E, 0x21F5, 0x7A15, 0x2379,
  509. 0x1517, 0x1ABA, 0x4E77, 0x15A1,
  510. 0x04FA, 0x2D61, 0x253A, 0x1302,
  511. 0x1F63, 0x5AB3, 0x049A, 0x5AE8,
  512. 0x1CD7, 0x4A00, 0x30C8, 0x3247,
  513. 0x729C, 0x5034, 0x2B0E, 0x57F2,
  514. 0x00E4, 0x575B, 0x6192, 0x38F8,
  515. 0x2F6A, 0x0C14, 0x45FC, 0x41DF,
  516. 0x38DA, 0x7AE1, 0x7322, 0x62DF,
  517. 0x5E39, 0x0E64, 0x6D85, 0x5951,
  518. 0x5937, 0x6281, 0x33A1, 0x6A32,
  519. 0x3A5A, 0x2BAC, 0x743A, 0x5E74,
  520. 0x3B2E, 0x7EC7, 0x4FD2, 0x5D28,
  521. 0x751F, 0x3EF8, 0x39B1, 0x4E49,
  522. 0x746B, 0x6EF6, 0x44BE, 0x6DB7
  523. };
  524. static int mtk_nand_interface_async()
  525. {
  526. if (DDR_INTERFACE == TRUE) {
  527. //nand_device_reset();
  528. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  529. #if 1 // TT_CLK_SETTING
  530. /* disable ecc bypass for TLC NAND, SLC NAND only set for async */
  531. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, NFI_BYPASS);
  532. NFI_CLN_REG32(ECC_BYPASS_REG32, ECC_BYPASS);
  533. /* enable fast ecc clk & set PFM improve --- add at L17 project */
  534. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, NFI_ECC_CLK_EN);
  535. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, (WBUF_EN | AUTOC_HPROT_EN));
  536. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, (HWDCM_SWCON_EN | HWDCM_SWCON_EN_VAL));
  537. // Disable NFI2X & ECC Clock
  538. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  539. // Update NFI INFRA
  540. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  541. // Clear NFI2X & ECC mux sel
  542. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLK_MASK << CLK_NFI2X_SEL_SHIFT) | (CLK_NFI_CLK_MASK << CLK_NFIECC_SEL_SHIFT));
  543. // Set NFI2X & ECC mux sel
  544. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI2X_CLK_364M << CLK_NFI2X_SEL_SHIFT) | (CLK_NFIECC_CLK_218M << CLK_NFIECC_SEL_SHIFT));
  545. // Enable NFI2X & ECC Clock
  546. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  547. // Update NFI INFRA
  548. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  549. #endif
  550. DRV_WriteReg32(NFI_ACCCON_REG32,devinfo.timmingsetting);
  551. DDR_INTERFACE = FALSE;
  552. }
  553. }
  554. static int mtk_nand_interface_config()
  555. {
  556. u32 timeout;
  557. u32 val;
  558. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  559. /* disable ecc bypass for TLC NAND, SLC NAND only set for async */
  560. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, NFI_BYPASS);
  561. NFI_CLN_REG32(ECC_BYPASS_REG32, ECC_BYPASS);
  562. /* enable fast ecc clk & set PFM improve --- add at L17 project */
  563. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, NFI_ECC_CLK_EN);
  564. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, (WBUF_EN | AUTOC_HPROT_EN));
  565. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, (HWDCM_SWCON_EN | HWDCM_SWCON_EN_VAL));
  566. MSG(INIT, "[NAND_DBG] %s 0x100000B0:0x%x\n", __func__, DRV_Reg32(MT_CLKMUX_NFI_INFRA_SEL));
  567. if (devinfo.iowidth == IO_ONFI || devinfo.iowidth == IO_TOGGLEDDR || devinfo.iowidth == IO_TOGGLESDR)
  568. {
  569. nand_set_command(NAND_CMD_RESET);
  570. timeout = TIMEOUT_4;
  571. while (timeout)
  572. timeout--;
  573. nand_reset();
  574. //set feature
  575. mtk_nand_SetFeature((u16) feature_set->sfeatureCmd, \
  576. feature_set->Interface.address, (u8 *)&feature_set->Interface.feature,\
  577. sizeof(feature_set->Interface.feature));
  578. #if 1 // TT_CLK_SETTING
  579. // Disable NFI2X & ECC Clock
  580. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  581. // Update NFI INFRA
  582. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  583. // Clear NFI2X & ECC mux sel
  584. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLK_MASK << CLK_NFI2X_SEL_SHIFT) | (CLK_NFI_CLK_MASK << CLK_NFIECC_SEL_SHIFT));
  585. // Set NFI2X & ECC mux sel
  586. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI2X_CLK_200M << CLK_NFI2X_SEL_SHIFT) | (CLK_NFIECC_CLK_218M << CLK_NFIECC_SEL_SHIFT));
  587. // Enable NFI2X & ECC Clock
  588. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  589. // Update NFI INFRA
  590. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  591. #endif
  592. DRV_WriteReg32(NFI_DLYCTRL_REG32, devinfo.dqs_delay_ctrl);
  593. //DRV_WriteReg32(NFI_DLYCTRL_REG32, 0x00028021);
  594. if (devinfo.iowidth == IO_ONFI)
  595. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 2);
  596. else
  597. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 1);
  598. DRV_WriteReg32(NFI_ACCCON1_REG32, devinfo.s_acccon1);
  599. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.s_acccon);
  600. //read back confirm
  601. mtk_nand_GetFeature(feature_set->gfeatureCmd, \
  602. feature_set->Interface.address, (u8 *)&val,4);
  603. if ((val&0xFF) != (feature_set->Interface.feature & 0xFF)) {
  604. MSG(INIT, "[%s] fail %d\n",__FUNCTION__,val);
  605. nand_set_command(NAND_CMD_RESET);
  606. timeout = TIMEOUT_4;
  607. while (timeout)
  608. timeout--;
  609. nand_reset();
  610. #if 1 // TT_CLK_SETTING
  611. // Disable NFI2X & ECC Clock
  612. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  613. // Update NFI INFRA
  614. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  615. // Clear NFI2X & ECC mux sel
  616. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLK_MASK << CLK_NFI2X_SEL_SHIFT) | (CLK_NFI_CLK_MASK << CLK_NFIECC_SEL_SHIFT));
  617. // Set NFI2X & ECC mux sel
  618. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI2X_CLK_364M << CLK_NFI2X_SEL_SHIFT) | (CLK_NFIECC_CLK_218M << CLK_NFIECC_SEL_SHIFT));
  619. // Enable NFI2X & ECC Clock
  620. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  621. // Update NFI INFRA
  622. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  623. #endif
  624. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.timmingsetting);
  625. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  626. return 0;
  627. }
  628. MSG(INIT, "[%s] success \n",__FUNCTION__);
  629. }
  630. else
  631. {
  632. #if 1 // TT_CLK_SETTING
  633. // Disable NFI2X & ECC Clock
  634. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  635. // Update NFI INFRA
  636. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  637. // Clear NFI2X & ECC mux sel
  638. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLK_MASK << CLK_NFI2X_SEL_SHIFT) | (CLK_NFI_CLK_MASK << CLK_NFIECC_SEL_SHIFT));
  639. // Set NFI2X & ECC mux sel
  640. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI2X_CLK_364M << CLK_NFI2X_SEL_SHIFT) | (CLK_NFIECC_CLK_218M << CLK_NFIECC_SEL_SHIFT));
  641. // Enable NFI2X & ECC Clock
  642. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  643. // Update NFI INFRA
  644. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  645. #endif
  646. MSG(INIT, "[%s] legacy interface \n",__FUNCTION__);
  647. }
  648. MSG(INIT, "[NAND_DBG] %s 0x100000B0:0x%x\n", __func__, DRV_Reg32(MT_CLKMUX_NFI_INFRA_SEL));
  649. return 1;
  650. }
  651. static void mtk_nand_set_async()
  652. {
  653. u32 val;
  654. u32 retry = 10;
  655. u32 id;
  656. mtk_nand_GetFeature(0xEE, 0x80, (u8 *)&val, 4);
  657. MSG(INIT, "[NAND_DBG] %s(%d) - check device feature value %d, reg 0x%x, ddr %s\n", __func__, __LINE__,
  658. val, DRV_Reg16(NFI_NAND_TYPE_CNFG_REG32), (DDR_INTERFACE ? "T" : "F"));
  659. /* disable ecc bypass for TLC NAND, SLC NAND only set for async */
  660. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, NFI_BYPASS);
  661. NFI_CLN_REG32(ECC_BYPASS_REG32, ECC_BYPASS);
  662. /* enable fast ecc clk & set PFM improve --- add at L17 project */
  663. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, NFI_ECC_CLK_EN);
  664. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, (WBUF_EN | AUTOC_HPROT_EN));
  665. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, (HWDCM_SWCON_EN | HWDCM_SWCON_EN_VAL));
  666. while ((val != 0x01) && retry--)
  667. {
  668. if ((val != 0x01) && (val != 0x00) && (DRV_Reg16(NFI_NAND_TYPE_CNFG_REG32) == 0x01))
  669. {
  670. MSG(INIT, "[NAND_DBG] %s Device maybe is DDR, NFI's HW is DDR\n", __func__);
  671. #if 1 // TT_CLK_SETTING
  672. // Disable NFI2X & ECC Clock
  673. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  674. // Update NFI INFRA
  675. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  676. // Clear NFI2X & ECC mux sel
  677. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLK_MASK << CLK_NFI2X_SEL_SHIFT) | (CLK_NFI_CLK_MASK << CLK_NFIECC_SEL_SHIFT));
  678. // Set NFI2X & ECC mux sel
  679. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI2X_CLK_200M << CLK_NFI2X_SEL_SHIFT) | (CLK_NFIECC_CLK_218M << CLK_NFIECC_SEL_SHIFT));
  680. // Enable NFI2X & ECC Clock
  681. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  682. // Update NFI INFRA
  683. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  684. #endif
  685. DRV_WriteReg32(NFI_DLYCTRL_REG32, 0x00028021);
  686. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 1);
  687. DRV_WriteReg32(NFI_ACCCON1_REG32,0x01010400);
  688. DRV_WriteReg32(NFI_ACCCON_REG32,0x33418010);
  689. DDR_INTERFACE = TRUE;
  690. }
  691. else if ((val == 0) && (DRV_Reg16(NFI_NAND_TYPE_CNFG_REG32) == 0x01))
  692. {
  693. MSG(INIT, "[NAND_DBG] %s Device is DDR, NFI's HW is DDR => need to change async\n", __func__);
  694. nand_set_command(NAND_CMD_RESET);
  695. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN))
  696. ;
  697. mtk_nand_GetFeature(0xEE, 0x80, (u8 *)&val, 4);
  698. MSG(INIT, "[NAND_DBG] %s(%d) - value %d, reg 0x%x, ddr %s\n", __func__, __LINE__,
  699. val, DRV_Reg16(NFI_NAND_TYPE_CNFG_REG32), (DDR_INTERFACE ? "T" : "F"));
  700. val = 0x01;
  701. mtk_nand_SetFeature(0xEF, 0x80,(u8 *)&val, 4);
  702. do {
  703. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  704. } while (DRV_Reg16(NFI_NAND_TYPE_CNFG_REG32) == 1);
  705. #if 1 // TT_CLK_SETTING
  706. // Disable NFI2X & ECC Clock
  707. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  708. // Update NFI INFRA
  709. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  710. // Clear NFI2X & ECC mux sel
  711. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLK_MASK << CLK_NFI2X_SEL_SHIFT) | (CLK_NFI_CLK_MASK << CLK_NFIECC_SEL_SHIFT));
  712. // Set NFI2X & ECC mux sel
  713. NFI_SET_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI2X_CLK_364M << CLK_NFI2X_SEL_SHIFT) | (CLK_NFIECC_CLK_218M << CLK_NFIECC_SEL_SHIFT));
  714. // Enable NFI2X & ECC Clock
  715. NFI_CLN_REG32(MT_CLKMUX_NFI_INFRA_SEL, (CLK_NFI_CLOCK_OFF << PDN_NFI2X_SHIFT) | (CLK_NFI_CLOCK_OFF << PDN_NFIECC_SHIFT));
  716. // Update NFI INFRA
  717. DRV_WriteReg32(MT_CLKMUX_NFI_INFRA_UPD, (1 << HF_FNFI2X_CK_UPDATE_SHIFT) | (1 << HF_FNFIECC_CK_UPDATE_SHIFT));
  718. #endif
  719. DRV_WriteReg32(NFI_ACCCON_REG32, 0x10818022);
  720. DDR_INTERFACE = FALSE;
  721. }
  722. mtk_nand_GetFeature(0xEE, 0x80, (u8 *)&val, 4);
  723. MSG(INIT, "[NAND_DBG] %s(%d) - value %d, reg 0x%x, ddr %s\n", __func__, __LINE__,
  724. val, DRV_Reg16(NFI_NAND_TYPE_CNFG_REG32), (DDR_INTERFACE ? "T" : "F"));
  725. }
  726. }
  727. static int mtk_nand_randomizer_config(struct gRandConfig *conf, kal_uint16 seed)
  728. {
  729. #if 1
  730. if (gVendor != VEND_NONE) {
  731. kal_uint16 nfi_cnfg = 0;
  732. kal_uint32 nfi_ran_cnfg = 0;
  733. kal_uint8 i;
  734. /* set up NFI_CNFG */
  735. nfi_cnfg = DRV_Reg(NFI_CNFG_REG16);
  736. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  737. if (conf->type == RAND_TYPE_SAMSUNG) {
  738. nfi_ran_cnfg =0;
  739. nfi_ran_cnfg |= seed << EN_SEED_SHIFT;
  740. nfi_ran_cnfg |= seed << DE_SEED_SHIFT;
  741. nfi_cnfg |= CNFG_RAN_SEC;
  742. nfi_cnfg |= CNFG_RAN_SEL;
  743. use_randomizer = TRUE;
  744. //nfi_ran_cnfg |= 0x00010001;
  745. } else if (conf->type == RAND_TYPE_TOSHIBA) {
  746. use_randomizer = TRUE;
  747. for (i = 0 ; i < 16 ; i++) {
  748. DRV_WriteReg32(NFI_RANDOM_TSB_SEED0+i, conf->seed[i]);
  749. }
  750. nfi_cnfg |= CNFG_RAN_SEC;
  751. nfi_cnfg &= ~CNFG_RAN_SEL;
  752. //nfi_ran_cnfg |= 0x00010001;
  753. } else {
  754. nfi_ran_cnfg &= ~0x00010001;
  755. use_randomizer = FALSE;
  756. return 0;
  757. }
  758. DRV_WriteReg(NFI_CNFG_REG16, nfi_cnfg);
  759. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, nfi_ran_cnfg);
  760. }
  761. return 0;
  762. #endif
  763. }
  764. static bool mtk_nand_israndomizeron()
  765. {
  766. #if 1
  767. if (gVendor == VEND_SANDISK || gVendor == VEND_TOSHIBA|| devinfo.vendor == VEND_HYNIX) {
  768. kal_uint32 nfi_ran_cnfg = 0;
  769. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  770. if (nfi_ran_cnfg&0x00010001)
  771. return TRUE;
  772. }
  773. #endif
  774. return FALSE;
  775. }
  776. static void mtk_nand_interface_switch()
  777. {
  778. if (devinfo.iowidth == IO_ONFI || devinfo.iowidth ==IO_TOGGLEDDR || devinfo.iowidth ==IO_TOGGLESDR) {
  779. if (DDR_INTERFACE == FALSE) {
  780. if (mtk_nand_interface_config()) {
  781. MSG(INIT,"[NFI] interface switch sync!!!!\n");
  782. DDR_INTERFACE = TRUE;
  783. } else {
  784. MSG(INIT,"[NFI] interface switch fail!!!!\n");
  785. DDR_INTERFACE = FALSE;
  786. }
  787. }
  788. }
  789. }
  790. static void mtk_nand_turn_on_randomizer(kal_uint32 page)
  791. {
  792. #if 1
  793. //struct gRandConfig *conf = &devinfo.feature_set.randConfig;
  794. if (gVendor != VEND_NONE) {
  795. u32 page_per_blk = BLOCK_SIZE/(g_nand_chip.page_size);
  796. kal_uint32 nfi_ran_cnfg = 0;
  797. kal_uint16 seed;
  798. if (page_per_blk < 128) {
  799. seed = randomizer_seed[page%page_per_blk];
  800. } else {
  801. seed = randomizer_seed[page%128];
  802. }
  803. mtk_nand_randomizer_config(&devinfo.feature_set.randConfig,seed);
  804. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  805. nfi_ran_cnfg |= 0x00010001;
  806. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, nfi_ran_cnfg);
  807. }
  808. #endif
  809. }
  810. static void mtk_nand_turn_off_randomizer()
  811. {
  812. #if 1
  813. if (gVendor != VEND_NONE) {
  814. kal_uint32 nfi_ran_cnfg = 0;
  815. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  816. nfi_ran_cnfg &= ~0x00010001;
  817. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, nfi_ran_cnfg);
  818. }
  819. #endif
  820. }
  821. static void ECC_Config(u32 ecc_level)
  822. {
  823. u32 u4ENCODESize;
  824. u32 u4DECODESize;
  825. u32 ecc_bit_cfg = 0;
  826. u32 sector_size = NAND_SECTOR_SIZE;
  827. if (devinfo.sectorsize == 1024)
  828. sector_size = 1024;
  829. u4DECODESize = ((sector_size + g_nand_chip.nand_fdm_size) << 3) + ecc_level * ECC_PARITY_BIT;
  830. switch (ecc_level) {
  831. case 4:
  832. ecc_bit_cfg = ECC_CNFG_ECC4;
  833. break;
  834. case 8:
  835. ecc_bit_cfg = ECC_CNFG_ECC8;
  836. break;
  837. case 10:
  838. ecc_bit_cfg = ECC_CNFG_ECC10;
  839. break;
  840. case 12:
  841. ecc_bit_cfg = ECC_CNFG_ECC12;
  842. break;
  843. case 14:
  844. ecc_bit_cfg = ECC_CNFG_ECC14;
  845. break;
  846. case 16:
  847. ecc_bit_cfg = ECC_CNFG_ECC16;
  848. break;
  849. case 18:
  850. ecc_bit_cfg = ECC_CNFG_ECC18;
  851. break;
  852. case 20:
  853. ecc_bit_cfg = ECC_CNFG_ECC20;
  854. break;
  855. case 22:
  856. ecc_bit_cfg = ECC_CNFG_ECC22;
  857. break;
  858. case 24:
  859. ecc_bit_cfg = ECC_CNFG_ECC24;
  860. break;
  861. case 28:
  862. ecc_bit_cfg = ECC_CNFG_ECC28;
  863. break;
  864. case 32:
  865. ecc_bit_cfg = ECC_CNFG_ECC32;
  866. break;
  867. case 36:
  868. ecc_bit_cfg = ECC_CNFG_ECC36;
  869. break;
  870. case 40:
  871. ecc_bit_cfg = ECC_CNFG_ECC40;
  872. break;
  873. case 44:
  874. ecc_bit_cfg = ECC_CNFG_ECC44;
  875. break;
  876. case 48:
  877. ecc_bit_cfg = ECC_CNFG_ECC48;
  878. break;
  879. case 52:
  880. ecc_bit_cfg = ECC_CNFG_ECC52;
  881. break;
  882. case 56:
  883. ecc_bit_cfg = ECC_CNFG_ECC56;
  884. break;
  885. case 60:
  886. ecc_bit_cfg = ECC_CNFG_ECC60;
  887. break;
  888. #if defined(MTK_TLC_NAND_SUPPORT)
  889. case 68:
  890. ecc_bit_cfg = ECC_CNFG_ECC68;
  891. u4DECODESize -= 7;
  892. break;
  893. case 72:
  894. ecc_bit_cfg = ECC_CNFG_ECC72;
  895. u4DECODESize -= 7;
  896. break;
  897. case 80:
  898. ecc_bit_cfg = ECC_CNFG_ECC80;
  899. u4DECODESize -= 7;
  900. break;
  901. #endif
  902. default:
  903. break;
  904. }
  905. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  906. do {
  907. ;
  908. } while (!DRV_Reg16(ECC_DECIDLE_REG16));
  909. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  910. do {
  911. ;
  912. } while (!DRV_Reg32(ECC_ENCIDLE_REG32));
  913. /* setup FDM register base */
  914. // DRV_WriteReg32(ECC_FDMADDR_REG32, NFI_FDM0L_REG32);
  915. u4ENCODESize = (sector_size + g_nand_chip.nand_fdm_size) << 3;
  916. /* configure ECC decoder && encoder */
  917. DRV_WriteReg32(ECC_DECCNFG_REG32, ecc_bit_cfg | DEC_CNFG_NFI | DEC_CNFG_EMPTY_EN | (u4DECODESize << DEC_CNFG_CODE_SHIFT));
  918. DRV_WriteReg32(ECC_ENCCNFG_REG32, ecc_bit_cfg | ENC_CNFG_NFI | (u4ENCODESize << ENC_CNFG_MSG_SHIFT));
  919. #ifndef MANUAL_CORRECT
  920. NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_CORRECT);
  921. #else
  922. NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_EL);
  923. #endif
  924. }
  925. static void ECC_Decode_Start(void)
  926. {
  927. /* wait for device returning idle */
  928. while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE)) ;
  929. DRV_WriteReg16(ECC_DECCON_REG16, DEC_EN);
  930. }
  931. static void ECC_Decode_End(void)
  932. {
  933. /* wait for device returning idle */
  934. while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE)) ;
  935. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  936. }
  937. //-------------------------------------------------------------------------------
  938. static void ECC_Encode_Start(void)
  939. {
  940. /* wait for device returning idle */
  941. while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE)) ;
  942. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_EN);
  943. }
  944. //-------------------------------------------------------------------------------
  945. static void ECC_Encode_End(void)
  946. {
  947. /* wait for device returning idle */
  948. while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE)) ;
  949. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  950. }
  951. //-------------------------------------------------------------------------------
  952. static bool nand_check_bch_error(u8 * pDataBuf, u32 u4SecIndex, u32 u4PageAddr)
  953. {
  954. bool bRet = true;
  955. u16 u2SectorDoneMask = 1 << u4SecIndex;
  956. u32 u4ErrorNumDebug0, u4ErrorNumDebug1,i, u4ErrNum;
  957. u32 timeout = 0xFFFF;
  958. #ifdef MANUAL_CORRECT
  959. u32 au4ErrBitLoc[6];
  960. u32 u4ErrByteLoc, u4BitOffset;
  961. u32 u4ErrBitLoc1th, u4ErrBitLoc2nd;
  962. #endif
  963. while (0 == (u2SectorDoneMask & DRV_Reg16(ECC_DECDONE_REG16))) {
  964. timeout--;
  965. if (0 == timeout) {
  966. return false;
  967. }
  968. }
  969. #ifndef MANUAL_CORRECT
  970. if (0 == (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  971. u4ErrorNumDebug0 = DRV_Reg32(ECC_DECENUM0_REG32);
  972. u4ErrorNumDebug1 = DRV_Reg32(ECC_DECENUM1_REG32);
  973. if (0 != (u4ErrorNumDebug0 & 0xFFFFFFFF) || 0 != (u4ErrorNumDebug1 & 0xFFFFFFFF)) {
  974. for (i = 0; i <= u4SecIndex; ++i) {
  975. #if 1
  976. u4ErrNum = (DRV_Reg32((ECC_DECENUM0_REG32+(i/4)))>>((i%4)*8))& ERR_NUM0;
  977. #else
  978. if (i < 4) {
  979. u4ErrNum = DRV_Reg32(ECC_DECENUM0_REG32) >> (i * 8);
  980. } else {
  981. u4ErrNum = DRV_Reg32(ECC_DECENUM1_REG32) >> ((i - 4) * 8);
  982. }
  983. u4ErrNum &= ERR_NUM0;
  984. #endif
  985. if (ERR_NUM0 == u4ErrNum) {
  986. MSG(ERR, "In LittleKernel UnCorrectable at PageAddr=%d, Sector=%d\n", u4PageAddr, i);
  987. bRet = false;
  988. } else {
  989. if (u4ErrNum) {
  990. //MSG(ERR, " In LittleKernel Correct %d at PageAddr=%d, Sector=%d\n", u4ErrNum, u4PageAddr, i);
  991. }
  992. }
  993. }
  994. if (bRet == false) {
  995. if (0 != (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  996. MSG(ERR, "Empty data at 0x%x\n", u4PageAddr);
  997. bRet=true;
  998. }
  999. }
  1000. }
  1001. }
  1002. #else
  1003. memset(au4ErrBitLoc, 0x0, sizeof(au4ErrBitLoc));
  1004. u4ErrorNumDebug0 = DRV_Reg32(ECC_DECENUM_REG32);
  1005. u4ErrNum = (DRV_Reg32((ECC_DECENUM_REG32+(u4SecIndex/4)))>>((u4SecIndex%4)*8))& ERR_NUM0;
  1006. if (u4ErrNum) {
  1007. if (ERR_NUM0 == u4ErrNum) {
  1008. MSG(ERR, "UnCorrectable at PageAddr=%d\n", u4PageAddr);
  1009. bRet = false;
  1010. } else {
  1011. for (i = 0; i < ((u4ErrNum + 1) >> 1); ++i) {
  1012. au4ErrBitLoc[i] = DRV_Reg32(ECC_DECEL0_REG32 + i);
  1013. u4ErrBitLoc1th = au4ErrBitLoc[i] & 0x3FFF;
  1014. if (u4ErrBitLoc1th < 0x1000) {
  1015. u4ErrByteLoc = u4ErrBitLoc1th / 8;
  1016. u4BitOffset = u4ErrBitLoc1th % 8;
  1017. pDataBuf[u4ErrByteLoc] = pDataBuf[u4ErrByteLoc] ^ (1 << u4BitOffset);
  1018. } else {
  1019. MSG(ERR, "UnCorrectable ErrLoc=%d\n", au4ErrBitLoc[i]);
  1020. }
  1021. u4ErrBitLoc2nd = (au4ErrBitLoc[i] >> 16) & 0x3FFF;
  1022. if (0 != u4ErrBitLoc2nd) {
  1023. if (u4ErrBitLoc2nd < 0x1000) {
  1024. u4ErrByteLoc = u4ErrBitLoc2nd / 8;
  1025. u4BitOffset = u4ErrBitLoc2nd % 8;
  1026. pDataBuf[u4ErrByteLoc] = pDataBuf[u4ErrByteLoc] ^ (1 << u4BitOffset);
  1027. } else {
  1028. MSG(ERR, "UnCorrectable High ErrLoc=%d\n", au4ErrBitLoc[i]);
  1029. }
  1030. }
  1031. }
  1032. bRet = true;
  1033. }
  1034. if (0 == (DRV_Reg16(ECC_DECFER_REG16) & (1 << u4SecIndex))) {
  1035. bRet = false;
  1036. }
  1037. }
  1038. #endif
  1039. return bRet;
  1040. }
  1041. #if 1
  1042. static bool nand_RFIFOValidSize(u16 u2Size)
  1043. {
  1044. u32 timeout = 0xFFFF;
  1045. while (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) < u2Size) {
  1046. timeout--;
  1047. if (0 == timeout) {
  1048. return false;
  1049. }
  1050. }
  1051. if (u2Size == 0) {
  1052. while (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16))) {
  1053. timeout--;
  1054. if (0 == timeout) {
  1055. dprintf(INFO,"nand_RFIFOValidSize failed: 0x%x\n", u2Size);
  1056. return false;
  1057. }
  1058. }
  1059. }
  1060. return true;
  1061. }
  1062. //-------------------------------------------------------------------------------
  1063. static bool nand_WFIFOValidSize(u16 u2Size)
  1064. {
  1065. u32 timeout = 0xFFFF;
  1066. while (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) > u2Size) {
  1067. timeout--;
  1068. if (0 == timeout) {
  1069. return false;
  1070. }
  1071. }
  1072. if (u2Size == 0) {
  1073. while (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16))) {
  1074. timeout--;
  1075. if (0 == timeout) {
  1076. dprintf(INFO,"nand_RFIFOValidSize failed: 0x%x\n", u2Size);
  1077. return false;
  1078. }
  1079. }
  1080. }
  1081. return true;
  1082. }
  1083. #endif
  1084. static bool nand_status_ready(u32 u4Status)
  1085. {
  1086. u32 timeout = 0xFFFF;
  1087. while ((DRV_Reg32(NFI_STA_REG32) & u4Status) != 0) {
  1088. timeout--;
  1089. if (0 == timeout) {
  1090. return false;
  1091. }
  1092. }
  1093. return true;
  1094. }
  1095. static bool nand_reset(void)
  1096. {
  1097. /* issue reset operation */
  1098. DRV_WriteReg32(NFI_CON_REG32, CON_FIFO_FLUSH | CON_NFI_RST);
  1099. return nand_status_ready(STA_NFI_FSM_MASK | STA_NAND_BUSY) && nand_RFIFOValidSize(0) && nand_WFIFOValidSize(0);
  1100. }
  1101. //-------------------------------------------------------------------------------
  1102. static void nand_set_mode(u16 u2OpMode)
  1103. {
  1104. u16 u2Mode = DRV_Reg16(NFI_CNFG_REG16);
  1105. u2Mode &= ~CNFG_OP_MODE_MASK;
  1106. u2Mode |= u2OpMode;
  1107. DRV_WriteReg16(NFI_CNFG_REG16, u2Mode);
  1108. }
  1109. //-------------------------------------------------------------------------------
  1110. static void nand_set_autoformat(bool bEnable)
  1111. {
  1112. if (bEnable) {
  1113. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  1114. } else {
  1115. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  1116. }
  1117. }
  1118. //-------------------------------------------------------------------------------
  1119. static void nand_configure_fdm(u16 u2FDMSize)
  1120. {
  1121. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_FDM_MASK | PAGEFMT_FDM_ECC_MASK);
  1122. NFI_SET_REG32(NFI_PAGEFMT_REG32, u2FDMSize << PAGEFMT_FDM_SHIFT);
  1123. NFI_SET_REG32(NFI_PAGEFMT_REG32, u2FDMSize << PAGEFMT_FDM_ECC_SHIFT);
  1124. }
  1125. //-------------------------------------------------------------------------------
  1126. static bool nand_set_command(u16 command)
  1127. {
  1128. /* Write command to device */
  1129. DRV_WriteReg16(NFI_CMD_REG16, command);
  1130. return nand_status_ready(STA_CMD_STATE);
  1131. }
  1132. //-------------------------------------------------------------------------------
  1133. static bool nand_set_address(u32 u4ColAddr, u32 u4RowAddr, u16 u2ColNOB, u16 u2RowNOB)
  1134. {
  1135. /* fill cycle addr */
  1136. DRV_WriteReg32(NFI_COLADDR_REG32, u4ColAddr);
  1137. DRV_WriteReg32(NFI_ROWADDR_REG32, u4RowAddr);
  1138. DRV_WriteReg16(NFI_ADDRNOB_REG16, u2ColNOB | (u2RowNOB << ADDR_ROW_NOB_SHIFT));
  1139. return nand_status_ready(STA_ADDR_STATE);
  1140. }
  1141. //-------------------------------------------------------------------------------
  1142. static bool nand_device_reset(void)
  1143. {
  1144. u32 timeout = 0xFFFF;
  1145. nand_reset();
  1146. DRV_WriteReg(NFI_CNFG_REG16, CNFG_OP_RESET);
  1147. nand_set_command(NAND_CMD_RESET);
  1148. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  1149. mtk_nand_interface_async();
  1150. if (!timeout)
  1151. return FALSE;
  1152. else
  1153. return TRUE;
  1154. }
  1155. //-------------------------------------------------------------------------------
  1156. static bool nand_check_RW_count(struct nand_chip *nand, u16 u2WriteSize)
  1157. {
  1158. u32 timeout = 0xFFFF;
  1159. u16 u2SecNum = u2WriteSize >> nand->sector_shift;
  1160. while (ADDRCNTR_CNTR(DRV_Reg32(NFI_ADDRCNTR_REG32)) < u2SecNum) {
  1161. timeout--;
  1162. if (0 == timeout) {
  1163. return false;
  1164. }
  1165. }
  1166. return true;
  1167. }
  1168. //-------------------------------------------------------------------------------
  1169. static bool nand_ready_for_read(struct nand_chip *nand, u32 u4RowAddr, u32 u4ColAddr, bool bFull, u8 * buf)
  1170. {
  1171. /* Reset NFI HW internal state machine and flush NFI in/out FIFO */
  1172. bool bRet = false;
  1173. u16 sec_num = 1 << (nand->page_shift - nand->sector_shift);
  1174. u32 col_addr = u4ColAddr;
  1175. if (nand->options & NAND_BUSWIDTH_16)
  1176. col_addr >>= 1;
  1177. u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  1178. if (!nand_reset()) {
  1179. goto cleanup;
  1180. }
  1181. if (DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32)&0x3) {
  1182. NFI_SET_REG32(NFI_MASTERRST_REG32, PAD_MACRO_RST);//reset
  1183. NFI_CLN_REG32(NFI_MASTERRST_REG32, PAD_MACRO_RST);//dereset
  1184. }
  1185. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1186. nand_set_mode(CNFG_OP_READ);
  1187. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  1188. #if !defined(MTK_TLC_NAND_SUPPORT)
  1189. DRV_WriteReg32(NFI_CON_REG32, sec_num << CON_NFI_SEC_SHIFT);
  1190. #endif
  1191. if (bFull) {
  1192. #if USE_AHB_MODE
  1193. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1194. #else
  1195. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1196. #endif
  1197. #if !defined(MTK_TLC_NAND_SUPPORT)
  1198. DRV_WriteReg32(NFI_STRADDR_REG32, buf);
  1199. #endif
  1200. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1201. } else {
  1202. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1203. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1204. }
  1205. nand_set_autoformat(bFull);
  1206. #if !defined(MTK_TLC_NAND_SUPPORT)
  1207. if (bFull)
  1208. ECC_Decode_Start();
  1209. #endif
  1210. if (!nand_set_command(NAND_CMD_READ_0)) {
  1211. goto cleanup;
  1212. }
  1213. if (!nand_set_address(col_addr, u4RowAddr, colnob, rownob)) {
  1214. goto cleanup;
  1215. }
  1216. if (!nand_set_command(NAND_CMD_READ_START)) {
  1217. goto cleanup;
  1218. }
  1219. if (!nand_status_ready(STA_NAND_BUSY)) {
  1220. goto cleanup;
  1221. }
  1222. bRet = true;
  1223. cleanup:
  1224. return bRet;
  1225. }
  1226. //-----------------------------------------------------------------------------
  1227. static bool nand_ready_for_write(struct nand_chip *nand, u32 u4RowAddr, u8 * buf)
  1228. {
  1229. bool bRet = false;
  1230. u16 sec_num = 1 << (nand->page_shift - nand->sector_shift);
  1231. u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  1232. u32 temp_sec_num;
  1233. temp_sec_num = sec_num;
  1234. #if defined(MTK_TLC_NAND_SUPPORT)
  1235. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  1236. && devinfo.tlcControl.normaltlc
  1237. && devinfo.tlcControl.pPlaneEn) {
  1238. temp_sec_num = sec_num / 2;
  1239. }
  1240. #endif
  1241. if (!nand_reset()) {
  1242. return false;
  1243. }
  1244. nand_set_mode(CNFG_OP_PRGM);
  1245. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  1246. DRV_WriteReg32(NFI_CON_REG32, temp_sec_num << CON_NFI_SEC_SHIFT);
  1247. #if USE_AHB_MODE
  1248. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1249. DRV_WriteReg32(NFI_STRADDR_REG32, buf);
  1250. #else
  1251. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1252. #endif
  1253. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1254. nand_set_autoformat(true);
  1255. ECC_Encode_Start();
  1256. if (!nand_set_command(NAND_CMD_SEQIN)) {
  1257. goto cleanup;
  1258. }
  1259. if (!nand_set_address(0, u4RowAddr, colnob, rownob)) {
  1260. goto cleanup;
  1261. }
  1262. if (!nand_status_ready(STA_NAND_BUSY)) {
  1263. goto cleanup;
  1264. }
  1265. bRet = true;
  1266. cleanup:
  1267. return bRet;
  1268. }
  1269. //-----------------------------------------------------------------------------
  1270. static bool nand_dma_read_data(u8 * pDataBuf, u32 u4Size)
  1271. {
  1272. u32 timeout = 0xFFFF;
  1273. arch_clean_invalidate_cache_range((addr_t)pDataBuf,(size_t)u4Size);
  1274. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1275. DRV_Reg16(NFI_INTR_REG16);
  1276. DRV_WriteReg32(NFI_INTR_EN_REG32, INTR_AHB_DONE_EN);
  1277. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1278. #if defined(MTK_TLC_NAND_SUPPORT)
  1279. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  1280. &&(devinfo.tlcControl.needchangecolumn))
  1281. DRV_WriteReg(NFI_TLC_RD_WHR2_REG16, (TLC_RD_WHR2_EN | 0x055)); //trigger data sample
  1282. #endif
  1283. if (en_interrupt) {
  1284. if (event_wait_timeout(&nand_int_event,100)) {
  1285. dprintf(INFO,"[nand_dma_read_data]wait for AHB done timeout\n");
  1286. dump_nfi();
  1287. return false;
  1288. }
  1289. timeout = 0xFFFF;
  1290. while ((u4Size >> g_nand_chip.sector_shift) > ((DRV_Reg32(NFI_BYTELEN_REG32) & 0x1f000) >> 12)) {
  1291. timeout--;
  1292. if (0 == timeout) {
  1293. return false; //4
  1294. }
  1295. }
  1296. } else {
  1297. while (!(DRV_Reg16(NFI_INTR_REG16) & INTR_AHB_DONE)) {
  1298. timeout--;
  1299. if (0 == timeout) {
  1300. return false;
  1301. }
  1302. }
  1303. timeout = 0xFFFF;
  1304. while ((u4Size >> g_nand_chip.sector_shift) > ((DRV_Reg32(NFI_BYTELEN_REG32) & 0x1f000) >> 12)) {
  1305. timeout--;
  1306. if (0 == timeout) {
  1307. return false; //4
  1308. }
  1309. }
  1310. }
  1311. return true;
  1312. }
  1313. static bool nand_mcu_read_data(u8 * pDataBuf, u32 length)
  1314. {
  1315. u32 timeout = 0xFFFF;
  1316. u32 i;
  1317. u32 *pBuf32;
  1318. if (length % 4) {
  1319. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1320. } else {
  1321. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1322. }
  1323. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1324. pBuf32 = (u32 *) pDataBuf;
  1325. if (length % 4) {
  1326. for (i = 0; (i < length) && (timeout > 0);) {
  1327. WAIT_NFI_PIO_READY(timeout);
  1328. *pDataBuf++ = DRV_Reg8(NFI_DATAR_REG32);
  1329. i++;
  1330. }
  1331. } else {
  1332. WAIT_NFI_PIO_READY(timeout);
  1333. for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  1334. WAIT_NFI_PIO_READY(timeout);
  1335. *pBuf32++ = DRV_Reg32(NFI_DATAR_REG32);
  1336. i++;
  1337. }
  1338. }
  1339. return true;
  1340. }
  1341. static bool nand_read_page_data(u8 * buf, u32 length)
  1342. {
  1343. #if USE_AHB_MODE
  1344. return nand_dma_read_data(buf, length);
  1345. #else
  1346. return nand_mcu_read_data(buf, length);
  1347. #endif
  1348. }
  1349. static bool nand_dma_write_data(u8 * buf, u32 length)
  1350. {
  1351. u32 timeout = 0xFFFF;
  1352. arch_clean_invalidate_cache_range((addr_t)buf,(size_t)length);
  1353. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1354. DRV_Reg16(NFI_INTR_REG16);
  1355. DRV_WriteReg32(NFI_INTR_EN_REG32, INTR_AHB_DONE_EN);
  1356. if ((unsigned int)buf % 16) {
  1357. //dprintf(INFO"Un-16-aligned address\n");
  1358. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  1359. } else {
  1360. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  1361. }
  1362. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  1363. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  1364. NFI_SET_REG16(NFI_DEBUG_CON1_REG16, REG_CE_HOLD);
  1365. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1366. if (en_interrupt) {
  1367. if (event_wait_timeout(&nand_int_event,100)) {
  1368. dprintf(INFO,"[nand_dma_write_data]wait for AHB done timeout\n");
  1369. dump_nfi();
  1370. return false;
  1371. }
  1372. } else {
  1373. while (!(DRV_Reg16(NFI_INTR_REG16) & INTR_AHB_DONE)) {
  1374. timeout--;
  1375. if (0 == timeout) {
  1376. dprintf(INFO,"wait write AHB done timeout\n");
  1377. dump_nfi();
  1378. return FALSE;
  1379. }
  1380. }
  1381. }
  1382. return true;
  1383. }
  1384. static bool nand_mcu_write_data(const u8 * buf, u32 length)
  1385. {
  1386. u32 timeout = 0xFFFF;
  1387. u32 i;
  1388. u32 *pBuf32 = (u32 *) buf;
  1389. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1390. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1391. if ((u32) buf % 4 || length % 4)
  1392. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1393. else
  1394. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1395. if ((u32) buf % 4 || length % 4) {
  1396. for (i = 0; (i < (length)) && (timeout > 0);) {
  1397. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  1398. DRV_WriteReg32(NFI_DATAW_REG32, *buf++);
  1399. i++;
  1400. } else {
  1401. timeout--;
  1402. }
  1403. if (0 == timeout) {
  1404. dprintf(INFO,"[%s] nand mcu write timeout\n", __FUNCTION__);
  1405. dump_nfi();
  1406. return false;
  1407. }
  1408. }
  1409. } else {
  1410. for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  1411. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  1412. DRV_WriteReg32(NFI_DATAW_REG32, *pBuf32++);
  1413. i++;
  1414. } else {
  1415. timeout--;
  1416. }
  1417. if (0 == timeout) {
  1418. dprintf(INFO,"[%s] nand mcu write timeout\n", __FUNCTION__);
  1419. dump_nfi();
  1420. return false;
  1421. }
  1422. }
  1423. }
  1424. return true;
  1425. }
  1426. //-----------------------------------------------------------------------------
  1427. static bool nand_write_page_data(u8 * buf, u32 length)
  1428. {
  1429. #if USE_AHB_MODE
  1430. return nand_dma_write_data(buf, length);
  1431. #else
  1432. return nand_mcu_write_data(buf, length);
  1433. #endif
  1434. }
  1435. static void nand_read_fdm_data(u8 * pDataBuf, u32 u4SecNum)
  1436. {
  1437. #ifndef MTK_TLC_NAND_SUPPORT
  1438. u32 i;
  1439. u32 *pBuf32 = (u32 *) pDataBuf;
  1440. for (i = 0; i < u4SecNum; ++i) {
  1441. *pBuf32++ = DRV_Reg32(NFI_FDM0L_REG32 + (i << 1));
  1442. *pBuf32++ = DRV_Reg32(NFI_FDM0M_REG32 + (i << 1));
  1443. }
  1444. #else
  1445. u32 fdm_temp[2];
  1446. u32 i, j;
  1447. u8 *byte_ptr;
  1448. byte_ptr = (u8*)fdm_temp;
  1449. if (pDataBuf) {
  1450. for (i = 0; i < u4SecNum; ++i) {
  1451. fdm_temp[0] = DRV_Reg32(NFI_FDM0L_REG32 + (i << 1));
  1452. fdm_temp[1] = DRV_Reg32(NFI_FDM0M_REG32 + (i << 1));
  1453. for (j = 0; j < g_nand_chip.nand_fdm_size; j++) {
  1454. *(pDataBuf + (i * g_nand_chip.nand_fdm_size) + j) = *(byte_ptr + j);
  1455. }
  1456. }
  1457. }
  1458. #endif
  1459. }
  1460. static void nand_write_fdm_data(u8 * pDataBuf, u32 u4SecNum)
  1461. {
  1462. u32 i, j;
  1463. u8 *pBuf;
  1464. u8* byte_ptr;
  1465. u32 fdm_data[2];
  1466. pBuf = (u8*)fdm_data;
  1467. byte_ptr = (u8*)pDataBuf;
  1468. for (i = 0; i < u4SecNum; ++i) {
  1469. fdm_data[0] = 0xFFFFFFFF;
  1470. fdm_data[1] = 0xFFFFFFFF;
  1471. for (j = 0; j < g_nand_chip.nand_fdm_size; j++)
  1472. {
  1473. *(pBuf + j) = *(byte_ptr + j + (i * g_nand_chip.nand_fdm_size));
  1474. }
  1475. DRV_WriteReg32(NFI_FDM0L_REG32 + (i << 1), fdm_data[0]);
  1476. DRV_WriteReg32(NFI_FDM0M_REG32 + (i << 1), fdm_data[1]);
  1477. }
  1478. }
  1479. static void nand_stop_read(void)
  1480. {
  1481. NFI_CLN_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1482. ECC_Decode_End();
  1483. }
  1484. static void nand_stop_write(void)
  1485. {
  1486. NFI_CLN_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1487. ECC_Encode_End();
  1488. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  1489. NFI_CLN_REG16(NFI_DEBUG_CON1_REG16, REG_CE_HOLD);
  1490. }
  1491. static bool nand_check_dececc_done(u32 u4SecNum)
  1492. {
  1493. u32 timeout, dec_mask;
  1494. timeout = 0xffff;
  1495. dec_mask = (1 << u4SecNum) - 1;
  1496. while ((dec_mask != DRV_Reg(ECC_DECDONE_REG16)) && timeout > 0)
  1497. timeout--;
  1498. if (timeout == 0) {
  1499. MSG(ERR, "ECC_DECDONE: timeout\n");
  1500. dump_nfi();
  1501. return false;
  1502. }
  1503. return true;
  1504. }
  1505. //---------------------------------------------------------------------------
  1506. static bool mtk_nand_read_status(void)
  1507. {
  1508. int status = 0;//, i;
  1509. unsigned int timeout;
  1510. nand_reset();
  1511. /* Disable HW ECC */
  1512. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1513. /* Disable 16-bit I/O */
  1514. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  1515. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_OP_SRD | CNFG_READ_EN | CNFG_BYTE_RW);
  1516. DRV_WriteReg32(NFI_CON_REG32, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  1517. DRV_WriteReg32(NFI_CON_REG32, 0x3);
  1518. nand_set_mode(CNFG_OP_SRD);
  1519. DRV_WriteReg16(NFI_CNFG_REG16, 0x2042);
  1520. nand_set_command(NAND_CMD_STATUS);
  1521. DRV_WriteReg32(NFI_CON_REG32, 0x90);
  1522. timeout = TIMEOUT_4;
  1523. WAIT_NFI_PIO_READY(timeout);
  1524. if (timeout) {
  1525. status = (DRV_Reg16(NFI_DATAR_REG32));
  1526. }
  1527. //~ clear NOB
  1528. DRV_WriteReg32(NFI_CON_REG32, 0);
  1529. if (g_nand_chip.bus16 == IO_WIDTH_16) {
  1530. NFI_SET_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  1531. NFI_CLN_REG32(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1532. }
  1533. // check READY/BUSY status first
  1534. if (!(STATUS_READY & status)) {
  1535. MSG(ERR, "status is not ready\n");
  1536. }
  1537. // flash is ready now, check status code
  1538. #if defined(MTK_TLC_NAND_SUPPORT)
  1539. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  1540. && (devinfo.tlcControl.slcopmodeEn)) { //hynix tlc need doule check
  1541. if (SLC_MODE_OP_FALI & status) {
  1542. if (!(STATUS_WR_ALLOW & status)) {
  1543. MSG(INIT, "status locked\n");
  1544. return FALSE;
  1545. } else {
  1546. MSG(INIT, "status unknown\n");
  1547. return FALSE;
  1548. }
  1549. } else {
  1550. return TRUE;
  1551. }
  1552. } else
  1553. #endif
  1554. {
  1555. if (STATUS_FAIL & status) {
  1556. if (!(STATUS_WR_ALLOW & status)) {
  1557. MSG(INIT, "status locked\n");
  1558. return FALSE;
  1559. } else {
  1560. MSG(INIT, "status unknown\n");
  1561. return FALSE;
  1562. }
  1563. } else {
  1564. return TRUE;
  1565. }
  1566. }
  1567. }
  1568. bool mtk_nand_SetFeature(u16 cmd, u32 addr, u8 *value, u8 bytes)
  1569. {
  1570. kal_uint16 reg_val = 0;
  1571. kal_uint8 write_count = 0;
  1572. kal_uint32 timeout=TIMEOUT_3;//0xffff;
  1573. kal_uint32 reg;
  1574. nand_reset();
  1575. reg = DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32);
  1576. if (reg != 4)
  1577. bytes = bytes * 2;
  1578. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  1579. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1580. nand_set_command(cmd);
  1581. nand_set_address(addr, 0, 1, 0);
  1582. //NFI_ISSUE_COMMAND(cmd, addr, 0, 1, 0)
  1583. //SAL_NFI_Config_Sector_Number(1);
  1584. DRV_WriteReg32(NFI_CON_REG32, 1 << CON_NFI_SEC_SHIFT);
  1585. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1586. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1587. //SAL_NFI_Start_Data_Transfer(KAL_FALSE, KAL_TRUE);
  1588. while ( (write_count < bytes) && timeout ) {
  1589. WAIT_NFI_PIO_READY(timeout)
  1590. if (timeout == 0) {
  1591. break;
  1592. }
  1593. if (reg == 4)
  1594. DRV_WriteReg32(NFI_DATAW_REG32, *value++);
  1595. else
  1596. {
  1597. if ((write_count%2) == 1)
  1598. DRV_WriteReg32(NFI_DATAW_REG32, *value++);
  1599. else
  1600. DRV_WriteReg32(NFI_DATAW_REG32, *value);
  1601. }
  1602. write_count++;
  1603. timeout = TIMEOUT_3;
  1604. }
  1605. while ( (*NFI_STA_REG32 & STA_NAND_BUSY) && (timeout) ) {timeout--;}
  1606. mtk_nand_read_status();
  1607. if (timeout != 0)
  1608. return TRUE;
  1609. else
  1610. return FALSE;
  1611. }
  1612. bool mtk_nand_GetFeature(u16 cmd, u32 addr, u8 *value, u8 bytes)
  1613. {
  1614. kal_uint16 reg_val = 0;
  1615. kal_uint8 read_count = 0;
  1616. kal_uint32 timeout=TIMEOUT_3;//0xffff;
  1617. nand_reset();
  1618. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN);
  1619. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1620. nand_set_command(cmd);
  1621. nand_set_address(addr, 0, 1, 0);
  1622. //SAL_NFI_Config_Sector_Number(0);
  1623. DRV_WriteReg32(NFI_CON_REG32, 0 << CON_NFI_SEC_SHIFT);
  1624. reg_val = DRV_Reg32(NFI_CON_REG32);
  1625. reg_val &= ~CON_NFI_NOB_MASK;
  1626. reg_val |= ((4 << CON_NFI_NOB_SHIFT)|CON_NFI_SRD);
  1627. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  1628. //NFI_SET_REG16(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1629. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1630. // SAL_NFI_Start_Data_Transfer(KAL_TRUE, KAL_TRUE);
  1631. while ( (read_count < bytes) && timeout ) {
  1632. WAIT_NFI_PIO_READY(timeout)
  1633. if (timeout == 0) {
  1634. break;
  1635. }
  1636. *value++ = DRV_Reg32(NFI_DATAR_REG32);
  1637. read_count++;
  1638. timeout = TIMEOUT_3;
  1639. }
  1640. mtk_nand_read_status();
  1641. if (timeout != 0)
  1642. return TRUE;
  1643. else
  1644. return FALSE;
  1645. }
  1646. #if 1
  1647. const u8 data_tbl[8][5] = {
  1648. {0x04, 0x04, 0x7C, 0x7E, 0x00},
  1649. {0x00, 0x7C, 0x78, 0x78, 0x00},
  1650. {0x7C, 0x76, 0x74, 0x72, 0x00},
  1651. {0x08, 0x08, 0x00, 0x00, 0x00},
  1652. {0x0B, 0x7E, 0x76, 0x74, 0x00},
  1653. {0x10, 0x76, 0x72, 0x70, 0x00},
  1654. {0x02, 0x7C, 0x7E, 0x70, 0x00},
  1655. {0x00, 0x00, 0x00, 0x00, 0x00}
  1656. };
  1657. static void mtk_nand_modeentry_rrtry(void)
  1658. {
  1659. nand_reset();
  1660. nand_set_mode(CNFG_OP_CUST);
  1661. nand_set_command(0x5C);
  1662. nand_set_command(0xC5);
  1663. nand_status_ready(STA_NFI_OP_MASK);
  1664. }
  1665. static void mtk_nand_rren_rrtry(bool needB3)
  1666. {
  1667. nand_reset();
  1668. nand_set_mode(CNFG_OP_CUST);
  1669. if (needB3)
  1670. nand_set_command(0xB3);
  1671. nand_set_command(0x26);
  1672. nand_set_command(0x5D);
  1673. nand_status_ready(STA_NFI_OP_MASK);
  1674. }
  1675. static void mtk_nand_sprmset_rrtry(u32 addr, u32 data) //single parameter setting
  1676. {
  1677. u16 reg_val = 0;
  1678. u8 write_count = 0;
  1679. u32 reg = 0;
  1680. u32 timeout=TIMEOUT_3;//0xffff;
  1681. nand_reset();
  1682. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  1683. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1684. nand_set_command(0x55);
  1685. nand_set_address(addr, 0, 1, 0);
  1686. nand_status_ready(STA_NFI_OP_MASK);
  1687. DRV_WriteReg32(NFI_CON_REG32, 1 << CON_NFI_SEC_SHIFT);
  1688. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1689. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1690. WAIT_NFI_PIO_READY(timeout);
  1691. timeout=TIMEOUT_3;
  1692. DRV_WriteReg8(NFI_DATAW_REG32, data);
  1693. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  1694. }
  1695. static void mtk_nand_toshiba_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1696. {
  1697. u32 acccon;
  1698. u8 add_reg[6] = {0x04, 0x05, 0x06, 0x07, 0x0D};
  1699. u8 cnt = 0;
  1700. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  1701. //DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); //to fit read retry timing
  1702. if (0 == retryCount)
  1703. mtk_nand_modeentry_rrtry();
  1704. for (cnt = 0; cnt < 5; cnt ++) {
  1705. mtk_nand_sprmset_rrtry(add_reg[cnt], data_tbl[retryCount][cnt]);
  1706. }
  1707. if (3 == retryCount)
  1708. mtk_nand_rren_rrtry(TRUE);
  1709. else if (6 > retryCount)
  1710. mtk_nand_rren_rrtry(FALSE);
  1711. if (7 == retryCount) { // to exit
  1712. nand_set_mode(CNFG_OP_RESET);
  1713. NFI_ISSUE_COMMAND (NAND_CMD_RESET, 0, 0, 0, 0);
  1714. nand_reset();
  1715. }
  1716. //DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1717. }
  1718. const u8 tb_slc_1y[8][2] = {
  1719. {0xF0, 0x00},
  1720. {0xE0, 0x00},
  1721. {0xD0, 0x00},
  1722. {0xC0, 0x00},
  1723. {0x20, 0x00},
  1724. {0x30, 0x00},
  1725. {0x40, 0x00},
  1726. {0x00, 0x00}
  1727. };
  1728. const u8 tb_tlc_1y[31][7] = {
  1729. {0xFE, 0x03, 0x02, 0x02, 0xFF, 0xFC, 0xFD},
  1730. {0xFE, 0x02, 0x01, 0x01, 0xFE, 0xFA, 0xFB},
  1731. {0xFE, 0x00, 0x00, 0xFF, 0xFC, 0xF8, 0xF9},
  1732. {0xFD, 0xFF, 0xFE, 0xFE, 0xFA, 0xF6, 0xF7},
  1733. {0xFD, 0xFE, 0xFD, 0xFC, 0xF8, 0xF4, 0xF5},
  1734. {0xFD, 0xFD, 0xFC, 0xFB, 0xF6, 0xF2, 0xF2},
  1735. {0xFD, 0xFB, 0xFB, 0xF9, 0xF5, 0xF0, 0xF0},
  1736. {0xFD, 0xFA, 0xF9, 0xF8, 0xF3, 0xEE, 0xEE},
  1737. {0xFD, 0xF9, 0xF8, 0xF6, 0xF1, 0xEC, 0xEC},
  1738. {0xFD, 0xF8, 0xF7, 0xF5, 0xEF, 0xEA, 0xE9},
  1739. {0xFC, 0xF6, 0xF6, 0xF3, 0xEE, 0xE8, 0xE7},
  1740. {0xFA, 0xFA, 0xFB, 0xFA, 0xFB, 0xFA, 0xFA},
  1741. {0xFA, 0xFA, 0xFA, 0xF9, 0xFA, 0xF8, 0xF8},
  1742. {0xFA, 0xFA, 0xFA, 0xF8, 0xF9, 0xF6, 0xF5},
  1743. {0xFB, 0xFA, 0xF9, 0xF7, 0xF7, 0xF4, 0xF3},
  1744. {0xFB, 0xFB, 0xF9, 0xF6, 0xF6, 0xF2, 0xF0},
  1745. {0xFB, 0xFB, 0xF8, 0xF5, 0xF5, 0xF0, 0xEE},
  1746. {0xFB, 0xFB, 0xF8, 0xF5, 0xF4, 0xEE, 0xEB},
  1747. {0xFC, 0xFB, 0xF7, 0xF4, 0xF2, 0xEC, 0xE9},
  1748. {0xFC, 0xFE, 0xFE, 0xF9, 0xFA, 0xF8, 0xF8},
  1749. {0xFD, 0xFE, 0xFD, 0xF7, 0xF7, 0xF4, 0xF3},
  1750. {0xFD, 0xFF, 0xFC, 0xF5, 0xF5, 0xF0, 0xEE},
  1751. {0xFE, 0x03, 0x03, 0x04, 0x01, 0xFF, 0x01},
  1752. {0xFC, 0x00, 0x00, 0x01, 0xFE, 0xFC, 0xFE},
  1753. {0xFA, 0xFA, 0xFC, 0xFC, 0xFA, 0xF7, 0xFA},
  1754. {0x00, 0x03, 0x02, 0x03, 0xFF, 0xFC, 0xFE},
  1755. {0x04, 0x03, 0x03, 0x03, 0x00, 0xFC, 0xFD},
  1756. {0x08, 0x04, 0x03, 0x04, 0x00, 0xFC, 0xFC},
  1757. {0xFC, 0x00, 0x00, 0x00, 0x04, 0x04, 0x08},
  1758. {0xF8, 0x00, 0x00, 0x00, 0x08, 0x08, 0x10},
  1759. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  1760. };
  1761. static void mtk_nand_modeentry_tlc_rrtry(void) //single parameter setting
  1762. {
  1763. u32 reg_val = 0;
  1764. u32 timeout=TIMEOUT_3;//0xffff;
  1765. nand_reset();
  1766. nand_set_mode(CNFG_OP_CUST);
  1767. nand_set_command(0x5C);
  1768. nand_set_command(0xC5);
  1769. nand_status_ready(STA_NFI_OP_MASK);
  1770. nand_reset();
  1771. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  1772. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1773. nand_set_command(0x55);
  1774. nand_set_address(0, 0, 1, 0);
  1775. nand_status_ready(STA_NFI_OP_MASK);
  1776. DRV_WriteReg32(NFI_CON_REG32, 1 << CON_NFI_SEC_SHIFT);
  1777. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  1778. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1779. WAIT_NFI_PIO_READY(timeout);
  1780. timeout=TIMEOUT_3;
  1781. DRV_WriteReg8(NFI_DATAW_REG32, 0x01);
  1782. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  1783. }
  1784. static void mtk_nand_toshiba_tlc_1y_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1785. {
  1786. u32 acccon;
  1787. u8 add_reg[7] = {0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
  1788. u8 cnt = 0;
  1789. if (TRUE == defValue) {
  1790. for (cnt = 0; cnt < 7; cnt ++) {
  1791. mtk_nand_sprmset_rrtry(add_reg[cnt], tb_tlc_1y[30][cnt]);
  1792. }
  1793. nand_set_mode(CNFG_OP_RESET);
  1794. NFI_ISSUE_COMMAND (NAND_CMD_RESET, 0, 0, 0, 0);
  1795. nand_reset();
  1796. return;
  1797. }
  1798. if (0 == retryCount)
  1799. mtk_nand_modeentry_tlc_rrtry();
  1800. for (cnt = 0; cnt < 7; cnt ++)
  1801. mtk_nand_sprmset_rrtry(add_reg[cnt], tb_tlc_1y[retryCount][cnt]);
  1802. nand_reset();
  1803. nand_set_mode(CNFG_OP_CUST);
  1804. if (31 == retryCount)
  1805. nand_set_command(0xB3);
  1806. nand_set_command(0x5D);
  1807. nand_status_ready(STA_NFI_OP_MASK);
  1808. //DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1809. }
  1810. static void mtk_nand_toshiba_slc_1y_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1811. {
  1812. u32 acccon;
  1813. u8 add_reg[2] = {0x0B, 0x0D};
  1814. u8 cnt = 0;
  1815. if (TRUE == defValue) {
  1816. for (cnt = 0; cnt < 2; cnt ++)
  1817. mtk_nand_sprmset_rrtry(add_reg[cnt], tb_slc_1y[7][cnt]);
  1818. nand_set_mode(CNFG_OP_RESET);
  1819. NFI_ISSUE_COMMAND (NAND_CMD_RESET, 0, 0, 0, 0);
  1820. nand_reset();
  1821. }
  1822. if (0 == retryCount)
  1823. mtk_nand_modeentry_tlc_rrtry();
  1824. for (cnt = 0; cnt < 2; cnt ++)
  1825. mtk_nand_sprmset_rrtry(add_reg[cnt], tb_slc_1y[retryCount][cnt]);
  1826. nand_reset();
  1827. nand_set_mode(CNFG_OP_CUST);
  1828. nand_set_command(0x5D);
  1829. nand_status_ready(STA_NFI_OP_MASK);
  1830. }
  1831. static void mtk_nand_toshiba_tlc_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1832. {
  1833. if (devinfo.tlcControl.slcopmodeEn)
  1834. mtk_nand_toshiba_slc_1y_rrtry(deviceinfo, retryCount, defValue);
  1835. else
  1836. mtk_nand_toshiba_tlc_1y_rrtry(deviceinfo, retryCount, defValue);
  1837. }
  1838. #endif
  1839. static void mtk_nand_micron_rrtry(flashdev_info deviceinfo, u32 feature, bool defValue)
  1840. {
  1841. //u32 feature = deviceinfo.feature_set.FeatureSet.readRetryStart+retryCount;
  1842. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd,\
  1843. deviceinfo.feature_set.FeatureSet.readRetryAddress,\
  1844. (u8 *)&feature,4);
  1845. }
  1846. static int g_sandisk_retry_case = 0;
  1847. static void mtk_nand_sandisk_rrtry(flashdev_info deviceinfo, u32 feature, bool defValue)
  1848. {
  1849. //u32 feature = deviceinfo.feature_set.FeatureSet.readRetryStart+retryCount;
  1850. if (FALSE == defValue)
  1851. nand_reset();
  1852. else {
  1853. nand_device_reset();
  1854. nand_reset();
  1855. }
  1856. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd,\
  1857. deviceinfo.feature_set.FeatureSet.readRetryAddress,\
  1858. (u8 *)&feature,4);
  1859. if (FALSE == defValue)
  1860. nand_set_command(deviceinfo.feature_set.FeatureSet.readRetryPreCmd);
  1861. }
  1862. u16 sandisk_19nm_rr_table[18] = {
  1863. 0x0000,
  1864. 0xFF0F, 0xEEFE, 0xDDFD, 0x11EE, //04h[7:4] | 07h[7:4] | 04h[3:0] | 05h[7:4]
  1865. 0x22ED, 0x33DF, 0xCDDE, 0x01DD,
  1866. 0x0211, 0x1222, 0xBD21, 0xAD32,
  1867. 0x9DF0, 0xBCEF, 0xACDC, 0x9CFF,
  1868. 0x0000
  1869. };
  1870. static void sandisk_19nm_rr_init(void)
  1871. {
  1872. u32 reg_val = 0;
  1873. u32 count = 0;
  1874. u32 timeout = 0xffff;
  1875. u32 acccon;
  1876. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  1877. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); //to fit read retry timing
  1878. nand_reset();
  1879. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  1880. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1881. nand_set_command(0x3B);
  1882. nand_set_command(0xB9);
  1883. for (count = 0; count < 9; count++) {
  1884. nand_set_command(0x53);
  1885. nand_set_address((0x04 + count), 0, 1, 0);
  1886. DRV_WriteReg32(NFI_CON_REG32, (CON_NFI_BRD | CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1887. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1888. timeout = 0xffff;
  1889. WAIT_NFI_PIO_READY(timeout);
  1890. DRV_WriteReg32(NFI_DATAW_REG32, 0x00);
  1891. nand_reset();
  1892. }
  1893. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1894. }
  1895. static void sandisk_19nm_rr_loading(u32 retryCount, bool defValue)
  1896. {
  1897. u32 reg_val = 0;
  1898. u32 timeout = 0xffff;
  1899. u32 acccon;
  1900. u8 count;
  1901. u8 cmd_reg[4] = {0x4, 0x5, 0x7};
  1902. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  1903. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); //to fit read retry timing
  1904. nand_reset();
  1905. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  1906. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1907. if ((0 != retryCount) || defValue) {
  1908. nand_set_command(0xD6);
  1909. }
  1910. nand_set_command(0x3B);
  1911. nand_set_command(0xB9);
  1912. for (count = 0; count < 3; count++) {
  1913. nand_set_command(0x53);
  1914. nand_set_address(cmd_reg[count], 0, 1, 0);
  1915. DRV_WriteReg32(NFI_CON_REG32, (CON_NFI_BRD | CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1916. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1917. timeout = 0xffff;
  1918. WAIT_NFI_PIO_READY(timeout);
  1919. if (count == 0)
  1920. DRV_WriteReg32(NFI_DATAW_REG32, (((sandisk_19nm_rr_table[retryCount] & 0xF000) >> 8) | ((sandisk_19nm_rr_table[retryCount] & 0x00F0) >> 4)));
  1921. else if (count == 1)
  1922. DRV_WriteReg32(NFI_DATAW_REG32, ((sandisk_19nm_rr_table[retryCount] & 0x000F) << 4));
  1923. else if (count == 2)
  1924. DRV_WriteReg32(NFI_DATAW_REG32, ((sandisk_19nm_rr_table[retryCount] & 0x0F00) >> 4));
  1925. nand_reset();
  1926. }
  1927. if (!defValue) {
  1928. nand_set_command(0xB6);
  1929. }
  1930. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1931. }
  1932. static void mtk_nand_sandisk_19nm_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1933. {
  1934. if ((retryCount == 0) && (!defValue))
  1935. sandisk_19nm_rr_init();
  1936. sandisk_19nm_rr_loading(retryCount, defValue);
  1937. }
  1938. #define HYNIX_RR_TABLE_SIZE (1026) //hynix read retry table size
  1939. #define SINGLE_RR_TABLE_SIZE (64)
  1940. #define READ_RETRY_STEP (devinfo.feature_set.FeatureSet.readRetryCnt + devinfo.feature_set.FeatureSet.readRetryStart) // 8 step or 12 step to fix read retry table
  1941. #define HYNIX_16NM_RR_TABLE_SIZE ((READ_RETRY_STEP == 12)?(784):(528)) //hynix read retry table size
  1942. #define SINGLE_RR_TABLE_16NM_SIZE ((READ_RETRY_STEP == 12)?(48):(32))
  1943. u8 nand_hynix_rr_table[(HYNIX_RR_TABLE_SIZE+16)/16*16]; //align as 16 byte
  1944. #define NAND_HYX_RR_TBL_BUF nand_hynix_rr_table
  1945. static u8 real_hynix_rr_table_idx = 0;
  1946. static u32 g_hynix_retry_count = 0;
  1947. static bool hynix_rr_table_select(u8 table_index, flashdev_info *deviceinfo)
  1948. {
  1949. u32 i;
  1950. u32 table_size = (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM)?SINGLE_RR_TABLE_16NM_SIZE : SINGLE_RR_TABLE_SIZE;
  1951. for (i = 0; i < table_size; i++) {
  1952. u8 *temp_rr_table = (u8 *)NAND_HYX_RR_TBL_BUF+table_size*table_index*2+2;
  1953. u8 *temp_inversed_rr_table = (u8 *)NAND_HYX_RR_TBL_BUF+table_size*table_index*2+table_size+2;
  1954. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1955. temp_rr_table += 14;
  1956. temp_inversed_rr_table += 14;
  1957. }
  1958. if (0xFF != (temp_rr_table[i] ^ temp_inversed_rr_table[i]))
  1959. return FALSE; // error table
  1960. }
  1961. return TRUE; // correct table
  1962. }
  1963. static void HYNIX_RR_TABLE_READ(flashdev_info *deviceinfo)
  1964. {
  1965. u32 reg_val = 0;
  1966. u32 read_count = 0, max_count = HYNIX_RR_TABLE_SIZE;
  1967. u32 timeout = 0xffff;
  1968. u8* rr_table = (u8*)(NAND_HYX_RR_TBL_BUF);
  1969. u8 table_index = 0;
  1970. u8 add_reg1[3] = {0xFF, 0xCC};
  1971. u8 data_reg1[3] = {0x40, 0x4D};
  1972. u8 cmd_reg[6] = {0x16, 0x17, 0x04, 0x19, 0x00};
  1973. u8 add_reg2[6] = {0x00, 0x00, 0x00, 0x02, 0x00};
  1974. bool RR_TABLE_EXIST = TRUE;
  1975. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1976. read_count = 1;
  1977. add_reg1[1]= 0x38;
  1978. data_reg1[1] = 0x52;
  1979. max_count = HYNIX_16NM_RR_TABLE_SIZE;
  1980. if (READ_RETRY_STEP == 12) {
  1981. add_reg2[2] = 0x1F;
  1982. }
  1983. }
  1984. nand_device_reset();
  1985. // take care under sync mode. need change nand device inferface xiaolei
  1986. nand_reset();
  1987. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  1988. nand_set_command(0x36);
  1989. for (; read_count < 2; read_count++) {
  1990. nand_set_address(add_reg1[read_count],0,1,0);
  1991. DRV_WriteReg32(NFI_CON_REG32, (CON_NFI_BRD | CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1992. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1993. timeout = 0xffff;
  1994. WAIT_NFI_PIO_READY(timeout);
  1995. DRV_WriteReg32(NFI_DATAW_REG32, data_reg1[read_count]);
  1996. nand_reset();
  1997. }
  1998. for (read_count = 0; read_count < 5; read_count++) {
  1999. nand_set_command(cmd_reg[read_count]);
  2000. }
  2001. for (read_count = 0; read_count < 5; read_count++) {
  2002. nand_set_address(add_reg2[read_count],0,1,0);
  2003. }
  2004. nand_set_command(0x30);
  2005. DRV_WriteReg(NFI_CNRNB_REG16, 0xF1);
  2006. timeout = 0xffff;
  2007. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  2008. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN);
  2009. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2010. DRV_WriteReg32(NFI_CON_REG32, (CON_NFI_BRD | CON_NFI_BWR | (2<< CON_NFI_SEC_SHIFT)));
  2011. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2012. timeout = 0xffff;
  2013. read_count = 0; // how????
  2014. while ((read_count < max_count) && timeout ) {
  2015. WAIT_NFI_PIO_READY(timeout);
  2016. *rr_table++ = (U8)DRV_Reg32(NFI_DATAR_REG32);
  2017. read_count++;
  2018. timeout = 0xFFFF;
  2019. }
  2020. nand_device_reset();
  2021. // take care under sync mode. need change nand device inferface xiaolei
  2022. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  2023. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  2024. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2025. nand_set_command(0x36);
  2026. nand_set_address(0x38,0,1,0);
  2027. DRV_WriteReg32(NFI_CON_REG32, (CON_NFI_BRD | CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  2028. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  2029. WAIT_NFI_PIO_READY(timeout);
  2030. DRV_WriteReg32(NFI_DATAW_REG32, 0x00);
  2031. nand_reset();
  2032. nand_set_command(0x16);
  2033. nand_set_command(0x00);
  2034. nand_set_address(0x00,0,1,0);//dummy read, add don't care
  2035. nand_set_command(0x30);
  2036. } else {
  2037. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2038. nand_set_command(0x38);
  2039. }
  2040. timeout = 0xffff;
  2041. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  2042. rr_table = (u8*)(NAND_HYX_RR_TBL_BUF);
  2043. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX) {
  2044. if ((rr_table[0] != 8) || (rr_table[1] != 8)) {
  2045. RR_TABLE_EXIST = FALSE;
  2046. ASSERT(0);
  2047. }
  2048. } else if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  2049. for (read_count=0; read_count<8; read_count++) {
  2050. if ((rr_table[read_count] != 8) || (rr_table[read_count+8] != 4)) {
  2051. RR_TABLE_EXIST = FALSE;
  2052. break;
  2053. }
  2054. }
  2055. }
  2056. if (RR_TABLE_EXIST) {
  2057. for (table_index = 0 ; table_index < 8; table_index++) {
  2058. if (hynix_rr_table_select(table_index, deviceinfo)) {
  2059. real_hynix_rr_table_idx = table_index;
  2060. MSG(INIT, "Hynix rr_tbl_id %d\n",real_hynix_rr_table_idx);
  2061. break;
  2062. }
  2063. }
  2064. if (table_index == 8) {
  2065. ASSERT(0);
  2066. }
  2067. } else {
  2068. MSG(INIT, "Hynix RR table index error!\n");
  2069. }
  2070. }
  2071. static void HYNIX_Set_RR_Para(u32 rr_index, flashdev_info *deviceinfo)
  2072. {
  2073. u32 reg_val = 0;
  2074. u32 timeout=0xffff;
  2075. u8 count, max_count = 8;
  2076. u8 add_reg[9] = {0xCC, 0xBF, 0xAA, 0xAB, 0xCD, 0xAD, 0xAE, 0xAF};
  2077. u8 *hynix_rr_table = (u8 *)NAND_HYX_RR_TBL_BUF+SINGLE_RR_TABLE_SIZE*real_hynix_rr_table_idx*2+2;
  2078. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  2079. add_reg[0] = 0x38; //0x38, 0x39, 0x3A, 0x3B
  2080. for (count =1; count < 4; count++) {
  2081. add_reg[count] = add_reg[0] + count;
  2082. }
  2083. hynix_rr_table += 14;
  2084. max_count = 4;
  2085. }
  2086. nand_reset();
  2087. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  2088. nand_set_command(0x36);
  2089. for (count = 0; count < max_count; count++) {
  2090. nand_set_address(add_reg[count], 0, 1, 0);
  2091. DRV_WriteReg32(NFI_CON_REG32, (CON_NFI_BRD | CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  2092. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  2093. timeout = 0xffff;
  2094. WAIT_NFI_PIO_READY(timeout);
  2095. DRV_WriteReg32(NFI_DATAW_REG32, hynix_rr_table[rr_index*max_count + count]);
  2096. }
  2097. nand_set_command(0x16);
  2098. nand_reset();
  2099. }
  2100. static void mtk_nand_hynix_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  2101. {
  2102. if (defValue == FALSE) {
  2103. if (g_hynix_retry_count == READ_RETRY_STEP) {
  2104. g_hynix_retry_count = 0;
  2105. }
  2106. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  2107. //HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo);
  2108. g_hynix_retry_count ++;
  2109. }
  2110. //HYNIX_Set_RR_Para(retryCount, &deviceinfo);
  2111. }
  2112. static void mtk_nand_hynix_16nm_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  2113. {
  2114. if (defValue == FALSE) {
  2115. if (g_hynix_retry_count == READ_RETRY_STEP) {
  2116. g_hynix_retry_count = 0;
  2117. }
  2118. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  2119. //mb();
  2120. //HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo);
  2121. g_hynix_retry_count ++;
  2122. }
  2123. //HYNIX_Set_RR_Para(retryCount, &deviceinfo);
  2124. }
  2125. /*
  2126. static void mtk_nand_hynix_fdie_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  2127. {
  2128. if(defValue == FALSE)
  2129. {
  2130. if(g_hynix_retry_count == READ_RETRY_STEP)
  2131. {
  2132. g_hynix_retry_count = 0;
  2133. }
  2134. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  2135. //mb();
  2136. //HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo);
  2137. g_hynix_retry_count ++;
  2138. }
  2139. //HYNIX_Set_RR_Para(retryCount, &deviceinfo);
  2140. }
  2141. */
  2142. // sandisk 1y nm
  2143. u32 special_rrtry_setting[37]= {
  2144. 0x00000000,0x7C00007C,0x787C0004,0x74780078,
  2145. 0x7C007C08,0x787C7C00,0x74787C7C,0x70747C00,
  2146. 0x7C007800,0x787C7800,0x74787800,0x70747800,
  2147. 0x6C707800,0x00040400,0x7C000400,0x787C040C,
  2148. 0x7478040C,0x7C000810,0x00040810,0x04040C0C,
  2149. 0x00040C10,0x00081014,0x000C1418,0x7C040C0C,
  2150. 0x74787478,0x70747478,0x6C707478,0x686C7478,
  2151. 0x74787078,0x70747078,0x686C7078,0x6C707078,
  2152. 0x6C706C78,0x686C6C78,0x64686C78,0x686C6874,
  2153. 0x64686874,
  2154. };
  2155. #if defined(MTK_TLC_NAND_SUPPORT)
  2156. u32 sandisk_tlc_rrtbl_12h[40]= {
  2157. 0x00000000, 0x08000004, 0x00000404, 0x04040408,
  2158. 0x08040408, 0x0004080C, 0x04040810, 0x0C0C0C00,
  2159. 0x0E0E0E00, 0x10101000, 0x12121200, 0x080808FC,
  2160. 0xFC08FCF8, 0x0000FBF6, 0x0408FBF4, 0xFEFCF8FA,
  2161. 0xFCF8F4EC, 0xF8F8F8EC, 0x0002FCE4, 0xFCFEFEFE,
  2162. 0xFFFC00FD, 0xFEFB00FC, 0xFEFAFEFA, 0xFDF9FDFA,
  2163. 0xFBF8FBFA, 0xF9F7FAF8, 0xF8F6F9F4, 0xF5F4F8F2,
  2164. 0xF4F2F6EE, 0xF0F0F4E8, 0xECECF0E6, 0x020400FA,
  2165. 0x00FEFFF8, 0xFEFEFDF6, 0xFDFDFCF4, 0xFBFCFCF2,
  2166. 0xF9FBFBF0, 0xF8F9F9EE, 0xF6F8F8ED, 0xF4F7F6EA,
  2167. };
  2168. u32 sandisk_tlc_rrtbl_13h[40]= {
  2169. 0x00000000, 0x00040800, 0x00080004, 0x00020404,
  2170. 0x00040800, 0x00080000, 0x00FC0000, 0x000C0C0C,
  2171. 0x000E0E0E, 0x00101010, 0x00141414, 0x000008FC,
  2172. 0x0004FCF8, 0x00FC00F6, 0x00FC0404, 0x00FCFE08,
  2173. 0x00FCFC00, 0x00F8F8FA, 0x000000F4, 0x00FAFC02,
  2174. 0x00F8FF00, 0x00F6FDFE, 0x00F4FBFC, 0x00F2F9FA,
  2175. 0x00F0F7F8, 0x00EEF5F6, 0x00ECF3F4, 0x00EAF1F2,
  2176. 0x00E8ECEE, 0x00E0E4E8, 0x00DAE0E2, 0x00000000,
  2177. 0x00FEFEFE, 0x00FBFCFC, 0x00F9FAFA, 0x00F7F8F8,
  2178. 0x00F5F6F6, 0x00F3F4F4, 0x00F1F2F2, 0x00EFF0EF,
  2179. };
  2180. u32 sandisk_tlc_rrtbl_14h[11]= {
  2181. 0x00000000, 0x00000010, 0x00000020, 0x00000030,
  2182. 0x00000040, 0x00000050, 0x00000060, 0x000000F0,
  2183. 0x000000E0, 0x000000D0, 0x000000C0,
  2184. };
  2185. static void mtk_nand_sandisk_tlc_1ynm_rrtry(flashdev_info deviceinfo, u32 feature, bool defValue)
  2186. {
  2187. u16 reg_val = 0;
  2188. u32 timeout = TIMEOUT_3;
  2189. u32 value1, value2, value3;
  2190. if ((feature > 1) || defValue) { //add exit rr cmd sequence
  2191. //set 0x55h cmd + 0x00h address + 0x00 data
  2192. nand_reset();
  2193. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  2194. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2195. nand_set_command(0x55);
  2196. nand_set_address(0, 0, 1, 0);
  2197. DRV_WriteReg32(NFI_CON_REG32, 1 << CON_NFI_SEC_SHIFT);
  2198. NFI_SET_REG32(NFI_CON_REG32, CON_NFI_BRD | CON_NFI_BWR);
  2199. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2200. WAIT_NFI_PIO_READY(timeout)
  2201. if (timeout == 0) {
  2202. MSG(INIT, "mtk_nand_sandisk_tlc_1ynm_rrtry: timeout\n");
  2203. }
  2204. DRV_WriteReg32(NFI_DATAW_REG32, 0);
  2205. //set device reset
  2206. nand_device_reset();
  2207. }
  2208. if (devinfo.tlcControl.slcopmodeEn) { //slc block
  2209. value3 = sandisk_tlc_rrtbl_14h[feature];
  2210. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd, 0x14, (u8 *)&value3, 4);
  2211. } else { //tlc block
  2212. value1 = sandisk_tlc_rrtbl_12h[feature];
  2213. value2 = sandisk_tlc_rrtbl_13h[feature];
  2214. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd, 0x12, (u8 *)&value1, 4);
  2215. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd, 0x13, (u8 *)&value2, 4);
  2216. }
  2217. if (FALSE == defValue) {
  2218. //set 0x5D cmd
  2219. nand_reset();
  2220. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  2221. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2222. nand_set_command(0x5D);
  2223. nand_reset();
  2224. }
  2225. }
  2226. #endif
  2227. static u32 mtk_nand_rrtry_setting(flashdev_info deviceinfo, enum readRetryType type, u32 retryStart, u32 loopNo)
  2228. {
  2229. u32 value;
  2230. //if(RTYPE_MICRON == type || RTYPE_SANDISK== type || RTYPE_TOSHIBA== type || RTYPE_HYNIX== type)
  2231. {
  2232. if (retryStart != 0xFFFFFFFF) {
  2233. value = retryStart+loopNo;
  2234. } else {
  2235. value = special_rrtry_setting[loopNo];
  2236. }
  2237. }
  2238. return value;
  2239. }
  2240. typedef u32 (*rrtryFunctionType)(flashdev_info deviceinfo, u32 feature, bool defValue);
  2241. static rrtryFunctionType rtyFuncArray[]= {
  2242. mtk_nand_micron_rrtry,
  2243. mtk_nand_sandisk_rrtry,
  2244. mtk_nand_sandisk_19nm_rrtry,
  2245. mtk_nand_toshiba_rrtry,
  2246. mtk_nand_hynix_rrtry,
  2247. mtk_nand_hynix_16nm_rrtry,
  2248. #if defined(MTK_TLC_NAND_SUPPORT)
  2249. mtk_nand_sandisk_tlc_1ynm_rrtry,
  2250. mtk_nand_toshiba_tlc_rrtry,
  2251. #endif
  2252. };
  2253. static void mtk_nand_rrtry_func(flashdev_info deviceinfo, u32 feature, bool defValue)
  2254. {
  2255. if (gVendor != VEND_NONE) {
  2256. rtyFuncArray[deviceinfo.feature_set.FeatureSet.rtype](deviceinfo, feature,defValue);
  2257. }
  2258. }
  2259. int nand_exec_read_page_hw(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2260. {
  2261. int bRet;
  2262. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  2263. //bool retry = FALSE;
  2264. bool readRetry = FALSE;
  2265. int retryCount = 0;
  2266. u32 retrytotalcnt = devinfo.feature_set.FeatureSet.readRetryCnt;
  2267. #if defined(MTK_TLC_NAND_SUPPORT)
  2268. NFI_TLC_WL_INFO tlc_wl_info;
  2269. bool tlc_left_plane = TRUE;
  2270. u32 reg_val = 0;
  2271. int spare_per_sector = nand->oobsize/u4SecNum;
  2272. #endif
  2273. u32 real_row_addr = 0;
  2274. u32 logical_plane_num = 1;
  2275. u32 data_sector_num = 0;
  2276. u8 *temp_byte_ptr = NULL;
  2277. u8 *spare_ptr = NULL;
  2278. mtk_wdt_restart();
  2279. #if CFG_2CS_NAND
  2280. if (g_bTricky_CS) {
  2281. u4RowAddr = mtk_nand_cs_on(NFI_TRICKY_CS, u4RowAddr);
  2282. }
  2283. #endif
  2284. do {
  2285. mtk_nand_interface_switch();
  2286. data_sector_num = u4SecNum;
  2287. temp_byte_ptr = pPageBuf;
  2288. spare_ptr = pFDMBuf;
  2289. logical_plane_num = 1;
  2290. tlc_wl_info.word_line_idx = u4RowAddr;
  2291. tlc_wl_info.wl_pre = WL_LOW_PAGE;
  2292. #if defined(MTK_TLC_NAND_SUPPORT)
  2293. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2294. if (devinfo.tlcControl.normaltlc) { // normal tlc
  2295. NFI_TLC_GetMappedWL(u4RowAddr, &tlc_wl_info);
  2296. real_row_addr = NFI_TLC_GetRowAddr(tlc_wl_info.word_line_idx);
  2297. if (devinfo.tlcControl.pPlaneEn) {
  2298. tlc_left_plane = TRUE; // begin at left logical plane
  2299. logical_plane_num = 2;
  2300. data_sector_num /= 2;
  2301. real_row_addr = NFI_TLC_SetpPlaneAddr(real_row_addr, tlc_left_plane);
  2302. }
  2303. } else {
  2304. real_row_addr = NFI_TLC_GetRowAddr(u4RowAddr);
  2305. }
  2306. if (devinfo.tlcControl.slcopmodeEn) { // slc mode
  2307. if (0xFF != devinfo.tlcControl.en_slc_mode_cmd) {
  2308. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2309. reg_val &= ~CNFG_READ_EN;
  2310. reg_val &= ~CNFG_OP_MODE_MASK;
  2311. reg_val |= CNFG_OP_CUST;
  2312. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2313. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  2314. reg_val = DRV_Reg32(NFI_CON_REG32);
  2315. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2316. /* issue reset operation */
  2317. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2318. }
  2319. } else { //tlc mode
  2320. if (devinfo.tlcControl.normaltlc) {
  2321. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2322. reg_val &= ~CNFG_READ_EN;
  2323. reg_val &= ~CNFG_OP_MODE_MASK;
  2324. reg_val |= CNFG_OP_CUST;
  2325. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2326. if (tlc_wl_info.wl_pre == WL_LOW_PAGE)
  2327. nand_set_command(LOW_PG_SELECT_CMD);
  2328. else if (tlc_wl_info.wl_pre == WL_MID_PAGE)
  2329. nand_set_command(MID_PG_SELECT_CMD);
  2330. else if (tlc_wl_info.wl_pre == WL_HIGH_PAGE)
  2331. nand_set_command(HIGH_PG_SELECT_CMD);
  2332. reg_val = DRV_Reg32(NFI_CON_REG32);
  2333. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2334. /* issue reset operation */
  2335. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2336. }
  2337. }
  2338. reg_val = 0; // reset reg_val
  2339. } else
  2340. #endif
  2341. {
  2342. real_row_addr = u4RowAddr;
  2343. }
  2344. if (use_randomizer ) {
  2345. if (devinfo.tlcControl.slcopmodeEn)
  2346. mtk_nand_turn_on_randomizer(tlc_wl_info.word_line_idx);
  2347. else
  2348. mtk_nand_turn_on_randomizer(tlc_wl_info.word_line_idx*3+tlc_wl_info.wl_pre);
  2349. }
  2350. // else
  2351. // mtk_nand_interface_async();
  2352. bRet = ERR_RTN_SUCCESS;
  2353. if (nand_ready_for_read(nand, real_row_addr, 0, true, pPageBuf)) {
  2354. while (logical_plane_num) {
  2355. #if defined(MTK_TLC_NAND_SUPPORT)
  2356. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2357. if (devinfo.tlcControl.needchangecolumn) {
  2358. ////////////change colunm address///////////
  2359. if (devinfo.tlcControl.pPlaneEn)
  2360. real_row_addr = NFI_TLC_SetpPlaneAddr(real_row_addr, tlc_left_plane);
  2361. #if 1 //reset here to flush fifo status left by the left plane read
  2362. reg_val = DRV_Reg32(NFI_CON_REG32);
  2363. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2364. /* issue reset operation */
  2365. DRV_WriteReg32(NFI_CON_REG32, reg_val); //not use SAL_NFI_Reset() here
  2366. #endif
  2367. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2368. reg_val &= ~CNFG_READ_EN;
  2369. reg_val &= ~CNFG_OP_MODE_MASK;
  2370. reg_val |= CNFG_OP_CUST;
  2371. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2372. nand_set_command(CHANGE_COLUNM_ADDR_1ST_CMD);
  2373. nand_set_address(0, real_row_addr, 2, devinfo.addr_cycle - 2);
  2374. nand_set_command(CHANGE_COLUNM_ADDR_2ND_CMD);
  2375. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2376. reg_val |= CNFG_READ_EN;
  2377. reg_val &= ~CNFG_OP_MODE_MASK;
  2378. reg_val |= CNFG_OP_READ;
  2379. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2380. }
  2381. }
  2382. DRV_WriteReg32(NFI_STRADDR_REG32, temp_byte_ptr);
  2383. DRV_WriteReg32(NFI_CON_REG32, data_sector_num << CON_NFI_SEC_SHIFT);
  2384. {
  2385. ECC_Decode_Start();
  2386. }
  2387. #endif
  2388. if (!nand_read_page_data(temp_byte_ptr, data_sector_num * (1 << nand->sector_shift))) {
  2389. MSG(INIT, "nand_exec_read_page_hw: fail 1\n");
  2390. //dump_nfi();
  2391. bRet = ERR_RTN_FAIL;
  2392. }
  2393. if (!nand_status_ready(STA_NAND_BUSY)) {
  2394. bRet = ERR_RTN_FAIL;
  2395. }
  2396. if (!nand_check_dececc_done(data_sector_num)) {
  2397. bRet = ERR_RTN_FAIL;
  2398. }
  2399. nand_read_fdm_data(spare_ptr, data_sector_num);
  2400. if (!nand_check_bch_error(temp_byte_ptr, data_sector_num - 1, u4RowAddr)) {
  2401. bRet = ERR_RTN_BCH_FAIL;
  2402. if (devinfo.vendor != VEND_NONE) {
  2403. readRetry = TRUE;
  2404. }
  2405. g_i4ErrNum++;
  2406. }
  2407. if (0 != (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  2408. if (retryCount != 0) {
  2409. MSG(INFO, "NFI read retry read empty page, return as uncorrectable!\n");
  2410. bRet = ERR_RTN_BCH_FAIL;
  2411. } else {
  2412. memset(pPageBuf, 0xFF, u4PageSize);
  2413. memset(pFDMBuf, 0xFF, nand->nand_fdm_size*u4SecNum);
  2414. readRetry = FALSE;
  2415. bRet = ERR_RTN_SUCCESS;
  2416. }
  2417. }
  2418. nand_stop_read();
  2419. #if defined(MTK_TLC_NAND_SUPPORT)
  2420. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2421. if (devinfo.tlcControl.needchangecolumn)
  2422. DRV_WriteReg(NFI_TLC_RD_WHR2_REG16, 0x055); //disable
  2423. if (2 == logical_plane_num) {
  2424. tlc_left_plane = FALSE;
  2425. spare_ptr += (nand->nand_fdm_size * data_sector_num);
  2426. temp_byte_ptr += (data_sector_num *(1 << nand->sector_shift));
  2427. }
  2428. }
  2429. #endif
  2430. logical_plane_num --;
  2431. if (bRet == ERR_RTN_BCH_FAIL)
  2432. break;
  2433. }
  2434. }
  2435. #if defined(MTK_TLC_NAND_SUPPORT)
  2436. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2437. if ((devinfo.tlcControl.slcopmodeEn)
  2438. &&(0xFF != devinfo.tlcControl.dis_slc_mode_cmd)) {
  2439. reg_val = DRV_Reg32(NFI_CON_REG32);
  2440. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2441. /* issue reset operation */
  2442. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2443. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2444. reg_val &= ~CNFG_READ_EN;
  2445. reg_val &= ~CNFG_OP_MODE_MASK;
  2446. reg_val |= CNFG_OP_CUST;
  2447. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2448. nand_set_command(devinfo.tlcControl.dis_slc_mode_cmd);
  2449. }
  2450. }
  2451. #endif
  2452. if (use_randomizer)
  2453. mtk_nand_turn_off_randomizer();
  2454. if (bRet == ERR_RTN_BCH_FAIL) {
  2455. u32 feature = mtk_nand_rrtry_setting(devinfo, devinfo.feature_set.FeatureSet.rtype,devinfo.feature_set.FeatureSet.readRetryStart,retryCount);
  2456. #if defined(MTK_TLC_NAND_SUPPORT)
  2457. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_SANDISK_TLC_1YNM)
  2458. && (devinfo.tlcControl.slcopmodeEn))
  2459. retrytotalcnt = 10;
  2460. if (devinfo.feature_set.FeatureSet.rtype == RTYPE_TOSHIBA_TLC) {
  2461. if (devinfo.tlcControl.slcopmodeEn)
  2462. retrytotalcnt = 8;
  2463. else
  2464. retrytotalcnt = 31;
  2465. }
  2466. #endif
  2467. if (retryCount < retrytotalcnt) {
  2468. mtk_nand_rrtry_func(devinfo,feature,FALSE);
  2469. retryCount++;
  2470. } else {
  2471. feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  2472. // sandisk case 2/3/4
  2473. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_SANDISK) && (g_sandisk_retry_case < 2)) {
  2474. g_sandisk_retry_case++;
  2475. mtk_nand_rrtry_func(devinfo,feature,FALSE);
  2476. retryCount = 0;
  2477. } else {
  2478. mtk_nand_rrtry_func(devinfo,feature,TRUE);
  2479. readRetry = FALSE;
  2480. g_sandisk_retry_case = 0;
  2481. }
  2482. }
  2483. if (g_sandisk_retry_case == 1) {
  2484. nand_set_command(0x26);
  2485. }
  2486. } else {
  2487. if (retryCount != 0) {
  2488. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  2489. mtk_nand_rrtry_func(devinfo,feature,TRUE);
  2490. }
  2491. readRetry = FALSE;
  2492. g_sandisk_retry_case = 0;
  2493. }
  2494. if (TRUE == readRetry)
  2495. bRet = ERR_RTN_SUCCESS;
  2496. } while (readRetry);
  2497. if (use_randomizer)
  2498. mtk_nand_turn_off_randomizer();
  2499. if (retryCount != 0) {
  2500. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  2501. if (bRet == ERR_RTN_SUCCESS) {
  2502. MSG(INIT, "u4RowAddr:0x%x read retry pass, retrycnt:%d ENUM0:%x,ENUM1:%x \n",u4RowAddr,retryCount,DRV_Reg32(ECC_DECENUM1_REG32),DRV_Reg32(ECC_DECENUM0_REG32));
  2503. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX)) {
  2504. g_hynix_retry_count--;
  2505. }
  2506. } else {
  2507. MSG(INIT, "u4RowAddr:0x%x read retry fail\n",u4RowAddr);
  2508. }
  2509. mtk_nand_rrtry_func(devinfo,feature,TRUE);
  2510. g_sandisk_retry_case = 0;
  2511. }
  2512. return bRet;
  2513. }
  2514. static bool nand_exec_read_page(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2515. {
  2516. int bRet = ERR_RTN_SUCCESS;
  2517. // u32 page_per_block = (BLOCK_SIZE/nand->page_size);
  2518. u32 block;
  2519. //int page_in_block = u4RowAddr % page_per_block;
  2520. u32 page_addr;
  2521. u32 mapped_block;
  2522. int i, start, len, offset;
  2523. struct nand_oobfree *free;
  2524. u8 oob[0x80];
  2525. //mapped_block = get_mapping_block_index(block);
  2526. page_addr= mtk_nand_page_transform((u64)u4RowAddr << g_nand_chip.page_shift,&block,&mapped_block);
  2527. //bRet = nand_exec_read_page_hw(nand, (mapped_block * page_per_block + page_in_block), u4PageSize, pPageBuf, oob);
  2528. bRet = nand_exec_read_page_hw(nand, page_addr, u4PageSize, pPageBuf, oob);
  2529. if (bRet == ERR_RTN_FAIL)
  2530. return false;
  2531. offset = 0;
  2532. free = nand->ecclayout->oobfree;
  2533. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES&&free[i].length; i++) {
  2534. start = free[i].offset;
  2535. len = free[i].length;
  2536. memcpy(pFDMBuf + offset, oob + start, len);
  2537. offset += len;
  2538. }
  2539. return bRet;
  2540. }
  2541. bool nand_exec_write_page_hw(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2542. {
  2543. bool bRet = true;
  2544. //u32 page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  2545. // int block = u4RowAddr / page_per_block;
  2546. // int page_in_block = u4RowAddr % page_per_block;
  2547. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  2548. u32 page_addr;
  2549. u32 mapped_block;
  2550. #if defined(MTK_TLC_NAND_SUPPORT)
  2551. NFI_TLC_WL_INFO tlc_wl_info;
  2552. u32 reg_val;
  2553. #endif
  2554. u32 real_row_addr = 0;
  2555. mtk_nand_interface_switch();
  2556. // else
  2557. // mtk_nand_interface_async();
  2558. #if defined(MTK_TLC_NAND_SUPPORT)
  2559. nand_reset();
  2560. tlc_wl_info.word_line_idx = u4RowAddr;
  2561. tlc_wl_info.wl_pre = WL_LOW_PAGE;
  2562. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2563. if (devinfo.tlcControl.normaltlc) { //normal tlc
  2564. NFI_TLC_GetMappedWL(u4RowAddr, &tlc_wl_info);
  2565. real_row_addr = NFI_TLC_GetRowAddr(tlc_wl_info.word_line_idx);
  2566. if (devinfo.tlcControl.pPlaneEn) {
  2567. real_row_addr = NFI_TLC_SetpPlaneAddr(real_row_addr, tlc_lg_left_plane);
  2568. }
  2569. } else {
  2570. real_row_addr = NFI_TLC_GetRowAddr(u4RowAddr);
  2571. }
  2572. if (devinfo.tlcControl.slcopmodeEn) { // slc mode
  2573. if ((!devinfo.tlcControl.pPlaneEn) || tlc_lg_left_plane) {
  2574. if (0xFF != devinfo.tlcControl.en_slc_mode_cmd) {
  2575. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2576. reg_val &= ~CNFG_READ_EN;
  2577. reg_val &= ~CNFG_OP_MODE_MASK;
  2578. reg_val |= CNFG_OP_CUST;
  2579. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2580. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  2581. reg_val = DRV_Reg32(NFI_CON_REG32);
  2582. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2583. /* issue reset operation */
  2584. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2585. }
  2586. }
  2587. } else { //tlc mode
  2588. if (devinfo.tlcControl.normaltlc) {
  2589. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2590. reg_val &= ~CNFG_READ_EN;
  2591. reg_val &= ~CNFG_OP_MODE_MASK;
  2592. reg_val |= CNFG_OP_CUST;
  2593. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2594. if (PROGRAM_1ST_CYCLE == tlc_program_cycle) {
  2595. nand_set_command(PROGRAM_1ST_CYCLE_CMD);
  2596. } else if (PROGRAM_2ND_CYCLE == tlc_program_cycle) {
  2597. nand_set_command(PROGRAM_2ND_CYCLE_CMD);
  2598. }
  2599. if (tlc_wl_info.wl_pre == WL_LOW_PAGE)
  2600. nand_set_command(LOW_PG_SELECT_CMD);
  2601. else if (tlc_wl_info.wl_pre == WL_MID_PAGE)
  2602. nand_set_command(MID_PG_SELECT_CMD);
  2603. else if (tlc_wl_info.wl_pre == WL_HIGH_PAGE)
  2604. nand_set_command(HIGH_PG_SELECT_CMD);
  2605. reg_val = DRV_Reg32(NFI_CON_REG32);
  2606. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2607. /* issue reset operation */
  2608. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2609. }
  2610. }
  2611. } else
  2612. #endif
  2613. {
  2614. real_row_addr = u4RowAddr;
  2615. }
  2616. if (use_randomizer ) {
  2617. if (devinfo.tlcControl.slcopmodeEn)
  2618. mtk_nand_turn_on_randomizer(tlc_wl_info.word_line_idx);
  2619. else
  2620. mtk_nand_turn_on_randomizer(tlc_wl_info.word_line_idx*3+tlc_wl_info.wl_pre);
  2621. }
  2622. if (nand_ready_for_write(nand, real_row_addr, pPageBuf)) {
  2623. nand_write_fdm_data(pFDMBuf, u4SecNum);
  2624. if (!nand_write_page_data(pPageBuf, u4PageSize)) {
  2625. bRet = false;
  2626. }
  2627. if (!nand_check_RW_count(nand, u4PageSize)) {
  2628. bRet = false;
  2629. }
  2630. nand_stop_write();
  2631. #if defined(MTK_TLC_NAND_SUPPORT)
  2632. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2633. if (devinfo.tlcControl.normaltlc) { //normal tlc
  2634. if ((devinfo.tlcControl.pPlaneEn) && tlc_lg_left_plane) {
  2635. nand_set_command(PROGRAM_LEFT_PLANE_CMD);
  2636. } else {
  2637. if ((tlc_wl_info.wl_pre == WL_HIGH_PAGE) || devinfo.tlcControl.slcopmodeEn) {
  2638. nand_set_command(NAND_CMD_PAGE_PROG);
  2639. } else {
  2640. nand_set_command(PROGRAM_RIGHT_PLANE_CMD);
  2641. }
  2642. }
  2643. } else //micron tlc
  2644. nand_set_command(NAND_CMD_PAGE_PROG);
  2645. } else
  2646. #endif
  2647. nand_set_command(NAND_CMD_PAGE_PROG);
  2648. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  2649. bRet = mtk_nand_read_status();
  2650. #if defined(MTK_TLC_NAND_SUPPORT)
  2651. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2652. if ((devinfo.tlcControl.slcopmodeEn)
  2653. &&(0xFF != devinfo.tlcControl.dis_slc_mode_cmd)) {
  2654. reg_val = DRV_Reg32(NFI_CON_REG32);
  2655. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2656. /* issue reset operation */
  2657. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2658. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2659. reg_val &= ~CNFG_READ_EN;
  2660. reg_val &= ~CNFG_OP_MODE_MASK;
  2661. reg_val |= CNFG_OP_CUST;
  2662. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2663. nand_set_command(devinfo.tlcControl.dis_slc_mode_cmd);
  2664. }
  2665. }
  2666. #endif
  2667. if (use_randomizer)
  2668. mtk_nand_turn_off_randomizer();
  2669. }
  2670. return bRet;
  2671. }
  2672. static bool nand_exec_write_page_raw(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2673. {
  2674. bool bRet = true;
  2675. #if defined(MTK_TLC_NAND_SUPPORT)
  2676. u8 *temp_page_buf = NULL;
  2677. u8 *temp_fdm_buf = NULL;
  2678. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  2679. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  2680. if ((devinfo.tlcControl.normaltlc) && (devinfo.tlcControl.pPlaneEn)) { //normal tlc && pplane enable
  2681. tlc_lg_left_plane = TRUE; //program left plane
  2682. temp_page_buf = pPageBuf;
  2683. temp_fdm_buf = pFDMBuf;
  2684. bRet = nand_exec_write_page_hw(nand, u4RowAddr, u4PageSize / 2, temp_page_buf, temp_fdm_buf); //u4PageSize must be mtd->writesize
  2685. if (!bRet) { //operation fail
  2686. return bRet;
  2687. }
  2688. tlc_lg_left_plane = FALSE; //program right plane
  2689. temp_page_buf += (u4PageSize / 2);
  2690. temp_fdm_buf += ((u4SecNum / 2) * nand->nand_fdm_size);
  2691. bRet = nand_exec_write_page_hw(nand, u4RowAddr, u4PageSize / 2, temp_page_buf, temp_fdm_buf); //u4PageSize must be mtd->writesize
  2692. } else {
  2693. bRet = nand_exec_write_page_hw(nand, u4RowAddr, u4PageSize, pPageBuf, pFDMBuf);
  2694. }
  2695. } else
  2696. #endif
  2697. {
  2698. bRet = nand_exec_write_page_hw(nand, u4RowAddr, u4PageSize, pPageBuf, pFDMBuf);
  2699. }
  2700. return bRet;
  2701. }
  2702. static bool nand_exec_write_page(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2703. {
  2704. bool bRet = true;
  2705. // u32 page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  2706. u32 block;
  2707. // int page_in_block = u4RowAddr % page_per_block;
  2708. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  2709. u32 page_addr;
  2710. u32 mapped_block;
  2711. u32 page_per_block = devinfo.blocksize * 1024 / devinfo.pagesize;
  2712. mtk_nand_interface_switch();
  2713. page_addr= mtk_nand_page_transform((u64)u4RowAddr << g_nand_chip.page_shift,&block,&mapped_block);
  2714. #if defined(MTK_TLC_NAND_SUPPORT)
  2715. bRet = nand_exec_write_page_raw(nand, page_addr, u4PageSize, pPageBuf, pFDMBuf);
  2716. #else
  2717. if (use_randomizer)
  2718. mtk_nand_turn_on_randomizer(u4RowAddr);
  2719. // else
  2720. // mtk_nand_interface_async();
  2721. if (nand_ready_for_write(nand, page_addr, pPageBuf)) {
  2722. nand_write_fdm_data(pFDMBuf, u4SecNum);
  2723. if (!nand_write_page_data(pPageBuf, u4PageSize)) {
  2724. bRet = false;
  2725. }
  2726. if (!nand_check_RW_count(nand, u4PageSize)) {
  2727. bRet = false;
  2728. }
  2729. nand_stop_write();
  2730. nand_set_command(NAND_CMD_PAGE_PROG);
  2731. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  2732. }
  2733. if (use_randomizer)
  2734. mtk_nand_turn_off_randomizer();
  2735. #endif
  2736. return bRet;
  2737. }
  2738. static bool nand_read_oob_raw(struct nand_chip *chip, u32 page_addr, u32 length, u8 * buf)
  2739. {
  2740. u32 sector = 0;
  2741. u32 col_addr = 0;
  2742. u32 spare_per_sec = devinfo.sparesize>>(chip->page_shift-chip->sector_shift);
  2743. if (length > 32 || length % OOB_AVAIL_PER_SECTOR || !buf) {
  2744. dprintf(INFO,"[%s] invalid parameter, length: %d, buf: %p\n", __FUNCTION__, length, buf);
  2745. return false;
  2746. }
  2747. while (length > 0) {
  2748. col_addr = chip->sector_size+ sector * (chip->sector_size + spare_per_sec);
  2749. if (!nand_ready_for_read(chip, page_addr, col_addr, false, NULL))
  2750. return false;
  2751. if (!nand_mcu_read_data(buf, length))
  2752. return false;
  2753. NFI_CLN_REG32(NFI_CON_REG32, CON_NFI_BRD|CON_NFI_BWR);
  2754. sector++;
  2755. length -= OOB_AVAIL_PER_SECTOR;
  2756. }
  2757. return true;
  2758. }
  2759. #if defined(MTK_TLC_NAND_SUPPORT)
  2760. bool mtk_nand_slc_write_wodata(u32 page)
  2761. {
  2762. bool bRet = FALSE; //FALSE --> Pass TRUE-->Fail
  2763. bool slc_en;
  2764. u32 real_row_addr;
  2765. u32 reg_val;
  2766. NFI_TLC_WL_INFO tlc_wl_info;
  2767. #if CFG_2CS_NAND
  2768. if (g_bTricky_CS) {
  2769. page = mtk_nand_cs_on(NFI_TRICKY_CS, page);
  2770. }
  2771. #endif
  2772. NFI_TLC_GetMappedWL(page, &tlc_wl_info);
  2773. real_row_addr = NFI_TLC_GetRowAddr(tlc_wl_info.word_line_idx);
  2774. //set 0xA2 cmd
  2775. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2776. reg_val &= ~CNFG_READ_EN;
  2777. reg_val &= ~CNFG_OP_MODE_MASK;
  2778. reg_val |= CNFG_OP_CUST;
  2779. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2780. nand_set_command(0xA2);
  2781. reg_val = DRV_Reg32(NFI_CON_REG32);
  2782. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2783. /* issue reset operation */
  2784. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  2785. //set 0x80 cmd
  2786. nand_set_mode(CNFG_OP_PRGM);
  2787. nand_set_command(NAND_CMD_SEQIN);
  2788. //set address
  2789. nand_set_address(0, real_row_addr, 2, 3);
  2790. //set 0x10 cmd
  2791. nand_set_command(NAND_CMD_PAGE_PROG);
  2792. //read status
  2793. slc_en = devinfo.tlcControl.slcopmodeEn;
  2794. devinfo.tlcControl.slcopmodeEn = TRUE;//get i/O 2
  2795. bRet = !mtk_nand_read_status();
  2796. devinfo.tlcControl.slcopmodeEn = slc_en;
  2797. return bRet;
  2798. }
  2799. #endif
  2800. bool nand_block_bad_hw(struct nand_chip * nand, u64 offset)
  2801. {
  2802. u32 page_per_block = BLOCK_SIZE / nand->page_size;
  2803. u32 page_addr;
  2804. u32 block;
  2805. u32 mapped_block;
  2806. #if defined(MTK_TLC_NAND_SUPPORT)
  2807. bool bRet = FALSE;
  2808. #endif
  2809. //mapped_block = get_mapping_block_index(block);
  2810. page_addr = mtk_nand_page_transform(offset,&block,&mapped_block);
  2811. memset(oob_buf_temp, 0,LSPARE);
  2812. #if !defined(MTK_TLC_NAND_SUPPORT) //dummy code
  2813. page_addr &= ~(page_per_block - 1);
  2814. #endif
  2815. #if 0//#if defined(MTK_TLC_NAND_SUPPORT)
  2816. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  2817. && (devinfo.vendor == VEND_SANDISK)) {
  2818. bRet = mtk_nand_slc_write_wodata(page_addr);
  2819. return bRet;
  2820. }
  2821. #endif
  2822. #if defined(MTK_TLC_NAND_SUPPORT)
  2823. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  2824. && (devinfo.vendor == VEND_SANDISK)
  2825. && (mtk_nand_IsBMTPOOL(offset))) {
  2826. bRet = mtk_nand_slc_write_wodata(page_addr);
  2827. return bRet;
  2828. }
  2829. #endif
  2830. if (FALSE == nand_exec_read_page_hw(nand, page_addr, nand->page_size, data_buf_temp , oob_buf_temp)) {
  2831. }
  2832. if (oob_buf_temp[0] != 0xff) {
  2833. dprintf(INFO,"Bad block detect at block 0x%x, oob_buf[0] is %x\n", page_addr / page_per_block, oob_buf_temp[0]);
  2834. return true;
  2835. }
  2836. return false;
  2837. }
  2838. static bool nand_block_bad(struct nand_chip *nand, u32 page_addr)
  2839. {
  2840. //u32 page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  2841. //int block = page_addr / page_per_block;
  2842. //int mapped_block = get_mapping_block_index(block);
  2843. return nand_block_bad_hw(nand, (((u64)page_addr) << nand->page_shift));
  2844. }
  2845. //not support un-block-aligned write
  2846. static int nand_part_write(part_dev_t * dev, uchar * src, u64 dst, int size, int id)
  2847. {
  2848. _dprintf("%s\n", __func__);
  2849. struct nand_chip *nand = (struct nand_chip *)dev->blkdev;
  2850. u8 res = 0;
  2851. u32 u4PageSize = 1 << nand->page_shift;
  2852. u32 u4PageNumPerBlock = BLOCK_SIZE/nand->page_size/2;
  2853. u32 u4BlkEnd = (u32)(nand->chipsize / BLOCK_SIZE);
  2854. u32 u4BlkAddr = (u32)(dst / BLOCK_SIZE);
  2855. u32 u4ColAddr = dst & (u4PageSize - 1);
  2856. u32 u4RowAddr = dst / nand->page_size;
  2857. u32 u4EraseAddr;
  2858. u32 u4RowEnd;
  2859. u32 u4WriteLen = 0;
  2860. u32 i4Len;
  2861. bool ret;
  2862. // u32 mapped;
  2863. u32 k = 0;
  2864. //mtk_nand_page_transform((u64)dst,&u4BlkAddr,&mapped);
  2865. for (k = 0; k < sizeof(g_kCMD.au1OOB); k++)
  2866. *(g_kCMD.au1OOB + k) = 0xFF;
  2867. MSG(ERR, "dst 0x%llx\n", dst);
  2868. #if defined(MTK_TLC_NAND_SUPPORT)
  2869. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  2870. u4PageNumPerBlock = BLOCK_SIZE/nand->page_size/3;
  2871. #endif
  2872. while (((u32)size > u4WriteLen) && (u4BlkAddr < u4BlkEnd)) {
  2873. #if 1
  2874. if (!u4ColAddr) {
  2875. MSG(ERR, "Erase the block of 0x%08x\n", u4BlkAddr);
  2876. #if defined(MTK_TLC_NAND_SUPPORT)
  2877. __nand_erase((u64)u4RowAddr * nand->page_size);
  2878. #else
  2879. u4EraseAddr = u4BlkAddr * u4PageNumPerBlock *2;
  2880. nand_reset();
  2881. nand_set_mode(CNFG_OP_ERASE);
  2882. nand_set_command(NAND_CMD_ERASE_1);
  2883. nand_set_address(0, u4EraseAddr, 0, 3);
  2884. nand_set_command(NAND_CMD_ERASE_2);
  2885. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  2886. ret = mtk_nand_read_status();
  2887. #endif
  2888. }
  2889. #else
  2890. if (__nand_erase(dst)== FALSE) {
  2891. MSG(ERR, "erase fail");
  2892. mark_block_bad ((u64)dst);
  2893. }
  2894. #endif
  2895. // res = nand_block_bad(nand, ((u4BlkAddr >> 1) * u4PageNumPerBlock));
  2896. if (!res) {
  2897. #if defined(MTK_TLC_NAND_SUPPORT)
  2898. u4RowEnd = ((u4RowAddr + u4PageNumPerBlock) / u4PageNumPerBlock) * u4PageNumPerBlock;
  2899. #else
  2900. u4RowEnd = (u4RowAddr + u4PageNumPerBlock) & (~u4PageNumPerBlock + 1);
  2901. #endif
  2902. for (; u4RowAddr < u4RowEnd; u4RowAddr++) {
  2903. i4Len = min(size - u4WriteLen, u4PageSize - u4ColAddr);
  2904. if (0 >= i4Len) {
  2905. break;
  2906. }
  2907. if ((u4ColAddr == 0) && (i4Len == u4PageSize)) {
  2908. memcpy(data_buf_temp,(src + u4WriteLen),u4PageSize);
  2909. nand_exec_write_page(nand, u4RowAddr, u4PageSize, data_buf_temp, g_kCMD.au1OOB);
  2910. } else {
  2911. nand_exec_read_page(nand, u4RowAddr, u4PageSize, nand->buffers->databuf, g_kCMD.au1OOB);
  2912. memcpy(nand->buffers->databuf + u4ColAddr, src + u4WriteLen, i4Len);
  2913. nand_exec_write_page(nand, u4RowAddr, u4PageSize, nand->buffers->databuf, g_kCMD.au1OOB);
  2914. }
  2915. u4WriteLen += i4Len;
  2916. u4ColAddr = (u4ColAddr + i4Len) & (u4PageSize - 1);
  2917. }
  2918. } else {
  2919. dprintf(INFO,"Detect bad block at block 0x%x\n", u4BlkAddr);
  2920. u4RowAddr += u4PageNumPerBlock;
  2921. }
  2922. u4BlkAddr++;
  2923. }
  2924. return (int)u4WriteLen;
  2925. }
  2926. static int nand_part_read(part_dev_t * dev, u64 source, uchar * dst, int size, int id)
  2927. {
  2928. struct nand_chip *nand = (struct nand_chip *)dev->blkdev;
  2929. uint8_t res;
  2930. u32 u4PageSize = 1 << nand->page_shift;
  2931. u32 u4PageNumPerBlock = BLOCK_SIZE/nand->page_size;
  2932. u32 u4BlkEnd = (u32)(nand->chipsize / BLOCK_SIZE);
  2933. u32 u4BlkAddr = (u32)(source / BLOCK_SIZE);
  2934. u32 u4ColAddr = (u32)(source & (u4PageSize - 1));
  2935. u32 u4RowAddr = (u32)(source/nand->page_size);
  2936. u32 u4RowEnd;
  2937. // u32 mapped;
  2938. u32 u4ReadLen = 0;
  2939. u32 i4Len;
  2940. //mtk_nand_page_transform((u64)source,&u4BlkAddr,&mapped);
  2941. #if defined(MTK_TLC_NAND_SUPPORT)
  2942. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  2943. u4PageNumPerBlock = BLOCK_SIZE/nand->page_size/3;
  2944. #endif
  2945. while (((u32)size > u4ReadLen) && (u4BlkAddr < u4BlkEnd)) {
  2946. #if !defined(MTK_TLC_NAND_SUPPORT)
  2947. //bug & useless
  2948. res = nand_block_bad(nand, (u4BlkAddr * u4PageNumPerBlock));
  2949. #endif
  2950. if (!res) {
  2951. #if defined(MTK_TLC_NAND_SUPPORT)
  2952. u4RowEnd = ((u4RowAddr + u4PageNumPerBlock) / u4PageNumPerBlock) * u4PageNumPerBlock;
  2953. #else
  2954. u4RowEnd = (u4RowAddr + u4PageNumPerBlock) & (~u4PageNumPerBlock + 1);
  2955. #endif
  2956. for (; u4RowAddr < u4RowEnd; u4RowAddr++) {
  2957. i4Len = min(size - u4ReadLen, u4PageSize - u4ColAddr);
  2958. if (0 >= i4Len) {
  2959. break;
  2960. }
  2961. if ((u4ColAddr == 0) && (i4Len == u4PageSize)) {
  2962. nand_exec_read_page(nand, u4RowAddr, u4PageSize, dst + u4ReadLen, g_kCMD.au1OOB);
  2963. } else {
  2964. nand_exec_read_page(nand, u4RowAddr, u4PageSize, nand->buffers->databuf, g_kCMD.au1OOB);
  2965. memcpy(dst + u4ReadLen, nand->buffers->databuf + u4ColAddr, i4Len);
  2966. }
  2967. u4ReadLen += i4Len;
  2968. u4ColAddr = (u4ColAddr + i4Len) & (u4PageSize - 1);
  2969. }
  2970. } else {
  2971. dprintf(INFO,"Detect bad block at block 0x%x\n", u4BlkAddr);
  2972. u4RowAddr += u4PageNumPerBlock;
  2973. }
  2974. u4BlkAddr++;
  2975. }
  2976. return (int)u4ReadLen;
  2977. }
  2978. static void nand_command_bp(struct nand_chip *nand_chip, unsigned command, int column, int page_addr)
  2979. {
  2980. struct nand_chip *nand = nand_chip;
  2981. u32 timeout;
  2982. switch (command) {
  2983. case NAND_CMD_SEQIN:
  2984. if (g_kCMD.u4RowAddr != (u32)page_addr) {
  2985. memset(g_kCMD.au1OOB, 0xFF, sizeof(g_kCMD.au1OOB));
  2986. g_kCMD.pDataBuf = NULL;
  2987. }
  2988. g_kCMD.u4RowAddr = page_addr;
  2989. g_kCMD.u4ColAddr = column;
  2990. break;
  2991. case NAND_CMD_PAGE_PROG:
  2992. if (g_kCMD.pDataBuf || (0xFF != g_kCMD.au1OOB[0])) {
  2993. u8 *pDataBuf = g_kCMD.pDataBuf ? g_kCMD.pDataBuf : nand->buffers->databuf;
  2994. nand_exec_write_page(nand, g_kCMD.u4RowAddr, nand->writesize, pDataBuf, g_kCMD.au1OOB);
  2995. g_kCMD.u4RowAddr = (u32) - 1;
  2996. g_kCMD.u4OOBRowAddr = (u32) - 1;
  2997. }
  2998. break;
  2999. case NAND_CMD_READ_OOB:
  3000. g_kCMD.u4RowAddr = page_addr;
  3001. g_kCMD.u4ColAddr = column + nand->writesize;
  3002. g_i4ErrNum = 0;
  3003. break;
  3004. case NAND_CMD_READ_0:
  3005. g_kCMD.u4RowAddr = page_addr;
  3006. g_kCMD.u4ColAddr = column;
  3007. g_i4ErrNum = 0;
  3008. break;
  3009. case NAND_CMD_ERASE_1:
  3010. nand_reset();
  3011. nand_set_mode(CNFG_OP_ERASE);
  3012. nand_set_command(NAND_CMD_ERASE_1);
  3013. nand_set_address(0, page_addr, 0, devinfo.addr_cycle - 2);
  3014. break;
  3015. case NAND_CMD_ERASE_2:
  3016. nand_set_command(NAND_CMD_ERASE_2);
  3017. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  3018. break;
  3019. case NAND_CMD_STATUS:
  3020. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  3021. nand_reset();
  3022. nand_set_mode(CNFG_OP_SRD);
  3023. nand_set_command(NAND_CMD_STATUS);
  3024. NFI_CLN_REG32(NFI_CON_REG32, CON_NFI_NOB_MASK);
  3025. DRV_WriteReg32(NFI_CON_REG32, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  3026. break;
  3027. case NAND_CMD_RESET:
  3028. nand_reset();
  3029. break;
  3030. case NAND_CMD_READ_ID:
  3031. NFI_ISSUE_COMMAND(NAND_CMD_RESET, 0, 0, 0, 0);
  3032. timeout = TIMEOUT_4;
  3033. while (timeout) {
  3034. timeout--;
  3035. }
  3036. nand_reset();
  3037. /* Disable HW ECC */
  3038. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  3039. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  3040. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN | CNFG_BYTE_RW);
  3041. nand_set_mode(CNFG_OP_SRD);
  3042. nand_set_command(NAND_CMD_READ_ID);
  3043. nand_set_address(0, 0, 1, 0);
  3044. DRV_WriteReg32(NFI_CON_REG32, CON_NFI_SRD);
  3045. while (DRV_Reg32(NFI_STA_REG32) & STA_DATAR_STATE) ;
  3046. break;
  3047. default:
  3048. dprintf(INFO,"[ERR] nand_command_bp : unknow command %d\n", command);
  3049. break;
  3050. }
  3051. }
  3052. static u_char nand_read_byte(void)
  3053. {
  3054. /* Check the PIO bit is ready or not */
  3055. unsigned int timeout = TIMEOUT_4;
  3056. WAIT_NFI_PIO_READY(timeout);
  3057. return DRV_Reg8(NFI_DATAR_REG32);
  3058. }
  3059. #if 0
  3060. static void nand_read_buf(struct nand_chip *nand, u_char * buf, int len)
  3061. {
  3062. struct nand_chip *nand = nand;
  3063. struct CMD *pkCMD = &g_kCMD;
  3064. u32 u4ColAddr = pkCMD->u4ColAddr;
  3065. u32 u4PageSize = nand->writesize;
  3066. if (u4ColAddr < u4PageSize) {
  3067. if ((u4ColAddr == 0) && (len >= u4PageSize)) {
  3068. nand_exec_read_page(nand, pkCMD->u4RowAddr, u4PageSize, buf, pkCMD->au1OOB);
  3069. if (len > u4PageSize) {
  3070. u32 u4Size = min(len - u4PageSize,
  3071. sizeof(pkCMD->au1OOB));
  3072. memcpy(buf + u4PageSize, pkCMD->au1OOB, u4Size);
  3073. }
  3074. } else {
  3075. nand_exec_read_page(nand, pkCMD->u4RowAddr, u4PageSize, nand->buffers->databuf, pkCMD->au1OOB);
  3076. memcpy(buf, nand->buffers->databuf + u4ColAddr, len);
  3077. }
  3078. pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  3079. } else {
  3080. u32 u4Offset = u4ColAddr - u4PageSize;
  3081. u32 u4Size = min(len - u4PageSize - u4Offset, sizeof(pkCMD->au1OOB));
  3082. if (pkCMD->u4OOBRowAddr != pkCMD->u4RowAddr) {
  3083. nand_exec_read_page(nand, pkCMD->u4RowAddr, u4PageSize, nand->buffers->databuf, pkCMD->au1OOB);
  3084. pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  3085. }
  3086. memcpy(buf, pkCMD->au1OOB + u4Offset, u4Size);
  3087. }
  3088. pkCMD->u4ColAddr += len;
  3089. }
  3090. static void nand_write_buf(struct nand_chip nand, const u_char * buf, int len)
  3091. {
  3092. struct CMD *pkCMD = &g_kCMD;
  3093. u32 u4ColAddr = pkCMD->u4ColAddr;
  3094. u32 u4PageSize = nand->writesize;
  3095. u32 i;
  3096. if (u4ColAddr >= u4PageSize) {
  3097. u8 *pOOB = pkCMD->au1OOB;
  3098. u32 u4Size = min(len, sizeof(pkCMD->au1OOB));
  3099. for (i = 0; i < u4Size; i++) {
  3100. pOOB[i] &= buf[i];
  3101. }
  3102. } else {
  3103. pkCMD->pDataBuf = (u8 *) buf;
  3104. }
  3105. pkCMD->u4ColAddr += len;
  3106. }
  3107. #endif
  3108. void lk_nand_irq_handler(unsigned int irq)
  3109. {
  3110. u32 inte,sts;
  3111. mt_irq_ack(irq);
  3112. inte = DRV_Reg32(NFI_INTR_EN_REG32);
  3113. sts = DRV_Reg16(NFI_INTR_REG16);
  3114. //MSG(INT, "[lk_nand_irq_handler]irq %x enable:%x %x\n",irq,inte,sts);
  3115. if (sts & inte) {
  3116. // dprintf(INFO,"[lk_nand_irq_handler]send event,\n");
  3117. DRV_WriteReg32(NFI_INTR_EN_REG32, 0);
  3118. DRV_WriteReg16(NFI_INTR_REG16,sts);
  3119. event_signal(&nand_int_event,0);
  3120. }
  3121. return;
  3122. }
  3123. int nand_init_device(struct nand_chip *nand)
  3124. {
  3125. int index;//j, busw,;
  3126. u8 id[NAND_MAX_ID];
  3127. u32 spare_bit;
  3128. u32 spare_per_sec;
  3129. u32 ecc_bit;
  3130. int bmt_sz = 0;
  3131. memset(&devinfo, 0, sizeof(devinfo));
  3132. g_bInitDone = FALSE;
  3133. g_kCMD.u4OOBRowAddr = (u32) - 1;
  3134. #ifdef MACH_FPGA // FPGA NAND is placed at CS1
  3135. DRV_WriteReg16(NFI_CSEL_REG16, 0);
  3136. #else
  3137. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  3138. #endif
  3139. DRV_WriteReg32(NFI_ACCCON_REG32, NFI_DEFAULT_ACCESS_TIMING);
  3140. DRV_WriteReg16(NFI_CNFG_REG16, 0);
  3141. DRV_WriteReg32(NFI_PAGEFMT_REG32, 4);
  3142. nand->nand_ecc_mode = NAND_ECC_HW;
  3143. //reset to legacy for read id
  3144. nand_reset();
  3145. mtk_nand_set_async();
  3146. DRV_WriteReg32(NFI_ACCCON_REG32,0x31C083F9); //very safe timing
  3147. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  3148. DDR_INTERFACE = FALSE;
  3149. nand_reset();
  3150. DRV_WriteReg(NFI_CNFG_REG16, CNFG_OP_RESET);
  3151. nand_set_command(NAND_CMD_RESET);
  3152. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN));
  3153. nand_reset();
  3154. nand_command_bp(&g_nand_chip, NAND_CMD_READ_ID, 0, 0);
  3155. MSG(INFO, "NAND ID: ");
  3156. for (index = 0; index < NAND_MAX_ID; index++) {
  3157. id[index] = nand_read_byte();
  3158. MSG(INFO, " %x", id[index]);
  3159. }
  3160. MSG(INFO, "\n ");
  3161. if (!get_device_info(id, &devinfo)) {
  3162. MSG(ERR, "NAND unsupport\n");
  3163. return -1;
  3164. }
  3165. nand->name = devinfo.devciename;
  3166. #if defined(MTK_TLC_NAND_SUPPORT)
  3167. nand->chipsize = (u64)devinfo.totalsize << 10;
  3168. #else
  3169. nand->chipsize = (u64)devinfo.totalsize << 20;
  3170. #endif
  3171. if (devinfo.sectorsize == 512)
  3172. nand->erasesize = devinfo.blocksize << 10;
  3173. else
  3174. nand->erasesize = (devinfo.blocksize << 10)/2;
  3175. #if defined(MTK_TLC_NAND_SUPPORT)
  3176. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  3177. && (devinfo.tlcControl.normaltlc)) {
  3178. nand->erasesize = (devinfo.blocksize << 10)/3;
  3179. }
  3180. #endif
  3181. BLOCK_SIZE = devinfo.blocksize << 10;
  3182. PAGES_PER_BLOCK = BLOCK_SIZE / devinfo.pagesize;
  3183. nand->phys_erase_shift = uffs(nand->erasesize) - 1;
  3184. nand->page_size = devinfo.pagesize;
  3185. nand->writesize = devinfo.pagesize;
  3186. nand->page_shift = uffs(nand->page_size) - 1;
  3187. nand->oobblock = nand->page_size;
  3188. nand->bus16 = devinfo.iowidth;
  3189. nand->id_length = devinfo.id_length;
  3190. nand->sector_size = NAND_SECTOR_SIZE;
  3191. nand->sector_shift = 9;
  3192. nand->nand_fdm_size = 8;
  3193. if (devinfo.sectorsize == 1024) {
  3194. nand->sector_size = 1024;
  3195. nand->sector_shift = 10;
  3196. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_SECTOR_SEL);
  3197. }
  3198. for (index = 0; index < devinfo.id_length; index++) {
  3199. nand->id[index] = id[index];
  3200. }
  3201. #if 1
  3202. if (devinfo.vendor != VEND_NONE) {
  3203. if (devinfo.feature_set.FeatureSet.Async_timing.feature != 0xFF) {
  3204. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  3205. mtk_nand_SetFeature((u16) feature_set->sfeatureCmd, \
  3206. feature_set->Async_timing.address, (u8*)&feature_set->Async_timing.feature,\
  3207. sizeof(feature_set->Async_timing.feature));
  3208. }
  3209. }
  3210. #endif
  3211. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.timmingsetting);
  3212. spare_per_sec = devinfo.sparesize>>(nand->page_shift-nand->sector_shift);
  3213. switch (spare_per_sec) {
  3214. case 16:
  3215. spare_bit = PAGEFMT_SPARE_16;
  3216. ecc_bit = 4;
  3217. spare_per_sec = 16;
  3218. break;
  3219. case 26:
  3220. case 27:
  3221. case 28:
  3222. spare_bit = PAGEFMT_SPARE_26;
  3223. ecc_bit = 10;
  3224. spare_per_sec = 26;
  3225. break;
  3226. case 32:
  3227. ecc_bit = 12;
  3228. if (devinfo.sectorsize == 1024)
  3229. spare_bit = PAGEFMT_SPARE_32_1KS;
  3230. else
  3231. spare_bit = PAGEFMT_SPARE_32;
  3232. spare_per_sec = 32;
  3233. break;
  3234. case 40:
  3235. ecc_bit = 18;
  3236. spare_bit = PAGEFMT_SPARE_40;
  3237. spare_per_sec = 40;
  3238. break;
  3239. case 44:
  3240. ecc_bit = 20;
  3241. spare_bit = PAGEFMT_SPARE_44;
  3242. spare_per_sec = 44;
  3243. break;
  3244. case 48:
  3245. case 49:
  3246. ecc_bit = 22;
  3247. spare_bit = PAGEFMT_SPARE_48;
  3248. spare_per_sec = 48;
  3249. break;
  3250. case 50:
  3251. case 51:
  3252. ecc_bit = 24;
  3253. spare_bit = PAGEFMT_SPARE_50;
  3254. spare_per_sec = 50;
  3255. break;
  3256. case 52:
  3257. case 54:
  3258. case 56:
  3259. ecc_bit = 24;
  3260. if (devinfo.sectorsize == 1024)
  3261. spare_bit = PAGEFMT_SPARE_52_1KS;
  3262. else
  3263. spare_bit = PAGEFMT_SPARE_52;
  3264. spare_per_sec = 32;
  3265. break;
  3266. case 62:
  3267. case 63:
  3268. ecc_bit = 28;
  3269. spare_bit = PAGEFMT_SPARE_62;
  3270. spare_per_sec = 62;
  3271. break;
  3272. case 64:
  3273. ecc_bit = 32;
  3274. if (devinfo.sectorsize == 1024)
  3275. spare_bit = PAGEFMT_SPARE_64_1KS;
  3276. else
  3277. spare_bit = PAGEFMT_SPARE_64;
  3278. spare_per_sec = 64;
  3279. break;
  3280. case 72:
  3281. ecc_bit = 36;
  3282. if (devinfo.sectorsize == 1024)
  3283. spare_bit = PAGEFMT_SPARE_72_1KS;
  3284. spare_per_sec = 72;
  3285. break;
  3286. case 80:
  3287. ecc_bit = 40;
  3288. if (devinfo.sectorsize == 1024)
  3289. spare_bit = PAGEFMT_SPARE_80_1KS;
  3290. spare_per_sec = 80;
  3291. break;
  3292. case 88:
  3293. ecc_bit = 44;
  3294. if (devinfo.sectorsize == 1024)
  3295. spare_bit = PAGEFMT_SPARE_88_1KS;
  3296. spare_per_sec = 88;
  3297. break;
  3298. case 96:
  3299. case 98:
  3300. ecc_bit = 48;
  3301. if (devinfo.sectorsize == 1024)
  3302. spare_bit = PAGEFMT_SPARE_96_1KS;
  3303. spare_per_sec = 96;
  3304. break;
  3305. case 100:
  3306. case 102:
  3307. case 104:
  3308. ecc_bit = 52;
  3309. if (devinfo.sectorsize == 1024)
  3310. spare_bit = PAGEFMT_SPARE_100_1KS;
  3311. spare_per_sec = 100;
  3312. break;
  3313. case 122:
  3314. case 124:
  3315. case 126:
  3316. #if defined(MTK_TLC_NAND_SUPPORT)
  3317. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  3318. && devinfo.tlcControl.ecc_recalculate_en) {
  3319. if (60 < devinfo.tlcControl.ecc_required) { //68,72,80
  3320. g_nand_chip.nand_fdm_size = 3;//122 - 119;// 119 = 68*14/8
  3321. ecc_bit = 68;
  3322. } else {
  3323. ecc_bit = 60;
  3324. }
  3325. } else
  3326. #endif
  3327. ecc_bit = 60;
  3328. if (devinfo.sectorsize == 1024)
  3329. spare_bit = PAGEFMT_SPARE_122_1KS;
  3330. spare_per_sec = 122;
  3331. break;
  3332. case 128:
  3333. #if defined(MTK_TLC_NAND_SUPPORT)
  3334. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  3335. && devinfo.tlcControl.ecc_recalculate_en) {
  3336. if (68 < devinfo.tlcControl.ecc_required) { //72,80
  3337. g_nand_chip.nand_fdm_size = 2;//128 - 126;// 126 = 72*14/8
  3338. ecc_bit = 72;
  3339. } else {
  3340. ecc_bit = 68;
  3341. }
  3342. } else
  3343. #endif
  3344. ecc_bit = 68;
  3345. if (devinfo.sectorsize == 1024)
  3346. spare_bit = PAGEFMT_SPARE_128_1KS;
  3347. spare_per_sec = 128;
  3348. break;
  3349. #if defined(MTK_TLC_NAND_SUPPORT)
  3350. case 134:
  3351. ecc_bit = 72;
  3352. if (devinfo.sectorsize == 1024)
  3353. spare_bit = PAGEFMT_SPARE_134_1KS;
  3354. spare_per_sec = 134;
  3355. break;
  3356. case 148:
  3357. ecc_bit = 80;
  3358. if (devinfo.sectorsize == 1024)
  3359. spare_bit = PAGEFMT_SPARE_148_1KS;
  3360. spare_per_sec = 148;
  3361. break;
  3362. #endif
  3363. default:
  3364. dprintf(INFO,"[NAND]: NFI not support oobsize: %x\n", spare_per_sec);
  3365. while (1);
  3366. return -1;
  3367. }
  3368. devinfo.sparesize = spare_per_sec<<(nand->page_shift-nand->sector_shift);
  3369. MSG(INFO, "[NAND]nand eccbit %d , sparesize %d\n",ecc_bit,devinfo.sparesize);
  3370. if (!devinfo.sparesize) {
  3371. nand->oobsize = (8 << ((id[3] >> 2) & 0x01)) * (nand->oobblock / nand->sector_size); //FIX ME ,kai
  3372. } else {
  3373. nand->oobsize = devinfo.sparesize;
  3374. }
  3375. nand->buffers = &nBuf;//malloc(sizeof(struct nand_buffers));
  3376. if (nand->bus16 == IO_WIDTH_16) {
  3377. NFI_SET_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  3378. nand->options |= NAND_BUSWIDTH_16;
  3379. }
  3380. if (16384 == nand->oobblock) {
  3381. NFI_SET_REG32(NFI_PAGEFMT_REG32, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_16K_1KS);
  3382. nand->ecclayout = &nand_oob_128;
  3383. } else if (8192 == nand->oobblock) {
  3384. NFI_SET_REG32(NFI_PAGEFMT_REG32, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_8K_1KS);
  3385. nand->ecclayout = &nand_oob_128;
  3386. } else if (4096 == nand->oobblock) {
  3387. if (devinfo.sectorsize == 512)
  3388. NFI_SET_REG32(NFI_PAGEFMT_REG32, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K);
  3389. else
  3390. NFI_SET_REG32(NFI_PAGEFMT_REG32, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K_1KS);
  3391. nand->ecclayout = &nand_oob_128;
  3392. } else if (2048 == nand->oobblock) {
  3393. if (devinfo.sectorsize == 512)
  3394. NFI_SET_REG32(NFI_PAGEFMT_REG32, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K);
  3395. else
  3396. NFI_SET_REG32(NFI_PAGEFMT_REG32, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K_1KS);
  3397. nand->ecclayout = &nand_oob_64;
  3398. }
  3399. if (nand->nand_ecc_mode == NAND_ECC_HW) {
  3400. NFI_SET_REG32(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  3401. ECC_Config(ecc_bit);
  3402. nand_configure_fdm(g_nand_chip.nand_fdm_size);
  3403. }
  3404. DRV_Reg16(NFI_INTR_REG16);
  3405. DRV_WriteReg32(NFI_INTR_EN_REG32, 0);
  3406. if (en_interrupt) {
  3407. event_init(&nand_int_event,false,EVENT_FLAG_AUTOUNSIGNAL);
  3408. mt_irq_set_sens(MT_NFI_IRQ_ID, MT65xx_EDGE_SENSITIVE);
  3409. mt_irq_set_polarity(MT_NFI_IRQ_ID, MT65xx_POLARITY_LOW);
  3410. mt_irq_unmask(MT_NFI_IRQ_ID);
  3411. }
  3412. if (devinfo.vendor != VEND_NONE) {
  3413. mtk_nand_randomizer_config(&devinfo.feature_set.randConfig, 0);
  3414. DRV_WriteReg32(NFI_DLYCTRL_REG32, devinfo.dqs_delay_ctrl);
  3415. //DRV_WriteReg32(NFI_DLYCTRL_REG32, 0x00028021);
  3416. }
  3417. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX))
  3418. HYNIX_RR_TABLE_READ(&devinfo);
  3419. g_nand_size = nand->chipsize;
  3420. #ifdef MNTL_SUPPORT
  3421. bmt_sz = (int)(g_nand_size/BLOCK_SIZE/100);
  3422. #else
  3423. bmt_sz = (int)(g_nand_size/BLOCK_SIZE/100*6);
  3424. #endif
  3425. //if (id[0] == 0x45)
  3426. //{
  3427. // bmt_sz = bmt_sz * 2;
  3428. //}
  3429. nand->chipsize -= (bmt_sz * BLOCK_SIZE);
  3430. g_bInitDone = true;
  3431. #if defined(MTK_TLC_NAND_SUPPORT)
  3432. mtk_pmt_reset();
  3433. #endif
  3434. if (!g_bmt) {
  3435. if (!(g_bmt = init_bmt(nand, bmt_sz))) {
  3436. MSG(INIT, "Error: init bmt failed\n");
  3437. return -1;
  3438. }
  3439. }
  3440. return 0;
  3441. }
  3442. void nand_init(void)
  3443. {
  3444. static part_dev_t dev;
  3445. if (!nand_init_device(&g_nand_chip)) {
  3446. struct nand_chip *t_nand = &g_nand_chip;
  3447. dprintf(INFO,"NAND init done in LK\n");
  3448. total_size = t_nand->chipsize - BLOCK_SIZE * (PMT_POOL_SIZE);
  3449. dev.id = 0;
  3450. dev.init = 1;
  3451. dev.blkdev = (block_dev_desc_t *) t_nand;
  3452. dev.read = nand_part_read;
  3453. dev.write = nand_part_write;
  3454. dev.erase = nand_part_erase;
  3455. mt_part_register_device(&dev);
  3456. mtk_nand_ops_init();
  3457. dprintf(INFO,"NAND register done in LK\n");
  3458. return;
  3459. } else {
  3460. dprintf(INFO,"NAND init fail in LK\n");
  3461. }
  3462. }
  3463. #ifdef TLC_LK_UT
  3464. __attribute__((aligned(64))) static u8 temp_buffer_tlc[LPAGE + LSPARE];
  3465. __attribute__((aligned(64))) static u8 temp_buffer_tlc_rd[LPAGE + LSPARE];
  3466. int mtk_tlc_unit_test(struct nand_chip *nand)
  3467. {
  3468. printf("Begin to lk tlc unit test ... \n");
  3469. int err = 0;
  3470. int patternbuff[128] = {
  3471. 0x0103D901, 0xFF1802DF, 0x01200400, 0x00000021, 0x02040122, 0x02010122, 0x03020407, 0x1A050103,
  3472. 0x00020F1B, 0x08C0C0A1, 0x01550800, 0x201B0AC1, 0x41990155, 0x64F0FFFF, 0x201B0C82, 0x4118EA61,
  3473. 0xF00107F6, 0x0301EE1B, 0x0C834118, 0xEA617001, 0x07760301, 0xEE151405, 0x00202020, 0x20202020,
  3474. 0x00202020, 0x2000302E, 0x3000FF14, 0x00FF0000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3475. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3476. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3477. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3478. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3479. 0x01D90301, 0xDF0218FF, 0x00042001, 0x21000000, 0x22010402, 0x22010102, 0x07040203, 0x0301051A,
  3480. 0x1B0F0200, 0xA1C0C008, 0x00085501, 0xC10A1B20, 0x55019941, 0xFFFFF064, 0x820C1B20, 0x61EA1841,
  3481. 0xF60701F0, 0x1BEE0103, 0x1841830C, 0x017061EA, 0x01037607, 0x051415EE, 0x20202000, 0x20202020,
  3482. 0x20202000, 0x2E300020, 0x14FF0030, 0x0000FF00, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3483. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3484. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3485. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  3486. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
  3487. };
  3488. u32 j, k, p = (devinfo.blocksize << 10)/devinfo.pagesize, m;
  3489. u32 test_page;
  3490. u8* buf = (u8*) malloc(devinfo.blocksize << 10);
  3491. u32 count;
  3492. printf("[P] %d\n", p);
  3493. for (m=0; m<32; m++)
  3494. memcpy(temp_buffer_tlc+(512*m),(u8*)patternbuff, 512);
  3495. memset(temp_buffer_tlc + 16384, 0xFF, LSPARE);
  3496. memset(temp_buffer_tlc_rd + 16384, 0xFF, LSPARE);
  3497. printf("***************read pl***********************\n");
  3498. memset(temp_buffer_tlc_rd, 0xA5, 16384);
  3499. if (!nand_exec_read_page(nand, 1 * p / 3, devinfo.pagesize, temp_buffer_tlc_rd, (temp_buffer_tlc_rd + 16384)))
  3500. printf("Read page 0x%x fail!\n", 1 * p / 3);
  3501. for (m=0; m<32; m++)
  3502. printf("[5]0x%x %x %x %x\n", *((int *)temp_buffer_tlc_rd+m*4), *((int *)temp_buffer_tlc_rd+1+m*4), *((int *)temp_buffer_tlc_rd+2+m*4), *((int *)temp_buffer_tlc_rd+3+m*4));
  3503. //slc mode test: 2nd block of misc part
  3504. printf("***************SLC MODE TEST***********************\n");
  3505. test_page = 84 * p + (p / 3);
  3506. __nand_erase((u64)test_page * devinfo.pagesize);
  3507. for (k = 0; k < (p/3); k++) {
  3508. printf("***************w p %d***********************\n",test_page + k);
  3509. if (!nand_exec_write_page(nand, test_page + k, devinfo.pagesize, temp_buffer_tlc, (temp_buffer_tlc + 16384)))
  3510. printf("Write page 0x%x fail!\n", test_page + k);
  3511. printf("***************r p %d***********************\n",test_page + k);
  3512. memset(temp_buffer_tlc_rd, 0x00, devinfo.pagesize);
  3513. if (!nand_exec_read_page(nand, test_page + k, devinfo.pagesize, temp_buffer_tlc_rd, (temp_buffer_tlc_rd + 16384)))
  3514. printf("Read page 0x%x fail!\n", test_page + k);
  3515. if (memcmp(temp_buffer_tlc, temp_buffer_tlc_rd, devinfo.pagesize)) {
  3516. printf("compare fail!\n");
  3517. err = 1;
  3518. break;
  3519. } else {
  3520. printf("compare OK!\n");
  3521. }
  3522. }
  3523. //tlc mode test: block 888
  3524. printf("***************TLC MODE TEST***********************\n");
  3525. test_page = 888 * p;
  3526. __nand_erase((u64)test_page * devinfo.pagesize);
  3527. memset(buf, 0x00, (devinfo.blocksize << 10));
  3528. for (k = 0; k < p; k++) {
  3529. memcpy(buf + (devinfo.pagesize * k), temp_buffer_tlc, devinfo.pagesize);
  3530. }
  3531. printf("***************w b %d***********************\n", test_page);
  3532. mtk_nand_write_tlc_block(nand, buf, test_page);
  3533. for (k = 0; k < p; k++) {
  3534. printf("***************r p %d***********************\n",test_page + k);
  3535. memset(temp_buffer_tlc_rd, 0x00, devinfo.pagesize);
  3536. if (!nand_exec_read_page(nand, test_page + k, devinfo.pagesize, temp_buffer_tlc_rd, (temp_buffer_tlc_rd + 16384)))
  3537. printf("Read page 0x%x fail!\n", test_page + k);
  3538. if (memcmp(temp_buffer_tlc, temp_buffer_tlc_rd, devinfo.pagesize)) {
  3539. printf("compare fail!\n");
  3540. err = 2;
  3541. break;
  3542. } else {
  3543. printf("compare OK!\n");
  3544. }
  3545. }
  3546. //data retention test.
  3547. //slc test read cycle: 100K
  3548. //tlc test read cycle: 30K
  3549. #if 0
  3550. test_page = 84 * p + (p / 3);
  3551. for (count = 0; count < 100000; count++) {
  3552. for (k = 0; k < (p/3); k++) {
  3553. printf("***************[SLC READ]p %d cnt %d***********************\n",test_page + k, count);
  3554. memset(temp_buffer_tlc_rd, 0x00, devinfo.pagesize);
  3555. if (!nand_exec_read_page(nand, test_page + k, devinfo.pagesize, temp_buffer_tlc_rd, (temp_buffer_tlc_rd + 16384)))
  3556. printf("Read page 0x%x fail!\n", test_page + k);
  3557. if (memcmp(temp_buffer_tlc, temp_buffer_tlc_rd, devinfo.pagesize)) {
  3558. printf("compare fail!\n");
  3559. err = 1;
  3560. break;
  3561. } else {
  3562. printf("compare OK!\n");
  3563. }
  3564. }
  3565. }
  3566. test_page = 888 * p;
  3567. for (count = 0; count < 30000; count++) {
  3568. for (k = 0; k < p; k++) {
  3569. printf("***************[TLC READ]p %d cnt %d***********************\n",test_page + k, count);
  3570. memset(temp_buffer_tlc_rd, 0x00, devinfo.pagesize);
  3571. if (!nand_exec_read_page(nand, test_page + k, devinfo.pagesize, temp_buffer_tlc_rd, (temp_buffer_tlc_rd + 16384)))
  3572. printf("Read page 0x%x fail!\n", test_page + k);
  3573. if (memcmp(temp_buffer_tlc, temp_buffer_tlc_rd, devinfo.pagesize)) {
  3574. printf("compare fail!\n");
  3575. err = 2;
  3576. break;
  3577. } else {
  3578. printf("compare OK!\n");
  3579. }
  3580. }
  3581. }
  3582. #endif
  3583. free(buf);
  3584. return err;
  3585. }
  3586. #endif
  3587. void nand_driver_test(void)
  3588. {
  3589. #ifdef NAND_LK_TEST
  3590. u32 test_len=2048*1024;
  3591. long len;
  3592. int fail=0;
  3593. u32 index = 0;
  3594. part_dev_t *dev = mt_part_get_device();
  3595. part_t *part = mt_part_get_partition(PART_EXPDB);
  3596. unsigned long start_addr = part->startblk * BLK_SIZE;
  3597. u8 *original = malloc(test_len);
  3598. u8 *source = malloc(test_len);
  3599. u8 *readback = malloc(test_len);
  3600. for (index = 0; index < test_len; index++) {
  3601. source[index] = index % 16;
  3602. }
  3603. memset(original, 0x0a, test_len);
  3604. memset(readback, 0x0b, test_len);
  3605. MSG(ERR,"~~~~~~~~~nand driver test in lk~~~~~~~~~~~~~~\n");
  3606. // len = dev->read(dev, start_addr, (uchar *) original, test_len, 0);
  3607. // if (len != test_len)
  3608. // {
  3609. // MSG(ERR,"read original fail %d\n", len);
  3610. // }
  3611. // MSG(ERR,"oringinal data:");
  3612. // for (index = 0; index < 300; index++)
  3613. // {
  3614. // MSG(ERR," %x", original[index]);
  3615. // }
  3616. // MSG(ERR,"\n");
  3617. len = dev->write(dev, (uchar *) source, start_addr, test_len, 0);
  3618. if (len != test_len) {
  3619. MSG(ERR,"write source fail %d\n", len);
  3620. }
  3621. len = dev->read(dev, start_addr, (uchar *) readback,test_len, 0);
  3622. if (len != test_len) {
  3623. MSG(ERR,"read back fail %d\n", len);
  3624. }
  3625. MSG(ERR,"readback data:");
  3626. for (index = 0; index < 300; index++) {
  3627. MSG(ERR," %x", readback[index]);
  3628. }
  3629. MSG(ERR,"\n");
  3630. for (index = 0; index < test_len; index++) {
  3631. if (source[index] != readback[index]) {
  3632. MSG(ERR,"compare fail %d\n", index);
  3633. fail=1;
  3634. break;
  3635. }
  3636. }
  3637. if (fail==0) {
  3638. MSG(ERR,"compare success!\n");
  3639. }
  3640. len = dev->write(dev, (uchar *) original, start_addr, test_len,0);
  3641. if (len != test_len) {
  3642. MSG(ERR,"write back fail %d\n", len);
  3643. } else {
  3644. MSG(ERR,"recovery success\n");
  3645. }
  3646. memset(original,0xd,test_len);
  3647. len = dev->read(dev, start_addr, (uchar *) original, test_len,0);
  3648. if (len != test_len) {
  3649. MSG(ERR,"read original fail %d\n", len);
  3650. }
  3651. dprintf(INFO,"read back oringinal data:");
  3652. for (index = 0; index < 300; index++) {
  3653. MSG(ERR," %x", original[index]);
  3654. }
  3655. MSG(ERR,"\n");
  3656. MSG(ERR,"~~~~~~~~~nand driver test in lk~~~~~~~~~~~~~~\n");
  3657. free(original);
  3658. free(source);
  3659. free(readback);
  3660. #endif
  3661. #ifdef TLC_LK_UT
  3662. mtk_tlc_unit_test(&g_nand_chip);
  3663. #endif
  3664. }
  3665. /******************** ***/
  3666. /* support for fast boot */
  3667. /***********************/
  3668. int nand_erase(u64 offset, u64 size)
  3669. {
  3670. u64 img_size = size;
  3671. // u32 tpgsz;
  3672. u32 tblksz;
  3673. u64 cur_offset;
  3674. // u32 i = 0;
  3675. u32 index;
  3676. u32 block_size;
  3677. // do block alignment check
  3678. //printf ("[ERASE] offset = 0x%x\n", (u32)offset);
  3679. part_get_startaddress(offset, &index);
  3680. //printf ("[ERASE] index = %d\n", index);
  3681. if (raw_partition(index)) {
  3682. printf ("[ERASE] raw TRUE\n");
  3683. block_size = BLOCK_SIZE/2;
  3684. #if defined(MTK_TLC_NAND_SUPPORT)
  3685. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  3686. block_size = BLOCK_SIZE/3;
  3687. #endif
  3688. } else {
  3689. block_size = BLOCK_SIZE;
  3690. }
  3691. if ((u32)(offset % block_size) != 0) {
  3692. dprintf(INFO,"offset must be block alignment (0x%x)\n", block_size);
  3693. return -1;
  3694. }
  3695. // calculate block number of this image
  3696. if ((img_size % block_size) == 0) {
  3697. tblksz = img_size / block_size;
  3698. } else {
  3699. tblksz = (img_size / block_size) + 1;
  3700. }
  3701. printf ("[ERASE] image size = 0x%llx\n", img_size);
  3702. printf ("[ERASE] the number of nand block of this image = %d\n", tblksz);
  3703. // erase nand block
  3704. cur_offset = (u64)offset;
  3705. while (tblksz != 0) {
  3706. //printf ("[ERASE] cur_offset = 0x%llx\n", cur_offset);
  3707. if (__nand_erase(cur_offset) == FALSE) {
  3708. dprintf(INFO,"[ERASE] erase 0x%x fail\n",cur_offset);
  3709. mark_block_bad (cur_offset);
  3710. }
  3711. cur_offset += block_size;
  3712. tblksz--;
  3713. if (tblksz != 0 && cur_offset >= total_size) {
  3714. dprintf(INFO,"[ERASE] cur offset (0x%x) exceeds erase limit address (0x%x)\n", cur_offset, total_size);
  3715. return 0;
  3716. }
  3717. }
  3718. return 0;
  3719. }
  3720. bool __nand_erase (u64 logical_addr)
  3721. {
  3722. u32 block;
  3723. u32 mapped_block;
  3724. u64 addr;
  3725. mtk_nand_page_transform(logical_addr,&block,&mapped_block);
  3726. addr = (u64)(mapped_block);
  3727. addr = addr * BLOCK_SIZE;
  3728. //printf("addr 0x%llx, mapped_block 0x%x, BLOCK_SIZE 0x%x\n",addr, mapped_block,BLOCK_SIZE);
  3729. if (!nand_erase_hw(addr)) {
  3730. dprintf(INFO,"erase block 0x%x failed\n", mapped_block);
  3731. if (update_bmt((u64)mapped_block * BLOCK_SIZE, UPDATE_ERASE_FAIL, NULL, NULL)) {
  3732. dprintf(INFO,"erase block fail and update bmt sucess\n");
  3733. return TRUE;
  3734. } else {
  3735. dprintf(INFO,"erase block 0x%x failed but update bmt fail\n",mapped_block);
  3736. return FALSE;
  3737. }
  3738. }
  3739. return TRUE;
  3740. }
  3741. static int erase_fail_test = 0;
  3742. bool nand_erase_hw (u64 offset)
  3743. {
  3744. bool bRet = TRUE;
  3745. // u32 timeout, u4SecNum = g_nand_chip.oobblock >> g_nand_chip.sector_shift;
  3746. u32 rownob = devinfo.addr_cycle - 2;
  3747. u32 page_addr = (u32)(offset / g_nand_chip.oobblock);
  3748. #if defined(MTK_TLC_NAND_SUPPORT)
  3749. NFI_TLC_WL_INFO tlc_wl_info;
  3750. u32 reg_val = 0;
  3751. #endif
  3752. u32 real_row_addr = 0;
  3753. #if !defined(MTK_TLC_NAND_SUPPORT)
  3754. if (nand_block_bad_hw(&g_nand_chip,offset)) {
  3755. return FALSE;
  3756. }
  3757. #endif
  3758. if (erase_fail_test) {
  3759. erase_fail_test = 0;
  3760. return FALSE;
  3761. }
  3762. nand_reset ();
  3763. #if defined(MTK_TLC_NAND_SUPPORT)
  3764. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  3765. if (devinfo.tlcControl.normaltlc) { //normal tlc
  3766. NFI_TLC_GetMappedWL(page_addr, &tlc_wl_info);
  3767. real_row_addr = NFI_TLC_GetRowAddr(tlc_wl_info.word_line_idx);
  3768. } else {
  3769. real_row_addr = NFI_TLC_GetRowAddr(page_addr);
  3770. }
  3771. } else
  3772. #endif
  3773. {
  3774. real_row_addr = page_addr;
  3775. }
  3776. #if defined(MTK_TLC_NAND_SUPPORT)
  3777. if (devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC) {
  3778. if ((devinfo.tlcControl.slcopmodeEn)
  3779. && (0xFF != devinfo.tlcControl.en_slc_mode_cmd)) {
  3780. reg_val = DRV_Reg(NFI_CNFG_REG16);
  3781. reg_val &= ~CNFG_READ_EN;
  3782. reg_val &= ~CNFG_OP_MODE_MASK;
  3783. reg_val |= CNFG_OP_CUST;
  3784. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  3785. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  3786. reg_val = DRV_Reg32(NFI_CON_REG32);
  3787. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  3788. /* issue reset operation */
  3789. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  3790. } else {
  3791. if (tlc_not_keep_erase_lvl) {
  3792. reg_val = DRV_Reg(NFI_CNFG_REG16);
  3793. reg_val &= ~CNFG_READ_EN;
  3794. reg_val &= ~CNFG_OP_MODE_MASK;
  3795. reg_val |= CNFG_OP_CUST;
  3796. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  3797. nand_set_command(NOT_KEEP_ERASE_LVL_A19NM_CMD);
  3798. reg_val = DRV_Reg32(NFI_CON_REG32);
  3799. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  3800. /* issue reset operation */
  3801. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  3802. }
  3803. }
  3804. }
  3805. #endif
  3806. nand_set_mode (CNFG_OP_ERASE);
  3807. nand_set_command (NAND_CMD_ERASE_1);
  3808. nand_set_address (0, real_row_addr, 0, rownob);
  3809. nand_set_command (NAND_CMD_ERASE_2);
  3810. if (!nand_status_ready(STA_NAND_BUSY)) {
  3811. return FALSE;
  3812. }
  3813. bRet = mtk_nand_read_status();
  3814. #if defined(MTK_TLC_NAND_SUPPORT)
  3815. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  3816. && (devinfo.tlcControl.slcopmodeEn)) { //hynix tlc need doule check
  3817. if (0xFF != devinfo.tlcControl.dis_slc_mode_cmd) {
  3818. reg_val = DRV_Reg32(NFI_CON_REG32);
  3819. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  3820. /* issue reset operation */
  3821. DRV_WriteReg32(NFI_CON_REG32, reg_val);
  3822. reg_val = DRV_Reg(NFI_CNFG_REG16);
  3823. reg_val &= ~CNFG_READ_EN;
  3824. reg_val &= ~CNFG_OP_MODE_MASK;
  3825. reg_val |= CNFG_OP_CUST;
  3826. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  3827. nand_set_command(devinfo.tlcControl.dis_slc_mode_cmd);
  3828. }
  3829. }
  3830. #endif
  3831. return bRet;
  3832. }
  3833. bool mark_block_bad_hw(u64 offset)
  3834. {
  3835. u32 index;
  3836. //unsigned char buf[4096];
  3837. unsigned char *buf = g_data_buf;
  3838. //unsigned char spare_buf[64];
  3839. unsigned char *spare_buf = g_spare_buf;
  3840. u32 page_addr = (u32)(offset / g_nand_chip.oobblock);
  3841. u32 u4SecNum = g_nand_chip.oobblock >> g_nand_chip.sector_shift;
  3842. u32 i, page_num = (BLOCK_SIZE / g_nand_chip.oobblock);
  3843. memset(buf,0x00,LPAGE);
  3844. for (index = 0; index < 64; index++)
  3845. *(spare_buf + index) = 0xFF;
  3846. for (index = 8, i = 0; i < u4SecNum; i++)
  3847. spare_buf[i * index] = 0x0;
  3848. #if !defined(MTK_TLC_NAND_SUPPORT) //dummy code
  3849. page_addr &= ~(page_num - 1);
  3850. #endif
  3851. MSG (INIT, "Mark bad block at 0x%x\n", page_addr);
  3852. while (DRV_Reg32 (NFI_STA_REG32) & STA_NAND_BUSY);
  3853. return nand_exec_write_page_raw(&g_nand_chip, page_addr, g_nand_chip.oobblock, (u8 *)buf,(u8 *)spare_buf);
  3854. }
  3855. int nand_part_erase(int dev_num, u64 offset, u64 size, int part_id)
  3856. {
  3857. return nand_erase(offset, size);
  3858. }
  3859. bool mark_block_bad (u64 logical_addr)
  3860. {
  3861. //u32 block;
  3862. //u32 mapped_block;
  3863. //mtk_nand_page_transform(logical_addr,&block,&mapped_block);
  3864. return mark_block_bad_hw((u64)logical_addr);
  3865. }
  3866. int nand_write_page_hw(u32 page, u8 *dat, u8 *oob)
  3867. {
  3868. // u32 pagesz = g_nand_chip.oobblock;
  3869. // u32 u4SecNum = pagesz >> g_nand_chip.sector_shift;
  3870. int i, j, start, len;
  3871. bool empty = TRUE;
  3872. u8 oob_checksum = 0;
  3873. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && g_nand_chip.ecclayout->oobfree[i].length; i++) {
  3874. /* Set the reserved bytes to 0xff */
  3875. start = g_nand_chip.ecclayout->oobfree[i].offset;
  3876. len = g_nand_chip.ecclayout->oobfree[i].length;
  3877. for (j = 0; j < len; j++) {
  3878. oob_checksum ^= oob[start + j];
  3879. if (oob[start + j] != 0xFF)
  3880. empty = FALSE;
  3881. }
  3882. }
  3883. if (!empty) {
  3884. oob[g_nand_chip.ecclayout->oobfree[i-1].offset + g_nand_chip.ecclayout->oobfree[i-1].length] = oob_checksum;
  3885. }
  3886. while (DRV_Reg32 (NFI_STA_REG32) & STA_NAND_BUSY);
  3887. return nand_exec_write_page_raw(&g_nand_chip, page, g_nand_chip.oobblock, (u8 *)dat,(u8 *)oob);
  3888. }
  3889. int nand_write_page_hwecc (u64 logical_addr, char *buf, char *oob_buf)
  3890. {
  3891. // u32 page_size = g_nand_chip.oobblock;
  3892. // u32 block_size = BLOCK_SIZE;
  3893. u32 block;
  3894. u32 mapped_block;
  3895. // u32 pages_per_blk = (block_size/page_size);
  3896. u32 page_no;
  3897. //u32 page_in_block = (logical_addr/page_size)%pages_per_blk;
  3898. u32 i;
  3899. int start, len, offset;
  3900. page_no = mtk_nand_page_transform(logical_addr,&block,&mapped_block);
  3901. for (i = 0; i < sizeof(g_spare_buf); i++)
  3902. *(g_spare_buf + i) = 0xFF;
  3903. offset = 0;
  3904. if (oob_buf != NULL) {
  3905. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && g_nand_chip.ecclayout->oobfree[i].length; i++) {
  3906. /* Set the reserved bytes to 0xff */
  3907. start = g_nand_chip.ecclayout->oobfree[i].offset;
  3908. len = g_nand_chip.ecclayout->oobfree[i].length;
  3909. memcpy ((g_spare_buf + start), (oob_buf + offset), len);
  3910. offset += len;
  3911. }
  3912. }
  3913. // write bad index into oob
  3914. if (mapped_block != block) {
  3915. // MSG(INIT, "page: 0x%x\n", page_in_block);
  3916. set_bad_index_to_oob(g_spare_buf, block);
  3917. } else {
  3918. set_bad_index_to_oob(g_spare_buf, FAKE_INDEX);
  3919. }
  3920. if (!nand_write_page_hw(page_no,(u8*)buf, g_spare_buf)) {
  3921. MSG(INIT, "write fail happened @ block 0x%x, page 0x%x\n", mapped_block, page_no);
  3922. return update_bmt( (u64)page_no * g_nand_chip.oobblock,
  3923. UPDATE_WRITE_FAIL, (u8*)buf, g_spare_buf);
  3924. }
  3925. return TRUE;
  3926. }
  3927. #if defined(MTK_TLC_NAND_SUPPORT)
  3928. bool mtk_nand_write_tlc_wl(struct nand_chip *chip,
  3929. uint8_t *buf, u32 wl, NFI_TLC_PG_CYCLE program_cycle)
  3930. {
  3931. int page_per_block = devinfo.blocksize * 1024 / devinfo.pagesize;
  3932. u32 block;
  3933. u32 page_in_block;
  3934. u32 mapped_block;
  3935. u32 page;
  3936. uint8_t *temp_buf = NULL;
  3937. devinfo.tlcControl.slcopmodeEn = FALSE;//tlc mode program
  3938. tlc_program_cycle = program_cycle;
  3939. page = wl * 3;
  3940. //buf may be virtual address
  3941. temp_buf = buf;
  3942. memcpy(local_tlc_wl_buffer, temp_buf, devinfo.pagesize);
  3943. if (!(nand_exec_write_page_raw(chip, page, devinfo.pagesize, local_tlc_wl_buffer, g_spare_buf))) {
  3944. MSG(INIT, "write fail at wl: 0x%x, page: 0x%x\n", wl, page);
  3945. return FALSE;
  3946. }
  3947. temp_buf += devinfo.pagesize;
  3948. memcpy(local_tlc_wl_buffer, temp_buf, devinfo.pagesize);
  3949. if (!(nand_exec_write_page_raw(chip, page + 1, devinfo.pagesize, local_tlc_wl_buffer, g_spare_buf))) {
  3950. MSG(INIT, "write fail at wl: 0x%x, page: 0x%x\n", wl, page);
  3951. return FALSE;
  3952. }
  3953. temp_buf += devinfo.pagesize;
  3954. memcpy(local_tlc_wl_buffer, temp_buf, devinfo.pagesize);
  3955. if (!(nand_exec_write_page_raw(chip, page + 2, devinfo.pagesize, local_tlc_wl_buffer, g_spare_buf))) {
  3956. MSG(INIT, "write fail at wl: 0x%x, page: 0x%x\n", wl, page);
  3957. return FALSE;
  3958. }
  3959. return TRUE;
  3960. }
  3961. bool mtk_nand_write_tlc_block_hw(struct nand_chip *chip,
  3962. uint8_t *buf, u32 mapped_block)
  3963. {
  3964. int page_per_block = devinfo.blocksize * 1024 / devinfo.pagesize;
  3965. u32 index;
  3966. bool bRet = TRUE;
  3967. u32 base_wl_index;
  3968. u8 *temp_buf = NULL;
  3969. base_wl_index = mapped_block * page_per_block / 3;
  3970. for (index = 0; index < (page_per_block / 3); index++) {
  3971. if (index == 0) {
  3972. temp_buf = buf + (index * 3 * devinfo.pagesize);
  3973. bRet = mtk_nand_write_tlc_wl(chip, temp_buf, base_wl_index + index, PROGRAM_1ST_CYCLE);
  3974. if (!bRet)
  3975. break;
  3976. temp_buf = buf + ((index + 1) * 3 * devinfo.pagesize);
  3977. bRet = mtk_nand_write_tlc_wl(chip, temp_buf, base_wl_index + index + 1, PROGRAM_1ST_CYCLE);
  3978. if (!bRet)
  3979. break;
  3980. temp_buf = buf + (index * 3 * devinfo.pagesize);
  3981. bRet = mtk_nand_write_tlc_wl(chip, temp_buf, base_wl_index + index, PROGRAM_2ND_CYCLE);
  3982. if (!bRet)
  3983. break;
  3984. }
  3985. if ((index + 2) < (page_per_block / 3)) {
  3986. temp_buf = buf + ((index + 2) * 3 * devinfo.pagesize);
  3987. bRet = mtk_nand_write_tlc_wl(chip, temp_buf, base_wl_index + index + 2, PROGRAM_1ST_CYCLE);
  3988. if (!bRet)
  3989. break;
  3990. }
  3991. if ((index + 1) < (page_per_block / 3)) {
  3992. temp_buf = buf + ((index + 1) * 3 * devinfo.pagesize);
  3993. bRet = mtk_nand_write_tlc_wl(chip, temp_buf, base_wl_index + index + 1, PROGRAM_2ND_CYCLE);
  3994. if (!bRet)
  3995. break;
  3996. }
  3997. temp_buf = buf + (index * 3 * devinfo.pagesize);
  3998. bRet = mtk_nand_write_tlc_wl(chip, temp_buf, base_wl_index + index, PROGRAM_3RD_CYCLE);
  3999. if (!bRet)
  4000. break;
  4001. }
  4002. //printk("[xiaolei] mtk_nand_write_page 0x%x\n", (u32)buf);
  4003. return bRet;
  4004. }
  4005. bool mtk_nand_write_tlc_block(struct nand_chip *chip,
  4006. uint8_t *buf,u32 page)
  4007. {
  4008. int page_per_block = devinfo.blocksize * 1024 / devinfo.pagesize;
  4009. u32 block;
  4010. u32 page_in_block;
  4011. u32 mapped_block;
  4012. u32 index;
  4013. bool bRet = TRUE;
  4014. u32 base_wl_index;
  4015. u8 *temp_buf = NULL;
  4016. if (devinfo.NAND_FLASH_TYPE != NAND_FLASH_TLC) {
  4017. MSG(INIT, "error : not tlc nand\n");
  4018. return FALSE;
  4019. }
  4020. if (!devinfo.tlcControl.normaltlc) {
  4021. MSG(INIT, "error : not normal tlc nand\n");
  4022. return FALSE;
  4023. }
  4024. page_in_block = mtk_nand_page_transform((u64)page * devinfo.pagesize,&block,&mapped_block);
  4025. if (page_in_block%(devinfo.blocksize * 1024 / devinfo.pagesize) != 0) {
  4026. MSG(INIT, "error : normal tlc block program is not block aligned %x\n",page_in_block%(devinfo.blocksize * 1024 / devinfo.pagesize));
  4027. return FALSE;
  4028. }
  4029. //MSG(INIT,"[WRITE] %d, %d, %d %d\n",mapped_block, block, page_in_block, page_per_block);
  4030. memset(g_spare_buf, 0xff, sizeof(g_spare_buf));
  4031. // write bad index into oob
  4032. if (mapped_block != block) {
  4033. set_bad_index_to_oob(g_spare_buf, block);
  4034. } else {
  4035. set_bad_index_to_oob(g_spare_buf, FAKE_INDEX);
  4036. }
  4037. bRet = mtk_nand_write_tlc_block_hw(chip, buf, mapped_block);
  4038. //printk("[xiaolei] mtk_nand_write_page 0x%x\n", (u32)buf);
  4039. if (!bRet) {
  4040. MSG(INIT, "write fail at block: 0x%x, page: 0x%x\n", mapped_block, page_in_block);
  4041. if (update_bmt((u64)((u64)page_in_block + (u64)mapped_block * page_per_block) << chip->page_shift, UPDATE_WRITE_FAIL, (u8 *) buf, g_spare_buf)) {
  4042. MSG(INIT, "Update BMT success\n");
  4043. return TRUE;
  4044. } else {
  4045. MSG(INIT, "Update BMT fail\n");
  4046. return FALSE;
  4047. }
  4048. }
  4049. return TRUE;
  4050. }
  4051. #endif
  4052. int nand_get_alignment(void)
  4053. {
  4054. return BLOCK_SIZE;
  4055. }
  4056. int nand_img_read(u64 source, uchar * dst, int size)
  4057. {
  4058. uint8_t res;
  4059. u32 u4PageSize = 1 << g_nand_chip.page_shift;
  4060. u32 u4PageNumPerBlock = BLOCK_SIZE/g_nand_chip.page_size;
  4061. u32 u4BlkEnd = (u32)(g_nand_chip.chipsize / BLOCK_SIZE);
  4062. u32 u4BlkAddr = (u32)(source / BLOCK_SIZE);
  4063. u32 u4ColAddr = (u32)(source & (u4PageSize - 1));
  4064. u32 u4RowAddr = (u32)(source/g_nand_chip.page_size);
  4065. u32 u4RowEnd;
  4066. // u32 mapped;
  4067. u32 u4ReadLen = 0;
  4068. u32 i4Len;
  4069. //mtk_nand_page_transform((u64)source,&u4BlkAddr,&mapped);
  4070. while (((u32)size > u4ReadLen) && (u4BlkAddr < u4BlkEnd)) {
  4071. res = nand_block_bad(&g_nand_chip, (u4BlkAddr * u4PageNumPerBlock));
  4072. if (!res) {
  4073. u4RowEnd = (u4RowAddr + u4PageNumPerBlock) & (~u4PageNumPerBlock + 1);
  4074. for (; u4RowAddr < u4RowEnd; u4RowAddr++) {
  4075. i4Len = min(size - u4ReadLen, u4PageSize - u4ColAddr);
  4076. if (0 >= i4Len) {
  4077. break;
  4078. }
  4079. if ((u4ColAddr == 0) && (i4Len == u4PageSize)) {
  4080. nand_exec_read_page(&g_nand_chip, u4RowAddr, u4PageSize, dst + u4ReadLen, g_kCMD.au1OOB);
  4081. } else {
  4082. nand_exec_read_page(&g_nand_chip, u4RowAddr, u4PageSize, g_nand_chip.buffers->databuf, g_kCMD.au1OOB);
  4083. memcpy(dst + u4ReadLen, g_nand_chip.buffers->databuf + u4ColAddr, i4Len);
  4084. }
  4085. u4ReadLen += i4Len;
  4086. u4ColAddr = (u4ColAddr + i4Len) & (u4PageSize - 1);
  4087. }
  4088. } else {
  4089. dprintf(INFO,"Detect bad block at block 0x%x\n", u4BlkAddr);
  4090. u4RowAddr += u4PageNumPerBlock;
  4091. }
  4092. u4BlkAddr++;
  4093. }
  4094. return (int)u4ReadLen;
  4095. }
  4096. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  4097. int nand_write_img(u64 addr, void *data, u32 img_sz,u64 partition_size,int img_type)
  4098. #else
  4099. int nand_write_img(u32 addr, void *data, u32 img_sz,u32 partition_size,int img_type)
  4100. #endif
  4101. {
  4102. unsigned int page_size = g_nand_chip.oobblock;
  4103. unsigned int img_spare_size = 64;
  4104. unsigned int write_size;
  4105. unsigned int block_size = BLOCK_SIZE;
  4106. u64 partition_start;
  4107. u64 partition_end;
  4108. bool ret;
  4109. u32 index;
  4110. bool tlc_block = FALSE;
  4111. unsigned int b_lastpage = 0;
  4112. printf("[nand_wite_img]write to img size, %x addr %llx img_type %d\n",img_sz,addr, img_type);
  4113. partition_start = part_get_startaddress((u64)addr, &index);
  4114. partition_end = partition_size + partition_start;
  4115. tlc_block = mtk_block_istlc(addr);
  4116. if (tlc_block) {
  4117. block_size = BLOCK_SIZE;
  4118. } else {
  4119. block_size = BLOCK_SIZE/3;
  4120. }
  4121. if (addr % block_size) {
  4122. dprintf(INFO,"[nand_write_img]partition address or partition size is not block size alignment %llx %x\n",partition_size,block_size);
  4123. return -1;
  4124. }
  4125. if (img_sz > partition_size) {
  4126. dprintf(INFO,"[nand_write_img]img size %x exceed partition size\n",img_sz);
  4127. return -1;
  4128. }
  4129. if (page_size == 16384) {
  4130. img_spare_size = 512;
  4131. } else if (page_size == 8192) {
  4132. img_spare_size = 256;
  4133. } else if (page_size == 4096) {
  4134. img_spare_size = 128;
  4135. } else if (page_size == 2048) {
  4136. img_spare_size = 64;
  4137. }
  4138. if (tlc_block)
  4139. write_size = block_size;
  4140. else
  4141. write_size = page_size;
  4142. printf ("[nand_write_img] index = %d write_size 0x%x block_size 0x%x\n", index, write_size,block_size);
  4143. while (img_sz>0) {
  4144. if ((addr+img_sz)>partition_end) {
  4145. dprintf(INFO,"[nand_wite_img]write to addr %x,img size %x exceed parition size,may be so many bad blocks\n",addr,img_sz);
  4146. return -1;
  4147. }
  4148. /*1. need to erase before write*/
  4149. if ((addr % block_size)==0) {
  4150. if (__nand_erase((u64)addr) == FALSE) {
  4151. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  4152. mark_block_bad ((u64)addr);
  4153. addr += block_size;
  4154. continue; //erase fail, skip this block
  4155. }
  4156. }
  4157. /*2. write page*/
  4158. if ((img_sz < write_size)) {
  4159. if (!tlc_block) {
  4160. b_lastpage = 1;
  4161. memset(g_data_buf,0xff,write_size);
  4162. memcpy(g_data_buf,data,img_sz);
  4163. if ((img_type == UBIFS_IMG)&& (check_data_empty((void *)g_data_buf,page_size))) {
  4164. dprintf(INFO,"[nand_write_img]skip empty page\n");
  4165. ret = true;
  4166. } else {
  4167. ret = nand_write_page_hwecc((u64)addr,(char*)g_data_buf,NULL);
  4168. }
  4169. }
  4170. } else {
  4171. if ((img_type == UBIFS_IMG)&& (check_data_empty((void *)data,page_size))) {
  4172. dprintf(INFO,"[nand_write_img]skip empty page\n");
  4173. ret = true;
  4174. } else {
  4175. if (tlc_block)
  4176. ret = mtk_nand_write_tlc_block(&g_nand_chip,data,addr>>g_nand_chip.page_shift);
  4177. else
  4178. ret = nand_write_page_hwecc((u64)addr,data,NULL);
  4179. }
  4180. }
  4181. if (ret == FALSE) {
  4182. printf("[nand_write_img]write fail at 0x%llx\n",addr);
  4183. if (__nand_erase((u64)addr) == FALSE) {
  4184. printf("[ERASE] erase 0x%llx fail\n",addr);
  4185. mark_block_bad ((u64)addr);
  4186. }
  4187. data -= ((addr%block_size)/page_size)*write_size;
  4188. img_sz += ((addr%block_size)/page_size)*write_size;
  4189. addr += block_size;
  4190. continue; // write fail, try to write the next block
  4191. }
  4192. if (b_lastpage) {
  4193. data += img_sz;
  4194. img_sz = 0 ;
  4195. addr += page_size;
  4196. } else {
  4197. data += write_size;
  4198. img_sz -= write_size;
  4199. addr += page_size;
  4200. }
  4201. }
  4202. /*3. erase any block remained in partition*/
  4203. addr = ((addr+block_size-1)/block_size)*block_size;
  4204. //nand_erase((u64)addr,(u64)(partition_end - addr)); //tlc cannot do this
  4205. return 0;
  4206. }
  4207. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  4208. int nand_write_img_ex(u64 addr, void *data, u32 length,u64 total_size, u32 *next_offset, u64 partition_start,u64 partition_size, int img_type)
  4209. #else
  4210. int nand_write_img_ex(u32 addr, void *data, u32 length,u32 total_size, u32 *next_offset, u32 partition_start,u32 partition_size, int img_type)
  4211. #endif
  4212. {
  4213. unsigned int page_size = g_nand_chip.oobblock;
  4214. unsigned int img_spare_size = 64;
  4215. unsigned int write_size;
  4216. unsigned int block_size = BLOCK_SIZE;
  4217. u64 partition_end = partition_start + partition_size;
  4218. // unsigned int first_chunk = 0;
  4219. unsigned int last_chunk = 0;
  4220. unsigned int left_size = 0;
  4221. bool ret;
  4222. bool tlc_block = FALSE;
  4223. u32 index;
  4224. u64 last_addr = (u64)addr;
  4225. u32 dst_block = 0;
  4226. part_get_startaddress((u64)addr, &index);
  4227. tlc_block = mtk_block_istlc(addr);
  4228. dprintf(INFO,"[nand_write_img_ex]write to img_type %d, addr %lx,img size %lx partition_start %lx\n",img_type,addr,length,partition_start);
  4229. if (tlc_block) {
  4230. block_size = BLOCK_SIZE;
  4231. } else {
  4232. block_size = BLOCK_SIZE/3;
  4233. }
  4234. if (partition_start % block_size || partition_size % block_size) {
  4235. dprintf(INFO,"[nand_write_img_ex]partition address or partition size is not block size alignment %lx,%lx\n",partition_start, partition_size);
  4236. return -1;
  4237. }
  4238. if (length > partition_size) {
  4239. dprintf(INFO,"[nand_write_img_ex]img size %x exceed partition size\n",length);
  4240. return -1;
  4241. }
  4242. if (page_size == 16384) {
  4243. img_spare_size = 512;
  4244. } else if (page_size == 8192) {
  4245. img_spare_size = 256;
  4246. } else if (page_size == 4096) {
  4247. img_spare_size = 128;
  4248. } else if (page_size == 2048) {
  4249. img_spare_size = 64;
  4250. }
  4251. if (last_addr % page_size) {
  4252. dprintf(INFO,"[nand_write_img_ex]write addr is not page_size %d alignment\n",page_size);
  4253. return -1;
  4254. }
  4255. if (tlc_block)
  4256. write_size = block_size;
  4257. else
  4258. write_size = page_size;
  4259. if (addr == partition_start) {
  4260. dprintf(INFO,"[nand_write_img_ex]first chunk\n");
  4261. download_size = 0;
  4262. memset(g_data_buf,0xff,write_size);
  4263. }
  4264. if ((length + download_size) >= total_size) {
  4265. dprintf(INFO,"[nand_write_img_ex]last chunk\n");
  4266. last_chunk = 1;
  4267. }
  4268. left_size = (download_size % write_size);
  4269. while (length>0) {
  4270. if ((addr+length)>partition_end) {
  4271. dprintf(INFO,"[nand_write_img_ex]write to addr %x,img size %x exceed parition size,may be so many bad blocks\n",addr,length);
  4272. return -1;
  4273. }
  4274. /*1. need to erase before write*/
  4275. if ((addr % block_size)==0) {
  4276. if (__nand_erase((u64)addr) == FALSE) {
  4277. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  4278. mark_block_bad ((u64)addr);
  4279. addr += block_size;
  4280. continue; //erase fail, skip this block
  4281. }
  4282. }
  4283. if (!tlc_block) {
  4284. if ((length < write_size)&&(!left_size)) {
  4285. memset(g_data_buf,0xff,write_size);
  4286. memcpy(g_data_buf,data,length);
  4287. if (!last_chunk) {
  4288. download_size += length;
  4289. break;
  4290. }
  4291. } else if (left_size) {
  4292. memcpy(&g_data_buf[left_size],data,write_size-left_size);
  4293. } else {
  4294. memcpy(g_data_buf,data,write_size);
  4295. }
  4296. }
  4297. /*2. write page*/
  4298. if ((img_type == UBIFS_IMG)&& (check_data_empty((void *)data,page_size))) {
  4299. dprintf(INFO,"[nand_write_img]skip empty page\n");
  4300. ret = true;
  4301. } else {
  4302. if (tlc_block)
  4303. ret = mtk_nand_write_tlc_block(&g_nand_chip,data,addr>>g_nand_chip.page_shift);
  4304. else
  4305. ret = nand_write_page_hwecc((u64)addr,data,NULL);
  4306. }
  4307. /*need to check?*/
  4308. if (ret == FALSE) {
  4309. dprintf(INFO,"[nand_write_img_ex]write fail at % 0x%x\n",addr);
  4310. while (1) {
  4311. dst_block = find_next_good_block((u64)addr/block_size);
  4312. if (dst_block == 0) {
  4313. dprintf(INFO,"[nand_write_img_ex]find next good block fail\n");
  4314. return -1;
  4315. }
  4316. ret = block_replace((u64)addr/block_size,dst_block,(u64)addr/page_size);
  4317. if (ret == FALSE) {
  4318. dprintf(INFO,"[nand_write_img_ex]block replace fail,continue\n");
  4319. continue;
  4320. } else {
  4321. dprintf(INFO,"[nand_write_img_ex]block replace sucess %x--> %x\n",addr/block_size,dst_block);
  4322. break;
  4323. }
  4324. }
  4325. addr = (addr%block_size) + (dst_block*block_size);
  4326. /* if (__nand_erase(addr) == FALSE)
  4327. {
  4328. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  4329. mark_block_bad (addr);
  4330. }
  4331. data -= ((addr%block_size)/page_size)*write_size;
  4332. length += ((addr%block_size)/page_size)*write_size;
  4333. addr += block_size;*/
  4334. continue; // write fail, try to write the next block
  4335. }
  4336. if (left_size) {
  4337. data += (write_size - left_size);
  4338. length -= (write_size - left_size);
  4339. addr += page_size;
  4340. download_size += (write_size - left_size);
  4341. left_size = 0;
  4342. } else {
  4343. data += write_size;
  4344. length -= write_size;
  4345. addr += page_size;
  4346. download_size += write_size;
  4347. }
  4348. }
  4349. *next_offset = addr - last_addr;
  4350. if (last_chunk) {
  4351. /*3. erase any block remained in partition*/
  4352. addr = ((addr+block_size-1)/block_size)*block_size;
  4353. nand_erase((u64)addr,(u64)(partition_end - addr));
  4354. }
  4355. return 0;
  4356. }
  4357. int check_data_empty(void *data, unsigned size)
  4358. {
  4359. unsigned int i;
  4360. u32 *tp = (u32 *)data;
  4361. for (i =0; i<size/4; i++) {
  4362. if (*(tp+i) != 0xffffffff) {
  4363. return 0;
  4364. }
  4365. }
  4366. return 1;
  4367. }
  4368. static u32 find_next_good_block(u32 start_block)
  4369. {
  4370. u32 i;
  4371. u32 dst_block = 0;
  4372. for (i=start_block; i<(total_size/BLOCK_SIZE); i++) {
  4373. if (!nand_block_bad(&g_nand_chip,i*(BLOCK_SIZE/g_nand_chip.page_size))) {
  4374. dst_block = i;
  4375. break;
  4376. }
  4377. }
  4378. return dst_block;
  4379. }
  4380. static bool block_replace(u32 src_block, u32 dst_block, u32 error_page)
  4381. {
  4382. bool ret;
  4383. u32 block_size = BLOCK_SIZE;
  4384. u32 page_size = g_nand_chip.page_size;
  4385. u32 i;
  4386. u8 *data_buf;
  4387. u8 *spare_buf;
  4388. ret = __nand_erase((u64)dst_block*block_size);
  4389. if (ret == FALSE) {
  4390. dprintf(INFO,"[block_replace]%x-->%x erase fail\n",src_block,dst_block);
  4391. mark_block_bad((u64)src_block*block_size);
  4392. return ret;
  4393. }
  4394. data_buf = (u8 *)malloc(LPAGE);
  4395. spare_buf = (u8 *)malloc(LSPARE);
  4396. if (!data_buf || !spare_buf) {
  4397. dprintf(INFO,"[block_replace]malloc mem fail\n");
  4398. return -1;
  4399. }
  4400. memset(data_buf,0xff,LPAGE);
  4401. memset(spare_buf,0xff,LSPARE);
  4402. for (i=0; i<error_page; i++) {
  4403. nand_exec_read_page(&g_nand_chip,src_block*(block_size/page_size) + i,page_size,data_buf,spare_buf);
  4404. ret = nand_write_page_hwecc((u64)dst_block*block_size + i*page_size,(char*)data_buf,(char*)spare_buf);
  4405. if (ret == FALSE)
  4406. mark_block_bad((u64)dst_block*block_size);
  4407. }
  4408. mark_block_bad((u64)src_block*block_size);
  4409. free(data_buf);
  4410. free(spare_buf);
  4411. return ret;
  4412. }
  4413. // Add for Get DL information
  4414. #define PRE_SCAN_BLOCK_NUM 20
  4415. /*Support to check format/download status, 2013/01/19 {*/
  4416. /* Max Number of Load Sections */
  4417. #define MAX_LOAD_SECTIONS 40
  4418. #define DL_MAGIC "DOWNLOAD INFORMATION!!"
  4419. #define DL_INFO_VER_V1 "V1.0"
  4420. #define DL_MAGIC_NUM_COUNT 32
  4421. #define DL_MAGIC_OFFSET 24
  4422. #define DL_IMG_NAME_LENGTH 16
  4423. #define DL_CUSTOM_INFO_SIZE (128)
  4424. /*download status v1 and old version for emmc*/
  4425. #define FORMAT_START "FORMAT_START"
  4426. #define FORMAT_DONE "FORMAT_DONE"
  4427. #define BL_START "BL_START"
  4428. #define BL_DONE "BL_DONE"
  4429. #define DL_START "DL_START"
  4430. #define DL_DONE "DL_DONE"
  4431. #define DL_ERROR "DL_ERROR"
  4432. #define DL_CK_DONE "DL_CK_DONE"
  4433. #define DL_CK_ERROR "DL_CK_ERROR"
  4434. /*v1 and old version for emmc*/
  4435. #define CHECKSUM_PASS "PASS"
  4436. #define CHECKSUM_FAIL "FAIL"
  4437. typedef enum {
  4438. DL_INFO_VERSION_V0 = 0,
  4439. DL_INFO_VERSION_V1 = 1,
  4440. DL_INFO_VERSION_UNKOWN = 0xFF,
  4441. } DLInfoVersion;
  4442. /*version v1.0 {*/
  4443. typedef struct {
  4444. char image_name[DL_IMG_NAME_LENGTH];
  4445. } IMG_DL_INFO;
  4446. typedef struct {
  4447. unsigned int image_index;
  4448. unsigned int pc_checksum;
  4449. unsigned int da_checksum;
  4450. char checksum_status[8];
  4451. } CHECKSUM_INFO_V1;
  4452. typedef struct {
  4453. char magic_num[DL_MAGIC_OFFSET];
  4454. char version[DL_MAGIC_NUM_COUNT-DL_MAGIC_OFFSET];
  4455. CHECKSUM_INFO_V1 part_info[MAX_LOAD_SECTIONS];
  4456. char ram_checksum[16];
  4457. char download_status[16];
  4458. IMG_DL_INFO img_dl_info[MAX_LOAD_SECTIONS];
  4459. } DL_STATUS_V1;
  4460. /*version v1.0 }*/
  4461. /*Support to check format/download status, 2013/01/19 {*/
  4462. #define DL_NOT_FOUND 2
  4463. #define DL_PASS 0
  4464. #define DL_FAIL 1
  4465. int nand_get_dl_info(void)
  4466. {
  4467. DL_STATUS_V1 download_info;
  4468. u8 *data_buf;
  4469. u8 *spare_buf;
  4470. int ret;
  4471. u32 block_size = BLOCK_SIZE;
  4472. u32 page_size = g_nand_chip.page_size;
  4473. u32 pages_per_block = block_size/page_size;
  4474. u32 total_blocks = (u32)(g_nand_size/BLOCK_SIZE);
  4475. u32 i,block_i,page_i;
  4476. u32 block_addr;
  4477. u32 dl_info_blkAddr = 0xFFFFFFFF;
  4478. u32 page_index[4];
  4479. data_buf = (u8 *)malloc(LPAGE);
  4480. spare_buf = (u8 *)malloc(LSPARE);
  4481. if (!data_buf || !spare_buf) {
  4482. dprintf(INFO,"[nand_get_dl_info]malloc mem fail\n");
  4483. ret = -1;
  4484. return ret;
  4485. }
  4486. // DL information block should program to good block instead of always at last block.
  4487. page_index[0] = 0;
  4488. page_index[1] = 1;
  4489. page_index[2] = pages_per_block-3;
  4490. page_index[3] = pages_per_block-1;
  4491. block_i = 1;
  4492. do {
  4493. block_addr = pages_per_block * (total_blocks-block_i);
  4494. for (page_i=0; page_i<4; page_i++) {
  4495. nand_exec_read_page(&g_nand_chip,block_addr+page_index[page_i],page_size,data_buf,spare_buf);
  4496. ret = memcmp((void*)data_buf,DL_MAGIC, sizeof(DL_MAGIC));
  4497. if (!ret) {
  4498. dl_info_blkAddr = block_addr;
  4499. break;
  4500. }
  4501. }
  4502. if (dl_info_blkAddr!=0xFFFFFFFF) {
  4503. break;
  4504. }
  4505. block_i++;
  4506. } while (block_i<=PRE_SCAN_BLOCK_NUM);
  4507. if (dl_info_blkAddr==0xFFFFFFFF) {
  4508. dprintf(INFO,"DL INFO NOT FOUND\n");
  4509. ret = DL_NOT_FOUND;
  4510. } else {
  4511. dprintf(INFO,"get dl info from 0x%x\n",dl_info_blkAddr);
  4512. memcpy(&download_info,data_buf,sizeof(download_info));
  4513. if (!memcmp(download_info.download_status,DL_DONE,sizeof(DL_DONE))||!memcmp(download_info.download_status,DL_CK_DONE,sizeof(DL_CK_DONE))) {
  4514. dprintf(INFO,"dl done. status = %s\n",download_info.download_status);
  4515. dprintf(INFO,"dram checksum : %s\n",download_info.ram_checksum);
  4516. for (i=0; i<PART_MAX_COUNT; i++) {
  4517. if (download_info.part_info[i].image_index!=0) {
  4518. dprintf(INFO,"image_index:%d, checksum: %s\n",download_info.part_info[i].image_index,download_info.part_info[i].checksum_status);
  4519. }
  4520. }
  4521. ret = DL_PASS;
  4522. } else {
  4523. dprintf(INFO,"dl error. status = %s\n",download_info.download_status);
  4524. dprintf(INFO,"dram checksum : %s\n",download_info.ram_checksum);
  4525. for (i=0; i<PART_MAX_COUNT; i++) {
  4526. if (download_info.part_info[i].image_index!=0) {
  4527. dprintf(INFO,"image_index:%d, checksum: %s\n",download_info.part_info[i].image_index,download_info.part_info[i].checksum_status);
  4528. }
  4529. }
  4530. ret = DL_FAIL;
  4531. }
  4532. }
  4533. free(data_buf);
  4534. free(spare_buf);
  4535. return ret;
  4536. }
  4537. u32 mtk_nand_erasesize(void)
  4538. {
  4539. return g_nand_chip.erasesize;
  4540. }
  4541. #endif