mtk_nand.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738
  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. u32 slc_ratio = 6; //slc mode block ration in FS partition. means slc_ration %
  101. u32 sys_slc_ratio = 6;
  102. u32 usr_slc_ratio = 6;
  103. extern void mtk_pmt_reset(void);
  104. extern void mtk_slc_blk_addr(u64 addr, u32* blk_num, u32* page_in_block);
  105. extern bool mtk_block_istlc(u64 addr);
  106. extern bool mtk_nand_IsBMTPOOL(u64 logical_address);
  107. #define ERR_RTN_SUCCESS 1
  108. #define ERR_RTN_FAIL 0
  109. #define ERR_RTN_BCH_FAIL -1
  110. u32 BLOCK_SIZE;
  111. #define NFI_ISSUE_COMMAND(cmd, col_addr, row_addr, col_num, row_num) \
  112. do { \
  113. DRV_WriteReg(NFI_CMD_REG16,cmd);\
  114. while (DRV_Reg32(NFI_STA_REG32) & STA_CMD_STATE);\
  115. DRV_WriteReg32(NFI_COLADDR_REG32, col_addr);\
  116. DRV_WriteReg32(NFI_ROWADDR_REG32, row_addr);\
  117. DRV_WriteReg(NFI_ADDRNOB_REG16, col_num | (row_num<<ADDR_ROW_NOB_SHIFT));\
  118. while (DRV_Reg32(NFI_STA_REG32) & STA_ADDR_STATE);\
  119. }while(0);
  120. flashdev_info devinfo;
  121. #define CHIPVER_ECO_1 (0x8a00)
  122. #define CHIPVER_ECO_2 (0x8a01)
  123. #define RAND_TYPE_SAMSUNG 0
  124. #define RAND_TYPE_TOSHIBA 1
  125. #define RAND_TYPE_NONE 2
  126. extern u64 part_get_startaddress(u64 byte_address, u32 *idx);
  127. extern bool raw_partition(u32 index);
  128. bool __nand_erase (u64 logical_addr);
  129. bool mark_block_bad (u64 logical_addr);
  130. bool nand_erase_hw (u64 offset);
  131. int check_data_empty(void *data, unsigned size);
  132. struct NAND_CMD g_kCMD;
  133. static u32 g_i4ErrNum;
  134. static bool g_bInitDone;
  135. u64 total_size;
  136. u64 g_nand_size = 0;
  137. static bool DDR_INTERFACE = FALSE;
  138. __attribute__ ((aligned(64))) static unsigned char g_data_buf[16384+1600];
  139. __attribute__ ((aligned(64))) static struct nand_buffers nBuf;
  140. __attribute__ ((aligned(64))) static unsigned char data_buf_temp[16384];
  141. __attribute__ ((aligned(64))) static unsigned char oob_buf_temp[1600];
  142. enum flashdev_vendor gVendor;
  143. static unsigned char g_spare_buf[1600];
  144. static u32 download_size = 0;
  145. static bool use_randomizer = FALSE;
  146. u32 MICRON_TRANSFER(u32 pageNo);
  147. u32 SANDISK_TRANSFER(u32 pageNo);
  148. u32 HYNIX_TRANSFER(u32 pageNo);
  149. typedef u32 (*GetLowPageNumber)(u32 pageNo);
  150. GetLowPageNumber functArray[]= {
  151. MICRON_TRANSFER,
  152. HYNIX_TRANSFER,
  153. SANDISK_TRANSFER,
  154. };
  155. u32 SANDISK_TRANSFER(u32 pageNo)
  156. {
  157. if (0 == pageNo) {
  158. return pageNo;
  159. } else {
  160. return pageNo+pageNo-1;
  161. }
  162. }
  163. u32 HYNIX_TRANSFER(u32 pageNo)
  164. {
  165. u32 temp;
  166. if (pageNo < 4)
  167. return pageNo;
  168. temp = pageNo+(pageNo&0xFFFFFFFE)-2;
  169. return temp;
  170. }
  171. u32 MICRON_TRANSFER(u32 pageNo)
  172. {
  173. u32 temp;
  174. if (pageNo < 4)
  175. return pageNo;
  176. temp = (pageNo - 4) & 0xFFFFFFFE;
  177. if (pageNo<=130)
  178. return (pageNo+temp);
  179. else
  180. return (pageNo+temp-2);
  181. }
  182. static bool mtk_nand_read_status(void);
  183. static inline unsigned int uffs(unsigned int x)
  184. {
  185. unsigned int r = 1;
  186. if (!x)
  187. return 0;
  188. if (!(x & 0xffff)) {
  189. x >>= 16;
  190. r += 16;
  191. }
  192. if (!(x & 0xff)) {
  193. x >>= 8;
  194. r += 8;
  195. }
  196. if (!(x & 0xf)) {
  197. x >>= 4;
  198. r += 4;
  199. }
  200. if (!(x & 3)) {
  201. x >>= 2;
  202. r += 2;
  203. }
  204. if (!(x & 1)) {
  205. x >>= 1;
  206. r += 1;
  207. }
  208. return r;
  209. }
  210. void dump_nfi(void)
  211. {
  212. dprintf(INFO,"~~~~Dump NFI Register in LK~~~~\n");
  213. dprintf(INFO,"NFI_CNFG_REG16: 0x%x\n", DRV_Reg16(NFI_CNFG_REG16));
  214. dprintf(INFO,"NFI_PAGEFMT_REG16: 0x%x\n", DRV_Reg16(NFI_PAGEFMT_REG16));
  215. dprintf(INFO,"NFI_CON_REG16: 0x%x\n", DRV_Reg16(NFI_CON_REG16));
  216. dprintf(INFO,"NFI_ACCCON_REG32: 0x%x\n", DRV_Reg32(NFI_ACCCON_REG32));
  217. dprintf(INFO,"NFI_INTR_EN_REG16: 0x%x\n", DRV_Reg16(NFI_INTR_EN_REG16));
  218. dprintf(INFO,"NFI_INTR_REG16: 0x%x\n", DRV_Reg16(NFI_INTR_REG16));
  219. dprintf(INFO,"NFI_CMD_REG16: 0x%x\n", DRV_Reg16(NFI_CMD_REG16));
  220. dprintf(INFO,"NFI_ADDRNOB_REG16: 0x%x\n", DRV_Reg16(NFI_ADDRNOB_REG16));
  221. dprintf(INFO,"NFI_COLADDR_REG32: 0x%x\n", DRV_Reg32(NFI_COLADDR_REG32));
  222. dprintf(INFO,"NFI_ROWADDR_REG32: 0x%x\n", DRV_Reg32(NFI_ROWADDR_REG32));
  223. dprintf(INFO,"NFI_STRDATA_REG16: 0x%x\n", DRV_Reg16(NFI_STRDATA_REG16));
  224. dprintf(INFO,"NFI_DATAW_REG32: 0x%x\n", DRV_Reg32(NFI_DATAW_REG32));
  225. dprintf(INFO,"NFI_DATAR_REG32: 0x%x\n", DRV_Reg32(NFI_DATAR_REG32));
  226. dprintf(INFO,"NFI_PIO_DIRDY_REG16: 0x%x\n", DRV_Reg16(NFI_PIO_DIRDY_REG16));
  227. dprintf(INFO,"NFI_STA_REG32: 0x%x\n", DRV_Reg32(NFI_STA_REG32));
  228. dprintf(INFO,"NFI_FIFOSTA_REG16: 0x%x\n", DRV_Reg16(NFI_FIFOSTA_REG16));
  229. // dprintf(INFO,"NFI_LOCKSTA_REG16: 0x%x\n", DRV_Reg16(NFI_LOCKSTA_REG16));
  230. dprintf(INFO,"NFI_ADDRCNTR_REG16: 0x%x\n", DRV_Reg16(NFI_ADDRCNTR_REG16));
  231. dprintf(INFO,"NFI_STRADDR_REG32: 0x%x\n", DRV_Reg32(NFI_STRADDR_REG32));
  232. dprintf(INFO,"NFI_BYTELEN_REG16: 0x%x\n", DRV_Reg16(NFI_BYTELEN_REG16));
  233. dprintf(INFO,"NFI_CSEL_REG16: 0x%x\n", DRV_Reg16(NFI_CSEL_REG16));
  234. dprintf(INFO,"NFI_IOCON_REG16: 0x%x\n", DRV_Reg16(NFI_IOCON_REG16));
  235. dprintf(INFO,"NFI_FDM0L_REG32: 0x%x\n", DRV_Reg32(NFI_FDM0L_REG32));
  236. dprintf(INFO,"NFI_FDM0M_REG32: 0x%x\n", DRV_Reg32(NFI_FDM0M_REG32));
  237. dprintf(INFO,"NFI_LOCK_REG16: 0x%x\n", DRV_Reg16(NFI_LOCK_REG16));
  238. dprintf(INFO,"NFI_LOCKCON_REG32: 0x%x\n", DRV_Reg32(NFI_LOCKCON_REG32));
  239. dprintf(INFO,"NFI_LOCKANOB_REG16: 0x%x\n", DRV_Reg16(NFI_LOCKANOB_REG16));
  240. dprintf(INFO,"NFI_FIFODATA0_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA0_REG32));
  241. dprintf(INFO,"NFI_FIFODATA1_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA1_REG32));
  242. dprintf(INFO,"NFI_FIFODATA2_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA2_REG32));
  243. dprintf(INFO,"NFI_FIFODATA3_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA3_REG32));
  244. dprintf(INFO,"NFI_MASTERSTA_REG16: 0x%x\n", DRV_Reg16(NFI_MASTERSTA_REG16));
  245. //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");
  246. //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");
  247. }
  248. extern bool init_pmt_done;
  249. u32 mtk_nand_page_transform(u64 logical_address, u32* blk, u32* map_blk)
  250. {
  251. u64 start_address;
  252. u32 index;
  253. u32 block;
  254. u32 page_in_block;
  255. u32 mapped_block;
  256. devinfo.tlcControl.slcopmodeEn = FALSE;
  257. if (VEND_NONE != gVendor && init_pmt_done == TRUE) {
  258. start_address = part_get_startaddress((u64)logical_address, &index);
  259. //MSG(ERR, "start_address(0x%llx), logical_address(0x%x) index(%d)\n",start_address,logical_address,index);
  260. if ((0xFFFFFFFF != index) && (raw_partition(index))) {
  261. block = (u32)((start_address/BLOCK_SIZE) + (logical_address-start_address) / g_nand_chip.erasesize);
  262. page_in_block = ((logical_address-start_address) /g_nand_chip.page_size)% ((1 << (g_nand_chip.phys_erase_shift-g_nand_chip.page_shift)));
  263. if (devinfo.vendor != VEND_NONE) {
  264. page_in_block = functArray[devinfo.feature_set.ptbl_idx](page_in_block);
  265. }
  266. mapped_block = get_mapping_block_index(block);
  267. //MSG(ERR, "transform_address(0x%x)\n",mapped_block*(BLOCK_SIZE/(g_nand_chip.page_size))+page_in_block);
  268. } else {
  269. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_MLC_HYBER) && (!mtk_block_istlc(logical_address))) {
  270. mtk_slc_blk_addr(logical_address, &block, &page_in_block);
  271. devinfo.tlcControl.slcopmodeEn = TRUE;
  272. dprintf(INFO,"devinfo.tlcControl.slcopmodeEn = TRUE\n");
  273. } else {
  274. block = (u32)(logical_address/BLOCK_SIZE);
  275. page_in_block = (u32)((logical_address/g_nand_chip.page_size) % (BLOCK_SIZE >> g_nand_chip.page_shift));
  276. }
  277. mapped_block = get_mapping_block_index(block);
  278. }
  279. } else {
  280. block = (u32)(logical_address/BLOCK_SIZE);
  281. mapped_block = get_mapping_block_index(block);
  282. page_in_block = (logical_address/g_nand_chip.page_size) % (1 << (g_nand_chip.phys_erase_shift-g_nand_chip.page_shift));
  283. }
  284. *blk = block;
  285. *map_blk = mapped_block;
  286. return mapped_block*(BLOCK_SIZE/(g_nand_chip.page_size))+page_in_block;
  287. }
  288. bool get_device_info(u8*id, flashdev_info *devinfo)
  289. {
  290. u32 i,m,n,mismatch;
  291. int target=-1;
  292. u8 target_id_len=0;
  293. unsigned int flash_number = sizeof(gen_FlashTable) / sizeof(gen_FlashTable[0]);
  294. for (i = 0; i<flash_number; i++) {
  295. mismatch=0;
  296. for (m=0; m<gen_FlashTable[i].id_length; m++) {
  297. if (id[m]!=gen_FlashTable[i].id[m]) {
  298. mismatch=1;
  299. break;
  300. }
  301. }
  302. if (mismatch == 0 && gen_FlashTable[i].id_length > target_id_len) {
  303. target=i;
  304. target_id_len=gen_FlashTable[i].id_length;
  305. }
  306. }
  307. dprintf(INFO,"target %d",target);
  308. if (target != -1) {
  309. dprintf(INFO,"Recognize NAND: ID [");
  310. for (n=0; n<gen_FlashTable[target].id_length; n++) {
  311. devinfo->id[n] = gen_FlashTable[target].id[n];
  312. MSG(INIT, "%x ",devinfo->id[n]);
  313. }
  314. dprintf(INFO,"], 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);
  315. devinfo->id_length=gen_FlashTable[i].id_length;
  316. devinfo->blocksize = gen_FlashTable[target].blocksize;
  317. devinfo->addr_cycle = gen_FlashTable[target].addr_cycle;
  318. devinfo->iowidth = gen_FlashTable[target].iowidth;
  319. devinfo->timmingsetting = gen_FlashTable[target].timmingsetting;
  320. devinfo->advancedmode = gen_FlashTable[target].advancedmode;
  321. devinfo->pagesize = gen_FlashTable[target].pagesize;
  322. devinfo->sparesize = gen_FlashTable[target].sparesize;
  323. devinfo->totalsize = gen_FlashTable[target].totalsize;
  324. devinfo->sectorsize = gen_FlashTable[target].sectorsize;
  325. devinfo->s_acccon= gen_FlashTable[target].s_acccon;
  326. devinfo->s_acccon1= gen_FlashTable[target].s_acccon1;
  327. devinfo->freq= gen_FlashTable[target].freq;
  328. devinfo->vendor = gen_FlashTable[target].vendor;
  329. gVendor = gen_FlashTable[target].vendor;
  330. devinfo->dqs_delay_ctrl = gen_FlashTable[target].dqs_delay_ctrl;
  331. memcpy((u8*)&devinfo->feature_set, (u8*)&gen_FlashTable[target].feature_set, sizeof(struct MLC_feature_set));
  332. memcpy(devinfo->devciename, gen_FlashTable[target].devciename, sizeof(devinfo->devciename));
  333. devinfo->NAND_FLASH_TYPE = gen_FlashTable[target].NAND_FLASH_TYPE;
  334. memcpy((u8*)&devinfo->tlcControl, (u8*)&gen_FlashTable[target].tlcControl, sizeof(NFI_TLC_CTRL));
  335. return true;
  336. } else {
  337. dprintf(INFO, "Not Found NAND: ID [");
  338. for (n=0; n<NAND_MAX_ID; n++) {
  339. MSG(INIT, "%x ",id[n]);
  340. }
  341. dprintf(INFO, "]\n");
  342. return false;
  343. }
  344. }
  345. u16 randomizer_seed[128] = {
  346. 0x576A, 0x05E8, 0x629D, 0x45A3,
  347. 0x649C, 0x4BF0, 0x2342, 0x272E,
  348. 0x7358, 0x4FF3, 0x73EC, 0x5F70,
  349. 0x7A60, 0x1AD8, 0x3472, 0x3612,
  350. 0x224F, 0x0454, 0x030E, 0x70A5,
  351. 0x7809, 0x2521, 0x48F4, 0x5A2D,
  352. 0x492A, 0x043D, 0x7F61, 0x3969,
  353. 0x517A, 0x3B42, 0x769D, 0x0647,
  354. 0x7E2A, 0x1383, 0x49D9, 0x07B8,
  355. 0x2578, 0x4EEC, 0x4423, 0x352F,
  356. 0x5B22, 0x72B9, 0x367B, 0x24B6,
  357. 0x7E8E, 0x2318, 0x6BD0, 0x5519,
  358. 0x1783, 0x18A7, 0x7B6E, 0x7602,
  359. 0x4B7F, 0x3648, 0x2C53, 0x6B99,
  360. 0x0C23, 0x67CF, 0x7E0E, 0x4D8C,
  361. 0x5079, 0x209D, 0x244A, 0x747B,
  362. 0x350B, 0x0E4D, 0x7004, 0x6AC3,
  363. 0x7F3E, 0x21F5, 0x7A15, 0x2379,
  364. 0x1517, 0x1ABA, 0x4E77, 0x15A1,
  365. 0x04FA, 0x2D61, 0x253A, 0x1302,
  366. 0x1F63, 0x5AB3, 0x049A, 0x5AE8,
  367. 0x1CD7, 0x4A00, 0x30C8, 0x3247,
  368. 0x729C, 0x5034, 0x2B0E, 0x57F2,
  369. 0x00E4, 0x575B, 0x6192, 0x38F8,
  370. 0x2F6A, 0x0C14, 0x45FC, 0x41DF,
  371. 0x38DA, 0x7AE1, 0x7322, 0x62DF,
  372. 0x5E39, 0x0E64, 0x6D85, 0x5951,
  373. 0x5937, 0x6281, 0x33A1, 0x6A32,
  374. 0x3A5A, 0x2BAC, 0x743A, 0x5E74,
  375. 0x3B2E, 0x7EC7, 0x4FD2, 0x5D28,
  376. 0x751F, 0x3EF8, 0x39B1, 0x4E49,
  377. 0x746B, 0x6EF6, 0x44BE, 0x6DB7
  378. };
  379. static int mtk_nand_interface_async()
  380. {
  381. if (DDR_INTERFACE == TRUE) {
  382. //nand_device_reset();
  383. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  384. NFI_CLN_REG32(MT_CLKMUX_NFI1X_INFRA_SEL,0x80);
  385. // clkmux_sel(MT_CLKMUX_NFI1X_INFRA_SEL, MT_CG_SYS_26M,"NFI");// TODO
  386. NFI_SET_REG32(NFI_DEBUG_CON1_REG16,NFI_BYPASS);
  387. //clear bypass of ecc
  388. NFI_SET_REG32(ECC_BYPASS_REG32,ECC_BYPASS);
  389. DRV_WriteReg32(NFI_ACCCON_REG32,devinfo.timmingsetting);
  390. DDR_INTERFACE = FALSE;
  391. }
  392. }
  393. static int mtk_nand_interface_config()
  394. {
  395. #if 1
  396. u32 timeout;
  397. u32 val;
  398. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  399. u32 id = DRV_Reg32(MT_CLKMUX_NFI1X_INFRA_SEL);
  400. u32 id2 = id;
  401. if (devinfo.iowidth == IO_ONFI || devinfo.iowidth ==IO_TOGGLEDDR || devinfo.iowidth ==IO_TOGGLESDR) {
  402. //nand_enable_clock();
  403. id = DRV_Reg32(MT_CLKMUX_NFI1X_INFRA_SEL)&~(0xFF);
  404. if (devinfo.freq == 80) {
  405. id |= 0x92;
  406. } else if (devinfo.freq == 100) {
  407. id |= 0xD2;
  408. }
  409. //reset
  410. nand_set_command(NAND_CMD_RESET);
  411. timeout = TIMEOUT_4;
  412. while (timeout)
  413. timeout--;
  414. nand_reset();
  415. //set feature
  416. mtk_nand_SetFeature((u16) feature_set->sfeatureCmd, \
  417. feature_set->Interface.address, (u8 *)&feature_set->Interface.feature,\
  418. sizeof(feature_set->Interface.feature));
  419. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16,HWDCM_SWCON_ON);
  420. //setup register
  421. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16,NFI_BYPASS);
  422. //clear bypass of ecc
  423. NFI_CLN_REG32(ECC_BYPASS_REG32,ECC_BYPASS);
  424. //set infra_sel
  425. // clkmux_sel(MT_CLKMUX_NFI_MUX_SEL, MT_VCG_BUS)
  426. ///mb();
  427. DRV_WriteReg32(MT_CLKMUX_NFI1X_INFRA_SEL,id);
  428. //clkmux_sel(MT_CLKMUX_NFI2X_GFMUX_SEL,id,"NFI");//TODO
  429. //enable_clock(MT_CG_MPLL_D7,"NFI");
  430. //clkmux_sel(MT_CLKMUX_NFI1X_INFRA_SEL, MT_CG_SYS_TEMP,"NFI");//TODO
  431. //mb();
  432. /////////////////////////////////////////////////////////////////////////////////////
  433. //val = DRV_Reg32(NFI_PULL2)&0xFFC00000;
  434. //DRV_WriteReg32(NFI_PULL1,val);
  435. DRV_WriteReg32(NFI_DLYCTRL_REG32,0xA001); //temp
  436. val = devinfo.dqs_delay_ctrl + (3<<24);
  437. DRV_WriteReg32(NFI_DQS_DELAY_CTRL,val); //temp
  438. ////////////////////////////////////////////////////////////////////////////////////
  439. //while(0 == (DRV_Reg(NFI_STA_REG32) && STA_FLASH_MACRO_IDLE));
  440. if (devinfo.iowidth == IO_ONFI)
  441. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 2);
  442. else
  443. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 1);
  444. DRV_WriteReg32(NFI_ACCCON1_REG3,devinfo.s_acccon1);
  445. DRV_WriteReg32(NFI_ACCCON_REG32,devinfo.s_acccon);
  446. //read back confirm
  447. mtk_nand_GetFeature(feature_set->gfeatureCmd, \
  448. feature_set->Interface.address, (u8 *)&val,4);
  449. if ((val&0xFF) != (feature_set->Interface.feature & 0xFF)) {
  450. MSG(INIT, "[%s] fail %d\n",__FUNCTION__,val);
  451. nand_set_command(NAND_CMD_RESET);
  452. timeout = TIMEOUT_4;
  453. while (timeout)
  454. timeout--;
  455. nand_reset();
  456. DRV_WriteReg32(MT_CLKMUX_NFI1X_INFRA_SEL,id2);
  457. //clkmux_sel(MT_CLKMUX_NFI1X_INFRA_SEL, MT_CG_SYS_26M,"NFI"); //TODO
  458. NFI_SET_REG32(NFI_DEBUG_CON1_REG16,NFI_BYPASS);
  459. NFI_SET_REG32(ECC_BYPASS_REG32,ECC_BYPASS);
  460. DRV_WriteReg32(NFI_ACCCON_REG32,devinfo.timmingsetting);
  461. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  462. return 0;
  463. }
  464. MSG(INIT, "[%s] success \n",__FUNCTION__);
  465. } else {
  466. MSG(INIT, "[%s] legacy interface \n",__FUNCTION__);
  467. }
  468. #endif //TODO
  469. return 1;
  470. }
  471. static int mtk_nand_randomizer_config(struct gRandConfig *conf, kal_uint16 seed)
  472. {
  473. if (gVendor == VEND_SANDISK || gVendor == VEND_TOSHIBA|| devinfo.vendor == VEND_HYNIX) {
  474. kal_uint16 nfi_cnfg = 0;
  475. kal_uint32 nfi_ran_cnfg = 0;
  476. kal_uint8 i;
  477. /* set up NFI_CNFG */
  478. nfi_cnfg = DRV_Reg(NFI_CNFG_REG16);
  479. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  480. if (conf->type == RAND_TYPE_SAMSUNG) {
  481. nfi_ran_cnfg =0;
  482. nfi_ran_cnfg |= seed << EN_SEED_SHIFT;
  483. nfi_ran_cnfg |= seed << DE_SEED_SHIFT;
  484. nfi_cnfg |= CNFG_RAN_SEC;
  485. nfi_cnfg |= CNFG_RAN_SEL;
  486. use_randomizer = TRUE;
  487. //nfi_ran_cnfg |= 0x00010001;
  488. } else if (conf->type == RAND_TYPE_TOSHIBA) {
  489. use_randomizer = TRUE;
  490. for (i = 0 ; i < 6 ; i++) {
  491. DRV_WriteReg32(NFI_RANDOM_ENSEED01_TS_REG32+i, conf->seed[i]);
  492. DRV_WriteReg32(NFI_RANDOM_DESEED01_TS_REG32+i, conf->seed[i]);
  493. }
  494. nfi_cnfg |= CNFG_RAN_SEC;
  495. nfi_cnfg &= ~CNFG_RAN_SEL;
  496. //nfi_ran_cnfg |= 0x00010001;
  497. } else {
  498. nfi_ran_cnfg &= ~0x00010001;
  499. use_randomizer = FALSE;
  500. return 0;
  501. }
  502. DRV_WriteReg(NFI_CNFG_REG16, nfi_cnfg);
  503. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, nfi_ran_cnfg);
  504. }
  505. return 0;
  506. }
  507. static bool mtk_nand_israndomizeron()
  508. {
  509. if (gVendor == VEND_SANDISK || gVendor == VEND_TOSHIBA|| devinfo.vendor == VEND_HYNIX) {
  510. kal_uint32 nfi_ran_cnfg = 0;
  511. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  512. if (nfi_ran_cnfg&0x00010001)
  513. return TRUE;
  514. }
  515. return FALSE;
  516. }
  517. static void mtk_nand_interface_switch()
  518. {
  519. if (devinfo.iowidth == IO_ONFI || devinfo.iowidth ==IO_TOGGLEDDR || devinfo.iowidth ==IO_TOGGLESDR) {
  520. if (DDR_INTERFACE == FALSE) {
  521. if (mtk_nand_interface_config()) {
  522. MSG(INIT,"[NFI] interface switch sync!!!!\n");
  523. DDR_INTERFACE = TRUE;
  524. } else {
  525. MSG(INIT,"[NFI] interface switch fail!!!!\n");
  526. DDR_INTERFACE = FALSE;
  527. }
  528. }
  529. }
  530. }
  531. static void mtk_nand_turn_on_randomizer(kal_uint32 page)
  532. {
  533. //struct gRandConfig *conf = &devinfo.feature_set.randConfig;
  534. if (gVendor == VEND_SANDISK || gVendor == VEND_TOSHIBA|| devinfo.vendor == VEND_HYNIX) {
  535. u32 page_per_blk = BLOCK_SIZE/(g_nand_chip.page_size);
  536. kal_uint32 nfi_ran_cnfg = 0;
  537. kal_uint16 seed;
  538. if (page_per_blk ==256) {
  539. seed = randomizer_seed[page%128];
  540. } else {
  541. seed = randomizer_seed[page%page_per_blk];
  542. }
  543. mtk_nand_randomizer_config(&devinfo.feature_set.randConfig,seed);
  544. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  545. nfi_ran_cnfg |= 0x00010001;
  546. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, nfi_ran_cnfg);
  547. }
  548. }
  549. static void mtk_nand_turn_off_randomizer()
  550. {
  551. if (gVendor == VEND_SANDISK || gVendor == VEND_TOSHIBA|| devinfo.vendor == VEND_HYNIX) {
  552. kal_uint32 nfi_ran_cnfg = 0;
  553. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  554. nfi_ran_cnfg &= ~0x00010001;
  555. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, nfi_ran_cnfg);
  556. }
  557. }
  558. static void ECC_Config(u32 ecc_level)
  559. {
  560. u32 u4ENCODESize;
  561. u32 u4DECODESize;
  562. u32 ecc_bit_cfg = 0;
  563. u32 sector_size = NAND_SECTOR_SIZE;
  564. if (devinfo.sectorsize == 1024)
  565. sector_size = 1024;
  566. switch (ecc_level) {
  567. case 4:
  568. ecc_bit_cfg = ECC_CNFG_ECC4;
  569. break;
  570. case 8:
  571. ecc_bit_cfg = ECC_CNFG_ECC8;
  572. break;
  573. case 10:
  574. ecc_bit_cfg = ECC_CNFG_ECC10;
  575. break;
  576. case 12:
  577. ecc_bit_cfg = ECC_CNFG_ECC12;
  578. break;
  579. case 14:
  580. ecc_bit_cfg = ECC_CNFG_ECC14;
  581. break;
  582. case 16:
  583. ecc_bit_cfg = ECC_CNFG_ECC16;
  584. break;
  585. case 18:
  586. ecc_bit_cfg = ECC_CNFG_ECC18;
  587. break;
  588. case 20:
  589. ecc_bit_cfg = ECC_CNFG_ECC20;
  590. break;
  591. case 22:
  592. ecc_bit_cfg = ECC_CNFG_ECC22;
  593. break;
  594. case 24:
  595. ecc_bit_cfg = ECC_CNFG_ECC24;
  596. break;
  597. case 28:
  598. ecc_bit_cfg = ECC_CNFG_ECC28;
  599. break;
  600. case 32:
  601. ecc_bit_cfg = ECC_CNFG_ECC32;
  602. break;
  603. case 36:
  604. ecc_bit_cfg = ECC_CNFG_ECC36;
  605. break;
  606. case 40:
  607. ecc_bit_cfg = ECC_CNFG_ECC40;
  608. break;
  609. case 44:
  610. ecc_bit_cfg = ECC_CNFG_ECC44;
  611. break;
  612. case 48:
  613. ecc_bit_cfg = ECC_CNFG_ECC48;
  614. break;
  615. case 52:
  616. ecc_bit_cfg = ECC_CNFG_ECC52;
  617. break;
  618. case 56:
  619. ecc_bit_cfg = ECC_CNFG_ECC56;
  620. break;
  621. case 60:
  622. ecc_bit_cfg = ECC_CNFG_ECC60;
  623. break;
  624. default:
  625. break;
  626. }
  627. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  628. do {
  629. ;
  630. } while (!DRV_Reg16(ECC_DECIDLE_REG16));
  631. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  632. do {
  633. ;
  634. } while (!DRV_Reg32(ECC_ENCIDLE_REG32));
  635. /* setup FDM register base */
  636. // DRV_WriteReg32(ECC_FDMADDR_REG32, NFI_FDM0L_REG32);
  637. u4ENCODESize = (sector_size + NAND_FDM_PER_SECTOR) << 3;
  638. u4DECODESize = ((sector_size + NAND_FDM_PER_SECTOR) << 3) + ecc_level * ECC_PARITY_BIT;
  639. /* configure ECC decoder && encoder */
  640. DRV_WriteReg32(ECC_DECCNFG_REG32, ecc_bit_cfg | DEC_CNFG_NFI | DEC_CNFG_EMPTY_EN | (u4DECODESize << DEC_CNFG_CODE_SHIFT));
  641. DRV_WriteReg32(ECC_ENCCNFG_REG32, ecc_bit_cfg | ENC_CNFG_NFI | (u4ENCODESize << ENC_CNFG_MSG_SHIFT));
  642. #ifndef MANUAL_CORRECT
  643. NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_CORRECT);
  644. #else
  645. NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_EL);
  646. #endif
  647. }
  648. static void ECC_Decode_Start(void)
  649. {
  650. /* wait for device returning idle */
  651. while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE)) ;
  652. DRV_WriteReg16(ECC_DECCON_REG16, DEC_EN);
  653. }
  654. static void ECC_Decode_End(void)
  655. {
  656. /* wait for device returning idle */
  657. while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE)) ;
  658. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  659. }
  660. //-------------------------------------------------------------------------------
  661. static void ECC_Encode_Start(void)
  662. {
  663. /* wait for device returning idle */
  664. while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE)) ;
  665. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_EN);
  666. }
  667. //-------------------------------------------------------------------------------
  668. static void ECC_Encode_End(void)
  669. {
  670. /* wait for device returning idle */
  671. while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE)) ;
  672. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  673. }
  674. //-------------------------------------------------------------------------------
  675. static bool nand_check_bch_error(u8 * pDataBuf, u32 u4SecIndex, u32 u4PageAddr)
  676. {
  677. bool bRet = true;
  678. u16 u2SectorDoneMask = 1 << u4SecIndex;
  679. u32 u4ErrorNumDebug0, u4ErrorNumDebug1,i, u4ErrNum;
  680. u32 timeout = 0xFFFF;
  681. #ifdef MANUAL_CORRECT
  682. u32 au4ErrBitLoc[6];
  683. u32 u4ErrByteLoc, u4BitOffset;
  684. u32 u4ErrBitLoc1th, u4ErrBitLoc2nd;
  685. #endif
  686. while (0 == (u2SectorDoneMask & DRV_Reg16(ECC_DECDONE_REG16))) {
  687. timeout--;
  688. if (0 == timeout) {
  689. return false;
  690. }
  691. }
  692. #ifndef MANUAL_CORRECT
  693. if (0 == (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  694. u4ErrorNumDebug0 = DRV_Reg32(ECC_DECENUM0_REG32);
  695. u4ErrorNumDebug1 = DRV_Reg32(ECC_DECENUM1_REG32);
  696. if (0 != (u4ErrorNumDebug0 & 0xFFFFFFFF) || 0 != (u4ErrorNumDebug1 & 0xFFFFFFFF)) {
  697. for (i = 0; i <= u4SecIndex; ++i) {
  698. #if 1
  699. u4ErrNum = (DRV_Reg32((ECC_DECENUM0_REG32+(i/4)))>>((i%4)*8))& ERR_NUM0;
  700. #else
  701. if (i < 4) {
  702. u4ErrNum = DRV_Reg32(ECC_DECENUM0_REG32) >> (i * 8);
  703. } else {
  704. u4ErrNum = DRV_Reg32(ECC_DECENUM1_REG32) >> ((i - 4) * 8);
  705. }
  706. u4ErrNum &= ERR_NUM0;
  707. #endif
  708. if (ERR_NUM0 == u4ErrNum) {
  709. MSG(ERR, "In LittleKernel UnCorrectable at PageAddr=%d, Sector=%d\n", u4PageAddr, i);
  710. bRet = false;
  711. } else {
  712. if (u4ErrNum) {
  713. MSG(ERR, " In LittleKernel Correct %d at PageAddr=%d, Sector=%d\n", u4ErrNum, u4PageAddr, i);
  714. }
  715. }
  716. }
  717. }
  718. }
  719. #else
  720. memset(au4ErrBitLoc, 0x0, sizeof(au4ErrBitLoc));
  721. u4ErrorNumDebug0 = DRV_Reg32(ECC_DECENUM_REG32);
  722. u4ErrNum = (DRV_Reg32((ECC_DECENUM_REG32+(u4SecIndex/4)))>>((u4SecIndex%4)*8))& ERR_NUM0;
  723. if (u4ErrNum) {
  724. if (ERR_NUM0 == u4ErrNum) {
  725. MSG(ERR, "UnCorrectable at PageAddr=%d\n", u4PageAddr);
  726. bRet = false;
  727. } else {
  728. for (i = 0; i < ((u4ErrNum + 1) >> 1); ++i) {
  729. au4ErrBitLoc[i] = DRV_Reg32(ECC_DECEL0_REG32 + i);
  730. u4ErrBitLoc1th = au4ErrBitLoc[i] & 0x3FFF;
  731. if (u4ErrBitLoc1th < 0x1000) {
  732. u4ErrByteLoc = u4ErrBitLoc1th / 8;
  733. u4BitOffset = u4ErrBitLoc1th % 8;
  734. pDataBuf[u4ErrByteLoc] = pDataBuf[u4ErrByteLoc] ^ (1 << u4BitOffset);
  735. } else {
  736. MSG(ERR, "UnCorrectable ErrLoc=%d\n", au4ErrBitLoc[i]);
  737. }
  738. u4ErrBitLoc2nd = (au4ErrBitLoc[i] >> 16) & 0x3FFF;
  739. if (0 != u4ErrBitLoc2nd) {
  740. if (u4ErrBitLoc2nd < 0x1000) {
  741. u4ErrByteLoc = u4ErrBitLoc2nd / 8;
  742. u4BitOffset = u4ErrBitLoc2nd % 8;
  743. pDataBuf[u4ErrByteLoc] = pDataBuf[u4ErrByteLoc] ^ (1 << u4BitOffset);
  744. } else {
  745. MSG(ERR, "UnCorrectable High ErrLoc=%d\n", au4ErrBitLoc[i]);
  746. }
  747. }
  748. }
  749. bRet = true;
  750. }
  751. if (0 == (DRV_Reg16(ECC_DECFER_REG16) & (1 << u4SecIndex))) {
  752. bRet = false;
  753. }
  754. }
  755. #endif
  756. return bRet;
  757. }
  758. #if 1
  759. static bool nand_RFIFOValidSize(u16 u2Size)
  760. {
  761. u32 timeout = 0xFFFF;
  762. while (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) < u2Size) {
  763. timeout--;
  764. if (0 == timeout) {
  765. return false;
  766. }
  767. }
  768. if (u2Size == 0) {
  769. while (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16))) {
  770. timeout--;
  771. if (0 == timeout) {
  772. dprintf(INFO,"nand_RFIFOValidSize failed: 0x%x\n", u2Size);
  773. return false;
  774. }
  775. }
  776. }
  777. return true;
  778. }
  779. //-------------------------------------------------------------------------------
  780. static bool nand_WFIFOValidSize(u16 u2Size)
  781. {
  782. u32 timeout = 0xFFFF;
  783. while (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) > u2Size) {
  784. timeout--;
  785. if (0 == timeout) {
  786. return false;
  787. }
  788. }
  789. if (u2Size == 0) {
  790. while (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16))) {
  791. timeout--;
  792. if (0 == timeout) {
  793. dprintf(INFO,"nand_RFIFOValidSize failed: 0x%x\n", u2Size);
  794. return false;
  795. }
  796. }
  797. }
  798. return true;
  799. }
  800. #endif
  801. static bool nand_status_ready(u32 u4Status)
  802. {
  803. u32 timeout = 0xFFFF;
  804. while ((DRV_Reg32(NFI_STA_REG32) & u4Status) != 0) {
  805. timeout--;
  806. if (0 == timeout) {
  807. return false;
  808. }
  809. }
  810. return true;
  811. }
  812. static bool nand_reset(void)
  813. {
  814. int timeout = 0xFFFF;
  815. bool ret;
  816. if (DRV_Reg16(NFI_MASTERSTA_REG16) & 0xFFF) { // master is busy
  817. DRV_WriteReg32(NFI_CON_REG16, CON_FIFO_FLUSH | CON_NFI_RST);
  818. while (DRV_Reg16(NFI_MASTERSTA_REG16) & 0xFFF) {
  819. timeout--;
  820. if (!timeout) {
  821. MSG(FUC, "Wait for NFI_MASTERSTA timeout\n");
  822. }
  823. }
  824. }
  825. /* issue reset operation */
  826. DRV_WriteReg32(NFI_CON_REG16, CON_FIFO_FLUSH | CON_NFI_RST);
  827. ret= nand_status_ready(STA_NFI_FSM_MASK | STA_NAND_BUSY) && nand_RFIFOValidSize(0) && nand_WFIFOValidSize(0);
  828. return ret;
  829. }
  830. //-------------------------------------------------------------------------------
  831. static void nand_set_mode(u16 u2OpMode)
  832. {
  833. u16 u2Mode = DRV_Reg16(NFI_CNFG_REG16);
  834. u2Mode &= ~CNFG_OP_MODE_MASK;
  835. u2Mode |= u2OpMode;
  836. DRV_WriteReg16(NFI_CNFG_REG16, u2Mode);
  837. }
  838. //-------------------------------------------------------------------------------
  839. static void nand_set_autoformat(bool bEnable)
  840. {
  841. if (bEnable) {
  842. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  843. } else {
  844. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  845. }
  846. }
  847. //-------------------------------------------------------------------------------
  848. static void nand_configure_fdm(u16 u2FDMSize)
  849. {
  850. NFI_CLN_REG32(NFI_PAGEFMT_REG16, PAGEFMT_FDM_MASK | PAGEFMT_FDM_ECC_MASK);
  851. NFI_SET_REG32(NFI_PAGEFMT_REG16, u2FDMSize << PAGEFMT_FDM_SHIFT);
  852. NFI_SET_REG32(NFI_PAGEFMT_REG16, u2FDMSize << PAGEFMT_FDM_ECC_SHIFT);
  853. }
  854. //-------------------------------------------------------------------------------
  855. static bool nand_set_command(u16 command)
  856. {
  857. /* Write command to device */
  858. DRV_WriteReg16(NFI_CMD_REG16, command);
  859. return nand_status_ready(STA_CMD_STATE);
  860. }
  861. //-------------------------------------------------------------------------------
  862. static bool nand_set_address(u32 u4ColAddr, u32 u4RowAddr, u16 u2ColNOB, u16 u2RowNOB)
  863. {
  864. /* fill cycle addr */
  865. DRV_WriteReg32(NFI_COLADDR_REG32, u4ColAddr);
  866. DRV_WriteReg32(NFI_ROWADDR_REG32, u4RowAddr);
  867. DRV_WriteReg16(NFI_ADDRNOB_REG16, u2ColNOB | (u2RowNOB << ADDR_ROW_NOB_SHIFT));
  868. return nand_status_ready(STA_ADDR_STATE);
  869. }
  870. //-------------------------------------------------------------------------------
  871. static bool nand_device_reset(void)
  872. {
  873. u32 timeout = 0xFFFF;
  874. nand_reset();
  875. DRV_WriteReg(NFI_CNFG_REG16, CNFG_OP_RESET);
  876. nand_set_command(NAND_CMD_RESET);
  877. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  878. mtk_nand_interface_async();
  879. if (!timeout)
  880. return FALSE;
  881. else
  882. return TRUE;
  883. }
  884. //-------------------------------------------------------------------------------
  885. static bool nand_check_RW_count(struct nand_chip *nand, u16 u2WriteSize)
  886. {
  887. u32 timeout = 0xFFFF;
  888. u16 u2SecNum = u2WriteSize >> nand->sector_shift;
  889. while (ADDRCNTR_CNTR(DRV_Reg32(NFI_ADDRCNTR_REG16)) < u2SecNum) {
  890. timeout--;
  891. if (0 == timeout) {
  892. return false;
  893. }
  894. }
  895. return true;
  896. }
  897. //-------------------------------------------------------------------------------
  898. static bool nand_ready_for_read(struct nand_chip *nand, u32 u4RowAddr, u32 u4ColAddr, bool bFull, u8 * buf)
  899. {
  900. /* Reset NFI HW internal state machine and flush NFI in/out FIFO */
  901. bool bRet = false;
  902. u16 sec_num = 1 << (nand->page_shift - nand->sector_shift);
  903. u32 col_addr = u4ColAddr;
  904. if (nand->options & NAND_BUSWIDTH_16)
  905. col_addr >>= 1;
  906. u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  907. if (!nand_reset()) {
  908. goto cleanup;
  909. }
  910. if (DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32)&0x3) {
  911. NFI_SET_REG16(NFI_MASTERRST_REG32, PAD_MACRO_RST);//reset
  912. NFI_CLN_REG16(NFI_MASTERRST_REG32, PAD_MACRO_RST);//dereset
  913. }
  914. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  915. nand_set_mode(CNFG_OP_READ);
  916. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  917. DRV_WriteReg32(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  918. if (bFull) {
  919. #if USE_AHB_MODE
  920. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  921. #else
  922. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  923. #endif
  924. DRV_WriteReg32(NFI_STRADDR_REG32, buf);
  925. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  926. } else {
  927. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  928. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  929. }
  930. nand_set_autoformat(bFull);
  931. if (bFull)
  932. ECC_Decode_Start();
  933. if (!nand_set_command(NAND_CMD_READ_0)) {
  934. goto cleanup;
  935. }
  936. if (!nand_set_address(col_addr, u4RowAddr, colnob, rownob)) {
  937. goto cleanup;
  938. }
  939. if (!nand_set_command(NAND_CMD_READ_START)) {
  940. goto cleanup;
  941. }
  942. if (!nand_status_ready(STA_NAND_BUSY)) {
  943. goto cleanup;
  944. }
  945. bRet = true;
  946. cleanup:
  947. return bRet;
  948. }
  949. //-----------------------------------------------------------------------------
  950. static bool nand_ready_for_write(struct nand_chip *nand, u32 u4RowAddr, u8 * buf)
  951. {
  952. bool bRet = false;
  953. u16 sec_num = 1 << (nand->page_shift - nand->sector_shift);
  954. u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  955. if (!nand_reset()) {
  956. return false;
  957. }
  958. nand_set_mode(CNFG_OP_PRGM);
  959. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  960. DRV_WriteReg32(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  961. #if USE_AHB_MODE
  962. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  963. DRV_WriteReg32(NFI_STRADDR_REG32, buf);
  964. #else
  965. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  966. #endif
  967. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  968. nand_set_autoformat(true);
  969. ECC_Encode_Start();
  970. if (!nand_set_command(NAND_CMD_SEQIN)) {
  971. goto cleanup;
  972. }
  973. if (!nand_set_address(0, u4RowAddr, colnob, rownob)) {
  974. goto cleanup;
  975. }
  976. if (!nand_status_ready(STA_NAND_BUSY)) {
  977. goto cleanup;
  978. }
  979. bRet = true;
  980. cleanup:
  981. return bRet;
  982. }
  983. //-----------------------------------------------------------------------------
  984. static bool nand_dma_read_data(u8 * pDataBuf, u32 u4Size)
  985. {
  986. u32 timeout = 0xFFFF;
  987. arch_clean_invalidate_cache_range((addr_t)pDataBuf,(size_t)u4Size);
  988. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  989. DRV_Reg16(NFI_INTR_REG16);
  990. DRV_WriteReg16(NFI_INTR_EN_REG16, INTR_AHB_DONE_EN);
  991. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BRD);
  992. if (en_interrupt) {
  993. if (event_wait_timeout(&nand_int_event,100)) {
  994. dprintf(INFO,"[nand_dma_read_data]wait for AHB done timeout\n");
  995. dump_nfi();
  996. return false;
  997. }
  998. timeout = 0xFFFF;
  999. while ((u4Size >> g_nand_chip.sector_shift) > ((DRV_Reg32(NFI_BYTELEN_REG16) & 0x1f000) >> 12)) {
  1000. timeout--;
  1001. if (0 == timeout) {
  1002. return false; //4
  1003. }
  1004. }
  1005. } else {
  1006. while (!(DRV_Reg16(NFI_INTR_REG16) & INTR_AHB_DONE)) {
  1007. timeout--;
  1008. if (0 == timeout) {
  1009. return false;
  1010. }
  1011. }
  1012. timeout = 0xFFFF;
  1013. while ((u4Size >> g_nand_chip.sector_shift) > ((DRV_Reg32(NFI_BYTELEN_REG16) & 0x1f000) >> 12)) {
  1014. timeout--;
  1015. if (0 == timeout) {
  1016. return false; //4
  1017. }
  1018. }
  1019. }
  1020. return true;
  1021. }
  1022. static bool nand_mcu_read_data(u8 * pDataBuf, u32 length)
  1023. {
  1024. u32 timeout = 0xFFFF;
  1025. u32 i;
  1026. u32 *pBuf32;
  1027. if (length % 4) {
  1028. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1029. } else {
  1030. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1031. }
  1032. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BRD);
  1033. pBuf32 = (u32 *) pDataBuf;
  1034. if (length % 4) {
  1035. for (i = 0; (i < length) && (timeout > 0);) {
  1036. WAIT_NFI_PIO_READY(timeout);
  1037. *pDataBuf++ = DRV_Reg8(NFI_DATAR_REG32);
  1038. i++;
  1039. }
  1040. } else {
  1041. WAIT_NFI_PIO_READY(timeout);
  1042. for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  1043. WAIT_NFI_PIO_READY(timeout);
  1044. *pBuf32++ = DRV_Reg32(NFI_DATAR_REG32);
  1045. i++;
  1046. }
  1047. }
  1048. return true;
  1049. }
  1050. static bool nand_read_page_data(u8 * buf, u32 length)
  1051. {
  1052. #if USE_AHB_MODE
  1053. return nand_dma_read_data(buf, length);
  1054. #else
  1055. return nand_mcu_read_data(buf, length);
  1056. #endif
  1057. }
  1058. static bool nand_dma_write_data(u8 * buf, u32 length)
  1059. {
  1060. u32 timeout = 0xFFFF;
  1061. arch_clean_invalidate_cache_range((addr_t)buf,(size_t)length);
  1062. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1063. DRV_Reg16(NFI_INTR_REG16);
  1064. DRV_WriteReg16(NFI_INTR_EN_REG16, INTR_AHB_DONE_EN);
  1065. if ((unsigned int)buf % 16) {
  1066. //dprintf(INFO"Un-16-aligned address\n");
  1067. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  1068. } else {
  1069. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  1070. }
  1071. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  1072. if (en_interrupt) {
  1073. if (event_wait_timeout(&nand_int_event,100)) {
  1074. dprintf(INFO,"[nand_dma_write_data]wait for AHB done timeout\n");
  1075. dump_nfi();
  1076. return false;
  1077. }
  1078. } else {
  1079. while (!(DRV_Reg16(NFI_INTR_REG16) & INTR_AHB_DONE)) {
  1080. timeout--;
  1081. if (0 == timeout) {
  1082. dprintf(INFO,"wait write AHB done timeout\n");
  1083. dump_nfi();
  1084. return FALSE;
  1085. }
  1086. }
  1087. }
  1088. return true;
  1089. }
  1090. static bool nand_mcu_write_data(const u8 * buf, u32 length)
  1091. {
  1092. u32 timeout = 0xFFFF;
  1093. u32 i;
  1094. u32 *pBuf32 = (u32 *) buf;
  1095. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1096. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  1097. if ((u32) buf % 4 || length % 4)
  1098. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1099. else
  1100. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1101. if ((u32) buf % 4 || length % 4) {
  1102. for (i = 0; (i < (length)) && (timeout > 0);) {
  1103. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  1104. DRV_WriteReg32(NFI_DATAW_REG32, *buf++);
  1105. i++;
  1106. } else {
  1107. timeout--;
  1108. }
  1109. if (0 == timeout) {
  1110. dprintf(INFO,"[%s] nand mcu write timeout\n", __FUNCTION__);
  1111. dump_nfi();
  1112. return false;
  1113. }
  1114. }
  1115. } else {
  1116. for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  1117. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  1118. DRV_WriteReg32(NFI_DATAW_REG32, *pBuf32++);
  1119. i++;
  1120. } else {
  1121. timeout--;
  1122. }
  1123. if (0 == timeout) {
  1124. dprintf(INFO,"[%s] nand mcu write timeout\n", __FUNCTION__);
  1125. dump_nfi();
  1126. return false;
  1127. }
  1128. }
  1129. }
  1130. return true;
  1131. }
  1132. //-----------------------------------------------------------------------------
  1133. static bool nand_write_page_data(u8 * buf, u32 length)
  1134. {
  1135. #if USE_AHB_MODE
  1136. return nand_dma_write_data(buf, length);
  1137. #else
  1138. return nand_mcu_write_data(buf, length);
  1139. #endif
  1140. }
  1141. static void nand_read_fdm_data(u8 * pDataBuf, u32 u4SecNum)
  1142. {
  1143. u32 i;
  1144. u32 *pBuf32 = (u32 *) pDataBuf;
  1145. for (i = 0; i < u4SecNum; ++i) {
  1146. *pBuf32++ = DRV_Reg32(NFI_FDM0L_REG32 + (i << 1));
  1147. *pBuf32++ = DRV_Reg32(NFI_FDM0M_REG32 + (i << 1));
  1148. }
  1149. }
  1150. static void nand_write_fdm_data(u8 * pDataBuf, u32 u4SecNum)
  1151. {
  1152. u32 i;
  1153. u32 *pBuf32 = (u32 *) pDataBuf;
  1154. for (i = 0; i < u4SecNum; ++i) {
  1155. DRV_WriteReg32(NFI_FDM0L_REG32 + (i << 1), *pBuf32++);
  1156. DRV_WriteReg32(NFI_FDM0M_REG32 + (i << 1), *pBuf32++);
  1157. }
  1158. }
  1159. static void nand_stop_read(void)
  1160. {
  1161. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BRD);
  1162. ECC_Decode_End();
  1163. }
  1164. static void nand_stop_write(void)
  1165. {
  1166. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BWR);
  1167. ECC_Encode_End();
  1168. }
  1169. static bool nand_check_dececc_done(u32 u4SecNum)
  1170. {
  1171. u32 timeout, dec_mask;
  1172. timeout = 0xffff;
  1173. dec_mask = (1 << u4SecNum) - 1;
  1174. while ((dec_mask != DRV_Reg(ECC_DECDONE_REG16)) && timeout > 0)
  1175. timeout--;
  1176. if (timeout == 0) {
  1177. MSG(ERR, "ECC_DECDONE: timeout\n");
  1178. dump_nfi();
  1179. return false;
  1180. }
  1181. return true;
  1182. }
  1183. //---------------------------------------------------------------------------
  1184. static bool mtk_nand_read_status(void)
  1185. {
  1186. int status = 0;//, i;
  1187. unsigned int timeout;
  1188. nand_reset();
  1189. /* Disable HW ECC */
  1190. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1191. /* Disable 16-bit I/O */
  1192. NFI_CLN_REG32(NFI_PAGEFMT_REG16, PAGEFMT_DBYTE_EN);
  1193. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_OP_SRD | CNFG_READ_EN | CNFG_BYTE_RW);
  1194. DRV_WriteReg32(NFI_CON_REG16, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  1195. DRV_WriteReg32(NFI_CON_REG16, 0x3);
  1196. nand_set_mode(CNFG_OP_SRD);
  1197. DRV_WriteReg16(NFI_CNFG_REG16, 0x2042);
  1198. nand_set_command(NAND_CMD_STATUS);
  1199. DRV_WriteReg32(NFI_CON_REG16, 0x90);
  1200. timeout = TIMEOUT_4;
  1201. WAIT_NFI_PIO_READY(timeout);
  1202. if (timeout) {
  1203. status = (DRV_Reg16(NFI_DATAR_REG32));
  1204. }
  1205. //~ clear NOB
  1206. DRV_WriteReg32(NFI_CON_REG16, 0);
  1207. if (g_nand_chip.bus16 == IO_WIDTH_16) {
  1208. NFI_SET_REG32(NFI_PAGEFMT_REG16, PAGEFMT_DBYTE_EN);
  1209. NFI_CLN_REG32(NFI_CNFG_REG16, CNFG_BYTE_RW);
  1210. }
  1211. // check READY/BUSY status first
  1212. if (!(STATUS_READY & status)) {
  1213. MSG(ERR, "status is not ready\n");
  1214. }
  1215. // flash is ready now, check status code
  1216. if (STATUS_FAIL & status) {
  1217. if (!(STATUS_WR_ALLOW & status)) {
  1218. MSG(INIT, "status locked\n");
  1219. return FALSE;
  1220. } else {
  1221. MSG(INIT, "status unknown\n");
  1222. return FALSE;
  1223. }
  1224. } else {
  1225. return TRUE;
  1226. }
  1227. }
  1228. bool mtk_nand_SetFeature(u16 cmd, u32 addr, u8 *value, u8 bytes)
  1229. {
  1230. kal_uint16 reg_val = 0;
  1231. kal_uint8 write_count = 0;
  1232. kal_uint32 timeout=TIMEOUT_3;//0xffff;
  1233. nand_reset();
  1234. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  1235. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1236. nand_set_command(cmd);
  1237. nand_set_address(addr, 0, 1, 0);
  1238. //NFI_ISSUE_COMMAND(cmd, addr, 0, 1, 0)
  1239. //SAL_NFI_Config_Sector_Number(1);
  1240. DRV_WriteReg32(NFI_CON_REG16, 1 << CON_NFI_SEC_SHIFT);
  1241. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  1242. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1243. //SAL_NFI_Start_Data_Transfer(KAL_FALSE, KAL_TRUE);
  1244. while ( (write_count < bytes) && timeout ) {
  1245. WAIT_NFI_PIO_READY(timeout)
  1246. if (timeout == 0) {
  1247. break;
  1248. }
  1249. DRV_WriteReg32(NFI_DATAW_REG32, *value++);
  1250. write_count++;
  1251. timeout = TIMEOUT_3;
  1252. }
  1253. while ( (*NFI_STA_REG32 & STA_NAND_BUSY) && (timeout) ) {timeout--;}
  1254. mtk_nand_read_status();
  1255. if (timeout != 0)
  1256. return TRUE;
  1257. else
  1258. return FALSE;
  1259. }
  1260. bool mtk_nand_GetFeature(u16 cmd, u32 addr, u8 *value, u8 bytes)
  1261. {
  1262. kal_uint16 reg_val = 0;
  1263. kal_uint8 read_count = 0;
  1264. kal_uint32 timeout=TIMEOUT_3;//0xffff;
  1265. nand_reset();
  1266. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN);
  1267. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1268. nand_set_command(cmd);
  1269. nand_set_address(addr, 0, 1, 0);
  1270. //SAL_NFI_Config_Sector_Number(0);
  1271. DRV_WriteReg32(NFI_CON_REG16, 0 << CON_NFI_SEC_SHIFT);
  1272. reg_val = DRV_Reg32(NFI_CON_REG16);
  1273. reg_val &= ~CON_NFI_NOB_MASK;
  1274. reg_val |= ((4 << CON_NFI_NOB_SHIFT)|CON_NFI_SRD);
  1275. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1276. //NFI_SET_REG16(NFI_CON_REG16, CON_NFI_BWR);
  1277. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1278. // SAL_NFI_Start_Data_Transfer(KAL_TRUE, KAL_TRUE);
  1279. while ( (read_count < bytes) && timeout ) {
  1280. WAIT_NFI_PIO_READY(timeout)
  1281. if (timeout == 0) {
  1282. break;
  1283. }
  1284. *value++ = DRV_Reg32(NFI_DATAR_REG32);
  1285. read_count++;
  1286. timeout = TIMEOUT_3;
  1287. }
  1288. mtk_nand_read_status();
  1289. if (timeout != 0)
  1290. return TRUE;
  1291. else
  1292. return FALSE;
  1293. }
  1294. #if 1
  1295. const u8 data_tbl[8][5] = {
  1296. {0x04, 0x04, 0x7C, 0x7E, 0x00},
  1297. {0x00, 0x7C, 0x78, 0x78, 0x00},
  1298. {0x7C, 0x76, 0x74, 0x72, 0x00},
  1299. {0x08, 0x08, 0x00, 0x00, 0x00},
  1300. {0x0B, 0x7E, 0x76, 0x74, 0x00},
  1301. {0x10, 0x76, 0x72, 0x70, 0x00},
  1302. {0x02, 0x7C, 0x7E, 0x70, 0x00},
  1303. {0x00, 0x00, 0x00, 0x00, 0x00}
  1304. };
  1305. static void mtk_nand_modeentry_rrtry(void)
  1306. {
  1307. nand_reset();
  1308. nand_set_mode(CNFG_OP_CUST);
  1309. nand_set_command(0x5C);
  1310. nand_set_command(0xC5);
  1311. nand_status_ready(STA_NFI_OP_MASK);
  1312. }
  1313. static void mtk_nand_rren_rrtry(bool needB3)
  1314. {
  1315. nand_reset();
  1316. nand_set_mode(CNFG_OP_CUST);
  1317. if (needB3)
  1318. nand_set_command(0xB3);
  1319. nand_set_command(0x26);
  1320. nand_set_command(0x5D);
  1321. nand_status_ready(STA_NFI_OP_MASK);
  1322. }
  1323. static void mtk_nand_sprmset_rrtry(u32 addr, u32 data) //single parameter setting
  1324. {
  1325. u16 reg_val = 0;
  1326. u8 write_count = 0;
  1327. u32 reg = 0;
  1328. u32 timeout=TIMEOUT_3;//0xffff;
  1329. nand_reset();
  1330. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  1331. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1332. nand_set_command(0x55);
  1333. nand_set_address(addr, 0, 1, 0);
  1334. nand_status_ready(STA_NFI_OP_MASK);
  1335. DRV_WriteReg32(NFI_CON_REG16, 1 << CON_NFI_SEC_SHIFT);
  1336. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  1337. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1338. WAIT_NFI_PIO_READY(timeout);
  1339. timeout=TIMEOUT_3;
  1340. DRV_WriteReg8(NFI_DATAW_REG32, data);
  1341. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  1342. }
  1343. static void mtk_nand_toshiba_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1344. {
  1345. u32 acccon;
  1346. u8 add_reg[6] = {0x04, 0x05, 0x06, 0x07, 0x0D};
  1347. u8 cnt = 0;
  1348. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  1349. //DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); //to fit read retry timing
  1350. if (0 == retryCount)
  1351. mtk_nand_modeentry_rrtry();
  1352. for (cnt = 0; cnt < 5; cnt ++) {
  1353. mtk_nand_sprmset_rrtry(add_reg[cnt], data_tbl[retryCount][cnt]);
  1354. }
  1355. if (3 == retryCount)
  1356. mtk_nand_rren_rrtry(TRUE);
  1357. else if (6 > retryCount)
  1358. mtk_nand_rren_rrtry(FALSE);
  1359. if (7 == retryCount) { // to exit
  1360. nand_set_mode(CNFG_OP_RESET);
  1361. NFI_ISSUE_COMMAND (NAND_CMD_RESET, 0, 0, 0, 0);
  1362. nand_reset();
  1363. }
  1364. //DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1365. }
  1366. #endif
  1367. static void mtk_nand_micron_rrtry(flashdev_info deviceinfo, u32 feature, bool defValue)
  1368. {
  1369. //u32 feature = deviceinfo.feature_set.FeatureSet.readRetryStart+retryCount;
  1370. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd,\
  1371. deviceinfo.feature_set.FeatureSet.readRetryAddress,\
  1372. (u8 *)&feature,4);
  1373. }
  1374. static int g_sandisk_retry_case = 0;
  1375. static void mtk_nand_sandisk_rrtry(flashdev_info deviceinfo, u32 feature, bool defValue)
  1376. {
  1377. //u32 feature = deviceinfo.feature_set.FeatureSet.readRetryStart+retryCount;
  1378. if (FALSE == defValue)
  1379. nand_reset();
  1380. else {
  1381. nand_device_reset();
  1382. nand_reset();
  1383. }
  1384. mtk_nand_SetFeature(deviceinfo.feature_set.FeatureSet.sfeatureCmd,\
  1385. deviceinfo.feature_set.FeatureSet.readRetryAddress,\
  1386. (u8 *)&feature,4);
  1387. if (FALSE == defValue)
  1388. nand_set_command(deviceinfo.feature_set.FeatureSet.readRetryPreCmd);
  1389. }
  1390. u16 sandisk_19nm_rr_table[18] = {
  1391. 0x0000,
  1392. 0xFF0F, 0xEEFE, 0xDDFD, 0x11EE, //04h[7:4] | 07h[7:4] | 04h[3:0] | 05h[7:4]
  1393. 0x22ED, 0x33DF, 0xCDDE, 0x01DD,
  1394. 0x0211, 0x1222, 0xBD21, 0xAD32,
  1395. 0x9DF0, 0xBCEF, 0xACDC, 0x9CFF,
  1396. 0x0000
  1397. };
  1398. static void sandisk_19nm_rr_init(void)
  1399. {
  1400. u32 reg_val = 0;
  1401. u32 count = 0;
  1402. u32 timeout = 0xffff;
  1403. u32 acccon;
  1404. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  1405. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); //to fit read retry timing
  1406. nand_reset();
  1407. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  1408. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1409. nand_set_command(0x3B);
  1410. nand_set_command(0xB9);
  1411. for (count = 0; count < 9; count++) {
  1412. nand_set_command(0x53);
  1413. nand_set_address((0x04 + count), 0, 1, 0);
  1414. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1415. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1416. timeout = 0xffff;
  1417. WAIT_NFI_PIO_READY(timeout);
  1418. DRV_WriteReg32(NFI_DATAW_REG32, 0x00);
  1419. nand_reset();
  1420. }
  1421. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1422. }
  1423. static void sandisk_19nm_rr_loading(u32 retryCount, bool defValue)
  1424. {
  1425. u32 reg_val = 0;
  1426. u32 timeout = 0xffff;
  1427. u32 acccon;
  1428. u8 count;
  1429. u8 cmd_reg[4] = {0x4, 0x5, 0x7};
  1430. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  1431. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); //to fit read retry timing
  1432. nand_reset();
  1433. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  1434. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1435. if ((0 != retryCount) || defValue) {
  1436. nand_set_command(0xD6);
  1437. }
  1438. nand_set_command(0x3B);
  1439. nand_set_command(0xB9);
  1440. for (count = 0; count < 3; count++) {
  1441. nand_set_command(0x53);
  1442. nand_set_address(cmd_reg[count], 0, 1, 0);
  1443. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1444. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1445. timeout = 0xffff;
  1446. WAIT_NFI_PIO_READY(timeout);
  1447. if (count == 0)
  1448. DRV_WriteReg32(NFI_DATAW_REG32, (((sandisk_19nm_rr_table[retryCount] & 0xF000) >> 8) | ((sandisk_19nm_rr_table[retryCount] & 0x00F0) >> 4)));
  1449. else if (count == 1)
  1450. DRV_WriteReg32(NFI_DATAW_REG32, ((sandisk_19nm_rr_table[retryCount] & 0x000F) << 4));
  1451. else if (count == 2)
  1452. DRV_WriteReg32(NFI_DATAW_REG32, ((sandisk_19nm_rr_table[retryCount] & 0x0F00) >> 4));
  1453. nand_reset();
  1454. }
  1455. if (!defValue) {
  1456. nand_set_command(0xB6);
  1457. }
  1458. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  1459. }
  1460. static void mtk_nand_sandisk_19nm_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1461. {
  1462. if ((retryCount == 0) && (!defValue))
  1463. sandisk_19nm_rr_init();
  1464. sandisk_19nm_rr_loading(retryCount, defValue);
  1465. }
  1466. #define HYNIX_RR_TABLE_SIZE (1026) //hynix read retry table size
  1467. #define SINGLE_RR_TABLE_SIZE (64)
  1468. #define READ_RETRY_STEP (devinfo.feature_set.FeatureSet.readRetryCnt + devinfo.feature_set.FeatureSet.readRetryStart) // 8 step or 12 step to fix read retry table
  1469. #define HYNIX_16NM_RR_TABLE_SIZE ((READ_RETRY_STEP == 12)?(784):(528)) //hynix read retry table size
  1470. #define SINGLE_RR_TABLE_16NM_SIZE ((READ_RETRY_STEP == 12)?(48):(32))
  1471. u8 nand_hynix_rr_table[(HYNIX_RR_TABLE_SIZE+16)/16*16]; //align as 16 byte
  1472. #define NAND_HYX_RR_TBL_BUF nand_hynix_rr_table
  1473. static u8 real_hynix_rr_table_idx = 0;
  1474. static u32 g_hynix_retry_count = 0;
  1475. static bool hynix_rr_table_select(u8 table_index, flashdev_info *deviceinfo)
  1476. {
  1477. u32 i;
  1478. u32 table_size = (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM)?SINGLE_RR_TABLE_16NM_SIZE : SINGLE_RR_TABLE_SIZE;
  1479. for (i = 0; i < table_size; i++) {
  1480. u8 *temp_rr_table = (u8 *)NAND_HYX_RR_TBL_BUF+table_size*table_index*2+2;
  1481. u8 *temp_inversed_rr_table = (u8 *)NAND_HYX_RR_TBL_BUF+table_size*table_index*2+table_size+2;
  1482. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1483. temp_rr_table += 14;
  1484. temp_inversed_rr_table += 14;
  1485. }
  1486. if (0xFF != (temp_rr_table[i] ^ temp_inversed_rr_table[i]))
  1487. return FALSE; // error table
  1488. }
  1489. return TRUE; // correct table
  1490. }
  1491. static void HYNIX_RR_TABLE_READ(flashdev_info *deviceinfo)
  1492. {
  1493. u32 reg_val = 0;
  1494. u32 read_count = 0, max_count = HYNIX_RR_TABLE_SIZE;
  1495. u32 timeout = 0xffff;
  1496. u8* rr_table = (u8*)(NAND_HYX_RR_TBL_BUF);
  1497. u8 table_index = 0;
  1498. u8 add_reg1[3] = {0xFF, 0xCC};
  1499. u8 data_reg1[3] = {0x40, 0x4D};
  1500. u8 cmd_reg[6] = {0x16, 0x17, 0x04, 0x19, 0x00};
  1501. u8 add_reg2[6] = {0x00, 0x00, 0x00, 0x02, 0x00};
  1502. bool RR_TABLE_EXIST = TRUE;
  1503. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1504. read_count = 1;
  1505. add_reg1[1]= 0x38;
  1506. data_reg1[1] = 0x52;
  1507. max_count = HYNIX_16NM_RR_TABLE_SIZE;
  1508. if (READ_RETRY_STEP == 12) {
  1509. add_reg2[2] = 0x1F;
  1510. }
  1511. }
  1512. nand_device_reset();
  1513. // take care under sync mode. need change nand device inferface xiaolei
  1514. nand_reset();
  1515. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  1516. nand_set_command(0x36);
  1517. for (; read_count < 2; read_count++) {
  1518. nand_set_address(add_reg1[read_count],0,1,0);
  1519. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1520. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1521. timeout = 0xffff;
  1522. WAIT_NFI_PIO_READY(timeout);
  1523. DRV_WriteReg32(NFI_DATAW_REG32, data_reg1[read_count]);
  1524. nand_reset();
  1525. }
  1526. for (read_count = 0; read_count < 5; read_count++) {
  1527. nand_set_command(cmd_reg[read_count]);
  1528. }
  1529. for (read_count = 0; read_count < 5; read_count++) {
  1530. nand_set_address(add_reg2[read_count],0,1,0);
  1531. }
  1532. nand_set_command(0x30);
  1533. DRV_WriteReg(NFI_CNRNB_REG16, 0xF1);
  1534. timeout = 0xffff;
  1535. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  1536. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN);
  1537. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1538. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BRD | (2<< CON_NFI_SEC_SHIFT)));
  1539. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  1540. timeout = 0xffff;
  1541. read_count = 0; // how????
  1542. while ((read_count < max_count) && timeout ) {
  1543. WAIT_NFI_PIO_READY(timeout);
  1544. *rr_table++ = (U8)DRV_Reg32(NFI_DATAR_REG32);
  1545. read_count++;
  1546. timeout = 0xFFFF;
  1547. }
  1548. nand_device_reset();
  1549. // take care under sync mode. need change nand device inferface xiaolei
  1550. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  1551. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1552. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1553. nand_set_command(0x36);
  1554. nand_set_address(0x38,0,1,0);
  1555. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1556. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1557. WAIT_NFI_PIO_READY(timeout);
  1558. DRV_WriteReg32(NFI_DATAW_REG32, 0x00);
  1559. nand_reset();
  1560. nand_set_command(0x16);
  1561. nand_set_command(0x00);
  1562. nand_set_address(0x00,0,1,0);//dummy read, add don't care
  1563. nand_set_command(0x30);
  1564. } else {
  1565. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1566. nand_set_command(0x38);
  1567. }
  1568. timeout = 0xffff;
  1569. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--));
  1570. rr_table = (u8*)(NAND_HYX_RR_TBL_BUF);
  1571. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX) {
  1572. if ((rr_table[0] != 8) || (rr_table[1] != 8)) {
  1573. RR_TABLE_EXIST = FALSE;
  1574. ASSERT(0);
  1575. }
  1576. } else if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1577. for (read_count=0; read_count<8; read_count++) {
  1578. if ((rr_table[read_count] != 8) || (rr_table[read_count+8] != 4)) {
  1579. RR_TABLE_EXIST = FALSE;
  1580. break;
  1581. }
  1582. }
  1583. }
  1584. if (RR_TABLE_EXIST) {
  1585. for (table_index = 0 ; table_index < 8; table_index++) {
  1586. if (hynix_rr_table_select(table_index, deviceinfo)) {
  1587. real_hynix_rr_table_idx = table_index;
  1588. MSG(INIT, "Hynix rr_tbl_id %d\n",real_hynix_rr_table_idx);
  1589. break;
  1590. }
  1591. }
  1592. if (table_index == 8) {
  1593. ASSERT(0);
  1594. }
  1595. } else {
  1596. MSG(INIT, "Hynix RR table index error!\n");
  1597. }
  1598. }
  1599. static void HYNIX_Set_RR_Para(u32 rr_index, flashdev_info *deviceinfo)
  1600. {
  1601. u32 reg_val = 0;
  1602. u32 timeout=0xffff;
  1603. u8 count, max_count = 8;
  1604. u8 add_reg[9] = {0xCC, 0xBF, 0xAA, 0xAB, 0xCD, 0xAD, 0xAE, 0xAF};
  1605. u8 *hynix_rr_table = (u8 *)NAND_HYX_RR_TBL_BUF+SINGLE_RR_TABLE_SIZE*real_hynix_rr_table_idx*2+2;
  1606. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  1607. add_reg[0] = 0x38; //0x38, 0x39, 0x3A, 0x3B
  1608. for (count =1; count < 4; count++) {
  1609. add_reg[count] = add_reg[0] + count;
  1610. }
  1611. hynix_rr_table += 14;
  1612. max_count = 4;
  1613. }
  1614. nand_reset();
  1615. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  1616. for (count = 0; count < max_count; count++) {
  1617. nand_set_command(0x36);
  1618. nand_set_address(add_reg[count], 0, 1, 0);
  1619. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  1620. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  1621. timeout = 0xffff;
  1622. WAIT_NFI_PIO_READY(timeout);
  1623. DRV_WriteReg32(NFI_DATAW_REG32, hynix_rr_table[rr_index*max_count + count]);
  1624. nand_reset();
  1625. }
  1626. nand_set_command(0x16);
  1627. }
  1628. static void mtk_nand_hynix_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1629. {
  1630. if (defValue == FALSE) {
  1631. if (g_hynix_retry_count == READ_RETRY_STEP) {
  1632. g_hynix_retry_count = 0;
  1633. }
  1634. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  1635. //HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo);
  1636. g_hynix_retry_count ++;
  1637. }
  1638. //HYNIX_Set_RR_Para(retryCount, &deviceinfo);
  1639. }
  1640. static void mtk_nand_hynix_16nm_rrtry(flashdev_info deviceinfo, u32 retryCount, bool defValue)
  1641. {
  1642. if (defValue == FALSE) {
  1643. if (g_hynix_retry_count == READ_RETRY_STEP) {
  1644. g_hynix_retry_count = 0;
  1645. }
  1646. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  1647. //mb();
  1648. //HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo);
  1649. g_hynix_retry_count ++;
  1650. }
  1651. //HYNIX_Set_RR_Para(retryCount, &deviceinfo);
  1652. }
  1653. // sandisk 1y nm
  1654. u32 special_rrtry_setting[36]= {
  1655. 0x00000000,0x7C00007C,0x787C0004,0x74780078,
  1656. 0x7C007C08,0x787C7C00,0x74787C7C,0x70747C00,
  1657. 0x7C007800,0x787C7800,0x74787800,0x70747800,
  1658. 0x6C707800,0x00040400,0x7C000400,0x787C040C,
  1659. 0x7478040C,0x7C000810,0x00040810,0x04040C0C,
  1660. 0x00040C10,0x00081014,0x000C1418,0x7C040C0C,
  1661. 0x74787478,0x70747478,0x6C707478,0x686C7478,
  1662. 0x74787078,0x70747078,0x686C7078,0x6C707078,
  1663. 0x6C706C78,0x686C6C78,0x64686C78,0x686C6874,
  1664. 0x64686874,
  1665. };
  1666. u32 special_mlcslc_rrtry_setting[23]= {
  1667. 0x00,0x04,0x08,0x0C,0x10,0x14,0x7C,0x78,
  1668. 0x74,0x18,0x1C,0x20,0x70,0x6C,0x68,0x24,
  1669. 0x28,0x2C,0x64,0x60,0x5C,0x58,0x54,
  1670. };
  1671. static u32 mtk_nand_rrtry_setting(flashdev_info deviceinfo, enum readRetryType type, u32 retryStart, u32 loopNo)
  1672. {
  1673. u32 value;
  1674. //if(RTYPE_MICRON == type || RTYPE_SANDISK== type || RTYPE_TOSHIBA== type || RTYPE_HYNIX== type)
  1675. {
  1676. if (retryStart != 0xFFFFFFFF) {
  1677. value = retryStart+loopNo;
  1678. } else {
  1679. if (devinfo.tlcControl.slcopmodeEn)
  1680. value = special_mlcslc_rrtry_setting[loopNo];
  1681. else
  1682. value = special_rrtry_setting[loopNo];
  1683. }
  1684. }
  1685. return value;
  1686. }
  1687. typedef u32 (*rrtryFunctionType)(flashdev_info deviceinfo, u32 feature, bool defValue);
  1688. static rrtryFunctionType rtyFuncArray[]= {
  1689. mtk_nand_micron_rrtry,
  1690. mtk_nand_sandisk_rrtry,
  1691. mtk_nand_sandisk_19nm_rrtry,
  1692. mtk_nand_toshiba_rrtry,
  1693. mtk_nand_hynix_rrtry,
  1694. mtk_nand_hynix_16nm_rrtry
  1695. };
  1696. static void mtk_nand_rrtry_func(flashdev_info deviceinfo, u32 feature, bool defValue)
  1697. {
  1698. if (gVendor != VEND_NONE) {
  1699. rtyFuncArray[deviceinfo.feature_set.FeatureSet.rtype](deviceinfo, feature,defValue);
  1700. }
  1701. }
  1702. int nand_exec_read_page_hw(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  1703. {
  1704. int bRet;
  1705. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  1706. bool retry = FALSE;
  1707. bool readRetry = FALSE;
  1708. int retryCount = 0;
  1709. u32 reg_val;
  1710. u32 retryCountTotal;
  1711. u32 block;
  1712. u32 page_in_block;
  1713. u32 page_addr_ori;
  1714. u32 page_per_block;
  1715. page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  1716. page_addr_ori = u4RowAddr;
  1717. do {
  1718. mtk_nand_interface_switch();
  1719. if (devinfo.tlcControl.slcopmodeEn) { // slc mode
  1720. if (0xFF != devinfo.tlcControl.en_slc_mode_cmd) {
  1721. reg_val = DRV_Reg(NFI_CNFG_REG16);
  1722. reg_val &= ~CNFG_READ_EN;
  1723. reg_val &= ~CNFG_OP_MODE_MASK;
  1724. reg_val |= CNFG_OP_CUST;
  1725. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1726. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  1727. reg_val = DRV_Reg32(NFI_CON_REG16);
  1728. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  1729. /* issue reset operation */
  1730. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1731. if (devinfo.vendor == VEND_SANDISK) {
  1732. block = u4RowAddr / page_per_block;
  1733. page_in_block = u4RowAddr % page_per_block;
  1734. page_in_block <<= 1;
  1735. u4RowAddr = block*page_per_block + page_in_block;
  1736. dprintf(INFO,"nand_exec_read_page_hw SLC mode page_addr_ori:0x%x page_addr:0x%x\n",
  1737. page_addr_ori, u4RowAddr);
  1738. }
  1739. }
  1740. }
  1741. if (use_randomizer && u4RowAddr >= RAND_START_ADDR && retry == FALSE)
  1742. mtk_nand_turn_on_randomizer(page_addr_ori);
  1743. // else
  1744. // mtk_nand_interface_async();
  1745. bRet = ERR_RTN_SUCCESS;
  1746. if (nand_ready_for_read(nand, u4RowAddr, 0, true, pPageBuf)) {
  1747. if (!nand_read_page_data(pPageBuf, u4PageSize)) {
  1748. bRet = ERR_RTN_FAIL;
  1749. }
  1750. if (!nand_status_ready(STA_NAND_BUSY)) {
  1751. bRet = ERR_RTN_FAIL;
  1752. }
  1753. if (!nand_check_dececc_done(u4SecNum)) {
  1754. bRet = ERR_RTN_FAIL;
  1755. }
  1756. nand_read_fdm_data(pFDMBuf, u4SecNum);
  1757. if (!nand_check_bch_error(pPageBuf, u4SecNum - 1, u4RowAddr)) {
  1758. bRet = ERR_RTN_BCH_FAIL;
  1759. if (devinfo.vendor != VEND_NONE) {
  1760. readRetry = TRUE;
  1761. }
  1762. g_i4ErrNum++;
  1763. }
  1764. if (0 != (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  1765. memset(pPageBuf, 0xFF, u4PageSize);
  1766. memset(pFDMBuf, 0xFF, 8*u4SecNum);
  1767. }
  1768. nand_stop_read();
  1769. if ((devinfo.tlcControl.slcopmodeEn)
  1770. &&(0xFF != devinfo.tlcControl.dis_slc_mode_cmd)) {
  1771. reg_val = DRV_Reg32(NFI_CON_REG16);
  1772. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  1773. /* issue reset operation */
  1774. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1775. reg_val = DRV_Reg(NFI_CNFG_REG16);
  1776. reg_val &= ~CNFG_READ_EN;
  1777. reg_val &= ~CNFG_OP_MODE_MASK;
  1778. reg_val |= CNFG_OP_CUST;
  1779. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1780. nand_set_command(devinfo.tlcControl.dis_slc_mode_cmd);
  1781. }
  1782. }
  1783. if (use_randomizer)
  1784. mtk_nand_turn_off_randomizer();
  1785. if (bRet == ERR_RTN_BCH_FAIL) {
  1786. u32 feature = mtk_nand_rrtry_setting(devinfo, devinfo.feature_set.FeatureSet.rtype,devinfo.feature_set.FeatureSet.readRetryStart,retryCount);
  1787. retryCountTotal = devinfo.feature_set.FeatureSet.readRetryCnt;
  1788. if ((devinfo.tlcControl.slcopmodeEn)
  1789. && (devinfo.feature_set.FeatureSet.rtype == RTYPE_SANDISK))
  1790. retryCountTotal = 22;
  1791. if (retryCount < retryCountTotal) {
  1792. mtk_nand_rrtry_func(devinfo,feature,FALSE);
  1793. retryCount++;
  1794. } else {
  1795. feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  1796. // sandisk case 2/3/4
  1797. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_SANDISK) && (g_sandisk_retry_case < 2)) {
  1798. g_sandisk_retry_case++;
  1799. mtk_nand_rrtry_func(devinfo,feature,FALSE);
  1800. retryCount = 0;
  1801. } else {
  1802. mtk_nand_rrtry_func(devinfo,feature,TRUE);
  1803. readRetry = FALSE;
  1804. g_sandisk_retry_case = 0;
  1805. }
  1806. }
  1807. if (g_sandisk_retry_case == 1) {
  1808. nand_set_command(0x26);
  1809. }
  1810. } else {
  1811. if (retryCount != 0) {
  1812. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  1813. mtk_nand_rrtry_func(devinfo,feature,TRUE);
  1814. }
  1815. readRetry = FALSE;
  1816. g_sandisk_retry_case = 0;
  1817. }
  1818. if (TRUE == readRetry)
  1819. bRet = ERR_RTN_SUCCESS;
  1820. } while (readRetry);
  1821. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  1822. mtk_nand_turn_off_randomizer();
  1823. if (retryCount != 0) {
  1824. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  1825. if (bRet == ERR_RTN_SUCCESS) {
  1826. 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));
  1827. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX)) {
  1828. g_hynix_retry_count--;
  1829. }
  1830. } else {
  1831. MSG(INIT, "u4RowAddr:0x%x read retry fail\n",u4RowAddr);
  1832. }
  1833. mtk_nand_rrtry_func(devinfo,feature,TRUE);
  1834. g_sandisk_retry_case = 0;
  1835. }
  1836. return bRet;
  1837. }
  1838. static bool nand_exec_read_page(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  1839. {
  1840. int bRet = ERR_RTN_SUCCESS;
  1841. // u32 page_per_block = (BLOCK_SIZE/nand->page_size);
  1842. u32 block;
  1843. //int page_in_block = u4RowAddr % page_per_block;
  1844. u32 page_addr;
  1845. u32 mapped_block;
  1846. int i, start, len, offset;
  1847. struct nand_oobfree *free;
  1848. u8 oob[0x80];
  1849. //mapped_block = get_mapping_block_index(block);
  1850. page_addr= mtk_nand_page_transform((u64)u4RowAddr << g_nand_chip.page_shift,&block,&mapped_block);
  1851. //bRet = nand_exec_read_page_hw(nand, (mapped_block * page_per_block + page_in_block), u4PageSize, pPageBuf, oob);
  1852. bRet = nand_exec_read_page_hw(nand, page_addr, u4PageSize, pPageBuf, oob);
  1853. if (bRet == ERR_RTN_FAIL)
  1854. return false;
  1855. offset = 0;
  1856. free = nand->ecclayout->oobfree;
  1857. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES&&free[i].length; i++) {
  1858. start = free[i].offset;
  1859. len = free[i].length;
  1860. memcpy(pFDMBuf + offset, oob + start, len);
  1861. offset += len;
  1862. }
  1863. return bRet;
  1864. }
  1865. static bool nand_exec_write_page(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  1866. {
  1867. bool bRet = true;
  1868. u32 page_per_block = 0;
  1869. u32 block;
  1870. u32 page_in_block = u4RowAddr % page_per_block;
  1871. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  1872. u32 page_addr, page_addr_ori;
  1873. u32 mapped_block;
  1874. u32 reg_val;
  1875. page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  1876. page_in_block = u4RowAddr % page_per_block;
  1877. page_addr = mtk_nand_page_transform((u64)u4RowAddr << g_nand_chip.page_shift,&block,&mapped_block);
  1878. page_addr_ori = page_addr;
  1879. mtk_nand_interface_switch();
  1880. if (devinfo.tlcControl.slcopmodeEn) { // slc mode
  1881. if (0xFF != devinfo.tlcControl.en_slc_mode_cmd) {
  1882. reg_val = DRV_Reg(NFI_CNFG_REG16);
  1883. reg_val &= ~CNFG_READ_EN;
  1884. reg_val &= ~CNFG_OP_MODE_MASK;
  1885. reg_val |= CNFG_OP_CUST;
  1886. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1887. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  1888. reg_val = DRV_Reg32(NFI_CON_REG16);
  1889. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  1890. /* issue reset operation */
  1891. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1892. if (devinfo.vendor == VEND_SANDISK) {
  1893. block = page_addr / page_per_block;
  1894. page_in_block = page_addr % page_per_block;
  1895. page_in_block <<= 1;
  1896. page_addr = block*page_per_block + page_in_block;
  1897. dprintf(INFO,"nand_exec_write_page SLC mode page_addr_ori:0x%x page_addr:0x%x\n",
  1898. page_addr_ori, page_addr);
  1899. }
  1900. }
  1901. }
  1902. if (use_randomizer && page_addr >= RAND_START_ADDR)
  1903. mtk_nand_turn_on_randomizer(page_addr_ori);
  1904. // else
  1905. // mtk_nand_interface_async();
  1906. if (nand_ready_for_write(nand, page_addr, pPageBuf)) {
  1907. nand_write_fdm_data(pFDMBuf, u4SecNum);
  1908. if (!nand_write_page_data(pPageBuf, u4PageSize)) {
  1909. bRet = false;
  1910. }
  1911. if (!nand_check_RW_count(nand, u4PageSize)) {
  1912. bRet = false;
  1913. }
  1914. nand_stop_write();
  1915. nand_set_command(NAND_CMD_PAGE_PROG);
  1916. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  1917. if ((devinfo.tlcControl.slcopmodeEn)
  1918. &&(0xFF != devinfo.tlcControl.dis_slc_mode_cmd)) {
  1919. reg_val = DRV_Reg32(NFI_CON_REG16);
  1920. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  1921. /* issue reset operation */
  1922. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1923. reg_val = DRV_Reg(NFI_CNFG_REG16);
  1924. reg_val &= ~CNFG_READ_EN;
  1925. reg_val &= ~CNFG_OP_MODE_MASK;
  1926. reg_val |= CNFG_OP_CUST;
  1927. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1928. nand_set_command(devinfo.tlcControl.dis_slc_mode_cmd);
  1929. }
  1930. }
  1931. if (use_randomizer && page_addr >= RAND_START_ADDR)
  1932. mtk_nand_turn_off_randomizer();
  1933. return bRet;
  1934. }
  1935. static bool nand_exec_write_page_raw(struct nand_chip *nand, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  1936. {
  1937. bool bRet = true;
  1938. u32 page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  1939. int block = u4RowAddr / page_per_block;
  1940. int page_in_block = u4RowAddr % page_per_block;
  1941. u32 u4SecNum = u4PageSize >> nand->sector_shift;
  1942. u32 reg_val;
  1943. u32 page_addr_ori;
  1944. page_addr_ori = u4RowAddr;
  1945. mtk_nand_interface_switch();
  1946. if (devinfo.tlcControl.slcopmodeEn) { // slc mode
  1947. dprintf(INFO,"nand_exec_write_page_raw SLC mode %x\n",u4RowAddr);
  1948. if (0xFF != devinfo.tlcControl.en_slc_mode_cmd) {
  1949. reg_val = DRV_Reg(NFI_CNFG_REG16);
  1950. reg_val &= ~CNFG_READ_EN;
  1951. reg_val &= ~CNFG_OP_MODE_MASK;
  1952. reg_val |= CNFG_OP_CUST;
  1953. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1954. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  1955. reg_val = DRV_Reg32(NFI_CON_REG16);
  1956. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  1957. /* issue reset operation */
  1958. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1959. if (devinfo.vendor == VEND_SANDISK) {
  1960. block = u4RowAddr / page_per_block;
  1961. page_in_block = u4RowAddr % page_per_block;
  1962. page_in_block <<= 1;
  1963. u4RowAddr = block*page_per_block + page_in_block;
  1964. dprintf(INFO,"nand_exec_write_page_raw SLC mode page_addr_ori:0x%x page_addr:0x%x\n",
  1965. page_addr_ori, u4RowAddr);
  1966. }
  1967. }
  1968. }
  1969. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  1970. mtk_nand_turn_on_randomizer(page_addr_ori);
  1971. // else
  1972. // mtk_nand_interface_async();
  1973. if (nand_ready_for_write(nand, u4RowAddr, pPageBuf)) {
  1974. nand_write_fdm_data(pFDMBuf, u4SecNum);
  1975. if (!nand_write_page_data(pPageBuf, u4PageSize)) {
  1976. bRet = false;
  1977. }
  1978. if (!nand_check_RW_count(nand, u4PageSize)) {
  1979. bRet = false;
  1980. }
  1981. nand_stop_write();
  1982. nand_set_command(NAND_CMD_PAGE_PROG);
  1983. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  1984. }
  1985. if ((devinfo.tlcControl.slcopmodeEn)
  1986. &&(0xFF != devinfo.tlcControl.dis_slc_mode_cmd)) {
  1987. reg_val = DRV_Reg32(NFI_CON_REG16);
  1988. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  1989. /* issue reset operation */
  1990. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  1991. reg_val = DRV_Reg(NFI_CNFG_REG16);
  1992. reg_val &= ~CNFG_READ_EN;
  1993. reg_val &= ~CNFG_OP_MODE_MASK;
  1994. reg_val |= CNFG_OP_CUST;
  1995. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  1996. nand_set_command(devinfo.tlcControl.dis_slc_mode_cmd);
  1997. }
  1998. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  1999. mtk_nand_turn_off_randomizer();
  2000. return bRet;
  2001. }
  2002. static bool nand_read_oob_raw(struct nand_chip *chip, u32 page_addr, u32 length, u8 * buf)
  2003. {
  2004. u32 sector = 0;
  2005. u32 col_addr = 0;
  2006. u32 spare_per_sec = devinfo.sparesize>>(chip->page_shift-chip->sector_shift);
  2007. if (length > 32 || length % OOB_AVAIL_PER_SECTOR || !buf) {
  2008. dprintf(INFO,"[%s] invalid parameter, length: %d, buf: %p\n", __FUNCTION__, length, buf);
  2009. return false;
  2010. }
  2011. while (length > 0) {
  2012. col_addr = chip->sector_size+ sector * (chip->sector_size + spare_per_sec);
  2013. if (!nand_ready_for_read(chip, page_addr, col_addr, false, NULL))
  2014. return false;
  2015. if (!nand_mcu_read_data(buf, length))
  2016. return false;
  2017. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BRD);
  2018. sector++;
  2019. length -= OOB_AVAIL_PER_SECTOR;
  2020. }
  2021. return true;
  2022. }
  2023. bool nand_block_bad_hw(struct nand_chip * nand, u64 offset)
  2024. {
  2025. u32 page_per_block = BLOCK_SIZE / nand->page_size;
  2026. u32 page_addr;
  2027. u32 block;
  2028. u32 mapped_block;
  2029. //mapped_block = get_mapping_block_index(block);
  2030. page_addr = mtk_nand_page_transform(offset,&block,&mapped_block);
  2031. memset(oob_buf_temp, 0,1024);
  2032. page_addr &= ~(page_per_block - 1);
  2033. if (FALSE == nand_exec_read_page_hw(nand, page_addr, nand->page_size, data_buf_temp , oob_buf_temp)) {
  2034. dprintf(INFO,"nand_read_oob_raw return fail\n");
  2035. }
  2036. if (oob_buf_temp[0] != 0xff) {
  2037. dprintf(INFO,"Bad block detect at block 0x%x, oob_buf[0] is %x\n", page_addr / page_per_block, oob_buf_temp[0]);
  2038. return true;
  2039. }
  2040. return false;
  2041. }
  2042. static bool nand_block_bad(struct nand_chip *nand, u32 page_addr)
  2043. {
  2044. //u32 page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift);
  2045. //int block = page_addr / page_per_block;
  2046. //int mapped_block = get_mapping_block_index(block);
  2047. return nand_block_bad_hw(nand, (((u64)page_addr) << nand->page_shift));
  2048. }
  2049. //not support un-block-aligned write
  2050. static int nand_part_write(part_dev_t * dev, uchar * src, u64 dst, int size, int id)
  2051. {
  2052. _dprintf("%s\n", __func__);
  2053. struct nand_chip *nand = (struct nand_chip *)dev->blkdev;
  2054. u8 res = 0;
  2055. u32 u4PageSize = 1 << nand->page_shift;
  2056. u32 u4PageNumPerBlock = BLOCK_SIZE/nand->page_size/2;
  2057. u32 u4BlkEnd = (u32)(nand->chipsize / BLOCK_SIZE);
  2058. u32 u4BlkAddr = (u32)(dst / BLOCK_SIZE);
  2059. u32 u4ColAddr = dst & (u4PageSize - 1);
  2060. u32 u4RowAddr = dst / nand->page_size;
  2061. u32 u4EraseAddr;
  2062. u32 u4RowEnd;
  2063. u32 u4WriteLen = 0;
  2064. u32 i4Len;
  2065. bool ret;
  2066. // u32 mapped;
  2067. u32 k = 0;
  2068. //mtk_nand_page_transform((u64)dst,&u4BlkAddr,&mapped);
  2069. for (k = 0; k < sizeof(g_kCMD.au1OOB); k++)
  2070. *(g_kCMD.au1OOB + k) = 0xFF;
  2071. MSG(ERR, "dst 0x%llx\n", dst);
  2072. while (((u32)size > u4WriteLen) && (u4BlkAddr < u4BlkEnd)) {
  2073. #if 1
  2074. if (!u4ColAddr) {
  2075. MSG(ERR, "Erase the block of 0x%08x\n", u4BlkAddr);
  2076. u4EraseAddr = u4BlkAddr * u4PageNumPerBlock *2;
  2077. nand_reset();
  2078. nand_set_mode(CNFG_OP_ERASE);
  2079. nand_set_command(NAND_CMD_ERASE_1);
  2080. nand_set_address(0, u4EraseAddr, 0, 3);
  2081. nand_set_command(NAND_CMD_ERASE_2);
  2082. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  2083. ret = mtk_nand_read_status();
  2084. }
  2085. #else
  2086. if (__nand_erase(dst)== FALSE) {
  2087. MSG(ERR, "erase fail");
  2088. mark_block_bad ((u64)dst);
  2089. }
  2090. #endif
  2091. // res = nand_block_bad(nand, ((u4BlkAddr >> 1) * u4PageNumPerBlock));
  2092. if (!res) {
  2093. u4RowEnd = (u4RowAddr + u4PageNumPerBlock) & (~u4PageNumPerBlock + 1);
  2094. for (; u4RowAddr < u4RowEnd; u4RowAddr++) {
  2095. i4Len = min(size - u4WriteLen, u4PageSize - u4ColAddr);
  2096. if (0 >= i4Len) {
  2097. break;
  2098. }
  2099. if ((u4ColAddr == 0) && (i4Len == u4PageSize)) {
  2100. memcpy(data_buf_temp,(src + u4WriteLen),u4PageSize);
  2101. nand_exec_write_page(nand, u4RowAddr, u4PageSize, data_buf_temp, g_kCMD.au1OOB);
  2102. } else {
  2103. nand_exec_read_page(nand, u4RowAddr, u4PageSize, nand->buffers->databuf, g_kCMD.au1OOB);
  2104. memcpy(nand->buffers->databuf + u4ColAddr, src + u4WriteLen, i4Len);
  2105. nand_exec_write_page(nand, u4RowAddr, u4PageSize, nand->buffers->databuf, g_kCMD.au1OOB);
  2106. }
  2107. u4WriteLen += i4Len;
  2108. u4ColAddr = (u4ColAddr + i4Len) & (u4PageSize - 1);
  2109. }
  2110. } else {
  2111. dprintf(INFO,"Detect bad block at block 0x%x\n", u4BlkAddr);
  2112. u4RowAddr += u4PageNumPerBlock;
  2113. }
  2114. u4BlkAddr++;
  2115. }
  2116. return (int)u4WriteLen;
  2117. }
  2118. static int nand_part_read(part_dev_t * dev, u64 source, uchar * dst, int size, int id)
  2119. {
  2120. struct nand_chip *nand = (struct nand_chip *)dev->blkdev;
  2121. uint8_t res;
  2122. u32 u4PageSize = 1 << nand->page_shift;
  2123. u32 u4PageNumPerBlock = BLOCK_SIZE/nand->page_size;
  2124. u32 u4BlkEnd = (u32)(nand->chipsize / BLOCK_SIZE);
  2125. u32 u4BlkAddr = (u32)(source / BLOCK_SIZE);
  2126. u32 u4ColAddr = (u32)(source & (u4PageSize - 1));
  2127. u32 u4RowAddr = (u32)(source/nand->page_size);
  2128. u32 u4RowEnd;
  2129. // u32 mapped;
  2130. u32 u4ReadLen = 0;
  2131. u32 i4Len;
  2132. //mtk_nand_page_transform((u64)source,&u4BlkAddr,&mapped);
  2133. while (((u32)size > u4ReadLen) && (u4BlkAddr < u4BlkEnd)) {
  2134. res = nand_block_bad(nand, (u4BlkAddr * u4PageNumPerBlock));
  2135. if (!res) {
  2136. u4RowEnd = (u4RowAddr + u4PageNumPerBlock) & (~u4PageNumPerBlock + 1);
  2137. for (; u4RowAddr < u4RowEnd; u4RowAddr++) {
  2138. i4Len = min(size - u4ReadLen, u4PageSize - u4ColAddr);
  2139. if (0 >= i4Len) {
  2140. break;
  2141. }
  2142. if ((u4ColAddr == 0) && (i4Len == u4PageSize)) {
  2143. nand_exec_read_page(nand, u4RowAddr, u4PageSize, dst + u4ReadLen, g_kCMD.au1OOB);
  2144. } else {
  2145. nand_exec_read_page(nand, u4RowAddr, u4PageSize, nand->buffers->databuf, g_kCMD.au1OOB);
  2146. memcpy(dst + u4ReadLen, nand->buffers->databuf + u4ColAddr, i4Len);
  2147. }
  2148. u4ReadLen += i4Len;
  2149. u4ColAddr = (u4ColAddr + i4Len) & (u4PageSize - 1);
  2150. }
  2151. } else {
  2152. dprintf(INFO,"Detect bad block at block 0x%x\n", u4BlkAddr);
  2153. u4RowAddr += u4PageNumPerBlock;
  2154. }
  2155. u4BlkAddr++;
  2156. }
  2157. return (int)u4ReadLen;
  2158. }
  2159. static void nand_command_bp(struct nand_chip *nand_chip, unsigned command, int column, int page_addr)
  2160. {
  2161. struct nand_chip *nand = nand_chip;
  2162. u32 timeout;
  2163. switch (command) {
  2164. case NAND_CMD_SEQIN:
  2165. if (g_kCMD.u4RowAddr != (u32)page_addr) {
  2166. memset(g_kCMD.au1OOB, 0xFF, sizeof(g_kCMD.au1OOB));
  2167. g_kCMD.pDataBuf = NULL;
  2168. }
  2169. g_kCMD.u4RowAddr = page_addr;
  2170. g_kCMD.u4ColAddr = column;
  2171. break;
  2172. case NAND_CMD_PAGE_PROG:
  2173. if (g_kCMD.pDataBuf || (0xFF != g_kCMD.au1OOB[0])) {
  2174. u8 *pDataBuf = g_kCMD.pDataBuf ? g_kCMD.pDataBuf : nand->buffers->databuf;
  2175. nand_exec_write_page(nand, g_kCMD.u4RowAddr, nand->writesize, pDataBuf, g_kCMD.au1OOB);
  2176. g_kCMD.u4RowAddr = (u32) - 1;
  2177. g_kCMD.u4OOBRowAddr = (u32) - 1;
  2178. }
  2179. break;
  2180. case NAND_CMD_READ_OOB:
  2181. g_kCMD.u4RowAddr = page_addr;
  2182. g_kCMD.u4ColAddr = column + nand->writesize;
  2183. g_i4ErrNum = 0;
  2184. break;
  2185. case NAND_CMD_READ_0:
  2186. g_kCMD.u4RowAddr = page_addr;
  2187. g_kCMD.u4ColAddr = column;
  2188. g_i4ErrNum = 0;
  2189. break;
  2190. case NAND_CMD_ERASE_1:
  2191. nand_reset();
  2192. nand_set_mode(CNFG_OP_ERASE);
  2193. nand_set_command(NAND_CMD_ERASE_1);
  2194. nand_set_address(0, page_addr, 0, devinfo.addr_cycle - 2);
  2195. break;
  2196. case NAND_CMD_ERASE_2:
  2197. nand_set_command(NAND_CMD_ERASE_2);
  2198. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  2199. break;
  2200. case NAND_CMD_STATUS:
  2201. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2202. nand_reset();
  2203. nand_set_mode(CNFG_OP_SRD);
  2204. nand_set_command(NAND_CMD_STATUS);
  2205. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_NOB_MASK);
  2206. DRV_WriteReg32(NFI_CON_REG16, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  2207. break;
  2208. case NAND_CMD_RESET:
  2209. nand_reset();
  2210. break;
  2211. case NAND_CMD_READ_ID:
  2212. NFI_ISSUE_COMMAND(NAND_CMD_RESET, 0, 0, 0, 0);
  2213. timeout = TIMEOUT_4;
  2214. while (timeout) {
  2215. timeout--;
  2216. }
  2217. nand_reset();
  2218. /* Disable HW ECC */
  2219. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2220. NFI_CLN_REG32(NFI_PAGEFMT_REG16, PAGEFMT_DBYTE_EN);
  2221. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN | CNFG_BYTE_RW);
  2222. nand_set_mode(CNFG_OP_SRD);
  2223. nand_set_command(NAND_CMD_READ_ID);
  2224. nand_set_address(0, 0, 1, 0);
  2225. DRV_WriteReg32(NFI_CON_REG16, CON_NFI_SRD);
  2226. while (DRV_Reg32(NFI_STA_REG32) & STA_DATAR_STATE) ;
  2227. break;
  2228. default:
  2229. dprintf(INFO,"[ERR] nand_command_bp : unknow command %d\n", command);
  2230. break;
  2231. }
  2232. }
  2233. static u_char nand_read_byte(void)
  2234. {
  2235. /* Check the PIO bit is ready or not */
  2236. unsigned int timeout = TIMEOUT_4;
  2237. WAIT_NFI_PIO_READY(timeout);
  2238. return DRV_Reg8(NFI_DATAR_REG32);
  2239. }
  2240. #if 0
  2241. static void nand_read_buf(struct nand_chip *nand, u_char * buf, int len)
  2242. {
  2243. struct nand_chip *nand = nand;
  2244. struct CMD *pkCMD = &g_kCMD;
  2245. u32 u4ColAddr = pkCMD->u4ColAddr;
  2246. u32 u4PageSize = nand->writesize;
  2247. if (u4ColAddr < u4PageSize) {
  2248. if ((u4ColAddr == 0) && (len >= u4PageSize)) {
  2249. nand_exec_read_page(nand, pkCMD->u4RowAddr, u4PageSize, buf, pkCMD->au1OOB);
  2250. if (len > u4PageSize) {
  2251. u32 u4Size = min(len - u4PageSize,
  2252. sizeof(pkCMD->au1OOB));
  2253. memcpy(buf + u4PageSize, pkCMD->au1OOB, u4Size);
  2254. }
  2255. } else {
  2256. nand_exec_read_page(nand, pkCMD->u4RowAddr, u4PageSize, nand->buffers->databuf, pkCMD->au1OOB);
  2257. memcpy(buf, nand->buffers->databuf + u4ColAddr, len);
  2258. }
  2259. pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  2260. } else {
  2261. u32 u4Offset = u4ColAddr - u4PageSize;
  2262. u32 u4Size = min(len - u4PageSize - u4Offset, sizeof(pkCMD->au1OOB));
  2263. if (pkCMD->u4OOBRowAddr != pkCMD->u4RowAddr) {
  2264. nand_exec_read_page(nand, pkCMD->u4RowAddr, u4PageSize, nand->buffers->databuf, pkCMD->au1OOB);
  2265. pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  2266. }
  2267. memcpy(buf, pkCMD->au1OOB + u4Offset, u4Size);
  2268. }
  2269. pkCMD->u4ColAddr += len;
  2270. }
  2271. static void nand_write_buf(struct nand_chip nand, const u_char * buf, int len)
  2272. {
  2273. struct CMD *pkCMD = &g_kCMD;
  2274. u32 u4ColAddr = pkCMD->u4ColAddr;
  2275. u32 u4PageSize = nand->writesize;
  2276. u32 i;
  2277. if (u4ColAddr >= u4PageSize) {
  2278. u8 *pOOB = pkCMD->au1OOB;
  2279. u32 u4Size = min(len, sizeof(pkCMD->au1OOB));
  2280. for (i = 0; i < u4Size; i++) {
  2281. pOOB[i] &= buf[i];
  2282. }
  2283. } else {
  2284. pkCMD->pDataBuf = (u8 *) buf;
  2285. }
  2286. pkCMD->u4ColAddr += len;
  2287. }
  2288. #endif
  2289. void lk_nand_irq_handler(unsigned int irq)
  2290. {
  2291. u32 inte,sts;
  2292. mt_irq_ack(irq);
  2293. inte = DRV_Reg16(NFI_INTR_EN_REG16);
  2294. sts = DRV_Reg16(NFI_INTR_REG16);
  2295. //MSG(INT, "[lk_nand_irq_handler]irq %x enable:%x %x\n",irq,inte,sts);
  2296. if (sts & inte) {
  2297. // dprintf(INFO,"[lk_nand_irq_handler]send event,\n");
  2298. DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2299. DRV_WriteReg16(NFI_INTR_REG16,sts);
  2300. event_signal(&nand_int_event,0);
  2301. }
  2302. return;
  2303. }
  2304. int nand_init_device(struct nand_chip *nand)
  2305. {
  2306. int index;//j, busw,;
  2307. u8 id[NAND_MAX_ID];
  2308. u32 spare_bit;
  2309. u32 spare_per_sec;
  2310. u32 ecc_bit;
  2311. int bmt_sz = 0;
  2312. memset(&devinfo, 0, sizeof(devinfo));
  2313. g_bInitDone = FALSE;
  2314. g_kCMD.u4OOBRowAddr = (u32) - 1;
  2315. #ifdef MACH_FPGA // FPGA NAND is placed at CS1
  2316. DRV_WriteReg16(NFI_CSEL_REG16, 0);
  2317. #else
  2318. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  2319. #endif
  2320. DRV_WriteReg32(NFI_ACCCON_REG32, NFI_DEFAULT_ACCESS_TIMING);
  2321. DRV_WriteReg16(NFI_CNFG_REG16, 0);
  2322. DRV_WriteReg32(NFI_PAGEFMT_REG16, 4);
  2323. nand->nand_ecc_mode = NAND_ECC_HW;
  2324. //reset to legacy for read id
  2325. nand_reset();
  2326. NFI_CLN_REG32(MT_CLKMUX_NFI1X_INFRA_SEL,0x80);
  2327. // clkmux_sel(MT_CLKMUX_NFI1X_INFRA_SEL, MT_CG_SYS_26M,"NFI");// TODO
  2328. NFI_SET_REG32(NFI_DEBUG_CON1_REG16,NFI_BYPASS);
  2329. //clear bypass of ecc
  2330. NFI_SET_REG32(ECC_BYPASS_REG32,ECC_BYPASS);
  2331. DRV_WriteReg32(NFI_ACCCON_REG32,0x31C083F9); //very safe timing
  2332. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0);
  2333. DDR_INTERFACE = FALSE;
  2334. nand_reset();
  2335. DRV_WriteReg(NFI_CNFG_REG16, CNFG_OP_RESET);
  2336. nand_set_command(NAND_CMD_RESET);
  2337. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN));
  2338. nand_reset();
  2339. nand_command_bp(&g_nand_chip, NAND_CMD_READ_ID, 0, 0);
  2340. dprintf(INFO, "NAND ID: ");
  2341. for (index = 0; index < NAND_MAX_ID; index++) {
  2342. id[index] = nand_read_byte();
  2343. }
  2344. if (!get_device_info(id, &devinfo)) {
  2345. dprintf(INFO,"NAND unsupport\n");
  2346. return -1;
  2347. }
  2348. nand->name = devinfo.devciename;
  2349. nand->chipsize = ((u64)devinfo.totalsize) << 10;
  2350. if (devinfo.sectorsize == 512)
  2351. nand->erasesize = devinfo.blocksize << 10;
  2352. else
  2353. nand->erasesize = (devinfo.blocksize << 10)/2;
  2354. BLOCK_SIZE = devinfo.blocksize << 10;
  2355. nand->phys_erase_shift = uffs(nand->erasesize) - 1;
  2356. nand->page_size = devinfo.pagesize;
  2357. nand->writesize = devinfo.pagesize;
  2358. nand->page_shift = uffs(nand->page_size) - 1;
  2359. nand->oobblock = nand->page_size;
  2360. nand->bus16 = devinfo.iowidth;
  2361. nand->id_length = devinfo.id_length;
  2362. nand->sector_size = NAND_SECTOR_SIZE;
  2363. nand->sector_shift = 9;
  2364. if (devinfo.sectorsize == 1024) {
  2365. nand->sector_size = 1024;
  2366. nand->sector_shift = 10;
  2367. NFI_CLN_REG32(NFI_PAGEFMT_REG16, PAGEFMT_SECTOR_SEL);
  2368. }
  2369. for (index = 0; index < devinfo.id_length; index++) {
  2370. nand->id[index] = id[index];
  2371. }
  2372. #if 1
  2373. if (devinfo.vendor != VEND_NONE) {
  2374. if (devinfo.feature_set.FeatureSet.Async_timing.feature != 0xFF) {
  2375. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  2376. mtk_nand_SetFeature((u16) feature_set->sfeatureCmd, \
  2377. feature_set->Async_timing.address, (u8*)&feature_set->Async_timing.feature,\
  2378. sizeof(feature_set->Async_timing.feature));
  2379. }
  2380. }
  2381. #endif
  2382. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.timmingsetting);
  2383. spare_per_sec = devinfo.sparesize>>(nand->page_shift-nand->sector_shift);
  2384. switch (spare_per_sec) {
  2385. case 16:
  2386. spare_bit = PAGEFMT_SPARE_16;
  2387. ecc_bit = 4;
  2388. spare_per_sec = 16;
  2389. break;
  2390. case 26:
  2391. case 27:
  2392. case 28:
  2393. spare_bit = PAGEFMT_SPARE_26;
  2394. ecc_bit = 10;
  2395. spare_per_sec = 26;
  2396. break;
  2397. case 32:
  2398. ecc_bit = 12;
  2399. if (devinfo.sectorsize == 1024)
  2400. spare_bit = PAGEFMT_SPARE_32_1KS;
  2401. else
  2402. spare_bit = PAGEFMT_SPARE_32;
  2403. spare_per_sec = 32;
  2404. break;
  2405. case 40:
  2406. ecc_bit = 18;
  2407. spare_bit = PAGEFMT_SPARE_40;
  2408. spare_per_sec = 40;
  2409. break;
  2410. case 44:
  2411. ecc_bit = 20;
  2412. spare_bit = PAGEFMT_SPARE_44;
  2413. spare_per_sec = 44;
  2414. break;
  2415. case 48:
  2416. case 49:
  2417. ecc_bit = 22;
  2418. spare_bit = PAGEFMT_SPARE_48;
  2419. spare_per_sec = 48;
  2420. break;
  2421. case 50:
  2422. case 51:
  2423. ecc_bit = 24;
  2424. spare_bit = PAGEFMT_SPARE_50;
  2425. spare_per_sec = 50;
  2426. break;
  2427. case 52:
  2428. case 54:
  2429. case 56:
  2430. ecc_bit = 24;
  2431. if (devinfo.sectorsize == 1024)
  2432. spare_bit = PAGEFMT_SPARE_52_1KS;
  2433. else
  2434. spare_bit = PAGEFMT_SPARE_52;
  2435. spare_per_sec = 32;
  2436. break;
  2437. case 62:
  2438. case 63:
  2439. ecc_bit = 28;
  2440. spare_bit = PAGEFMT_SPARE_62;
  2441. spare_per_sec = 62;
  2442. break;
  2443. case 64:
  2444. ecc_bit = 32;
  2445. if (devinfo.sectorsize == 1024)
  2446. spare_bit = PAGEFMT_SPARE_64_1KS;
  2447. else
  2448. spare_bit = PAGEFMT_SPARE_64;
  2449. spare_per_sec = 64;
  2450. break;
  2451. case 72:
  2452. ecc_bit = 36;
  2453. if (devinfo.sectorsize == 1024)
  2454. spare_bit = PAGEFMT_SPARE_72_1KS;
  2455. spare_per_sec = 72;
  2456. break;
  2457. case 80:
  2458. ecc_bit = 40;
  2459. if (devinfo.sectorsize == 1024)
  2460. spare_bit = PAGEFMT_SPARE_80_1KS;
  2461. spare_per_sec = 80;
  2462. break;
  2463. case 88:
  2464. ecc_bit = 44;
  2465. if (devinfo.sectorsize == 1024)
  2466. spare_bit = PAGEFMT_SPARE_88_1KS;
  2467. spare_per_sec = 88;
  2468. break;
  2469. case 96:
  2470. case 98:
  2471. ecc_bit = 48;
  2472. if (devinfo.sectorsize == 1024)
  2473. spare_bit = PAGEFMT_SPARE_96_1KS;
  2474. spare_per_sec = 96;
  2475. break;
  2476. case 100:
  2477. case 102:
  2478. case 104:
  2479. ecc_bit = 52;
  2480. if (devinfo.sectorsize == 1024)
  2481. spare_bit = PAGEFMT_SPARE_100_1KS;
  2482. spare_per_sec = 100;
  2483. break;
  2484. case 124:
  2485. case 126:
  2486. case 128:
  2487. ecc_bit = 60;
  2488. if (devinfo.sectorsize == 1024)
  2489. spare_bit = PAGEFMT_SPARE_124_1KS;
  2490. spare_per_sec = 124;
  2491. break;
  2492. default:
  2493. dprintf(INFO,"[NAND]: NFI not support oobsize: %x\n", spare_per_sec);
  2494. while (1);
  2495. return -1;
  2496. }
  2497. devinfo.sparesize = spare_per_sec<<(nand->page_shift-nand->sector_shift);
  2498. MSG(INFO, "[NAND]nand eccbit %d , sparesize %d\n",ecc_bit,devinfo.sparesize);
  2499. if (!devinfo.sparesize) {
  2500. nand->oobsize = (8 << ((id[3] >> 2) & 0x01)) * (nand->oobblock / nand->sector_size); //FIX ME ,kai
  2501. } else {
  2502. nand->oobsize = devinfo.sparesize;
  2503. }
  2504. nand->buffers = &nBuf;//malloc(sizeof(struct nand_buffers));
  2505. if (nand->bus16 == IO_WIDTH_16) {
  2506. NFI_SET_REG32(NFI_PAGEFMT_REG16, PAGEFMT_DBYTE_EN);
  2507. nand->options |= NAND_BUSWIDTH_16;
  2508. }
  2509. if (16384 == nand->oobblock) {
  2510. NFI_SET_REG32(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_16K_1KS);
  2511. nand->ecclayout = &nand_oob_128;
  2512. } else if (8192 == nand->oobblock) {
  2513. NFI_SET_REG32(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_8K_1KS);
  2514. nand->ecclayout = &nand_oob_128;
  2515. } else if (4096 == nand->oobblock) {
  2516. if (devinfo.sectorsize == 512)
  2517. NFI_SET_REG32(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K);
  2518. else
  2519. NFI_SET_REG32(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K_1KS);
  2520. nand->ecclayout = &nand_oob_128;
  2521. } else if (2048 == nand->oobblock) {
  2522. if (devinfo.sectorsize == 512)
  2523. NFI_SET_REG32(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K);
  2524. else
  2525. NFI_SET_REG32(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K_1KS);
  2526. nand->ecclayout = &nand_oob_64;
  2527. }
  2528. if (nand->nand_ecc_mode == NAND_ECC_HW) {
  2529. NFI_SET_REG32(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2530. ECC_Config(ecc_bit);
  2531. nand_configure_fdm(NAND_FDM_PER_SECTOR);
  2532. }
  2533. DRV_Reg16(NFI_INTR_REG16);
  2534. DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2535. if (en_interrupt) {
  2536. event_init(&nand_int_event,false,EVENT_FLAG_AUTOUNSIGNAL);
  2537. mt_irq_set_sens(MT_NFI_IRQ_ID, MT65xx_EDGE_SENSITIVE);
  2538. mt_irq_set_polarity(MT_NFI_IRQ_ID, MT65xx_POLARITY_LOW);
  2539. mt_irq_unmask(MT_NFI_IRQ_ID);
  2540. }
  2541. if (devinfo.vendor != VEND_NONE) {
  2542. mtk_nand_randomizer_config(&devinfo.feature_set.randConfig,0);
  2543. DRV_WriteReg32(NFI_DLYCTRL_REG32,0x8001);
  2544. DRV_WriteReg32(NFI_DQS_DELAY_CTRL,0x000F0000); //temp
  2545. DRV_WriteReg32(NFI_DQS_DELAY_MUX,0x3); //temp
  2546. }
  2547. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX))
  2548. HYNIX_RR_TABLE_READ(&devinfo);
  2549. g_nand_size = nand->chipsize;
  2550. bmt_sz = (int)(g_nand_size/BLOCK_SIZE/100*6);
  2551. //if (id[0] == 0x45)
  2552. //{
  2553. // bmt_sz = bmt_sz * 2;
  2554. //}
  2555. nand->chipsize -= (bmt_sz * BLOCK_SIZE);
  2556. g_bInitDone = true;
  2557. if (!g_bmt) {
  2558. if (!(g_bmt = init_bmt(nand, bmt_sz))) {
  2559. MSG(INIT, "Error: init bmt failed\n");
  2560. return -1;
  2561. }
  2562. }
  2563. return 0;
  2564. }
  2565. void nand_init(void)
  2566. {
  2567. static part_dev_t dev;
  2568. if (!nand_init_device(&g_nand_chip)) {
  2569. struct nand_chip *t_nand = &g_nand_chip;
  2570. dprintf(INFO,"NAND init done in LK\n");
  2571. total_size = t_nand->chipsize - BLOCK_SIZE * (PMT_POOL_SIZE);
  2572. dev.id = 0;
  2573. dev.init = 1;
  2574. dev.blkdev = (block_dev_desc_t *) t_nand;
  2575. dev.read = nand_part_read;
  2576. dev.write = nand_part_write;
  2577. mt_part_register_device(&dev);
  2578. dprintf(INFO,"NAND register done in LK\n");
  2579. return;
  2580. } else {
  2581. dprintf(INFO,"NAND init fail in LK\n");
  2582. }
  2583. }
  2584. void nand_driver_test(void)
  2585. {
  2586. #ifdef NAND_LK_TEST
  2587. u32 test_len=2048*1024;
  2588. long len;
  2589. int fail=0;
  2590. u32 index = 0;
  2591. part_dev_t *dev = mt_part_get_device();
  2592. part_t *part = mt_part_get_partition(PART_EXPDB);
  2593. unsigned long start_addr = part->startblk * BLK_SIZE;
  2594. u8 *original = malloc(test_len);
  2595. u8 *source = malloc(test_len);
  2596. u8 *readback = malloc(test_len);
  2597. for (index = 0; index < test_len; index++) {
  2598. source[index] = index % 16;
  2599. }
  2600. memset(original, 0x0a, test_len);
  2601. memset(readback, 0x0b, test_len);
  2602. MSG(ERR,"~~~~~~~~~nand driver test in lk~~~~~~~~~~~~~~\n");
  2603. // len = dev->read(dev, start_addr, (uchar *) original, test_len, 0);
  2604. // if (len != test_len)
  2605. // {
  2606. // MSG(ERR,"read original fail %d\n", len);
  2607. // }
  2608. // MSG(ERR,"oringinal data:");
  2609. // for (index = 0; index < 300; index++)
  2610. // {
  2611. // MSG(ERR," %x", original[index]);
  2612. // }
  2613. // MSG(ERR,"\n");
  2614. len = dev->write(dev, (uchar *) source, start_addr, test_len, 0);
  2615. if (len != test_len) {
  2616. MSG(ERR,"write source fail %d\n", len);
  2617. }
  2618. len = dev->read(dev, start_addr, (uchar *) readback,test_len, 0);
  2619. if (len != test_len) {
  2620. MSG(ERR,"read back fail %d\n", len);
  2621. }
  2622. MSG(ERR,"readback data:");
  2623. for (index = 0; index < 300; index++) {
  2624. MSG(ERR," %x", readback[index]);
  2625. }
  2626. MSG(ERR,"\n");
  2627. for (index = 0; index < test_len; index++) {
  2628. if (source[index] != readback[index]) {
  2629. MSG(ERR,"compare fail %d\n", index);
  2630. fail=1;
  2631. break;
  2632. }
  2633. }
  2634. if (fail==0) {
  2635. MSG(ERR,"compare success!\n");
  2636. }
  2637. len = dev->write(dev, (uchar *) original, start_addr, test_len,0);
  2638. if (len != test_len) {
  2639. MSG(ERR,"write back fail %d\n", len);
  2640. } else {
  2641. MSG(ERR,"recovery success\n");
  2642. }
  2643. memset(original,0xd,test_len);
  2644. len = dev->read(dev, start_addr, (uchar *) original, test_len,0);
  2645. if (len != test_len) {
  2646. MSG(ERR,"read original fail %d\n", len);
  2647. }
  2648. dprintf(INFO,"read back oringinal data:");
  2649. for (index = 0; index < 300; index++) {
  2650. MSG(ERR," %x", original[index]);
  2651. }
  2652. MSG(ERR,"\n");
  2653. MSG(ERR,"~~~~~~~~~nand driver test in lk~~~~~~~~~~~~~~\n");
  2654. free(original);
  2655. free(source);
  2656. free(readback);
  2657. #endif
  2658. }
  2659. /******************** ***/
  2660. /* support for fast boot */
  2661. /***********************/
  2662. int nand_erase(u64 offset, u64 size)
  2663. {
  2664. u64 img_size = size;
  2665. // u32 tpgsz;
  2666. u32 tblksz;
  2667. u64 cur_offset;
  2668. // u32 i = 0;
  2669. u32 index;
  2670. u32 block_size;
  2671. // do block alignment check
  2672. ///printf ("[ERASE] offset = 0x%x\n", (u32)offset);
  2673. part_get_startaddress(offset, &index);
  2674. //printf ("[ERASE] index = %d\n", index);
  2675. if (raw_partition(index)) {
  2676. block_size = BLOCK_SIZE/2;
  2677. } else {
  2678. block_size = BLOCK_SIZE;
  2679. }
  2680. if ((u32)(offset % block_size) != 0) {
  2681. dprintf(INFO,"offset must be block alignment (0x%x)\n", block_size);
  2682. return -1;
  2683. }
  2684. // calculate block number of this image
  2685. if ((img_size % block_size) == 0) {
  2686. tblksz = img_size / block_size;
  2687. } else {
  2688. tblksz = (img_size / block_size) + 1;
  2689. }
  2690. dprintf(INFO,"[ERASE] image size = 0x%llx\n", img_size);
  2691. dprintf(INFO,"[ERASE] the number of nand block of this image = %d\n", tblksz);
  2692. // erase nand block
  2693. cur_offset = (u64)offset;
  2694. while (tblksz != 0) {
  2695. //printf ("[ERASE] cur_offset = 0x%x\n", cur_offset);
  2696. if (__nand_erase(cur_offset) == FALSE) {
  2697. dprintf(INFO,"[ERASE] erase 0x%x fail\n",cur_offset);
  2698. mark_block_bad (cur_offset);
  2699. }
  2700. cur_offset += block_size;
  2701. tblksz--;
  2702. if (tblksz != 0 && cur_offset >= total_size) {
  2703. dprintf(INFO,"[ERASE] cur offset (0x%x) exceeds erase limit address (0x%x)\n", cur_offset, total_size);
  2704. return 0;
  2705. }
  2706. }
  2707. return 0;
  2708. }
  2709. bool __nand_erase (u64 logical_addr)
  2710. {
  2711. u32 block;
  2712. u32 mapped_block;
  2713. u64 addr;
  2714. mtk_nand_page_transform(logical_addr,&block,&mapped_block);
  2715. addr = (u64)(mapped_block);
  2716. addr = addr * BLOCK_SIZE;
  2717. dprintf(INFO,"addr 0x%llx, mapped_block 0x%x, BLOCK_SIZE 0x%x\n",logical_addr, mapped_block,BLOCK_SIZE);
  2718. if (!nand_erase_hw(addr)) {
  2719. dprintf(INFO,"erase block 0x%x failed\n", mapped_block);
  2720. if (update_bmt((u64)mapped_block * BLOCK_SIZE, UPDATE_ERASE_FAIL, NULL, NULL)) {
  2721. dprintf(INFO,"erase block fail and update bmt sucess\n");
  2722. return TRUE;
  2723. } else {
  2724. dprintf(INFO,"erase block 0x%x failed but update bmt fail\n",mapped_block);
  2725. return FALSE;
  2726. }
  2727. }
  2728. return TRUE;
  2729. }
  2730. static int erase_fail_test = 0;
  2731. bool nand_erase_hw (u64 offset)
  2732. {
  2733. bool bRet = TRUE;
  2734. // u32 timeout, u4SecNum = g_nand_chip.oobblock >> g_nand_chip.sector_shift;
  2735. u32 rownob = devinfo.addr_cycle - 2;
  2736. u32 page_addr = offset / g_nand_chip.oobblock;
  2737. u32 reg_val =0;
  2738. if (nand_block_bad_hw(&g_nand_chip,offset)) {
  2739. return FALSE;
  2740. }
  2741. if (erase_fail_test) {
  2742. erase_fail_test = 0;
  2743. return FALSE;
  2744. }
  2745. if (devinfo.tlcControl.slcopmodeEn) { // slc mode
  2746. if ((0xFF != devinfo.tlcControl.en_slc_mode_cmd) && (gVendor == VEND_SANDISK)) {
  2747. reg_val = DRV_Reg(NFI_CNFG_REG16);
  2748. reg_val &= ~CNFG_READ_EN;
  2749. reg_val &= ~CNFG_OP_MODE_MASK;
  2750. reg_val |= CNFG_OP_CUST;
  2751. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2752. nand_set_command(devinfo.tlcControl.en_slc_mode_cmd);
  2753. reg_val = DRV_Reg32(NFI_CON_REG16);
  2754. reg_val |= CON_FIFO_FLUSH|CON_NFI_RST;
  2755. /* issue reset operation */
  2756. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  2757. dprintf(INFO,"nand_erase_hw SLC mode %x\n",page_addr);
  2758. }
  2759. }
  2760. nand_reset ();
  2761. nand_set_mode (CNFG_OP_ERASE);
  2762. nand_set_command (NAND_CMD_ERASE_1);
  2763. nand_set_address (0, page_addr, 0, rownob);
  2764. nand_set_command (NAND_CMD_ERASE_2);
  2765. if (!nand_status_ready(STA_NAND_BUSY)) {
  2766. return FALSE;
  2767. }
  2768. return bRet;
  2769. }
  2770. bool mark_block_bad_hw(u64 offset)
  2771. {
  2772. u32 index;
  2773. u32 page_addr = offset / g_nand_chip.oobblock;
  2774. u32 u4SecNum = g_nand_chip.oobblock >> g_nand_chip.sector_shift;
  2775. u32 i, page_num = (BLOCK_SIZE / g_nand_chip.oobblock);
  2776. memset(data_buf_temp,0xAA,4096);
  2777. for (index = 0; index < 64; index++)
  2778. *(oob_buf_temp+ index) = 0xFF;
  2779. for (index = 8, i = 0; i < u4SecNum; i++)
  2780. oob_buf_temp[i * index] = 0x0;
  2781. page_addr &= ~(page_num - 1);
  2782. MSG (INIT, "Mark bad block at 0x%x\n", page_addr);
  2783. while (DRV_Reg32 (NFI_STA_REG32) & STA_NAND_BUSY);
  2784. return nand_exec_write_page_raw(&g_nand_chip, page_addr, g_nand_chip.oobblock, data_buf_temp,oob_buf_temp);
  2785. }
  2786. bool mark_block_bad (u64 logical_addr)
  2787. {
  2788. //u32 block;
  2789. //u32 mapped_block;
  2790. //mtk_nand_page_transform(logical_addr,&block,&mapped_block);
  2791. return mark_block_bad_hw((u64)logical_addr);
  2792. }
  2793. int nand_write_page_hw(u32 page, u8 *dat, u8 *oob)
  2794. {
  2795. // u32 pagesz = g_nand_chip.oobblock;
  2796. // u32 u4SecNum = pagesz >> g_nand_chip.sector_shift;
  2797. int i, j, start, len;
  2798. bool empty = TRUE;
  2799. u8 oob_checksum = 0;
  2800. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && g_nand_chip.ecclayout->oobfree[i].length; i++) {
  2801. /* Set the reserved bytes to 0xff */
  2802. start = g_nand_chip.ecclayout->oobfree[i].offset;
  2803. len = g_nand_chip.ecclayout->oobfree[i].length;
  2804. for (j = 0; j < len; j++) {
  2805. oob_checksum ^= oob[start + j];
  2806. if (oob[start + j] != 0xFF)
  2807. empty = FALSE;
  2808. }
  2809. }
  2810. if (!empty) {
  2811. oob[g_nand_chip.ecclayout->oobfree[i-1].offset + g_nand_chip.ecclayout->oobfree[i-1].length] = oob_checksum;
  2812. }
  2813. while (DRV_Reg32 (NFI_STA_REG32) & STA_NAND_BUSY);
  2814. return nand_exec_write_page_raw(&g_nand_chip, page, g_nand_chip.oobblock, (u8 *)dat,(u8 *)oob);
  2815. }
  2816. int nand_write_page_hwecc (u64 logical_addr, char *buf, char *oob_buf)
  2817. {
  2818. // u32 page_size = g_nand_chip.oobblock;
  2819. // u32 block_size = BLOCK_SIZE;
  2820. u32 block;
  2821. u32 mapped_block;
  2822. // u32 pages_per_blk = (block_size/page_size);
  2823. u32 page_no;
  2824. //u32 page_in_block = (logical_addr/page_size)%pages_per_blk;
  2825. u32 i;
  2826. int start, len, offset;
  2827. page_no = mtk_nand_page_transform(logical_addr,&block,&mapped_block);
  2828. for (i = 0; i < sizeof(g_spare_buf); i++)
  2829. *(g_spare_buf + i) = 0xFF;
  2830. offset = 0;
  2831. if (oob_buf != NULL) {
  2832. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && g_nand_chip.ecclayout->oobfree[i].length; i++) {
  2833. /* Set the reserved bytes to 0xff */
  2834. start = g_nand_chip.ecclayout->oobfree[i].offset;
  2835. len = g_nand_chip.ecclayout->oobfree[i].length;
  2836. memcpy ((g_spare_buf + start), (oob_buf + offset), len);
  2837. offset += len;
  2838. }
  2839. }
  2840. // write bad index into oob
  2841. if (mapped_block != block) {
  2842. // MSG(INIT, "page: 0x%x\n", page_in_block);
  2843. set_bad_index_to_oob(g_spare_buf, block);
  2844. } else {
  2845. set_bad_index_to_oob(g_spare_buf, FAKE_INDEX);
  2846. }
  2847. if (!nand_write_page_hw(page_no,(u8*)buf, g_spare_buf)) {
  2848. MSG(INIT, "write fail happened @ block 0x%x, page 0x%x\n", mapped_block, page_no);
  2849. return update_bmt( (u64)page_no * g_nand_chip.oobblock,
  2850. UPDATE_WRITE_FAIL, (u8*)buf, g_spare_buf);
  2851. }
  2852. return TRUE;
  2853. }
  2854. int nand_get_alignment(void)
  2855. {
  2856. return BLOCK_SIZE;
  2857. }
  2858. int nand_img_read(u64 source, uchar * dst, int size)
  2859. {
  2860. uint8_t res;
  2861. u32 u4PageSize = 1 << g_nand_chip.page_shift;
  2862. u32 u4PageNumPerBlock = BLOCK_SIZE/g_nand_chip.page_size;
  2863. u32 u4BlkEnd = (u32)(g_nand_chip.chipsize / BLOCK_SIZE);
  2864. u32 u4BlkAddr = (u32)(source / BLOCK_SIZE);
  2865. u32 u4ColAddr = (u32)(source & (u4PageSize - 1));
  2866. u32 u4RowAddr = (u32)(source/g_nand_chip.page_size);
  2867. u32 u4RowEnd;
  2868. // u32 mapped;
  2869. u32 u4ReadLen = 0;
  2870. u32 i4Len;
  2871. //mtk_nand_page_transform((u64)source,&u4BlkAddr,&mapped);
  2872. while (((u32)size > u4ReadLen) && (u4BlkAddr < u4BlkEnd)) {
  2873. res = nand_block_bad(&g_nand_chip, (u4BlkAddr * u4PageNumPerBlock));
  2874. if (!res) {
  2875. u4RowEnd = (u4RowAddr + u4PageNumPerBlock) & (~u4PageNumPerBlock + 1);
  2876. for (; u4RowAddr < u4RowEnd; u4RowAddr++) {
  2877. i4Len = min(size - u4ReadLen, u4PageSize - u4ColAddr);
  2878. if (0 >= i4Len) {
  2879. break;
  2880. }
  2881. if ((u4ColAddr == 0) && (i4Len == u4PageSize)) {
  2882. nand_exec_read_page(&g_nand_chip, u4RowAddr, u4PageSize, dst + u4ReadLen, g_kCMD.au1OOB);
  2883. } else {
  2884. nand_exec_read_page(&g_nand_chip, u4RowAddr, u4PageSize, g_nand_chip.buffers->databuf, g_kCMD.au1OOB);
  2885. memcpy(dst + u4ReadLen, g_nand_chip.buffers->databuf + u4ColAddr, i4Len);
  2886. }
  2887. u4ReadLen += i4Len;
  2888. u4ColAddr = (u4ColAddr + i4Len) & (u4PageSize - 1);
  2889. }
  2890. } else {
  2891. dprintf(INFO,"Detect bad block at block 0x%x\n", u4BlkAddr);
  2892. u4RowAddr += u4PageNumPerBlock;
  2893. }
  2894. u4BlkAddr++;
  2895. }
  2896. return (int)u4ReadLen;
  2897. }
  2898. #if defined(MTK_MLC_NAND_SUPPORT)
  2899. int nand_write_img(u64 addr, void *data, u32 img_sz,u64 partition_size,int img_type)
  2900. #else
  2901. int nand_write_img(u32 addr, void *data, u32 img_sz,u32 partition_size,int img_type)
  2902. #endif
  2903. {
  2904. unsigned int page_size = g_nand_chip.oobblock;
  2905. unsigned int img_spare_size,write_size;
  2906. unsigned int block_size;
  2907. u64 partition_end = (u64)addr + partition_size;
  2908. bool ret;
  2909. u32 index;
  2910. bool bIsRaw = FALSE;
  2911. unsigned int b_lastpage = 0;
  2912. dprintf(INFO,"[nand_wite_img]write to img size, %x addr %llx\n",img_sz,addr);
  2913. part_get_startaddress((u64)addr, &index);
  2914. dprintf(INFO,"[nand_write_img] index = %d\n", index);
  2915. bIsRaw = raw_partition(index);
  2916. if (!bIsRaw) {
  2917. if (mtk_block_istlc(addr))
  2918. block_size = BLOCK_SIZE;
  2919. else
  2920. block_size = BLOCK_SIZE/2;
  2921. } else {
  2922. block_size = BLOCK_SIZE/2;
  2923. }
  2924. if (addr % block_size || partition_size % block_size) {
  2925. dprintf(INFO,"[nand_write_img]partition address or partition size is not block size alignment\n");
  2926. return -1;
  2927. }
  2928. if (img_sz > partition_size) {
  2929. dprintf(INFO,"[nand_write_img]img size %x exceed partition size\n",img_sz);
  2930. return -1;
  2931. }
  2932. if (page_size == 4096) {
  2933. img_spare_size = 128;
  2934. } else if (page_size == 2048) {
  2935. img_spare_size = 64;
  2936. }
  2937. write_size = page_size;
  2938. dprintf(INFO,"[nand_write_img]img_sz %x write_size %x addr %llx\n",img_sz,write_size, addr);
  2939. while (img_sz>0) {
  2940. if ((addr+img_sz)>partition_end) {
  2941. 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);
  2942. return -1;
  2943. }
  2944. /*1. need to erase before write*/
  2945. if ((addr % block_size)==0) {
  2946. if (__nand_erase((u64)addr) == FALSE) {
  2947. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  2948. mark_block_bad ((u64)addr);
  2949. addr += block_size;
  2950. continue; //erase fail, skip this block
  2951. }
  2952. }
  2953. /*2. write page*/
  2954. if ((img_sz < write_size)) {
  2955. b_lastpage = 1;
  2956. memset(g_data_buf,0xff,write_size);
  2957. memcpy(g_data_buf,data,img_sz);
  2958. if (check_data_empty((void *)g_data_buf,page_size)) {
  2959. dprintf(INFO,"[nand_write_img]skip empty page\n");
  2960. ret = true;
  2961. } else {
  2962. ret = nand_write_page_hwecc((u64)addr,(char*)g_data_buf,NULL);
  2963. dprintf(INFO,"[nand_write_img]none empty page %x %x\n",g_data_buf[0],data);
  2964. }
  2965. } else {
  2966. memcpy(g_data_buf,data,write_size);
  2967. if (check_data_empty((void *)data,page_size)) {
  2968. dprintf(INFO,"[nand_write_img]skip empty page\n");
  2969. ret = true;
  2970. } else {
  2971. ret = nand_write_page_hwecc((u64)addr,g_data_buf,NULL);
  2972. dprintf(INFO,"[nand_write_img]none empty page %x %x\n",g_data_buf[0],data);
  2973. }
  2974. }
  2975. if (ret == FALSE) {
  2976. dprintf(INFO,"[nand_write_img]write fail at % 0x%x\n",addr);
  2977. if (__nand_erase((u64)addr) == FALSE) {
  2978. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  2979. mark_block_bad ((u64)addr);
  2980. }
  2981. data -= ((addr%block_size)/page_size)*write_size;
  2982. img_sz += ((addr%block_size)/page_size)*write_size;
  2983. addr += block_size;
  2984. continue; // write fail, try to write the next block
  2985. }
  2986. if (b_lastpage) {
  2987. data += img_sz;
  2988. img_sz = 0 ;
  2989. addr += page_size;
  2990. } else {
  2991. data += write_size;
  2992. img_sz -= write_size;
  2993. addr += page_size;
  2994. }
  2995. }
  2996. /*3. erase any block remained in partition*/
  2997. addr = ((addr+block_size-1)/block_size)*block_size;
  2998. //nand_erase((u64)addr,(u64)(partition_end - addr));
  2999. return 0;
  3000. }
  3001. #if defined(MTK_MLC_NAND_SUPPORT)
  3002. 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)
  3003. #else
  3004. 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)
  3005. #endif
  3006. {
  3007. unsigned int page_size = g_nand_chip.oobblock;
  3008. unsigned int img_spare_size = 0;
  3009. unsigned int write_size;
  3010. unsigned int block_size = BLOCK_SIZE;
  3011. u64 partition_end = partition_start + partition_size;
  3012. // unsigned int first_chunk = 0;
  3013. unsigned int last_chunk = 0;
  3014. unsigned int left_size = 0;
  3015. bool ret;
  3016. u32 index;
  3017. u64 last_addr = (u64)addr;
  3018. u32 dst_block = 0;
  3019. part_get_startaddress((u64)addr, &index);
  3020. 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);
  3021. if (raw_partition(index)) {
  3022. block_size = BLOCK_SIZE/2;
  3023. } else {
  3024. block_size = BLOCK_SIZE;
  3025. }
  3026. if (partition_start % block_size || partition_size % block_size) {
  3027. dprintf(INFO,"[nand_write_img_ex]partition address or partition size is not block size alignment %lx,%lx\n",partition_start, partition_size);
  3028. return -1;
  3029. }
  3030. if (length > partition_size) {
  3031. dprintf(INFO,"[nand_write_img_ex]img size %x exceed partition size\n",length);
  3032. return -1;
  3033. }
  3034. if (page_size == 4096) {
  3035. img_spare_size = 128;
  3036. } else if (page_size == 2048) {
  3037. img_spare_size = 64;
  3038. }
  3039. if (last_addr % page_size) {
  3040. dprintf(INFO,"[nand_write_img_ex]write addr is not page_size %d alignment\n",page_size);
  3041. return -1;
  3042. }
  3043. if (img_type == YFFS2_IMG) {
  3044. write_size = page_size + img_spare_size;
  3045. if (total_size % write_size) {
  3046. dprintf(INFO,"[nand_write_img_ex]total image size %d is not w_size %d alignment\n",total_size,write_size);
  3047. return -1;
  3048. }
  3049. } else {
  3050. write_size = page_size;
  3051. }
  3052. if (addr == partition_start) {
  3053. dprintf(INFO,"[nand_write_img_ex]first chunk\n");
  3054. // first_chunk = 1;
  3055. download_size = 0;
  3056. memset(g_data_buf,0xff,write_size);
  3057. }
  3058. if ((length + download_size) >= total_size) {
  3059. dprintf(INFO,"[nand_write_img_ex]last chunk\n");
  3060. last_chunk = 1;
  3061. }
  3062. left_size = (download_size % write_size);
  3063. while (length>0) {
  3064. if ((addr+length)>partition_end) {
  3065. 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);
  3066. return -1;
  3067. }
  3068. /*1. need to erase before write*/
  3069. if ((addr % block_size)==0) {
  3070. if (__nand_erase((u64)addr) == FALSE) {
  3071. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  3072. mark_block_bad ((u64)addr);
  3073. addr += block_size;
  3074. continue; //erase fail, skip this block
  3075. }
  3076. }
  3077. if ((length < write_size)&&(!left_size)) {
  3078. memset(g_data_buf,0xff,write_size);
  3079. memcpy(g_data_buf,data,length);
  3080. if (!last_chunk) {
  3081. download_size += length;
  3082. break;
  3083. }
  3084. } else if (left_size) {
  3085. memcpy(&g_data_buf[left_size],data,write_size-left_size);
  3086. } else {
  3087. memcpy(g_data_buf,data,write_size);
  3088. }
  3089. /*2. write page*/
  3090. if (img_type == YFFS2_IMG) {
  3091. ret = nand_write_page_hwecc((u64)addr,(char *)g_data_buf,(char *)g_data_buf + page_size);
  3092. } else {
  3093. if ((img_type == UBIFS_IMG)&& (check_data_empty((void *)g_data_buf,page_size))) {
  3094. dprintf(INFO,"[nand_write_img_ex]skip empty page\n");
  3095. ret = true;
  3096. } else {
  3097. ret = nand_write_page_hwecc((u64)addr,(char *)g_data_buf,NULL);
  3098. }
  3099. }
  3100. /*need to check?*/
  3101. if (ret == FALSE) {
  3102. dprintf(INFO,"[nand_write_img_ex]write fail at % 0x%x\n",addr);
  3103. while (1) {
  3104. dst_block = find_next_good_block((u64)addr/block_size);
  3105. if (dst_block == 0) {
  3106. dprintf(INFO,"[nand_write_img_ex]find next good block fail\n");
  3107. return -1;
  3108. }
  3109. ret = block_replace((u64)addr/block_size,dst_block,(u64)addr/page_size);
  3110. if (ret == FALSE) {
  3111. dprintf(INFO,"[nand_write_img_ex]block replace fail,continue\n");
  3112. continue;
  3113. } else {
  3114. dprintf(INFO,"[nand_write_img_ex]block replace sucess %x--> %x\n",addr/block_size,dst_block);
  3115. break;
  3116. }
  3117. }
  3118. addr = (addr%block_size) + (dst_block*block_size);
  3119. /* if (__nand_erase(addr) == FALSE)
  3120. {
  3121. dprintf(INFO,"[ERASE] erase 0x%x fail\n",addr);
  3122. mark_block_bad (addr);
  3123. }
  3124. data -= ((addr%block_size)/page_size)*write_size;
  3125. length += ((addr%block_size)/page_size)*write_size;
  3126. addr += block_size;*/
  3127. continue; // write fail, try to write the next block
  3128. }
  3129. if (left_size) {
  3130. data += (write_size - left_size);
  3131. length -= (write_size - left_size);
  3132. addr += page_size;
  3133. download_size += (write_size - left_size);
  3134. left_size = 0;
  3135. } else {
  3136. data += write_size;
  3137. length -= write_size;
  3138. addr += page_size;
  3139. download_size += write_size;
  3140. }
  3141. }
  3142. *next_offset = addr - last_addr;
  3143. if (last_chunk) {
  3144. /*3. erase any block remained in partition*/
  3145. addr = ((addr+block_size-1)/block_size)*block_size;
  3146. nand_erase((u64)addr,(u64)(partition_end - addr));
  3147. }
  3148. return 0;
  3149. }
  3150. int check_data_empty(void *data, unsigned size)
  3151. {
  3152. unsigned int i;
  3153. u32 *tp = (u32 *)data;
  3154. for (i =0; i<size/4; i++) {
  3155. if (*(tp+i) != 0xffffffff) {
  3156. return 0;
  3157. }
  3158. }
  3159. return 1;
  3160. }
  3161. static u32 find_next_good_block(u32 start_block)
  3162. {
  3163. u32 i;
  3164. u32 dst_block = 0;
  3165. for (i=start_block; i<(total_size/BLOCK_SIZE); i++) {
  3166. if (!nand_block_bad(&g_nand_chip,i*(BLOCK_SIZE/g_nand_chip.page_size))) {
  3167. dst_block = i;
  3168. break;
  3169. }
  3170. }
  3171. return dst_block;
  3172. }
  3173. static bool block_replace(u32 src_block, u32 dst_block, u32 error_page)
  3174. {
  3175. bool ret;
  3176. u32 block_size = BLOCK_SIZE;
  3177. u32 page_size = g_nand_chip.page_size;
  3178. u32 i;
  3179. u8 *data_buf;
  3180. u8 *spare_buf;
  3181. ret = __nand_erase((u64)dst_block*block_size);
  3182. if (ret == FALSE) {
  3183. dprintf(INFO,"[block_replace]%x-->%x erase fail\n",src_block,dst_block);
  3184. mark_block_bad((u64)src_block*block_size);
  3185. return ret;
  3186. }
  3187. data_buf = malloc(16384);
  3188. spare_buf = malloc(1024);
  3189. if (!data_buf || !spare_buf) {
  3190. dprintf(INFO,"[block_replace]malloc mem fail\n");
  3191. return -1;
  3192. }
  3193. memset(data_buf,0xff,16384);
  3194. memset(spare_buf,0xff,1024);
  3195. for (i=0; i<error_page; i++) {
  3196. nand_exec_read_page(&g_nand_chip,src_block*(block_size/page_size) + i,page_size,data_buf,spare_buf);
  3197. ret = nand_write_page_hwecc((u64)dst_block*block_size + i*page_size,(char*)data_buf,(char*)spare_buf);
  3198. if (ret == FALSE)
  3199. mark_block_bad((u64)dst_block*block_size);
  3200. }
  3201. mark_block_bad((u64)src_block*block_size);
  3202. free(data_buf);
  3203. free(spare_buf);
  3204. return ret;
  3205. }
  3206. // Add for Get DL information
  3207. #define PRE_SCAN_BLOCK_NUM 20
  3208. /*Support to check format/download status, 2013/01/19 {*/
  3209. /* Max Number of Load Sections */
  3210. #define MAX_LOAD_SECTIONS 40
  3211. #define DL_MAGIC "DOWNLOAD INFORMATION!!"
  3212. #define DL_INFO_VER_V1 "V1.0"
  3213. #define DL_MAGIC_NUM_COUNT 32
  3214. #define DL_MAGIC_OFFSET 24
  3215. #define DL_IMG_NAME_LENGTH 16
  3216. #define DL_CUSTOM_INFO_SIZE (128)
  3217. /*download status v1 and old version for emmc*/
  3218. #define FORMAT_START "FORMAT_START"
  3219. #define FORMAT_DONE "FORMAT_DONE"
  3220. #define BL_START "BL_START"
  3221. #define BL_DONE "BL_DONE"
  3222. #define DL_START "DL_START"
  3223. #define DL_DONE "DL_DONE"
  3224. #define DL_ERROR "DL_ERROR"
  3225. #define DL_CK_DONE "DL_CK_DONE"
  3226. #define DL_CK_ERROR "DL_CK_ERROR"
  3227. /*v1 and old version for emmc*/
  3228. #define CHECKSUM_PASS "PASS"
  3229. #define CHECKSUM_FAIL "FAIL"
  3230. typedef enum {
  3231. DL_INFO_VERSION_V0 = 0,
  3232. DL_INFO_VERSION_V1 = 1,
  3233. DL_INFO_VERSION_UNKOWN = 0xFF,
  3234. } DLInfoVersion;
  3235. /*version v1.0 {*/
  3236. typedef struct {
  3237. char image_name[DL_IMG_NAME_LENGTH];
  3238. } IMG_DL_INFO;
  3239. typedef struct {
  3240. unsigned int image_index;
  3241. unsigned int pc_checksum;
  3242. unsigned int da_checksum;
  3243. char checksum_status[8];
  3244. } CHECKSUM_INFO_V1;
  3245. typedef struct {
  3246. char magic_num[DL_MAGIC_OFFSET];
  3247. char version[DL_MAGIC_NUM_COUNT-DL_MAGIC_OFFSET];
  3248. CHECKSUM_INFO_V1 part_info[MAX_LOAD_SECTIONS];
  3249. char ram_checksum[16];
  3250. char download_status[16];
  3251. IMG_DL_INFO img_dl_info[MAX_LOAD_SECTIONS];
  3252. } DL_STATUS_V1;
  3253. /*version v1.0 }*/
  3254. /*Support to check format/download status, 2013/01/19 {*/
  3255. #define DL_NOT_FOUND 2
  3256. #define DL_PASS 0
  3257. #define DL_FAIL 1
  3258. int nand_get_dl_info(void)
  3259. {
  3260. DL_STATUS_V1 download_info;
  3261. u8 *data_buf;
  3262. u8 *spare_buf;
  3263. int ret;
  3264. u32 block_size = BLOCK_SIZE;
  3265. u32 page_size = g_nand_chip.page_size;
  3266. u32 pages_per_block = block_size/page_size;
  3267. u32 total_blocks = g_nand_size/BLOCK_SIZE;
  3268. u32 i,block_i,page_i;
  3269. u32 block_addr;
  3270. u32 dl_info_blkAddr = 0xFFFFFFFF;
  3271. u32 page_index[4];
  3272. data_buf = malloc(16384);
  3273. spare_buf = malloc(1024);
  3274. if (!data_buf || !spare_buf) {
  3275. dprintf(INFO,"[nand_get_dl_info]malloc mem fail\n");
  3276. ret = -1;
  3277. return ret;
  3278. }
  3279. // DL information block should program to good block instead of always at last block.
  3280. page_index[0] = 0;
  3281. page_index[1] = 1;
  3282. page_index[2] = pages_per_block-3;
  3283. page_index[3] = pages_per_block-1;
  3284. block_i = 1;
  3285. do {
  3286. block_addr = pages_per_block * (total_blocks-block_i);
  3287. for (page_i=0; page_i<4; page_i++) {
  3288. nand_exec_read_page(&g_nand_chip,block_addr+page_index[page_i],page_size,data_buf,spare_buf);
  3289. ret = memcmp((void*)data_buf,DL_MAGIC, sizeof(DL_MAGIC));
  3290. if (!ret) {
  3291. dl_info_blkAddr = block_addr;
  3292. break;
  3293. }
  3294. }
  3295. if (dl_info_blkAddr!=0xFFFFFFFF) {
  3296. break;
  3297. }
  3298. block_i++;
  3299. } while (block_i<=PRE_SCAN_BLOCK_NUM);
  3300. if (dl_info_blkAddr==0xFFFFFFFF) {
  3301. dprintf(INFO,"DL INFO NOT FOUND\n");
  3302. ret = DL_NOT_FOUND;
  3303. } else {
  3304. dprintf(INFO,"get dl info from 0x%x\n",dl_info_blkAddr);
  3305. memcpy(&download_info,data_buf,sizeof(download_info));
  3306. if (!memcmp(download_info.download_status,DL_DONE,sizeof(DL_DONE))||!memcmp(download_info.download_status,DL_CK_DONE,sizeof(DL_CK_DONE))) {
  3307. dprintf(INFO,"dl done. status = %s\n",download_info.download_status);
  3308. dprintf(INFO,"dram checksum : %s\n",download_info.ram_checksum);
  3309. for (i=0; i<PART_MAX_COUNT; i++) {
  3310. if (download_info.part_info[i].image_index!=0) {
  3311. dprintf(INFO,"image_index:%d, checksum: %s\n",download_info.part_info[i].image_index,download_info.part_info[i].checksum_status);
  3312. }
  3313. }
  3314. ret = DL_PASS;
  3315. } else {
  3316. dprintf(INFO,"dl error. status = %s\n",download_info.download_status);
  3317. dprintf(INFO,"dram checksum : %s\n",download_info.ram_checksum);
  3318. for (i=0; i<PART_MAX_COUNT; i++) {
  3319. if (download_info.part_info[i].image_index!=0) {
  3320. dprintf(INFO,"image_index:%d, checksum: %s\n",download_info.part_info[i].image_index,download_info.part_info[i].checksum_status);
  3321. }
  3322. }
  3323. ret = DL_FAIL;
  3324. }
  3325. }
  3326. free(data_buf);
  3327. free(spare_buf);
  3328. return ret;
  3329. }
  3330. u32 mtk_nand_erasesize(void)
  3331. {
  3332. return g_nand_chip.erasesize;
  3333. }
  3334. #endif