mtk_nand_tlc.c 142 KB

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