2
0

0039-mtd-add-mt7621-nand-support.patch 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436
  1. From 0e1c4e3c97b83b4e7da65b1c56f0a7d40736ac53 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Sun, 27 Jul 2014 11:05:17 +0100
  4. Subject: [PATCH 39/53] mtd: add mt7621 nand support
  5. Signed-off-by: John Crispin <[email protected]>
  6. ---
  7. drivers/mtd/nand/Kconfig | 6 +
  8. drivers/mtd/nand/Makefile | 1 +
  9. drivers/mtd/nand/bmt.c | 750 ++++++++++++
  10. drivers/mtd/nand/bmt.h | 80 ++
  11. drivers/mtd/nand/dev-nand.c | 63 +
  12. drivers/mtd/nand/mt6575_typedefs.h | 340 ++++++
  13. drivers/mtd/nand/mtk_nand2.c | 2304 +++++++++++++++++++++++++++++++++++
  14. drivers/mtd/nand/mtk_nand2.h | 452 +++++++
  15. drivers/mtd/nand/nand_base.c | 6 +-
  16. drivers/mtd/nand/nand_def.h | 123 ++
  17. drivers/mtd/nand/nand_device_list.h | 55 +
  18. drivers/mtd/nand/partition.h | 115 ++
  19. 13 files changed, 4311 insertions(+), 3 deletions(-)
  20. create mode 100644 drivers/mtd/nand/bmt.c
  21. create mode 100644 drivers/mtd/nand/bmt.h
  22. create mode 100644 drivers/mtd/nand/dev-nand.c
  23. create mode 100644 drivers/mtd/nand/mt6575_typedefs.h
  24. create mode 100644 drivers/mtd/nand/mtk_nand2.c
  25. create mode 100644 drivers/mtd/nand/mtk_nand2.h
  26. create mode 100644 drivers/mtd/nand/nand_def.h
  27. create mode 100644 drivers/mtd/nand/nand_device_list.h
  28. create mode 100644 drivers/mtd/nand/partition.h
  29. --- a/drivers/mtd/nand/Kconfig
  30. +++ b/drivers/mtd/nand/Kconfig
  31. @@ -563,4 +563,10 @@ config MTD_NAND_MTK
  32. Enables support for NAND controller on MTK SoCs.
  33. This controller is found on mt27xx, mt81xx, mt65xx SoCs.
  34. +config MTK_MTD_NAND
  35. + tristate "Support for MTK SoC NAND controller"
  36. + depends on SOC_MT7621
  37. + select MTD_NAND_IDS
  38. + select MTD_NAND_ECC
  39. +
  40. endif # MTD_NAND
  41. --- a/drivers/mtd/nand/Makefile
  42. +++ b/drivers/mtd/nand/Makefile
  43. @@ -60,6 +60,7 @@ obj-$(CONFIG_MTD_NAND_HISI504) +
  44. obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/
  45. obj-$(CONFIG_MTD_NAND_QCOM) += qcom_nandc.o
  46. obj-$(CONFIG_MTD_NAND_MTK) += mtk_nand.o mtk_ecc.o
  47. +obj-$(CONFIG_MTK_MTD_NAND) += mtk_nand2.o bmt.o
  48. nand-objs := nand_base.o nand_bbt.o nand_timings.o nand_ids.o
  49. nand-objs += nand_amd.o
  50. --- /dev/null
  51. +++ b/drivers/mtd/nand/bmt.c
  52. @@ -0,0 +1,750 @@
  53. +#include "bmt.h"
  54. +
  55. +typedef struct
  56. +{
  57. + char signature[3];
  58. + u8 version;
  59. + u8 bad_count; // bad block count in pool
  60. + u8 mapped_count; // mapped block count in pool
  61. + u8 checksum;
  62. + u8 reseverd[13];
  63. +} phys_bmt_header;
  64. +
  65. +typedef struct
  66. +{
  67. + phys_bmt_header header;
  68. + bmt_entry table[MAX_BMT_SIZE];
  69. +} phys_bmt_struct;
  70. +
  71. +typedef struct
  72. +{
  73. + char signature[3];
  74. +} bmt_oob_data;
  75. +
  76. +static char MAIN_SIGNATURE[] = "BMT";
  77. +static char OOB_SIGNATURE[] = "bmt";
  78. +#define SIGNATURE_SIZE (3)
  79. +
  80. +#define MAX_DAT_SIZE 0x1000
  81. +#define MAX_OOB_SIZE 0x80
  82. +
  83. +static struct mtd_info *mtd_bmt;
  84. +static struct nand_chip *nand_chip_bmt;
  85. +#define BLOCK_SIZE_BMT (1 << nand_chip_bmt->phys_erase_shift)
  86. +#define PAGE_SIZE_BMT (1 << nand_chip_bmt->page_shift)
  87. +
  88. +#define OFFSET(block) ((block) * BLOCK_SIZE_BMT)
  89. +#define PAGE_ADDR(block) ((block) * BLOCK_SIZE_BMT / PAGE_SIZE_BMT)
  90. +
  91. +/*********************************************************************
  92. +* Flash is splited into 2 parts, system part is for normal system *
  93. +* system usage, size is system_block_count, another is replace pool *
  94. +* +-------------------------------------------------+ *
  95. +* | system_block_count | bmt_block_count | *
  96. +* +-------------------------------------------------+ *
  97. +*********************************************************************/
  98. +static u32 total_block_count; // block number in flash
  99. +static u32 system_block_count;
  100. +static int bmt_block_count; // bmt table size
  101. +// static int bmt_count; // block used in bmt
  102. +static int page_per_block; // page per count
  103. +
  104. +static u32 bmt_block_index; // bmt block index
  105. +static bmt_struct bmt; // dynamic created global bmt table
  106. +
  107. +static u8 dat_buf[MAX_DAT_SIZE];
  108. +static u8 oob_buf[MAX_OOB_SIZE];
  109. +static bool pool_erased;
  110. +
  111. +/***************************************************************
  112. +*
  113. +* Interface adaptor for preloader/uboot/kernel
  114. +* These interfaces operate on physical address, read/write
  115. +* physical data.
  116. +*
  117. +***************************************************************/
  118. +int nand_read_page_bmt(u32 page, u8 * dat, u8 * oob)
  119. +{
  120. + return mtk_nand_exec_read_page(mtd_bmt, page, PAGE_SIZE_BMT, dat, oob);
  121. +}
  122. +
  123. +bool nand_block_bad_bmt(u32 offset)
  124. +{
  125. + return mtk_nand_block_bad_hw(mtd_bmt, offset);
  126. +}
  127. +
  128. +bool nand_erase_bmt(u32 offset)
  129. +{
  130. + int status;
  131. + if (offset < 0x20000)
  132. + {
  133. + MSG(INIT, "erase offset: 0x%x\n", offset);
  134. + }
  135. +
  136. + status = mtk_nand_erase_hw(mtd_bmt, offset / PAGE_SIZE_BMT); // as nand_chip structure doesn't have a erase function defined
  137. + if (status & NAND_STATUS_FAIL)
  138. + return false;
  139. + else
  140. + return true;
  141. +}
  142. +
  143. +int mark_block_bad_bmt(u32 offset)
  144. +{
  145. + return mtk_nand_block_markbad_hw(mtd_bmt, offset); //mark_block_bad_hw(offset);
  146. +}
  147. +
  148. +bool nand_write_page_bmt(u32 page, u8 * dat, u8 * oob)
  149. +{
  150. + if (mtk_nand_exec_write_page(mtd_bmt, page, PAGE_SIZE_BMT, dat, oob))
  151. + return false;
  152. + else
  153. + return true;
  154. +}
  155. +
  156. +/***************************************************************
  157. +* *
  158. +* static internal function *
  159. +* *
  160. +***************************************************************/
  161. +static void dump_bmt_info(bmt_struct * bmt)
  162. +{
  163. + int i;
  164. +
  165. + MSG(INIT, "BMT v%d. total %d mapping:\n", bmt->version, bmt->mapped_count);
  166. + for (i = 0; i < bmt->mapped_count; i++)
  167. + {
  168. + MSG(INIT, "\t0x%x -> 0x%x\n", bmt->table[i].bad_index, bmt->table[i].mapped_index);
  169. + }
  170. +}
  171. +
  172. +static bool match_bmt_signature(u8 * dat, u8 * oob)
  173. +{
  174. +
  175. + if (memcmp(dat + MAIN_SIGNATURE_OFFSET, MAIN_SIGNATURE, SIGNATURE_SIZE))
  176. + {
  177. + return false;
  178. + }
  179. +
  180. + if (memcmp(oob + OOB_SIGNATURE_OFFSET, OOB_SIGNATURE, SIGNATURE_SIZE))
  181. + {
  182. + MSG(INIT, "main signature match, oob signature doesn't match, but ignore\n");
  183. + }
  184. + return true;
  185. +}
  186. +
  187. +static u8 cal_bmt_checksum(phys_bmt_struct * phys_table, int bmt_size)
  188. +{
  189. + int i;
  190. + u8 checksum = 0;
  191. + u8 *dat = (u8 *) phys_table;
  192. +
  193. + checksum += phys_table->header.version;
  194. + checksum += phys_table->header.mapped_count;
  195. +
  196. + dat += sizeof(phys_bmt_header);
  197. + for (i = 0; i < bmt_size * sizeof(bmt_entry); i++)
  198. + {
  199. + checksum += dat[i];
  200. + }
  201. +
  202. + return checksum;
  203. +}
  204. +
  205. +
  206. +static int is_block_mapped(int index)
  207. +{
  208. + int i;
  209. + for (i = 0; i < bmt.mapped_count; i++)
  210. + {
  211. + if (index == bmt.table[i].mapped_index)
  212. + return i;
  213. + }
  214. + return -1;
  215. +}
  216. +
  217. +static bool is_page_used(u8 * dat, u8 * oob)
  218. +{
  219. + return ((oob[OOB_INDEX_OFFSET] != 0xFF) || (oob[OOB_INDEX_OFFSET + 1] != 0xFF));
  220. +}
  221. +
  222. +static bool valid_bmt_data(phys_bmt_struct * phys_table)
  223. +{
  224. + int i;
  225. + u8 checksum = cal_bmt_checksum(phys_table, bmt_block_count);
  226. +
  227. + // checksum correct?
  228. + if (phys_table->header.checksum != checksum)
  229. + {
  230. + MSG(INIT, "BMT Data checksum error: %x %x\n", phys_table->header.checksum, checksum);
  231. + return false;
  232. + }
  233. +
  234. + MSG(INIT, "BMT Checksum is: 0x%x\n", phys_table->header.checksum);
  235. +
  236. + // block index correct?
  237. + for (i = 0; i < phys_table->header.mapped_count; i++)
  238. + {
  239. + if (phys_table->table[i].bad_index >= total_block_count || phys_table->table[i].mapped_index >= total_block_count || phys_table->table[i].mapped_index < system_block_count)
  240. + {
  241. + MSG(INIT, "index error: bad_index: %d, mapped_index: %d\n", phys_table->table[i].bad_index, phys_table->table[i].mapped_index);
  242. + return false;
  243. + }
  244. + }
  245. +
  246. + // pass check, valid bmt.
  247. + MSG(INIT, "Valid BMT, version v%d\n", phys_table->header.version);
  248. + return true;
  249. +}
  250. +
  251. +static void fill_nand_bmt_buffer(bmt_struct * bmt, u8 * dat, u8 * oob)
  252. +{
  253. + phys_bmt_struct phys_bmt;
  254. +
  255. + dump_bmt_info(bmt);
  256. +
  257. + // fill phys_bmt_struct structure with bmt_struct
  258. + memset(&phys_bmt, 0xFF, sizeof(phys_bmt));
  259. +
  260. + memcpy(phys_bmt.header.signature, MAIN_SIGNATURE, SIGNATURE_SIZE);
  261. + phys_bmt.header.version = BMT_VERSION;
  262. + // phys_bmt.header.bad_count = bmt->bad_count;
  263. + phys_bmt.header.mapped_count = bmt->mapped_count;
  264. + memcpy(phys_bmt.table, bmt->table, sizeof(bmt_entry) * bmt_block_count);
  265. +
  266. + phys_bmt.header.checksum = cal_bmt_checksum(&phys_bmt, bmt_block_count);
  267. +
  268. + memcpy(dat + MAIN_SIGNATURE_OFFSET, &phys_bmt, sizeof(phys_bmt));
  269. + memcpy(oob + OOB_SIGNATURE_OFFSET, OOB_SIGNATURE, SIGNATURE_SIZE);
  270. +}
  271. +
  272. +// return valid index if found BMT, else return 0
  273. +static int load_bmt_data(int start, int pool_size)
  274. +{
  275. + int bmt_index = start + pool_size - 1; // find from the end
  276. + phys_bmt_struct phys_table;
  277. + int i;
  278. +
  279. + MSG(INIT, "[%s]: begin to search BMT from block 0x%x\n", __FUNCTION__, bmt_index);
  280. +
  281. + for (bmt_index = start + pool_size - 1; bmt_index >= start; bmt_index--)
  282. + {
  283. + if (nand_block_bad_bmt(OFFSET(bmt_index)))
  284. + {
  285. + MSG(INIT, "Skip bad block: %d\n", bmt_index);
  286. + continue;
  287. + }
  288. +
  289. + if (!nand_read_page_bmt(PAGE_ADDR(bmt_index), dat_buf, oob_buf))
  290. + {
  291. + MSG(INIT, "Error found when read block %d\n", bmt_index);
  292. + continue;
  293. + }
  294. +
  295. + if (!match_bmt_signature(dat_buf, oob_buf))
  296. + {
  297. + continue;
  298. + }
  299. +
  300. + MSG(INIT, "Match bmt signature @ block: 0x%x\n", bmt_index);
  301. +
  302. + memcpy(&phys_table, dat_buf + MAIN_SIGNATURE_OFFSET, sizeof(phys_table));
  303. +
  304. + if (!valid_bmt_data(&phys_table))
  305. + {
  306. + MSG(INIT, "BMT data is not correct %d\n", bmt_index);
  307. + continue;
  308. + } else
  309. + {
  310. + bmt.mapped_count = phys_table.header.mapped_count;
  311. + bmt.version = phys_table.header.version;
  312. + // bmt.bad_count = phys_table.header.bad_count;
  313. + memcpy(bmt.table, phys_table.table, bmt.mapped_count * sizeof(bmt_entry));
  314. +
  315. + MSG(INIT, "bmt found at block: %d, mapped block: %d\n", bmt_index, bmt.mapped_count);
  316. +
  317. + for (i = 0; i < bmt.mapped_count; i++)
  318. + {
  319. + if (!nand_block_bad_bmt(OFFSET(bmt.table[i].bad_index)))
  320. + {
  321. + MSG(INIT, "block 0x%x is not mark bad, should be power lost last time\n", bmt.table[i].bad_index);
  322. + mark_block_bad_bmt(OFFSET(bmt.table[i].bad_index));
  323. + }
  324. + }
  325. +
  326. + return bmt_index;
  327. + }
  328. + }
  329. +
  330. + MSG(INIT, "bmt block not found!\n");
  331. + return 0;
  332. +}
  333. +
  334. +/*************************************************************************
  335. +* Find an available block and erase. *
  336. +* start_from_end: if true, find available block from end of flash. *
  337. +* else, find from the beginning of the pool *
  338. +* need_erase: if true, all unmapped blocks in the pool will be erased *
  339. +*************************************************************************/
  340. +static int find_available_block(bool start_from_end)
  341. +{
  342. + int i; // , j;
  343. + int block = system_block_count;
  344. + int direction;
  345. + // int avail_index = 0;
  346. + MSG(INIT, "Try to find_available_block, pool_erase: %d\n", pool_erased);
  347. +
  348. + // erase all un-mapped blocks in pool when finding avaliable block
  349. + if (!pool_erased)
  350. + {
  351. + MSG(INIT, "Erase all un-mapped blocks in pool\n");
  352. + for (i = 0; i < bmt_block_count; i++)
  353. + {
  354. + if (block == bmt_block_index)
  355. + {
  356. + MSG(INIT, "Skip bmt block 0x%x\n", block);
  357. + continue;
  358. + }
  359. +
  360. + if (nand_block_bad_bmt(OFFSET(block + i)))
  361. + {
  362. + MSG(INIT, "Skip bad block 0x%x\n", block + i);
  363. + continue;
  364. + }
  365. +//if(block==4095)
  366. +//{
  367. +// continue;
  368. +//}
  369. +
  370. + if (is_block_mapped(block + i) >= 0)
  371. + {
  372. + MSG(INIT, "Skip mapped block 0x%x\n", block + i);
  373. + continue;
  374. + }
  375. +
  376. + if (!nand_erase_bmt(OFFSET(block + i)))
  377. + {
  378. + MSG(INIT, "Erase block 0x%x failed\n", block + i);
  379. + mark_block_bad_bmt(OFFSET(block + i));
  380. + }
  381. + }
  382. +
  383. + pool_erased = 1;
  384. + }
  385. +
  386. + if (start_from_end)
  387. + {
  388. + block = total_block_count - 1;
  389. + direction = -1;
  390. + } else
  391. + {
  392. + block = system_block_count;
  393. + direction = 1;
  394. + }
  395. +
  396. + for (i = 0; i < bmt_block_count; i++, block += direction)
  397. + {
  398. + if (block == bmt_block_index)
  399. + {
  400. + MSG(INIT, "Skip bmt block 0x%x\n", block);
  401. + continue;
  402. + }
  403. +
  404. + if (nand_block_bad_bmt(OFFSET(block)))
  405. + {
  406. + MSG(INIT, "Skip bad block 0x%x\n", block);
  407. + continue;
  408. + }
  409. +
  410. + if (is_block_mapped(block) >= 0)
  411. + {
  412. + MSG(INIT, "Skip mapped block 0x%x\n", block);
  413. + continue;
  414. + }
  415. +
  416. + MSG(INIT, "Find block 0x%x available\n", block);
  417. + return block;
  418. + }
  419. +
  420. + return 0;
  421. +}
  422. +
  423. +static unsigned short get_bad_index_from_oob(u8 * oob_buf)
  424. +{
  425. + unsigned short index;
  426. + memcpy(&index, oob_buf + OOB_INDEX_OFFSET, OOB_INDEX_SIZE);
  427. +
  428. + return index;
  429. +}
  430. +
  431. +void set_bad_index_to_oob(u8 * oob, u16 index)
  432. +{
  433. + memcpy(oob + OOB_INDEX_OFFSET, &index, sizeof(index));
  434. +}
  435. +
  436. +static int migrate_from_bad(int offset, u8 * write_dat, u8 * write_oob)
  437. +{
  438. + int page;
  439. + int error_block = offset / BLOCK_SIZE_BMT;
  440. + int error_page = (offset / PAGE_SIZE_BMT) % page_per_block;
  441. + int to_index;
  442. +
  443. + memcpy(oob_buf, write_oob, MAX_OOB_SIZE);
  444. +
  445. + to_index = find_available_block(false);
  446. +
  447. + if (!to_index)
  448. + {
  449. + MSG(INIT, "Cannot find an available block for BMT\n");
  450. + return 0;
  451. + }
  452. +
  453. + { // migrate error page first
  454. + MSG(INIT, "Write error page: 0x%x\n", error_page);
  455. + if (!write_dat)
  456. + {
  457. + nand_read_page_bmt(PAGE_ADDR(error_block) + error_page, dat_buf, NULL);
  458. + write_dat = dat_buf;
  459. + }
  460. + // memcpy(oob_buf, write_oob, MAX_OOB_SIZE);
  461. +
  462. + if (error_block < system_block_count)
  463. + set_bad_index_to_oob(oob_buf, error_block); // if error_block is already a mapped block, original mapping index is in OOB.
  464. +
  465. + if (!nand_write_page_bmt(PAGE_ADDR(to_index) + error_page, write_dat, oob_buf))
  466. + {
  467. + MSG(INIT, "Write to page 0x%x fail\n", PAGE_ADDR(to_index) + error_page);
  468. + mark_block_bad_bmt(to_index);
  469. + return migrate_from_bad(offset, write_dat, write_oob);
  470. + }
  471. + }
  472. +
  473. + for (page = 0; page < page_per_block; page++)
  474. + {
  475. + if (page != error_page)
  476. + {
  477. + nand_read_page_bmt(PAGE_ADDR(error_block) + page, dat_buf, oob_buf);
  478. + if (is_page_used(dat_buf, oob_buf))
  479. + {
  480. + if (error_block < system_block_count)
  481. + {
  482. + set_bad_index_to_oob(oob_buf, error_block);
  483. + }
  484. + MSG(INIT, "\tmigrate page 0x%x to page 0x%x\n", PAGE_ADDR(error_block) + page, PAGE_ADDR(to_index) + page);
  485. + if (!nand_write_page_bmt(PAGE_ADDR(to_index) + page, dat_buf, oob_buf))
  486. + {
  487. + MSG(INIT, "Write to page 0x%x fail\n", PAGE_ADDR(to_index) + page);
  488. + mark_block_bad_bmt(to_index);
  489. + return migrate_from_bad(offset, write_dat, write_oob);
  490. + }
  491. + }
  492. + }
  493. + }
  494. +
  495. + MSG(INIT, "Migrate from 0x%x to 0x%x done!\n", error_block, to_index);
  496. +
  497. + return to_index;
  498. +}
  499. +
  500. +static bool write_bmt_to_flash(u8 * dat, u8 * oob)
  501. +{
  502. + bool need_erase = true;
  503. + MSG(INIT, "Try to write BMT\n");
  504. +
  505. + if (bmt_block_index == 0)
  506. + {
  507. + // if we don't have index, we don't need to erase found block as it has been erased in find_available_block()
  508. + need_erase = false;
  509. + if (!(bmt_block_index = find_available_block(true)))
  510. + {
  511. + MSG(INIT, "Cannot find an available block for BMT\n");
  512. + return false;
  513. + }
  514. + }
  515. +
  516. + MSG(INIT, "Find BMT block: 0x%x\n", bmt_block_index);
  517. +
  518. + // write bmt to flash
  519. + if (need_erase)
  520. + {
  521. + if (!nand_erase_bmt(OFFSET(bmt_block_index)))
  522. + {
  523. + MSG(INIT, "BMT block erase fail, mark bad: 0x%x\n", bmt_block_index);
  524. + mark_block_bad_bmt(OFFSET(bmt_block_index));
  525. + // bmt.bad_count++;
  526. +
  527. + bmt_block_index = 0;
  528. + return write_bmt_to_flash(dat, oob); // recursive call
  529. + }
  530. + }
  531. +
  532. + if (!nand_write_page_bmt(PAGE_ADDR(bmt_block_index), dat, oob))
  533. + {
  534. + MSG(INIT, "Write BMT data fail, need to write again\n");
  535. + mark_block_bad_bmt(OFFSET(bmt_block_index));
  536. + // bmt.bad_count++;
  537. +
  538. + bmt_block_index = 0;
  539. + return write_bmt_to_flash(dat, oob); // recursive call
  540. + }
  541. +
  542. + MSG(INIT, "Write BMT data to block 0x%x success\n", bmt_block_index);
  543. + return true;
  544. +}
  545. +
  546. +/*******************************************************************
  547. +* Reconstruct bmt, called when found bmt info doesn't match bad
  548. +* block info in flash.
  549. +*
  550. +* Return NULL for failure
  551. +*******************************************************************/
  552. +bmt_struct *reconstruct_bmt(bmt_struct * bmt)
  553. +{
  554. + int i;
  555. + int index = system_block_count;
  556. + unsigned short bad_index;
  557. + int mapped;
  558. +
  559. + // init everything in BMT struct
  560. + bmt->version = BMT_VERSION;
  561. + bmt->bad_count = 0;
  562. + bmt->mapped_count = 0;
  563. +
  564. + memset(bmt->table, 0, bmt_block_count * sizeof(bmt_entry));
  565. +
  566. + for (i = 0; i < bmt_block_count; i++, index++)
  567. + {
  568. + if (nand_block_bad_bmt(OFFSET(index)))
  569. + {
  570. + MSG(INIT, "Skip bad block: 0x%x\n", index);
  571. + // bmt->bad_count++;
  572. + continue;
  573. + }
  574. +
  575. + MSG(INIT, "read page: 0x%x\n", PAGE_ADDR(index));
  576. + nand_read_page_bmt(PAGE_ADDR(index), dat_buf, oob_buf);
  577. + /* if (mtk_nand_read_page_hw(PAGE_ADDR(index), dat_buf))
  578. + {
  579. + MSG(INIT, "Error when read block %d\n", bmt_block_index);
  580. + continue;
  581. + } */
  582. +
  583. + if ((bad_index = get_bad_index_from_oob(oob_buf)) >= system_block_count)
  584. + {
  585. + MSG(INIT, "get bad index: 0x%x\n", bad_index);
  586. + if (bad_index != 0xFFFF)
  587. + MSG(INIT, "Invalid bad index found in block 0x%x, bad index 0x%x\n", index, bad_index);
  588. + continue;
  589. + }
  590. +
  591. + MSG(INIT, "Block 0x%x is mapped to bad block: 0x%x\n", index, bad_index);
  592. +
  593. + if (!nand_block_bad_bmt(OFFSET(bad_index)))
  594. + {
  595. + MSG(INIT, "\tbut block 0x%x is not marked as bad, invalid mapping\n", bad_index);
  596. + continue; // no need to erase here, it will be erased later when trying to write BMT
  597. + }
  598. +
  599. + if ((mapped = is_block_mapped(bad_index)) >= 0)
  600. + {
  601. + MSG(INIT, "bad block 0x%x is mapped to 0x%x, should be caused by power lost, replace with one\n", bmt->table[mapped].bad_index, bmt->table[mapped].mapped_index);
  602. + bmt->table[mapped].mapped_index = index; // use new one instead.
  603. + } else
  604. + {
  605. + // add mapping to BMT
  606. + bmt->table[bmt->mapped_count].bad_index = bad_index;
  607. + bmt->table[bmt->mapped_count].mapped_index = index;
  608. + bmt->mapped_count++;
  609. + }
  610. +
  611. + MSG(INIT, "Add mapping: 0x%x -> 0x%x to BMT\n", bad_index, index);
  612. +
  613. + }
  614. +
  615. + MSG(INIT, "Scan replace pool done, mapped block: %d\n", bmt->mapped_count);
  616. + // dump_bmt_info(bmt);
  617. +
  618. + // fill NAND BMT buffer
  619. + memset(oob_buf, 0xFF, sizeof(oob_buf));
  620. + fill_nand_bmt_buffer(bmt, dat_buf, oob_buf);
  621. +
  622. + // write BMT back
  623. + if (!write_bmt_to_flash(dat_buf, oob_buf))
  624. + {
  625. + MSG(INIT, "TRAGEDY: cannot find a place to write BMT!!!!\n");
  626. + }
  627. +
  628. + return bmt;
  629. +}
  630. +
  631. +/*******************************************************************
  632. +* [BMT Interface]
  633. +*
  634. +* Description:
  635. +* Init bmt from nand. Reconstruct if not found or data error
  636. +*
  637. +* Parameter:
  638. +* size: size of bmt and replace pool
  639. +*
  640. +* Return:
  641. +* NULL for failure, and a bmt struct for success
  642. +*******************************************************************/
  643. +bmt_struct *init_bmt(struct nand_chip * chip, int size)
  644. +{
  645. + struct mtk_nand_host *host;
  646. +
  647. + if (size > 0 && size < MAX_BMT_SIZE)
  648. + {
  649. + MSG(INIT, "Init bmt table, size: %d\n", size);
  650. + bmt_block_count = size;
  651. + } else
  652. + {
  653. + MSG(INIT, "Invalid bmt table size: %d\n", size);
  654. + return NULL;
  655. + }
  656. + nand_chip_bmt = chip;
  657. + system_block_count = chip->chipsize >> chip->phys_erase_shift;
  658. + total_block_count = bmt_block_count + system_block_count;
  659. + page_per_block = BLOCK_SIZE_BMT / PAGE_SIZE_BMT;
  660. + host = (struct mtk_nand_host *)chip->priv;
  661. + mtd_bmt = host->mtd;
  662. +
  663. + MSG(INIT, "mtd_bmt: %p, nand_chip_bmt: %p\n", mtd_bmt, nand_chip_bmt);
  664. + MSG(INIT, "bmt count: %d, system count: %d\n", bmt_block_count, system_block_count);
  665. +
  666. + // set this flag, and unmapped block in pool will be erased.
  667. + pool_erased = 0;
  668. + memset(bmt.table, 0, size * sizeof(bmt_entry));
  669. + if ((bmt_block_index = load_bmt_data(system_block_count, size)))
  670. + {
  671. + MSG(INIT, "Load bmt data success @ block 0x%x\n", bmt_block_index);
  672. + dump_bmt_info(&bmt);
  673. + return &bmt;
  674. + } else
  675. + {
  676. + MSG(INIT, "Load bmt data fail, need re-construct!\n");
  677. +#ifndef __UBOOT_NAND__ // BMT is not re-constructed in UBOOT.
  678. + if (reconstruct_bmt(&bmt))
  679. + return &bmt;
  680. + else
  681. +#endif
  682. + return NULL;
  683. + }
  684. +}
  685. +
  686. +/*******************************************************************
  687. +* [BMT Interface]
  688. +*
  689. +* Description:
  690. +* Update BMT.
  691. +*
  692. +* Parameter:
  693. +* offset: update block/page offset.
  694. +* reason: update reason, see update_reason_t for reason.
  695. +* dat/oob: data and oob buffer for write fail.
  696. +*
  697. +* Return:
  698. +* Return true for success, and false for failure.
  699. +*******************************************************************/
  700. +bool update_bmt(u32 offset, update_reason_t reason, u8 * dat, u8 * oob)
  701. +{
  702. + int map_index;
  703. + int orig_bad_block = -1;
  704. + // int bmt_update_index;
  705. + int i;
  706. + int bad_index = offset / BLOCK_SIZE_BMT;
  707. +
  708. +#ifndef MTK_NAND_BMT
  709. + return false;
  710. +#endif
  711. + if (reason == UPDATE_WRITE_FAIL)
  712. + {
  713. + MSG(INIT, "Write fail, need to migrate\n");
  714. + if (!(map_index = migrate_from_bad(offset, dat, oob)))
  715. + {
  716. + MSG(INIT, "migrate fail\n");
  717. + return false;
  718. + }
  719. + } else
  720. + {
  721. + if (!(map_index = find_available_block(false)))
  722. + {
  723. + MSG(INIT, "Cannot find block in pool\n");
  724. + return false;
  725. + }
  726. + }
  727. +
  728. + // now let's update BMT
  729. + if (bad_index >= system_block_count) // mapped block become bad, find original bad block
  730. + {
  731. + for (i = 0; i < bmt_block_count; i++)
  732. + {
  733. + if (bmt.table[i].mapped_index == bad_index)
  734. + {
  735. + orig_bad_block = bmt.table[i].bad_index;
  736. + break;
  737. + }
  738. + }
  739. + // bmt.bad_count++;
  740. + MSG(INIT, "Mapped block becomes bad, orig bad block is 0x%x\n", orig_bad_block);
  741. +
  742. + bmt.table[i].mapped_index = map_index;
  743. + } else
  744. + {
  745. + bmt.table[bmt.mapped_count].mapped_index = map_index;
  746. + bmt.table[bmt.mapped_count].bad_index = bad_index;
  747. + bmt.mapped_count++;
  748. + }
  749. +
  750. + memset(oob_buf, 0xFF, sizeof(oob_buf));
  751. + fill_nand_bmt_buffer(&bmt, dat_buf, oob_buf);
  752. + if (!write_bmt_to_flash(dat_buf, oob_buf))
  753. + return false;
  754. +
  755. + mark_block_bad_bmt(offset);
  756. +
  757. + return true;
  758. +}
  759. +
  760. +/*******************************************************************
  761. +* [BMT Interface]
  762. +*
  763. +* Description:
  764. +* Given an block index, return mapped index if it's mapped, else
  765. +* return given index.
  766. +*
  767. +* Parameter:
  768. +* index: given an block index. This value cannot exceed
  769. +* system_block_count.
  770. +*
  771. +* Return NULL for failure
  772. +*******************************************************************/
  773. +u16 get_mapping_block_index(int index)
  774. +{
  775. + int i;
  776. +#ifndef MTK_NAND_BMT
  777. + return index;
  778. +#endif
  779. + if (index > system_block_count)
  780. + {
  781. + return index;
  782. + }
  783. +
  784. + for (i = 0; i < bmt.mapped_count; i++)
  785. + {
  786. + if (bmt.table[i].bad_index == index)
  787. + {
  788. + return bmt.table[i].mapped_index;
  789. + }
  790. + }
  791. +
  792. + return index;
  793. +}
  794. +#ifdef __KERNEL_NAND__
  795. +EXPORT_SYMBOL_GPL(init_bmt);
  796. +EXPORT_SYMBOL_GPL(update_bmt);
  797. +EXPORT_SYMBOL_GPL(get_mapping_block_index);
  798. +
  799. +MODULE_LICENSE("GPL");
  800. +MODULE_AUTHOR("MediaTek");
  801. +MODULE_DESCRIPTION("Bad Block mapping management for MediaTek NAND Flash Driver");
  802. +#endif
  803. --- /dev/null
  804. +++ b/drivers/mtd/nand/bmt.h
  805. @@ -0,0 +1,80 @@
  806. +#ifndef __BMT_H__
  807. +#define __BMT_H__
  808. +
  809. +#include "nand_def.h"
  810. +
  811. +#if defined(__PRELOADER_NAND__)
  812. +
  813. +#include "nand.h"
  814. +
  815. +#elif defined(__UBOOT_NAND__)
  816. +
  817. +#include <linux/mtd/nand.h>
  818. +#include "mtk_nand2.h"
  819. +
  820. +#elif defined(__KERNEL_NAND__)
  821. +
  822. +#include <linux/mtd/mtd.h>
  823. +#include <linux/mtd/rawnand.h>
  824. +#include <linux/module.h>
  825. +#include "mtk_nand2.h"
  826. +
  827. +#endif
  828. +
  829. +
  830. +#define MAX_BMT_SIZE (0x80)
  831. +#define BMT_VERSION (1) // initial version
  832. +
  833. +#define MAIN_SIGNATURE_OFFSET (0)
  834. +#define OOB_SIGNATURE_OFFSET (1)
  835. +#define OOB_INDEX_OFFSET (29)
  836. +#define OOB_INDEX_SIZE (2)
  837. +#define FAKE_INDEX (0xAAAA)
  838. +
  839. +typedef struct _bmt_entry_
  840. +{
  841. + u16 bad_index; // bad block index
  842. + u16 mapped_index; // mapping block index in the replace pool
  843. +} bmt_entry;
  844. +
  845. +typedef enum
  846. +{
  847. + UPDATE_ERASE_FAIL,
  848. + UPDATE_WRITE_FAIL,
  849. + UPDATE_UNMAPPED_BLOCK,
  850. + UPDATE_REASON_COUNT,
  851. +} update_reason_t;
  852. +
  853. +typedef struct
  854. +{
  855. + bmt_entry table[MAX_BMT_SIZE];
  856. + u8 version;
  857. + u8 mapped_count; // mapped block count in pool
  858. + u8 bad_count; // bad block count in pool. Not used in V1
  859. +} bmt_struct;
  860. +
  861. +/***************************************************************
  862. +* *
  863. +* Interface BMT need to use *
  864. +* *
  865. +***************************************************************/
  866. +extern bool mtk_nand_exec_read_page(struct mtd_info *mtd, u32 row, u32 page_size, u8 * dat, u8 * oob);
  867. +extern int mtk_nand_block_bad_hw(struct mtd_info *mtd, loff_t ofs);
  868. +extern int mtk_nand_erase_hw(struct mtd_info *mtd, int page);
  869. +extern int mtk_nand_block_markbad_hw(struct mtd_info *mtd, loff_t ofs);
  870. +extern int mtk_nand_exec_write_page(struct mtd_info *mtd, u32 row, u32 page_size, u8 * dat, u8 * oob);
  871. +
  872. +
  873. +/***************************************************************
  874. +* *
  875. +* Different function interface for preloader/uboot/kernel *
  876. +* *
  877. +***************************************************************/
  878. +void set_bad_index_to_oob(u8 * oob, u16 index);
  879. +
  880. +
  881. +bmt_struct *init_bmt(struct nand_chip *nand, int size);
  882. +bool update_bmt(u32 offset, update_reason_t reason, u8 * dat, u8 * oob);
  883. +unsigned short get_mapping_block_index(int index);
  884. +
  885. +#endif // #ifndef __BMT_H__
  886. --- /dev/null
  887. +++ b/drivers/mtd/nand/dev-nand.c
  888. @@ -0,0 +1,63 @@
  889. +#include <linux/init.h>
  890. +#include <linux/kernel.h>
  891. +#include <linux/platform_device.h>
  892. +
  893. +#include "mt6575_typedefs.h"
  894. +
  895. +#define RALINK_NAND_CTRL_BASE 0xBE003000
  896. +#define NFI_base RALINK_NAND_CTRL_BASE
  897. +#define RALINK_NANDECC_CTRL_BASE 0xBE003800
  898. +#define NFIECC_base RALINK_NANDECC_CTRL_BASE
  899. +#define MT7621_NFI_IRQ_ID SURFBOARDINT_NAND
  900. +#define MT7621_NFIECC_IRQ_ID SURFBOARDINT_NAND_ECC
  901. +
  902. +#define SURFBOARDINT_NAND 22
  903. +#define SURFBOARDINT_NAND_ECC 23
  904. +
  905. +static struct resource MT7621_resource_nand[] = {
  906. + {
  907. + .start = NFI_base,
  908. + .end = NFI_base + 0x1A0,
  909. + .flags = IORESOURCE_MEM,
  910. + },
  911. + {
  912. + .start = NFIECC_base,
  913. + .end = NFIECC_base + 0x150,
  914. + .flags = IORESOURCE_MEM,
  915. + },
  916. + {
  917. + .start = MT7621_NFI_IRQ_ID,
  918. + .flags = IORESOURCE_IRQ,
  919. + },
  920. + {
  921. + .start = MT7621_NFIECC_IRQ_ID,
  922. + .flags = IORESOURCE_IRQ,
  923. + },
  924. +};
  925. +
  926. +static struct platform_device MT7621_nand_dev = {
  927. + .name = "MT7621-NAND",
  928. + .id = 0,
  929. + .num_resources = ARRAY_SIZE(MT7621_resource_nand),
  930. + .resource = MT7621_resource_nand,
  931. + .dev = {
  932. + .platform_data = &mt7621_nand_hw,
  933. + },
  934. +};
  935. +
  936. +
  937. +int __init mtk_nand_register(void)
  938. +{
  939. +
  940. + int retval = 0;
  941. +
  942. + retval = platform_device_register(&MT7621_nand_dev);
  943. + if (retval != 0) {
  944. + printk(KERN_ERR "register nand device fail\n");
  945. + return retval;
  946. + }
  947. +
  948. +
  949. + return retval;
  950. +}
  951. +arch_initcall(mtk_nand_register);
  952. --- /dev/null
  953. +++ b/drivers/mtd/nand/mt6575_typedefs.h
  954. @@ -0,0 +1,340 @@
  955. +/* Copyright Statement:
  956. + *
  957. + * This software/firmware and related documentation ("MediaTek Software") are
  958. + * protected under relevant copyright laws. The information contained herein
  959. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  960. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  961. + * any reproduction, modification, use or disclosure of MediaTek Software,
  962. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  963. + */
  964. +/* MediaTek Inc. (C) 2010. All rights reserved.
  965. + *
  966. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  967. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  968. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  969. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  970. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  971. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  972. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  973. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  974. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  975. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  976. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  977. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  978. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  979. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  980. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  981. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  982. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  983. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  984. + *
  985. + * The following software/firmware and/or related documentation ("MediaTek Software")
  986. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  987. + * applicable license agreements with MediaTek Inc.
  988. + */
  989. +
  990. +/*****************************************************************************
  991. +* Copyright Statement:
  992. +* --------------------
  993. +* This software is protected by Copyright and the information contained
  994. +* herein is confidential. The software may not be copied and the information
  995. +* contained herein may not be used or disclosed except with the written
  996. +* permission of MediaTek Inc. (C) 2008
  997. +*
  998. +* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  999. +* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  1000. +* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  1001. +* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  1002. +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  1003. +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  1004. +* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  1005. +* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  1006. +* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  1007. +* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  1008. +* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  1009. +* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  1010. +*
  1011. +* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  1012. +* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  1013. +* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  1014. +* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  1015. +* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  1016. +*
  1017. +* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  1018. +* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  1019. +* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  1020. +* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  1021. +* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  1022. +*
  1023. +*****************************************************************************/
  1024. +
  1025. +#ifndef _MT6575_TYPEDEFS_H
  1026. +#define _MT6575_TYPEDEFS_H
  1027. +
  1028. +#if defined (__KERNEL_NAND__)
  1029. +#include <linux/bug.h>
  1030. +#else
  1031. +#define true 1
  1032. +#define false 0
  1033. +#define bool u8
  1034. +#endif
  1035. +
  1036. +// ---------------------------------------------------------------------------
  1037. +// Basic Type Definitions
  1038. +// ---------------------------------------------------------------------------
  1039. +
  1040. +typedef volatile unsigned char *P_kal_uint8;
  1041. +typedef volatile unsigned short *P_kal_uint16;
  1042. +typedef volatile unsigned int *P_kal_uint32;
  1043. +
  1044. +typedef long LONG;
  1045. +typedef unsigned char UBYTE;
  1046. +typedef short SHORT;
  1047. +
  1048. +typedef signed char kal_int8;
  1049. +typedef signed short kal_int16;
  1050. +typedef signed int kal_int32;
  1051. +typedef long long kal_int64;
  1052. +typedef unsigned char kal_uint8;
  1053. +typedef unsigned short kal_uint16;
  1054. +typedef unsigned int kal_uint32;
  1055. +typedef unsigned long long kal_uint64;
  1056. +typedef char kal_char;
  1057. +
  1058. +typedef unsigned int *UINT32P;
  1059. +typedef volatile unsigned short *UINT16P;
  1060. +typedef volatile unsigned char *UINT8P;
  1061. +typedef unsigned char *U8P;
  1062. +
  1063. +typedef volatile unsigned char *P_U8;
  1064. +typedef volatile signed char *P_S8;
  1065. +typedef volatile unsigned short *P_U16;
  1066. +typedef volatile signed short *P_S16;
  1067. +typedef volatile unsigned int *P_U32;
  1068. +typedef volatile signed int *P_S32;
  1069. +typedef unsigned long long *P_U64;
  1070. +typedef signed long long *P_S64;
  1071. +
  1072. +typedef unsigned char U8;
  1073. +typedef signed char S8;
  1074. +typedef unsigned short U16;
  1075. +typedef signed short S16;
  1076. +typedef unsigned int U32;
  1077. +typedef signed int S32;
  1078. +typedef unsigned long long U64;
  1079. +typedef signed long long S64;
  1080. +//typedef unsigned char bool;
  1081. +
  1082. +typedef unsigned char UINT8;
  1083. +typedef unsigned short UINT16;
  1084. +typedef unsigned int UINT32;
  1085. +typedef unsigned short USHORT;
  1086. +typedef signed char INT8;
  1087. +typedef signed short INT16;
  1088. +typedef signed int INT32;
  1089. +typedef unsigned int DWORD;
  1090. +typedef void VOID;
  1091. +typedef unsigned char BYTE;
  1092. +typedef float FLOAT;
  1093. +
  1094. +typedef char *LPCSTR;
  1095. +typedef short *LPWSTR;
  1096. +
  1097. +
  1098. +// ---------------------------------------------------------------------------
  1099. +// Constants
  1100. +// ---------------------------------------------------------------------------
  1101. +
  1102. +#define IMPORT EXTERN
  1103. +#ifndef __cplusplus
  1104. + #define EXTERN extern
  1105. +#else
  1106. + #define EXTERN extern "C"
  1107. +#endif
  1108. +#define LOCAL static
  1109. +#define GLOBAL
  1110. +#define EXPORT GLOBAL
  1111. +
  1112. +#define EQ ==
  1113. +#define NEQ !=
  1114. +#define AND &&
  1115. +#define OR ||
  1116. +#define XOR(A,B) ((!(A) AND (B)) OR ((A) AND !(B)))
  1117. +
  1118. +#ifndef FALSE
  1119. + #define FALSE (0)
  1120. +#endif
  1121. +
  1122. +#ifndef TRUE
  1123. + #define TRUE (1)
  1124. +#endif
  1125. +
  1126. +#ifndef NULL
  1127. + #define NULL (0)
  1128. +#endif
  1129. +
  1130. +//enum boolean {false, true};
  1131. +enum {RX, TX, NONE};
  1132. +
  1133. +#ifndef BOOL
  1134. +typedef unsigned char BOOL;
  1135. +#endif
  1136. +
  1137. +typedef enum {
  1138. + KAL_FALSE = 0,
  1139. + KAL_TRUE = 1,
  1140. +} kal_bool;
  1141. +
  1142. +
  1143. +// ---------------------------------------------------------------------------
  1144. +// Type Casting
  1145. +// ---------------------------------------------------------------------------
  1146. +
  1147. +#define AS_INT32(x) (*(INT32 *)((void*)x))
  1148. +#define AS_INT16(x) (*(INT16 *)((void*)x))
  1149. +#define AS_INT8(x) (*(INT8 *)((void*)x))
  1150. +
  1151. +#define AS_UINT32(x) (*(UINT32 *)((void*)x))
  1152. +#define AS_UINT16(x) (*(UINT16 *)((void*)x))
  1153. +#define AS_UINT8(x) (*(UINT8 *)((void*)x))
  1154. +
  1155. +
  1156. +// ---------------------------------------------------------------------------
  1157. +// Register Manipulations
  1158. +// ---------------------------------------------------------------------------
  1159. +
  1160. +#define READ_REGISTER_UINT32(reg) \
  1161. + (*(volatile UINT32 * const)(reg))
  1162. +
  1163. +#define WRITE_REGISTER_UINT32(reg, val) \
  1164. + (*(volatile UINT32 * const)(reg)) = (val)
  1165. +
  1166. +#define READ_REGISTER_UINT16(reg) \
  1167. + (*(volatile UINT16 * const)(reg))
  1168. +
  1169. +#define WRITE_REGISTER_UINT16(reg, val) \
  1170. + (*(volatile UINT16 * const)(reg)) = (val)
  1171. +
  1172. +#define READ_REGISTER_UINT8(reg) \
  1173. + (*(volatile UINT8 * const)(reg))
  1174. +
  1175. +#define WRITE_REGISTER_UINT8(reg, val) \
  1176. + (*(volatile UINT8 * const)(reg)) = (val)
  1177. +
  1178. +#define INREG8(x) READ_REGISTER_UINT8((UINT8*)((void*)(x)))
  1179. +#define OUTREG8(x, y) WRITE_REGISTER_UINT8((UINT8*)((void*)(x)), (UINT8)(y))
  1180. +#define SETREG8(x, y) OUTREG8(x, INREG8(x)|(y))
  1181. +#define CLRREG8(x, y) OUTREG8(x, INREG8(x)&~(y))
  1182. +#define MASKREG8(x, y, z) OUTREG8(x, (INREG8(x)&~(y))|(z))
  1183. +
  1184. +#define INREG16(x) READ_REGISTER_UINT16((UINT16*)((void*)(x)))
  1185. +#define OUTREG16(x, y) WRITE_REGISTER_UINT16((UINT16*)((void*)(x)),(UINT16)(y))
  1186. +#define SETREG16(x, y) OUTREG16(x, INREG16(x)|(y))
  1187. +#define CLRREG16(x, y) OUTREG16(x, INREG16(x)&~(y))
  1188. +#define MASKREG16(x, y, z) OUTREG16(x, (INREG16(x)&~(y))|(z))
  1189. +
  1190. +#define INREG32(x) READ_REGISTER_UINT32((UINT32*)((void*)(x)))
  1191. +#define OUTREG32(x, y) WRITE_REGISTER_UINT32((UINT32*)((void*)(x)), (UINT32)(y))
  1192. +#define SETREG32(x, y) OUTREG32(x, INREG32(x)|(y))
  1193. +#define CLRREG32(x, y) OUTREG32(x, INREG32(x)&~(y))
  1194. +#define MASKREG32(x, y, z) OUTREG32(x, (INREG32(x)&~(y))|(z))
  1195. +
  1196. +
  1197. +#define DRV_Reg8(addr) INREG8(addr)
  1198. +#define DRV_WriteReg8(addr, data) OUTREG8(addr, data)
  1199. +#define DRV_SetReg8(addr, data) SETREG8(addr, data)
  1200. +#define DRV_ClrReg8(addr, data) CLRREG8(addr, data)
  1201. +
  1202. +#define DRV_Reg16(addr) INREG16(addr)
  1203. +#define DRV_WriteReg16(addr, data) OUTREG16(addr, data)
  1204. +#define DRV_SetReg16(addr, data) SETREG16(addr, data)
  1205. +#define DRV_ClrReg16(addr, data) CLRREG16(addr, data)
  1206. +
  1207. +#define DRV_Reg32(addr) INREG32(addr)
  1208. +#define DRV_WriteReg32(addr, data) OUTREG32(addr, data)
  1209. +#define DRV_SetReg32(addr, data) SETREG32(addr, data)
  1210. +#define DRV_ClrReg32(addr, data) CLRREG32(addr, data)
  1211. +
  1212. +// !!! DEPRECATED, WILL BE REMOVED LATER !!!
  1213. +#define DRV_Reg(addr) DRV_Reg16(addr)
  1214. +#define DRV_WriteReg(addr, data) DRV_WriteReg16(addr, data)
  1215. +#define DRV_SetReg(addr, data) DRV_SetReg16(addr, data)
  1216. +#define DRV_ClrReg(addr, data) DRV_ClrReg16(addr, data)
  1217. +
  1218. +
  1219. +// ---------------------------------------------------------------------------
  1220. +// Compiler Time Deduction Macros
  1221. +// ---------------------------------------------------------------------------
  1222. +
  1223. +#define _MASK_OFFSET_1(x, n) ((x) & 0x1) ? (n) :
  1224. +#define _MASK_OFFSET_2(x, n) _MASK_OFFSET_1((x), (n)) _MASK_OFFSET_1((x) >> 1, (n) + 1)
  1225. +#define _MASK_OFFSET_4(x, n) _MASK_OFFSET_2((x), (n)) _MASK_OFFSET_2((x) >> 2, (n) + 2)
  1226. +#define _MASK_OFFSET_8(x, n) _MASK_OFFSET_4((x), (n)) _MASK_OFFSET_4((x) >> 4, (n) + 4)
  1227. +#define _MASK_OFFSET_16(x, n) _MASK_OFFSET_8((x), (n)) _MASK_OFFSET_8((x) >> 8, (n) + 8)
  1228. +#define _MASK_OFFSET_32(x, n) _MASK_OFFSET_16((x), (n)) _MASK_OFFSET_16((x) >> 16, (n) + 16)
  1229. +
  1230. +#define MASK_OFFSET_ERROR (0xFFFFFFFF)
  1231. +
  1232. +#define MASK_OFFSET(x) (_MASK_OFFSET_32(x, 0) MASK_OFFSET_ERROR)
  1233. +
  1234. +
  1235. +// ---------------------------------------------------------------------------
  1236. +// Assertions
  1237. +// ---------------------------------------------------------------------------
  1238. +
  1239. +#ifndef ASSERT
  1240. + #define ASSERT(expr) BUG_ON(!(expr))
  1241. +#endif
  1242. +
  1243. +#ifndef NOT_IMPLEMENTED
  1244. + #define NOT_IMPLEMENTED() BUG_ON(1)
  1245. +#endif
  1246. +
  1247. +#define STATIC_ASSERT(pred) STATIC_ASSERT_X(pred, __LINE__)
  1248. +#define STATIC_ASSERT_X(pred, line) STATIC_ASSERT_XX(pred, line)
  1249. +#define STATIC_ASSERT_XX(pred, line) \
  1250. + extern char assertion_failed_at_##line[(pred) ? 1 : -1]
  1251. +
  1252. +// ---------------------------------------------------------------------------
  1253. +// Resolve Compiler Warnings
  1254. +// ---------------------------------------------------------------------------
  1255. +
  1256. +#define NOT_REFERENCED(x) { (x) = (x); }
  1257. +
  1258. +
  1259. +// ---------------------------------------------------------------------------
  1260. +// Utilities
  1261. +// ---------------------------------------------------------------------------
  1262. +
  1263. +#define MAXIMUM(A,B) (((A)>(B))?(A):(B))
  1264. +#define MINIMUM(A,B) (((A)<(B))?(A):(B))
  1265. +
  1266. +#define ARY_SIZE(x) (sizeof((x)) / sizeof((x[0])))
  1267. +#define DVT_DELAYMACRO(u4Num) \
  1268. +{ \
  1269. + UINT32 u4Count = 0 ; \
  1270. + for (u4Count = 0; u4Count < u4Num; u4Count++ ); \
  1271. +} \
  1272. +
  1273. +#define A68351B 0
  1274. +#define B68351B 1
  1275. +#define B68351D 2
  1276. +#define B68351E 3
  1277. +#define UNKNOWN_IC_VERSION 0xFF
  1278. +
  1279. +/* NAND driver */
  1280. +struct mtk_nand_host_hw {
  1281. + unsigned int nfi_bus_width; /* NFI_BUS_WIDTH */
  1282. + unsigned int nfi_access_timing; /* NFI_ACCESS_TIMING */
  1283. + unsigned int nfi_cs_num; /* NFI_CS_NUM */
  1284. + unsigned int nand_sec_size; /* NAND_SECTOR_SIZE */
  1285. + unsigned int nand_sec_shift; /* NAND_SECTOR_SHIFT */
  1286. + unsigned int nand_ecc_size;
  1287. + unsigned int nand_ecc_bytes;
  1288. + unsigned int nand_ecc_mode;
  1289. +};
  1290. +extern struct mtk_nand_host_hw mt7621_nand_hw;
  1291. +extern unsigned int CFG_BLOCKSIZE;
  1292. +
  1293. +#endif // _MT6575_TYPEDEFS_H
  1294. +
  1295. --- /dev/null
  1296. +++ b/drivers/mtd/nand/mtk_nand2.c
  1297. @@ -0,0 +1,2345 @@
  1298. +/******************************************************************************
  1299. +* mtk_nand2.c - MTK NAND Flash Device Driver
  1300. + *
  1301. +* Copyright 2009-2012 MediaTek Co.,Ltd.
  1302. + *
  1303. +* DESCRIPTION:
  1304. +* This file provid the other drivers nand relative functions
  1305. + *
  1306. +* modification history
  1307. +* ----------------------------------------
  1308. +* v3.0, 11 Feb 2010, mtk
  1309. +* ----------------------------------------
  1310. +******************************************************************************/
  1311. +#include "nand_def.h"
  1312. +#include <linux/slab.h>
  1313. +#include <linux/init.h>
  1314. +#include <linux/module.h>
  1315. +#include <linux/delay.h>
  1316. +#include <linux/errno.h>
  1317. +#include <linux/sched.h>
  1318. +#include <linux/types.h>
  1319. +#include <linux/wait.h>
  1320. +#include <linux/spinlock.h>
  1321. +#include <linux/interrupt.h>
  1322. +#include <linux/mtd/mtd.h>
  1323. +#include <linux/mtd/rawnand.h>
  1324. +#include <linux/mtd/partitions.h>
  1325. +#include <linux/mtd/nand_ecc.h>
  1326. +#include <linux/dma-mapping.h>
  1327. +#include <linux/jiffies.h>
  1328. +#include <linux/platform_device.h>
  1329. +#include <linux/proc_fs.h>
  1330. +#include <linux/time.h>
  1331. +#include <linux/mm.h>
  1332. +#include <asm/io.h>
  1333. +#include <asm/cacheflush.h>
  1334. +#include <asm/uaccess.h>
  1335. +#include <linux/miscdevice.h>
  1336. +#include "mtk_nand2.h"
  1337. +#include "nand_device_list.h"
  1338. +
  1339. +#include "bmt.h"
  1340. +#include "partition.h"
  1341. +
  1342. +unsigned int CFG_BLOCKSIZE;
  1343. +
  1344. +static int shift_on_bbt = 0;
  1345. +int mtk_nand_read_oob_hw(struct mtd_info *mtd, struct nand_chip *chip, int page);
  1346. +
  1347. +static const char * const probe_types[] = { "cmdlinepart", "ofpart", NULL };
  1348. +
  1349. +#define NAND_CMD_STATUS_MULTI 0x71
  1350. +
  1351. +void show_stack(struct task_struct *tsk, unsigned long *sp);
  1352. +extern void mt_irq_set_sens(unsigned int irq, unsigned int sens);
  1353. +extern void mt_irq_set_polarity(unsigned int irq,unsigned int polarity);
  1354. +
  1355. +struct mtk_nand_host mtk_nand_host; /* include mtd_info and nand_chip structs */
  1356. +struct mtk_nand_host_hw mt7621_nand_hw = {
  1357. + .nfi_bus_width = 8,
  1358. + .nfi_access_timing = NFI_DEFAULT_ACCESS_TIMING,
  1359. + .nfi_cs_num = NFI_CS_NUM,
  1360. + .nand_sec_size = 512,
  1361. + .nand_sec_shift = 9,
  1362. + .nand_ecc_size = 2048,
  1363. + .nand_ecc_bytes = 32,
  1364. + .nand_ecc_mode = NAND_ECC_HW,
  1365. +};
  1366. +
  1367. +
  1368. +/*******************************************************************************
  1369. + * Gloable Varible Definition
  1370. + *******************************************************************************/
  1371. +
  1372. +#define NFI_ISSUE_COMMAND(cmd, col_addr, row_addr, col_num, row_num) \
  1373. + do { \
  1374. + DRV_WriteReg(NFI_CMD_REG16,cmd);\
  1375. + while (DRV_Reg32(NFI_STA_REG32) & STA_CMD_STATE);\
  1376. + DRV_WriteReg32(NFI_COLADDR_REG32, col_addr);\
  1377. + DRV_WriteReg32(NFI_ROWADDR_REG32, row_addr);\
  1378. + DRV_WriteReg(NFI_ADDRNOB_REG16, col_num | (row_num<<ADDR_ROW_NOB_SHIFT));\
  1379. + while (DRV_Reg32(NFI_STA_REG32) & STA_ADDR_STATE);\
  1380. + }while(0);
  1381. +
  1382. +//-------------------------------------------------------------------------------
  1383. +static struct NAND_CMD g_kCMD;
  1384. +static u32 g_u4ChipVer;
  1385. +bool g_bInitDone;
  1386. +static bool g_bcmdstatus;
  1387. +static u32 g_value = 0;
  1388. +static int g_page_size;
  1389. +
  1390. +BOOL g_bHwEcc = true;
  1391. +
  1392. +
  1393. +extern void nand_release_device(struct mtd_info *mtd);
  1394. +extern int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state);
  1395. +
  1396. +#if defined(MTK_NAND_BMT)
  1397. +static bmt_struct *g_bmt;
  1398. +#endif
  1399. +struct mtk_nand_host *host;
  1400. +extern struct mtd_partition g_pasStatic_Partition[];
  1401. +int part_num = NUM_PARTITIONS;
  1402. +int manu_id;
  1403. +int dev_id;
  1404. +
  1405. +/* this constant was taken from linux/nand/nand.h v 3.14
  1406. + * in later versions it seems it was removed in order to save a bit of space
  1407. + */
  1408. +#define NAND_MAX_OOBSIZE 774
  1409. +static u8 local_oob_buf[NAND_MAX_OOBSIZE];
  1410. +
  1411. +static u8 nand_badblock_offset = 0;
  1412. +
  1413. +static void nand_bbt_set(struct mtd_info *mtd, int page, int flag)
  1414. +{
  1415. + struct nand_chip *this = mtd->priv;
  1416. + int block;
  1417. +
  1418. + block = (int)(page >> (this->bbt_erase_shift - this->page_shift - 1));
  1419. + this->bbt[block >> 3] &= ~(0x03 << (block & 0x6));
  1420. + this->bbt[block >> 3] |= (flag & 0x3) << (block & 0x6);
  1421. +}
  1422. +
  1423. +static int nand_bbt_get(struct mtd_info *mtd, int page)
  1424. +{
  1425. + struct nand_chip *this = mtd->priv;
  1426. + int block;
  1427. +
  1428. + block = (int)(page >> (this->bbt_erase_shift - this->page_shift - 1));
  1429. + return (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1430. +}
  1431. +
  1432. +void nand_enable_clock(void)
  1433. +{
  1434. + //enable_clock(MT65XX_PDN_PERI_NFI, "NAND");
  1435. +}
  1436. +
  1437. +void nand_disable_clock(void)
  1438. +{
  1439. + //disable_clock(MT65XX_PDN_PERI_NFI, "NAND");
  1440. +}
  1441. +
  1442. +struct nand_ecclayout {
  1443. + __u32 eccbytes;
  1444. + __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
  1445. + __u32 oobavail;
  1446. + struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
  1447. +};
  1448. +
  1449. +static struct nand_ecclayout *layout;
  1450. +
  1451. +static struct nand_ecclayout nand_oob_16 = {
  1452. + .eccbytes = 8,
  1453. + .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  1454. + .oobfree = {{1, 6}, {0, 0}}
  1455. +};
  1456. +
  1457. +struct nand_ecclayout nand_oob_64 = {
  1458. + .eccbytes = 32,
  1459. + .eccpos = {32, 33, 34, 35, 36, 37, 38, 39,
  1460. + 40, 41, 42, 43, 44, 45, 46, 47,
  1461. + 48, 49, 50, 51, 52, 53, 54, 55,
  1462. + 56, 57, 58, 59, 60, 61, 62, 63},
  1463. + .oobfree = {{1, 7}, {9, 7}, {17, 7}, {25, 6}, {0, 0}}
  1464. +};
  1465. +
  1466. +struct nand_ecclayout nand_oob_128 = {
  1467. + .eccbytes = 64,
  1468. + .eccpos = {
  1469. + 64, 65, 66, 67, 68, 69, 70, 71,
  1470. + 72, 73, 74, 75, 76, 77, 78, 79,
  1471. + 80, 81, 82, 83, 84, 85, 86, 86,
  1472. + 88, 89, 90, 91, 92, 93, 94, 95,
  1473. + 96, 97, 98, 99, 100, 101, 102, 103,
  1474. + 104, 105, 106, 107, 108, 109, 110, 111,
  1475. + 112, 113, 114, 115, 116, 117, 118, 119,
  1476. + 120, 121, 122, 123, 124, 125, 126, 127},
  1477. + .oobfree = {{1, 7}, {9, 7}, {17, 7}, {25, 7}, {33, 7}, {41, 7}, {49, 7}, {57, 6}}
  1478. +};
  1479. +
  1480. +flashdev_info devinfo;
  1481. +
  1482. +void dump_nfi(void)
  1483. +{
  1484. +}
  1485. +
  1486. +void dump_ecc(void)
  1487. +{
  1488. +}
  1489. +
  1490. +u32
  1491. +nand_virt_to_phys_add(u32 va)
  1492. +{
  1493. + u32 pageOffset = (va & (PAGE_SIZE - 1));
  1494. + pgd_t *pgd;
  1495. + pmd_t *pmd;
  1496. + pte_t *pte;
  1497. + u32 pa;
  1498. +
  1499. + if (virt_addr_valid(va))
  1500. + return __virt_to_phys(va);
  1501. +
  1502. + if (NULL == current) {
  1503. + printk(KERN_ERR "[nand_virt_to_phys_add] ERROR ,current is NULL! \n");
  1504. + return 0;
  1505. + }
  1506. +
  1507. + if (NULL == current->mm) {
  1508. + printk(KERN_ERR "[nand_virt_to_phys_add] ERROR current->mm is NULL! tgid=0x%x, name=%s \n", current->tgid, current->comm);
  1509. + return 0;
  1510. + }
  1511. +
  1512. + pgd = pgd_offset(current->mm, va); /* what is tsk->mm */
  1513. + if (pgd_none(*pgd) || pgd_bad(*pgd)) {
  1514. + printk(KERN_ERR "[nand_virt_to_phys_add] ERROR, va=0x%x, pgd invalid! \n", va);
  1515. + return 0;
  1516. + }
  1517. +
  1518. + pmd = pmd_offset((pud_t *)pgd, va);
  1519. + if (pmd_none(*pmd) || pmd_bad(*pmd)) {
  1520. + printk(KERN_ERR "[nand_virt_to_phys_add] ERROR, va=0x%x, pmd invalid! \n", va);
  1521. + return 0;
  1522. + }
  1523. +
  1524. + pte = pte_offset_map(pmd, va);
  1525. + if (pte_present(*pte)) {
  1526. + pa = (pte_val(*pte) & (PAGE_MASK)) | pageOffset;
  1527. + return pa;
  1528. + }
  1529. +
  1530. + printk(KERN_ERR "[nand_virt_to_phys_add] ERROR va=0x%x, pte invalid! \n", va);
  1531. + return 0;
  1532. +}
  1533. +EXPORT_SYMBOL(nand_virt_to_phys_add);
  1534. +
  1535. +bool
  1536. +get_device_info(u16 id, u32 ext_id, flashdev_info * pdevinfo)
  1537. +{
  1538. + u32 index;
  1539. + for (index = 0; gen_FlashTable[index].id != 0; index++) {
  1540. + if (id == gen_FlashTable[index].id && ext_id == gen_FlashTable[index].ext_id) {
  1541. + pdevinfo->id = gen_FlashTable[index].id;
  1542. + pdevinfo->ext_id = gen_FlashTable[index].ext_id;
  1543. + pdevinfo->blocksize = gen_FlashTable[index].blocksize;
  1544. + pdevinfo->addr_cycle = gen_FlashTable[index].addr_cycle;
  1545. + pdevinfo->iowidth = gen_FlashTable[index].iowidth;
  1546. + pdevinfo->timmingsetting = gen_FlashTable[index].timmingsetting;
  1547. + pdevinfo->advancedmode = gen_FlashTable[index].advancedmode;
  1548. + pdevinfo->pagesize = gen_FlashTable[index].pagesize;
  1549. + pdevinfo->sparesize = gen_FlashTable[index].sparesize;
  1550. + pdevinfo->totalsize = gen_FlashTable[index].totalsize;
  1551. + memcpy(pdevinfo->devciename, gen_FlashTable[index].devciename, sizeof(pdevinfo->devciename));
  1552. + printk(KERN_INFO "Device found in MTK table, ID: %x, EXT_ID: %x\n", id, ext_id);
  1553. +
  1554. + goto find;
  1555. + }
  1556. + }
  1557. +
  1558. +find:
  1559. + if (0 == pdevinfo->id) {
  1560. + printk(KERN_INFO "Device not found, ID: %x\n", id);
  1561. + return false;
  1562. + } else {
  1563. + return true;
  1564. + }
  1565. +}
  1566. +
  1567. +static void
  1568. +ECC_Config(struct mtk_nand_host_hw *hw,u32 ecc_bit)
  1569. +{
  1570. + u32 u4ENCODESize;
  1571. + u32 u4DECODESize;
  1572. + u32 ecc_bit_cfg = ECC_CNFG_ECC4;
  1573. +
  1574. + switch(ecc_bit){
  1575. + case 4:
  1576. + ecc_bit_cfg = ECC_CNFG_ECC4;
  1577. + break;
  1578. + case 8:
  1579. + ecc_bit_cfg = ECC_CNFG_ECC8;
  1580. + break;
  1581. + case 10:
  1582. + ecc_bit_cfg = ECC_CNFG_ECC10;
  1583. + break;
  1584. + case 12:
  1585. + ecc_bit_cfg = ECC_CNFG_ECC12;
  1586. + break;
  1587. + default:
  1588. + break;
  1589. + }
  1590. + DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  1591. + do {
  1592. + } while (!DRV_Reg16(ECC_DECIDLE_REG16));
  1593. +
  1594. + DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  1595. + do {
  1596. + } while (!DRV_Reg32(ECC_ENCIDLE_REG32));
  1597. +
  1598. + /* setup FDM register base */
  1599. + DRV_WriteReg32(ECC_FDMADDR_REG32, NFI_FDM0L_REG32);
  1600. +
  1601. + /* Sector + FDM */
  1602. + u4ENCODESize = (hw->nand_sec_size + 8) << 3;
  1603. + /* Sector + FDM + YAFFS2 meta data bits */
  1604. + u4DECODESize = ((hw->nand_sec_size + 8) << 3) + ecc_bit * 13;
  1605. +
  1606. + /* configure ECC decoder && encoder */
  1607. + DRV_WriteReg32(ECC_DECCNFG_REG32, ecc_bit_cfg | DEC_CNFG_NFI | DEC_CNFG_EMPTY_EN | (u4DECODESize << DEC_CNFG_CODE_SHIFT));
  1608. +
  1609. + DRV_WriteReg32(ECC_ENCCNFG_REG32, ecc_bit_cfg | ENC_CNFG_NFI | (u4ENCODESize << ENC_CNFG_MSG_SHIFT));
  1610. + NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_EL);
  1611. +}
  1612. +
  1613. +static void
  1614. +ECC_Decode_Start(void)
  1615. +{
  1616. + while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE))
  1617. + ;
  1618. + DRV_WriteReg16(ECC_DECCON_REG16, DEC_EN);
  1619. +}
  1620. +
  1621. +static void
  1622. +ECC_Decode_End(void)
  1623. +{
  1624. + while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE))
  1625. + ;
  1626. + DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  1627. +}
  1628. +
  1629. +static void
  1630. +ECC_Encode_Start(void)
  1631. +{
  1632. + while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE))
  1633. + ;
  1634. + mb();
  1635. + DRV_WriteReg16(ECC_ENCCON_REG16, ENC_EN);
  1636. +}
  1637. +
  1638. +static void
  1639. +ECC_Encode_End(void)
  1640. +{
  1641. + /* wait for device returning idle */
  1642. + while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE)) ;
  1643. + mb();
  1644. + DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  1645. +}
  1646. +
  1647. +static bool
  1648. +mtk_nand_check_bch_error(struct mtd_info *mtd, u8 * pDataBuf, u32 u4SecIndex, u32 u4PageAddr)
  1649. +{
  1650. + bool bRet = true;
  1651. + u16 u2SectorDoneMask = 1 << u4SecIndex;
  1652. + u32 u4ErrorNumDebug, i, u4ErrNum;
  1653. + u32 timeout = 0xFFFF;
  1654. + // int el;
  1655. + u32 au4ErrBitLoc[6];
  1656. + u32 u4ErrByteLoc, u4BitOffset;
  1657. + u32 u4ErrBitLoc1th, u4ErrBitLoc2nd;
  1658. +
  1659. + //4 // Wait for Decode Done
  1660. + while (0 == (u2SectorDoneMask & DRV_Reg16(ECC_DECDONE_REG16))) {
  1661. + timeout--;
  1662. + if (0 == timeout)
  1663. + return false;
  1664. + }
  1665. + /* We will manually correct the error bits in the last sector, not all the sectors of the page! */
  1666. + memset(au4ErrBitLoc, 0x0, sizeof(au4ErrBitLoc));
  1667. + u4ErrorNumDebug = DRV_Reg32(ECC_DECENUM_REG32);
  1668. + u4ErrNum = DRV_Reg32(ECC_DECENUM_REG32) >> (u4SecIndex << 2);
  1669. + u4ErrNum &= 0xF;
  1670. +
  1671. + if (u4ErrNum) {
  1672. + if (0xF == u4ErrNum) {
  1673. + mtd->ecc_stats.failed++;
  1674. + bRet = false;
  1675. + printk(KERN_ERR"mtk_nand: UnCorrectable at PageAddr=%d\n", u4PageAddr);
  1676. + } else {
  1677. + for (i = 0; i < ((u4ErrNum + 1) >> 1); ++i) {
  1678. + au4ErrBitLoc[i] = DRV_Reg32(ECC_DECEL0_REG32 + i);
  1679. + u4ErrBitLoc1th = au4ErrBitLoc[i] & 0x1FFF;
  1680. + if (u4ErrBitLoc1th < 0x1000) {
  1681. + u4ErrByteLoc = u4ErrBitLoc1th / 8;
  1682. + u4BitOffset = u4ErrBitLoc1th % 8;
  1683. + pDataBuf[u4ErrByteLoc] = pDataBuf[u4ErrByteLoc] ^ (1 << u4BitOffset);
  1684. + mtd->ecc_stats.corrected++;
  1685. + } else {
  1686. + mtd->ecc_stats.failed++;
  1687. + }
  1688. + u4ErrBitLoc2nd = (au4ErrBitLoc[i] >> 16) & 0x1FFF;
  1689. + if (0 != u4ErrBitLoc2nd) {
  1690. + if (u4ErrBitLoc2nd < 0x1000) {
  1691. + u4ErrByteLoc = u4ErrBitLoc2nd / 8;
  1692. + u4BitOffset = u4ErrBitLoc2nd % 8;
  1693. + pDataBuf[u4ErrByteLoc] = pDataBuf[u4ErrByteLoc] ^ (1 << u4BitOffset);
  1694. + mtd->ecc_stats.corrected++;
  1695. + } else {
  1696. + mtd->ecc_stats.failed++;
  1697. + //printk(KERN_ERR"UnCorrectable High ErrLoc=%d\n", au4ErrBitLoc[i]);
  1698. + }
  1699. + }
  1700. + }
  1701. + }
  1702. + if (0 == (DRV_Reg16(ECC_DECFER_REG16) & (1 << u4SecIndex)))
  1703. + bRet = false;
  1704. + }
  1705. + return bRet;
  1706. +}
  1707. +
  1708. +static bool
  1709. +mtk_nand_RFIFOValidSize(u16 u2Size)
  1710. +{
  1711. + u32 timeout = 0xFFFF;
  1712. + while (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) < u2Size) {
  1713. + timeout--;
  1714. + if (0 == timeout)
  1715. + return false;
  1716. + }
  1717. + return true;
  1718. +}
  1719. +
  1720. +static bool
  1721. +mtk_nand_WFIFOValidSize(u16 u2Size)
  1722. +{
  1723. + u32 timeout = 0xFFFF;
  1724. +
  1725. + while (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) > u2Size) {
  1726. + timeout--;
  1727. + if (0 == timeout)
  1728. + return false;
  1729. + }
  1730. + return true;
  1731. +}
  1732. +
  1733. +static bool
  1734. +mtk_nand_status_ready(u32 u4Status)
  1735. +{
  1736. + u32 timeout = 0xFFFF;
  1737. +
  1738. + while ((DRV_Reg32(NFI_STA_REG32) & u4Status) != 0) {
  1739. + timeout--;
  1740. + if (0 == timeout)
  1741. + return false;
  1742. + }
  1743. + return true;
  1744. +}
  1745. +
  1746. +static bool
  1747. +mtk_nand_reset(void)
  1748. +{
  1749. + int timeout = 0xFFFF;
  1750. + if (DRV_Reg16(NFI_MASTERSTA_REG16)) {
  1751. + mb();
  1752. + DRV_WriteReg16(NFI_CON_REG16, CON_FIFO_FLUSH | CON_NFI_RST);
  1753. + while (DRV_Reg16(NFI_MASTERSTA_REG16)) {
  1754. + timeout--;
  1755. + if (!timeout)
  1756. + MSG(INIT, "Wait for NFI_MASTERSTA timeout\n");
  1757. + }
  1758. + }
  1759. + /* issue reset operation */
  1760. + mb();
  1761. + DRV_WriteReg16(NFI_CON_REG16, CON_FIFO_FLUSH | CON_NFI_RST);
  1762. +
  1763. + return mtk_nand_status_ready(STA_NFI_FSM_MASK | STA_NAND_BUSY) && mtk_nand_RFIFOValidSize(0) && mtk_nand_WFIFOValidSize(0);
  1764. +}
  1765. +
  1766. +static void
  1767. +mtk_nand_set_mode(u16 u2OpMode)
  1768. +{
  1769. + u16 u2Mode = DRV_Reg16(NFI_CNFG_REG16);
  1770. + u2Mode &= ~CNFG_OP_MODE_MASK;
  1771. + u2Mode |= u2OpMode;
  1772. + DRV_WriteReg16(NFI_CNFG_REG16, u2Mode);
  1773. +}
  1774. +
  1775. +static void
  1776. +mtk_nand_set_autoformat(bool bEnable)
  1777. +{
  1778. + if (bEnable)
  1779. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  1780. + else
  1781. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  1782. +}
  1783. +
  1784. +static void
  1785. +mtk_nand_configure_fdm(u16 u2FDMSize)
  1786. +{
  1787. + NFI_CLN_REG16(NFI_PAGEFMT_REG16, PAGEFMT_FDM_MASK | PAGEFMT_FDM_ECC_MASK);
  1788. + NFI_SET_REG16(NFI_PAGEFMT_REG16, u2FDMSize << PAGEFMT_FDM_SHIFT);
  1789. + NFI_SET_REG16(NFI_PAGEFMT_REG16, u2FDMSize << PAGEFMT_FDM_ECC_SHIFT);
  1790. +}
  1791. +
  1792. +static void
  1793. +mtk_nand_configure_lock(void)
  1794. +{
  1795. + u32 u4WriteColNOB = 2;
  1796. + u32 u4WriteRowNOB = 3;
  1797. + u32 u4EraseColNOB = 0;
  1798. + u32 u4EraseRowNOB = 3;
  1799. + DRV_WriteReg16(NFI_LOCKANOB_REG16,
  1800. + (u4WriteColNOB << PROG_CADD_NOB_SHIFT) | (u4WriteRowNOB << PROG_RADD_NOB_SHIFT) | (u4EraseColNOB << ERASE_CADD_NOB_SHIFT) | (u4EraseRowNOB << ERASE_RADD_NOB_SHIFT));
  1801. +
  1802. + if (CHIPVER_ECO_1 == g_u4ChipVer) {
  1803. + int i;
  1804. + for (i = 0; i < 16; ++i) {
  1805. + DRV_WriteReg32(NFI_LOCK00ADD_REG32 + (i << 1), 0xFFFFFFFF);
  1806. + DRV_WriteReg32(NFI_LOCK00FMT_REG32 + (i << 1), 0xFFFFFFFF);
  1807. + }
  1808. + //DRV_WriteReg16(NFI_LOCKANOB_REG16, 0x0);
  1809. + DRV_WriteReg32(NFI_LOCKCON_REG32, 0xFFFFFFFF);
  1810. + DRV_WriteReg16(NFI_LOCK_REG16, NFI_LOCK_ON);
  1811. + }
  1812. +}
  1813. +
  1814. +static bool
  1815. +mtk_nand_pio_ready(void)
  1816. +{
  1817. + int count = 0;
  1818. + while (!(DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1)) {
  1819. + count++;
  1820. + if (count > 0xffff) {
  1821. + printk("PIO_DIRDY timeout\n");
  1822. + return false;
  1823. + }
  1824. + }
  1825. +
  1826. + return true;
  1827. +}
  1828. +
  1829. +static bool
  1830. +mtk_nand_set_command(u16 command)
  1831. +{
  1832. + mb();
  1833. + DRV_WriteReg16(NFI_CMD_REG16, command);
  1834. + return mtk_nand_status_ready(STA_CMD_STATE);
  1835. +}
  1836. +
  1837. +static bool
  1838. +mtk_nand_set_address(u32 u4ColAddr, u32 u4RowAddr, u16 u2ColNOB, u16 u2RowNOB)
  1839. +{
  1840. + mb();
  1841. + DRV_WriteReg32(NFI_COLADDR_REG32, u4ColAddr);
  1842. + DRV_WriteReg32(NFI_ROWADDR_REG32, u4RowAddr);
  1843. + DRV_WriteReg16(NFI_ADDRNOB_REG16, u2ColNOB | (u2RowNOB << ADDR_ROW_NOB_SHIFT));
  1844. + return mtk_nand_status_ready(STA_ADDR_STATE);
  1845. +}
  1846. +
  1847. +static void mtk_nfc_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
  1848. +{
  1849. + if (ctrl & NAND_ALE) {
  1850. + mtk_nand_set_address(dat, 0, 1, 0);
  1851. + } else if (ctrl & NAND_CLE) {
  1852. + mtk_nand_reset();
  1853. + mtk_nand_set_mode(0x6000);
  1854. + mtk_nand_set_command(dat);
  1855. + }
  1856. +}
  1857. +
  1858. +static bool
  1859. +mtk_nand_check_RW_count(u16 u2WriteSize)
  1860. +{
  1861. + u32 timeout = 0xFFFF;
  1862. + u16 u2SecNum = u2WriteSize >> 9;
  1863. +
  1864. + while (ADDRCNTR_CNTR(DRV_Reg16(NFI_ADDRCNTR_REG16)) < u2SecNum) {
  1865. + timeout--;
  1866. + if (0 == timeout) {
  1867. + printk(KERN_INFO "[%s] timeout\n", __FUNCTION__);
  1868. + return false;
  1869. + }
  1870. + }
  1871. + return true;
  1872. +}
  1873. +
  1874. +static bool
  1875. +mtk_nand_ready_for_read(struct nand_chip *nand, u32 u4RowAddr, u32 u4ColAddr, bool full, u8 * buf)
  1876. +{
  1877. + /* Reset NFI HW internal state machine and flush NFI in/out FIFO */
  1878. + bool bRet = false;
  1879. + u16 sec_num = 1 << (nand->page_shift - 9);
  1880. + u32 col_addr = u4ColAddr;
  1881. + u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  1882. + if (nand->options & NAND_BUSWIDTH_16)
  1883. + col_addr /= 2;
  1884. +
  1885. + if (!mtk_nand_reset())
  1886. + goto cleanup;
  1887. + if (g_bHwEcc) {
  1888. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1889. + } else {
  1890. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1891. + }
  1892. +
  1893. + mtk_nand_set_mode(CNFG_OP_READ);
  1894. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  1895. + DRV_WriteReg16(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  1896. +
  1897. + if (full) {
  1898. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1899. +
  1900. + if (g_bHwEcc)
  1901. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1902. + else
  1903. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1904. + } else {
  1905. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1906. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1907. + }
  1908. +
  1909. + mtk_nand_set_autoformat(full);
  1910. + if (full)
  1911. + if (g_bHwEcc)
  1912. + ECC_Decode_Start();
  1913. + if (!mtk_nand_set_command(NAND_CMD_READ0))
  1914. + goto cleanup;
  1915. + if (!mtk_nand_set_address(col_addr, u4RowAddr, colnob, rownob))
  1916. + goto cleanup;
  1917. + if (!mtk_nand_set_command(NAND_CMD_READSTART))
  1918. + goto cleanup;
  1919. + if (!mtk_nand_status_ready(STA_NAND_BUSY))
  1920. + goto cleanup;
  1921. +
  1922. + bRet = true;
  1923. +
  1924. +cleanup:
  1925. + return bRet;
  1926. +}
  1927. +
  1928. +static bool
  1929. +mtk_nand_ready_for_write(struct nand_chip *nand, u32 u4RowAddr, u32 col_addr, bool full, u8 * buf)
  1930. +{
  1931. + bool bRet = false;
  1932. + u32 sec_num = 1 << (nand->page_shift - 9);
  1933. + u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  1934. + if (nand->options & NAND_BUSWIDTH_16)
  1935. + col_addr /= 2;
  1936. +
  1937. + /* Reset NFI HW internal state machine and flush NFI in/out FIFO */
  1938. + if (!mtk_nand_reset())
  1939. + return false;
  1940. +
  1941. + mtk_nand_set_mode(CNFG_OP_PRGM);
  1942. +
  1943. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  1944. +
  1945. + DRV_WriteReg16(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  1946. +
  1947. + if (full) {
  1948. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1949. + if (g_bHwEcc)
  1950. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1951. + else
  1952. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1953. + } else {
  1954. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1955. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1956. + }
  1957. +
  1958. + mtk_nand_set_autoformat(full);
  1959. +
  1960. + if (full)
  1961. + if (g_bHwEcc)
  1962. + ECC_Encode_Start();
  1963. +
  1964. + if (!mtk_nand_set_command(NAND_CMD_SEQIN))
  1965. + goto cleanup;
  1966. + //1 FIXED ME: For Any Kind of AddrCycle
  1967. + if (!mtk_nand_set_address(col_addr, u4RowAddr, colnob, rownob))
  1968. + goto cleanup;
  1969. +
  1970. + if (!mtk_nand_status_ready(STA_NAND_BUSY))
  1971. + goto cleanup;
  1972. +
  1973. + bRet = true;
  1974. +
  1975. +cleanup:
  1976. + return bRet;
  1977. +}
  1978. +
  1979. +static bool
  1980. +mtk_nand_check_dececc_done(u32 u4SecNum)
  1981. +{
  1982. + u32 timeout, dec_mask;
  1983. +
  1984. + timeout = 0xffff;
  1985. + dec_mask = (1 << u4SecNum) - 1;
  1986. + while ((dec_mask != DRV_Reg(ECC_DECDONE_REG16)) && timeout > 0)
  1987. + timeout--;
  1988. + if (timeout == 0) {
  1989. + MSG(VERIFY, "ECC_DECDONE: timeout\n");
  1990. + return false;
  1991. + }
  1992. + return true;
  1993. +}
  1994. +
  1995. +static bool
  1996. +mtk_nand_mcu_read_data(u8 * buf, u32 length)
  1997. +{
  1998. + int timeout = 0xffff;
  1999. + u32 i;
  2000. + u32 *buf32 = (u32 *) buf;
  2001. + if ((u32) buf % 4 || length % 4)
  2002. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2003. + else
  2004. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2005. +
  2006. + //DRV_WriteReg32(NFI_STRADDR_REG32, 0);
  2007. + mb();
  2008. + NFI_SET_REG16(NFI_CON_REG16, CON_NFI_BRD);
  2009. +
  2010. + if ((u32) buf % 4 || length % 4) {
  2011. + for (i = 0; (i < (length)) && (timeout > 0);) {
  2012. + if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2013. + *buf++ = (u8) DRV_Reg32(NFI_DATAR_REG32);
  2014. + i++;
  2015. + } else {
  2016. + timeout--;
  2017. + }
  2018. + if (0 == timeout) {
  2019. + printk(KERN_ERR "[%s] timeout\n", __FUNCTION__);
  2020. + dump_nfi();
  2021. + return false;
  2022. + }
  2023. + }
  2024. + } else {
  2025. + for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  2026. + if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2027. + *buf32++ = DRV_Reg32(NFI_DATAR_REG32);
  2028. + i++;
  2029. + } else {
  2030. + timeout--;
  2031. + }
  2032. + if (0 == timeout) {
  2033. + printk(KERN_ERR "[%s] timeout\n", __FUNCTION__);
  2034. + dump_nfi();
  2035. + return false;
  2036. + }
  2037. + }
  2038. + }
  2039. + return true;
  2040. +}
  2041. +
  2042. +static bool
  2043. +mtk_nand_read_page_data(struct mtd_info *mtd, u8 * pDataBuf, u32 u4Size)
  2044. +{
  2045. + return mtk_nand_mcu_read_data(pDataBuf, u4Size);
  2046. +}
  2047. +
  2048. +static bool
  2049. +mtk_nand_mcu_write_data(struct mtd_info *mtd, const u8 * buf, u32 length)
  2050. +{
  2051. + u32 timeout = 0xFFFF;
  2052. + u32 i;
  2053. + u32 *pBuf32;
  2054. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2055. + mb();
  2056. + NFI_SET_REG16(NFI_CON_REG16, CON_NFI_BWR);
  2057. + pBuf32 = (u32 *) buf;
  2058. +
  2059. + if ((u32) buf % 4 || length % 4)
  2060. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2061. + else
  2062. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2063. +
  2064. + if ((u32) buf % 4 || length % 4) {
  2065. + for (i = 0; (i < (length)) && (timeout > 0);) {
  2066. + if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2067. + DRV_WriteReg32(NFI_DATAW_REG32, *buf++);
  2068. + i++;
  2069. + } else {
  2070. + timeout--;
  2071. + }
  2072. + if (0 == timeout) {
  2073. + printk(KERN_ERR "[%s] timeout\n", __FUNCTION__);
  2074. + dump_nfi();
  2075. + return false;
  2076. + }
  2077. + }
  2078. + } else {
  2079. + for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  2080. + if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2081. + DRV_WriteReg32(NFI_DATAW_REG32, *pBuf32++);
  2082. + i++;
  2083. + } else {
  2084. + timeout--;
  2085. + }
  2086. + if (0 == timeout) {
  2087. + printk(KERN_ERR "[%s] timeout\n", __FUNCTION__);
  2088. + dump_nfi();
  2089. + return false;
  2090. + }
  2091. + }
  2092. + }
  2093. +
  2094. + return true;
  2095. +}
  2096. +
  2097. +static bool
  2098. +mtk_nand_write_page_data(struct mtd_info *mtd, u8 * buf, u32 size)
  2099. +{
  2100. + return mtk_nand_mcu_write_data(mtd, buf, size);
  2101. +}
  2102. +
  2103. +static void
  2104. +mtk_nand_read_fdm_data(u8 * pDataBuf, u32 u4SecNum)
  2105. +{
  2106. + u32 i;
  2107. + u32 *pBuf32 = (u32 *) pDataBuf;
  2108. +
  2109. + if (pBuf32) {
  2110. + for (i = 0; i < u4SecNum; ++i) {
  2111. + *pBuf32++ = DRV_Reg32(NFI_FDM0L_REG32 + (i << 1));
  2112. + *pBuf32++ = DRV_Reg32(NFI_FDM0M_REG32 + (i << 1));
  2113. + }
  2114. + }
  2115. +}
  2116. +
  2117. +static u8 fdm_buf[64];
  2118. +static void
  2119. +mtk_nand_write_fdm_data(struct nand_chip *chip, u8 * pDataBuf, u32 u4SecNum)
  2120. +{
  2121. + u32 i, j;
  2122. + u8 checksum = 0;
  2123. + bool empty = true;
  2124. + struct nand_oobfree *free_entry;
  2125. + u32 *pBuf32;
  2126. +
  2127. + memcpy(fdm_buf, pDataBuf, u4SecNum * 8);
  2128. +
  2129. + free_entry = layout->oobfree;
  2130. + for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free_entry[i].length; i++) {
  2131. + for (j = 0; j < free_entry[i].length; j++) {
  2132. + if (pDataBuf[free_entry[i].offset + j] != 0xFF)
  2133. + empty = false;
  2134. + checksum ^= pDataBuf[free_entry[i].offset + j];
  2135. + }
  2136. + }
  2137. +
  2138. + if (!empty) {
  2139. + fdm_buf[free_entry[i - 1].offset + free_entry[i - 1].length] = checksum;
  2140. + }
  2141. +
  2142. + pBuf32 = (u32 *) fdm_buf;
  2143. + for (i = 0; i < u4SecNum; ++i) {
  2144. + DRV_WriteReg32(NFI_FDM0L_REG32 + (i << 1), *pBuf32++);
  2145. + DRV_WriteReg32(NFI_FDM0M_REG32 + (i << 1), *pBuf32++);
  2146. + }
  2147. +}
  2148. +
  2149. +static void
  2150. +mtk_nand_stop_read(void)
  2151. +{
  2152. + NFI_CLN_REG16(NFI_CON_REG16, CON_NFI_BRD);
  2153. + mtk_nand_reset();
  2154. + if (g_bHwEcc)
  2155. + ECC_Decode_End();
  2156. + DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2157. +}
  2158. +
  2159. +static void
  2160. +mtk_nand_stop_write(void)
  2161. +{
  2162. + NFI_CLN_REG16(NFI_CON_REG16, CON_NFI_BWR);
  2163. + if (g_bHwEcc)
  2164. + ECC_Encode_End();
  2165. + DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2166. +}
  2167. +
  2168. +bool
  2169. +mtk_nand_exec_read_page(struct mtd_info *mtd, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2170. +{
  2171. + u8 *buf;
  2172. + bool bRet = true;
  2173. + struct nand_chip *nand = mtd->priv;
  2174. + u32 u4SecNum = u4PageSize >> 9;
  2175. +
  2176. + buf = pPageBuf;
  2177. + if (mtk_nand_ready_for_read(nand, u4RowAddr, 0, true, buf)) {
  2178. + int j;
  2179. + for (j = 0 ; j < u4SecNum; j++) {
  2180. + if (!mtk_nand_read_page_data(mtd, buf+j*512, 512))
  2181. + bRet = false;
  2182. + if(g_bHwEcc && !mtk_nand_check_dececc_done(j+1))
  2183. + bRet = false;
  2184. + if(g_bHwEcc && !mtk_nand_check_bch_error(mtd, buf+j*512, j, u4RowAddr))
  2185. + bRet = false;
  2186. + }
  2187. + if (!mtk_nand_status_ready(STA_NAND_BUSY))
  2188. + bRet = false;
  2189. +
  2190. + mtk_nand_read_fdm_data(pFDMBuf, u4SecNum);
  2191. + mtk_nand_stop_read();
  2192. + }
  2193. +
  2194. + return bRet;
  2195. +}
  2196. +
  2197. +int
  2198. +mtk_nand_exec_write_page(struct mtd_info *mtd, u32 u4RowAddr, u32 u4PageSize, u8 * pPageBuf, u8 * pFDMBuf)
  2199. +{
  2200. + struct nand_chip *chip = mtd->priv;
  2201. + u32 u4SecNum = u4PageSize >> 9;
  2202. + u8 *buf;
  2203. + u8 status;
  2204. +
  2205. + MSG(WRITE, "mtk_nand_exec_write_page, page: 0x%x\n", u4RowAddr);
  2206. +
  2207. + buf = pPageBuf;
  2208. +
  2209. + if (mtk_nand_ready_for_write(chip, u4RowAddr, 0, true, buf)) {
  2210. + mtk_nand_write_fdm_data(chip, pFDMBuf, u4SecNum);
  2211. + (void)mtk_nand_write_page_data(mtd, buf, u4PageSize);
  2212. + (void)mtk_nand_check_RW_count(u4PageSize);
  2213. + mtk_nand_stop_write();
  2214. + (void)mtk_nand_set_command(NAND_CMD_PAGEPROG);
  2215. + while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY) ;
  2216. + }
  2217. +
  2218. + status = chip->waitfunc(mtd, chip);
  2219. + if (status & NAND_STATUS_FAIL)
  2220. + return -EIO;
  2221. + return 0;
  2222. +}
  2223. +
  2224. +static int
  2225. +get_start_end_block(struct mtd_info *mtd, int block, int *start_blk, int *end_blk)
  2226. +{
  2227. + struct nand_chip *chip = mtd->priv;
  2228. + int i;
  2229. +
  2230. + *start_blk = 0;
  2231. + for (i = 0; i <= part_num; i++)
  2232. + {
  2233. + if (i == part_num)
  2234. + {
  2235. + // try the last reset partition
  2236. + *end_blk = (chip->chipsize >> chip->phys_erase_shift) - 1;
  2237. + if (*start_blk <= *end_blk)
  2238. + {
  2239. + if ((block >= *start_blk) && (block <= *end_blk))
  2240. + break;
  2241. + }
  2242. + }
  2243. + // skip All partition entry
  2244. + else if (g_pasStatic_Partition[i].size == MTDPART_SIZ_FULL)
  2245. + {
  2246. + continue;
  2247. + }
  2248. + *end_blk = *start_blk + (g_pasStatic_Partition[i].size >> chip->phys_erase_shift) - 1;
  2249. + if ((block >= *start_blk) && (block <= *end_blk))
  2250. + break;
  2251. + *start_blk = *end_blk + 1;
  2252. + }
  2253. + if (*start_blk > *end_blk)
  2254. + {
  2255. + return -1;
  2256. + }
  2257. + return 0;
  2258. +}
  2259. +
  2260. +static int
  2261. +block_remap(struct mtd_info *mtd, int block)
  2262. +{
  2263. + struct nand_chip *chip = mtd->priv;
  2264. + int start_blk, end_blk;
  2265. + int j, block_offset;
  2266. + int bad_block = 0;
  2267. +
  2268. + if (chip->bbt == NULL) {
  2269. + printk("ERROR!! no bbt table for block_remap\n");
  2270. + return -1;
  2271. + }
  2272. +
  2273. + if (get_start_end_block(mtd, block, &start_blk, &end_blk) < 0) {
  2274. + printk("ERROR!! can not find start_blk and end_blk\n");
  2275. + return -1;
  2276. + }
  2277. +
  2278. + block_offset = block - start_blk;
  2279. + for (j = start_blk; j <= end_blk;j++) {
  2280. + if (((chip->bbt[j >> 2] >> ((j<<1) & 0x6)) & 0x3) == 0x0) {
  2281. + if (!block_offset)
  2282. + break;
  2283. + block_offset--;
  2284. + } else {
  2285. + bad_block++;
  2286. + }
  2287. + }
  2288. + if (j <= end_blk) {
  2289. + return j;
  2290. + } else {
  2291. + // remap to the bad block
  2292. + for (j = end_blk; bad_block > 0; j--)
  2293. + {
  2294. + if (((chip->bbt[j >> 2] >> ((j<<1) & 0x6)) & 0x3) != 0x0)
  2295. + {
  2296. + bad_block--;
  2297. + if (bad_block <= block_offset)
  2298. + return j;
  2299. + }
  2300. + }
  2301. + }
  2302. +
  2303. + printk("Error!! block_remap error\n");
  2304. + return -1;
  2305. +}
  2306. +
  2307. +int
  2308. +check_block_remap(struct mtd_info *mtd, int block)
  2309. +{
  2310. + if (shift_on_bbt)
  2311. + return block_remap(mtd, block);
  2312. + else
  2313. + return block;
  2314. +}
  2315. +EXPORT_SYMBOL(check_block_remap);
  2316. +
  2317. +
  2318. +static int
  2319. +write_next_on_fail(struct mtd_info *mtd, char *write_buf, int page, int * to_blk)
  2320. +{
  2321. + struct nand_chip *chip = mtd->priv;
  2322. + int i, j, to_page = 0, first_page;
  2323. + char *buf, *oob;
  2324. + int start_blk = 0, end_blk;
  2325. + int mapped_block;
  2326. + int page_per_block_bit = chip->phys_erase_shift - chip->page_shift;
  2327. + int block = page >> page_per_block_bit;
  2328. +
  2329. + // find next available block in the same MTD partition
  2330. + mapped_block = block_remap(mtd, block);
  2331. + if (mapped_block == -1)
  2332. + return NAND_STATUS_FAIL;
  2333. +
  2334. + get_start_end_block(mtd, block, &start_blk, &end_blk);
  2335. +
  2336. + buf = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL | GFP_DMA);
  2337. + if (buf == NULL)
  2338. + return -1;
  2339. +
  2340. + oob = buf + mtd->writesize;
  2341. + for ((*to_blk) = block + 1; (*to_blk) <= end_blk ; (*to_blk)++) {
  2342. + if (nand_bbt_get(mtd, (*to_blk) << page_per_block_bit) == 0) {
  2343. + int status;
  2344. + status = mtk_nand_erase_hw(mtd, (*to_blk) << page_per_block_bit);
  2345. + if (status & NAND_STATUS_FAIL) {
  2346. + mtk_nand_block_markbad_hw(mtd, (*to_blk) << chip->phys_erase_shift);
  2347. + nand_bbt_set(mtd, (*to_blk) << page_per_block_bit, 0x3);
  2348. + } else {
  2349. + /* good block */
  2350. + to_page = (*to_blk) << page_per_block_bit;
  2351. + break;
  2352. + }
  2353. + }
  2354. + }
  2355. +
  2356. + if (!to_page) {
  2357. + kfree(buf);
  2358. + return -1;
  2359. + }
  2360. +
  2361. + first_page = (page >> page_per_block_bit) << page_per_block_bit;
  2362. + for (i = 0; i < (1 << page_per_block_bit); i++) {
  2363. + if ((first_page + i) != page) {
  2364. + mtk_nand_read_oob_hw(mtd, chip, (first_page+i));
  2365. + for (j = 0; j < mtd->oobsize; j++)
  2366. + if (chip->oob_poi[j] != (unsigned char)0xff)
  2367. + break;
  2368. + if (j < mtd->oobsize) {
  2369. + mtk_nand_exec_read_page(mtd, (first_page+i), mtd->writesize, buf, oob);
  2370. + memset(oob, 0xff, mtd->oobsize);
  2371. + if (mtk_nand_exec_write_page(mtd, to_page + i, mtd->writesize, (u8 *)buf, oob) != 0) {
  2372. + int ret, new_blk = 0;
  2373. + nand_bbt_set(mtd, to_page, 0x3);
  2374. + ret = write_next_on_fail(mtd, buf, to_page + i, &new_blk);
  2375. + if (ret) {
  2376. + kfree(buf);
  2377. + mtk_nand_block_markbad_hw(mtd, to_page << chip->page_shift);
  2378. + return ret;
  2379. + }
  2380. + mtk_nand_block_markbad_hw(mtd, to_page << chip->page_shift);
  2381. + *to_blk = new_blk;
  2382. + to_page = ((*to_blk) << page_per_block_bit);
  2383. + }
  2384. + }
  2385. + } else {
  2386. + memset(chip->oob_poi, 0xff, mtd->oobsize);
  2387. + if (mtk_nand_exec_write_page(mtd, to_page + i, mtd->writesize, (u8 *)write_buf, chip->oob_poi) != 0) {
  2388. + int ret, new_blk = 0;
  2389. + nand_bbt_set(mtd, to_page, 0x3);
  2390. + ret = write_next_on_fail(mtd, write_buf, to_page + i, &new_blk);
  2391. + if (ret) {
  2392. + kfree(buf);
  2393. + mtk_nand_block_markbad_hw(mtd, to_page << chip->page_shift);
  2394. + return ret;
  2395. + }
  2396. + mtk_nand_block_markbad_hw(mtd, to_page << chip->page_shift);
  2397. + *to_blk = new_blk;
  2398. + to_page = ((*to_blk) << page_per_block_bit);
  2399. + }
  2400. + }
  2401. + }
  2402. +
  2403. + kfree(buf);
  2404. +
  2405. + return 0;
  2406. +}
  2407. +
  2408. +static int
  2409. +mtk_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, uint32_t offset,
  2410. + int data_len, const u8 * buf, int oob_required, int page, int raw)
  2411. +{
  2412. + int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2413. + int block = page / page_per_block;
  2414. + u16 page_in_block = page % page_per_block;
  2415. + int mapped_block = block;
  2416. +
  2417. +#if defined(MTK_NAND_BMT)
  2418. + mapped_block = get_mapping_block_index(block);
  2419. + // write bad index into oob
  2420. + if (mapped_block != block)
  2421. + set_bad_index_to_oob(chip->oob_poi, block);
  2422. + else
  2423. + set_bad_index_to_oob(chip->oob_poi, FAKE_INDEX);
  2424. +#else
  2425. + if (shift_on_bbt) {
  2426. + mapped_block = block_remap(mtd, block);
  2427. + if (mapped_block == -1)
  2428. + return NAND_STATUS_FAIL;
  2429. + if (nand_bbt_get(mtd, mapped_block << (chip->phys_erase_shift - chip->page_shift)) != 0x0)
  2430. + return NAND_STATUS_FAIL;
  2431. + }
  2432. +#endif
  2433. + do {
  2434. + if (mtk_nand_exec_write_page(mtd, page_in_block + mapped_block * page_per_block, mtd->writesize, (u8 *)buf, chip->oob_poi)) {
  2435. + MSG(INIT, "write fail at block: 0x%x, page: 0x%x\n", mapped_block, page_in_block);
  2436. +#if defined(MTK_NAND_BMT)
  2437. + if (update_bmt((page_in_block + mapped_block * page_per_block) << chip->page_shift, UPDATE_WRITE_FAIL, (u8 *) buf, chip->oob_poi)) {
  2438. + MSG(INIT, "Update BMT success\n");
  2439. + return 0;
  2440. + } else {
  2441. + MSG(INIT, "Update BMT fail\n");
  2442. + return -EIO;
  2443. + }
  2444. +#else
  2445. + {
  2446. + int new_blk;
  2447. + nand_bbt_set(mtd, page_in_block + mapped_block * page_per_block, 0x3);
  2448. + if (write_next_on_fail(mtd, (char *)buf, page_in_block + mapped_block * page_per_block, &new_blk) != 0)
  2449. + {
  2450. + mtk_nand_block_markbad_hw(mtd, (page_in_block + mapped_block * page_per_block) << chip->page_shift);
  2451. + return NAND_STATUS_FAIL;
  2452. + }
  2453. + mtk_nand_block_markbad_hw(mtd, (page_in_block + mapped_block * page_per_block) << chip->page_shift);
  2454. + break;
  2455. + }
  2456. +#endif
  2457. + } else
  2458. + break;
  2459. + } while(1);
  2460. +
  2461. + return 0;
  2462. +}
  2463. +
  2464. +static void
  2465. +mtk_nand_command_bp(struct mtd_info *mtd, unsigned int command, int column, int page_addr)
  2466. +{
  2467. + struct nand_chip *nand = mtd->priv;
  2468. +
  2469. + switch (command) {
  2470. + case NAND_CMD_SEQIN:
  2471. + memset(g_kCMD.au1OOB, 0xFF, sizeof(g_kCMD.au1OOB));
  2472. + g_kCMD.pDataBuf = NULL;
  2473. + g_kCMD.u4RowAddr = page_addr;
  2474. + g_kCMD.u4ColAddr = column;
  2475. + break;
  2476. +
  2477. + case NAND_CMD_PAGEPROG:
  2478. + if (g_kCMD.pDataBuf || (0xFF != g_kCMD.au1OOB[nand_badblock_offset])) {
  2479. + u8 *pDataBuf = g_kCMD.pDataBuf ? g_kCMD.pDataBuf : nand->buffers->databuf;
  2480. + mtk_nand_exec_write_page(mtd, g_kCMD.u4RowAddr, mtd->writesize, pDataBuf, g_kCMD.au1OOB);
  2481. + g_kCMD.u4RowAddr = (u32) - 1;
  2482. + g_kCMD.u4OOBRowAddr = (u32) - 1;
  2483. + }
  2484. + break;
  2485. +
  2486. + case NAND_CMD_READOOB:
  2487. + g_kCMD.u4RowAddr = page_addr;
  2488. + g_kCMD.u4ColAddr = column + mtd->writesize;
  2489. + break;
  2490. +
  2491. + case NAND_CMD_READ0:
  2492. + g_kCMD.u4RowAddr = page_addr;
  2493. + g_kCMD.u4ColAddr = column;
  2494. + break;
  2495. +
  2496. + case NAND_CMD_ERASE1:
  2497. + nand->state=FL_ERASING;
  2498. + (void)mtk_nand_reset();
  2499. + mtk_nand_set_mode(CNFG_OP_ERASE);
  2500. + (void)mtk_nand_set_command(NAND_CMD_ERASE1);
  2501. + (void)mtk_nand_set_address(0, page_addr, 0, devinfo.addr_cycle - 2);
  2502. + break;
  2503. +
  2504. + case NAND_CMD_ERASE2:
  2505. + (void)mtk_nand_set_command(NAND_CMD_ERASE2);
  2506. + while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY)
  2507. + ;
  2508. + break;
  2509. +
  2510. + case NAND_CMD_STATUS:
  2511. + (void)mtk_nand_reset();
  2512. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2513. + mtk_nand_set_mode(CNFG_OP_SRD);
  2514. + mtk_nand_set_mode(CNFG_READ_EN);
  2515. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  2516. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2517. + (void)mtk_nand_set_command(NAND_CMD_STATUS);
  2518. + NFI_CLN_REG16(NFI_CON_REG16, CON_NFI_NOB_MASK);
  2519. + mb();
  2520. + DRV_WriteReg16(NFI_CON_REG16, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  2521. + g_bcmdstatus = true;
  2522. + break;
  2523. +
  2524. + case NAND_CMD_RESET:
  2525. + (void)mtk_nand_reset();
  2526. + DRV_WriteReg16(NFI_INTR_EN_REG16, INTR_RST_DONE_EN);
  2527. + (void)mtk_nand_set_command(NAND_CMD_RESET);
  2528. + DRV_WriteReg16(NFI_BASE+0x44, 0xF1);
  2529. + while(!(DRV_Reg16(NFI_INTR_REG16)&INTR_RST_DONE_EN))
  2530. + ;
  2531. + break;
  2532. +
  2533. + case NAND_CMD_READID:
  2534. + mtk_nand_reset();
  2535. + /* Disable HW ECC */
  2536. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2537. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  2538. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN | CNFG_BYTE_RW);
  2539. + (void)mtk_nand_reset();
  2540. + mb();
  2541. + mtk_nand_set_mode(CNFG_OP_SRD);
  2542. + (void)mtk_nand_set_command(NAND_CMD_READID);
  2543. + (void)mtk_nand_set_address(0, 0, 1, 0);
  2544. + DRV_WriteReg16(NFI_CON_REG16, CON_NFI_SRD);
  2545. + while (DRV_Reg32(NFI_STA_REG32) & STA_DATAR_STATE)
  2546. + ;
  2547. + break;
  2548. +
  2549. + default:
  2550. + BUG();
  2551. + break;
  2552. + }
  2553. +}
  2554. +
  2555. +static void
  2556. +mtk_nand_select_chip(struct mtd_info *mtd, int chip)
  2557. +{
  2558. + if ((chip == -1) && (false == g_bInitDone)) {
  2559. + struct nand_chip *nand = mtd->priv;
  2560. + struct mtk_nand_host *host = nand->priv;
  2561. + struct mtk_nand_host_hw *hw = host->hw;
  2562. + u32 spare_per_sector = mtd->oobsize / (mtd->writesize / 512);
  2563. + u32 ecc_bit = 4;
  2564. + u32 spare_bit = PAGEFMT_SPARE_16;
  2565. +
  2566. + if (spare_per_sector >= 28) {
  2567. + spare_bit = PAGEFMT_SPARE_28;
  2568. + ecc_bit = 12;
  2569. + spare_per_sector = 28;
  2570. + } else if (spare_per_sector >= 27) {
  2571. + spare_bit = PAGEFMT_SPARE_27;
  2572. + ecc_bit = 8;
  2573. + spare_per_sector = 27;
  2574. + } else if (spare_per_sector >= 26) {
  2575. + spare_bit = PAGEFMT_SPARE_26;
  2576. + ecc_bit = 8;
  2577. + spare_per_sector = 26;
  2578. + } else if (spare_per_sector >= 16) {
  2579. + spare_bit = PAGEFMT_SPARE_16;
  2580. + ecc_bit = 4;
  2581. + spare_per_sector = 16;
  2582. + } else {
  2583. + MSG(INIT, "[NAND]: NFI not support oobsize: %x\n", spare_per_sector);
  2584. + ASSERT(0);
  2585. + }
  2586. + mtd->oobsize = spare_per_sector*(mtd->writesize/512);
  2587. + MSG(INIT, "[NAND]select ecc bit:%d, sparesize :%d spare_per_sector=%d\n",ecc_bit,mtd->oobsize,spare_per_sector);
  2588. + /* Setup PageFormat */
  2589. + if (4096 == mtd->writesize) {
  2590. + NFI_SET_REG16(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K);
  2591. + nand->cmdfunc = mtk_nand_command_bp;
  2592. + } else if (2048 == mtd->writesize) {
  2593. + NFI_SET_REG16(NFI_PAGEFMT_REG16, (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K);
  2594. + nand->cmdfunc = mtk_nand_command_bp;
  2595. + }
  2596. + ECC_Config(hw,ecc_bit);
  2597. + g_bInitDone = true;
  2598. + }
  2599. + switch (chip) {
  2600. + case -1:
  2601. + break;
  2602. + case 0:
  2603. + case 1:
  2604. + /* Jun Shen, 2011.04.13 */
  2605. + /* Note: MT6577 EVB NAND is mounted on CS0, but FPGA is CS1 */
  2606. + DRV_WriteReg16(NFI_CSEL_REG16, chip);
  2607. + /* Jun Shen, 2011.04.13 */
  2608. + break;
  2609. + }
  2610. +}
  2611. +
  2612. +static uint8_t
  2613. +mtk_nand_read_byte(struct mtd_info *mtd)
  2614. +{
  2615. + uint8_t retval = 0;
  2616. +
  2617. + if (!mtk_nand_pio_ready()) {
  2618. + printk("pio ready timeout\n");
  2619. + retval = false;
  2620. + }
  2621. +
  2622. + if (g_bcmdstatus) {
  2623. + retval = DRV_Reg8(NFI_DATAR_REG32);
  2624. + NFI_CLN_REG16(NFI_CON_REG16, CON_NFI_NOB_MASK);
  2625. + mtk_nand_reset();
  2626. + if (g_bHwEcc) {
  2627. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2628. + } else {
  2629. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2630. + }
  2631. + g_bcmdstatus = false;
  2632. + } else
  2633. + retval = DRV_Reg8(NFI_DATAR_REG32);
  2634. +
  2635. + return retval;
  2636. +}
  2637. +
  2638. +static void
  2639. +mtk_nand_read_buf(struct mtd_info *mtd, uint8_t * buf, int len)
  2640. +{
  2641. + struct nand_chip *nand = (struct nand_chip *)mtd->priv;
  2642. + struct NAND_CMD *pkCMD = &g_kCMD;
  2643. + u32 u4ColAddr = pkCMD->u4ColAddr;
  2644. + u32 u4PageSize = mtd->writesize;
  2645. +
  2646. + if (u4ColAddr < u4PageSize) {
  2647. + if ((u4ColAddr == 0) && (len >= u4PageSize)) {
  2648. + mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, buf, pkCMD->au1OOB);
  2649. + if (len > u4PageSize) {
  2650. + u32 u4Size = min(len - u4PageSize, sizeof(pkCMD->au1OOB));
  2651. + memcpy(buf + u4PageSize, pkCMD->au1OOB, u4Size);
  2652. + }
  2653. + } else {
  2654. + mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, nand->buffers->databuf, pkCMD->au1OOB);
  2655. + memcpy(buf, nand->buffers->databuf + u4ColAddr, len);
  2656. + }
  2657. + pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  2658. + } else {
  2659. + u32 u4Offset = u4ColAddr - u4PageSize;
  2660. + u32 u4Size = min(len - u4Offset, sizeof(pkCMD->au1OOB));
  2661. + if (pkCMD->u4OOBRowAddr != pkCMD->u4RowAddr) {
  2662. + mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, nand->buffers->databuf, pkCMD->au1OOB);
  2663. + pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  2664. + }
  2665. + memcpy(buf, pkCMD->au1OOB + u4Offset, u4Size);
  2666. + }
  2667. + pkCMD->u4ColAddr += len;
  2668. +}
  2669. +
  2670. +static void
  2671. +mtk_nand_write_buf(struct mtd_info *mtd, const uint8_t * buf, int len)
  2672. +{
  2673. + struct NAND_CMD *pkCMD = &g_kCMD;
  2674. + u32 u4ColAddr = pkCMD->u4ColAddr;
  2675. + u32 u4PageSize = mtd->writesize;
  2676. + int i4Size, i;
  2677. +
  2678. + if (u4ColAddr >= u4PageSize) {
  2679. + u32 u4Offset = u4ColAddr - u4PageSize;
  2680. + u8 *pOOB = pkCMD->au1OOB + u4Offset;
  2681. + i4Size = min(len, (int)(sizeof(pkCMD->au1OOB) - u4Offset));
  2682. + for (i = 0; i < i4Size; i++) {
  2683. + pOOB[i] &= buf[i];
  2684. + }
  2685. + } else {
  2686. + pkCMD->pDataBuf = (u8 *) buf;
  2687. + }
  2688. +
  2689. + pkCMD->u4ColAddr += len;
  2690. +}
  2691. +
  2692. +static int
  2693. +mtk_nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, const uint8_t * buf, int oob_required, int page)
  2694. +{
  2695. + mtk_nand_write_buf(mtd, buf, mtd->writesize);
  2696. + mtk_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  2697. + return 0;
  2698. +}
  2699. +
  2700. +static int
  2701. +mtk_nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, uint8_t * buf, int oob_required, int page)
  2702. +{
  2703. + struct NAND_CMD *pkCMD = &g_kCMD;
  2704. + u32 u4ColAddr = pkCMD->u4ColAddr;
  2705. + u32 u4PageSize = mtd->writesize;
  2706. +
  2707. + if (u4ColAddr == 0) {
  2708. + mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, buf, chip->oob_poi);
  2709. + pkCMD->u4ColAddr += u4PageSize + mtd->oobsize;
  2710. + }
  2711. +
  2712. + return 0;
  2713. +}
  2714. +
  2715. +static int
  2716. +mtk_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip, u8 * buf, int page)
  2717. +{
  2718. + int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2719. + int block = page / page_per_block;
  2720. + u16 page_in_block = page % page_per_block;
  2721. + int mapped_block = block;
  2722. +
  2723. +#if defined (MTK_NAND_BMT)
  2724. + mapped_block = get_mapping_block_index(block);
  2725. + if (mtk_nand_exec_read_page(mtd, page_in_block + mapped_block * page_per_block,
  2726. + mtd->writesize, buf, chip->oob_poi))
  2727. + return 0;
  2728. +#else
  2729. + if (shift_on_bbt) {
  2730. + mapped_block = block_remap(mtd, block);
  2731. + if (mapped_block == -1)
  2732. + return NAND_STATUS_FAIL;
  2733. + if (nand_bbt_get(mtd, mapped_block << (chip->phys_erase_shift - chip->page_shift)) != 0x0)
  2734. + return NAND_STATUS_FAIL;
  2735. + }
  2736. +
  2737. + if (mtk_nand_exec_read_page(mtd, page_in_block + mapped_block * page_per_block, mtd->writesize, buf, chip->oob_poi))
  2738. + return 0;
  2739. + else
  2740. + return -EIO;
  2741. +#endif
  2742. +}
  2743. +
  2744. +int
  2745. +mtk_nand_erase_hw(struct mtd_info *mtd, int page)
  2746. +{
  2747. + struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  2748. +
  2749. + chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2750. + chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2751. +
  2752. + return chip->waitfunc(mtd, chip);
  2753. +}
  2754. +
  2755. +static int
  2756. +mtk_nand_erase(struct mtd_info *mtd, int page)
  2757. +{
  2758. + // get mapping
  2759. + struct nand_chip *chip = mtd->priv;
  2760. + int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2761. + int page_in_block = page % page_per_block;
  2762. + int block = page / page_per_block;
  2763. + int mapped_block = block;
  2764. +
  2765. +#if defined(MTK_NAND_BMT)
  2766. + mapped_block = get_mapping_block_index(block);
  2767. +#else
  2768. + if (shift_on_bbt) {
  2769. + mapped_block = block_remap(mtd, block);
  2770. + if (mapped_block == -1)
  2771. + return NAND_STATUS_FAIL;
  2772. + if (nand_bbt_get(mtd, mapped_block << (chip->phys_erase_shift - chip->page_shift)) != 0x0)
  2773. + return NAND_STATUS_FAIL;
  2774. + }
  2775. +#endif
  2776. +
  2777. + do {
  2778. + int status = mtk_nand_erase_hw(mtd, page_in_block + page_per_block * mapped_block);
  2779. +
  2780. + if (status & NAND_STATUS_FAIL) {
  2781. +#if defined (MTK_NAND_BMT)
  2782. + if (update_bmt( (page_in_block + mapped_block * page_per_block) << chip->page_shift,
  2783. + UPDATE_ERASE_FAIL, NULL, NULL))
  2784. + {
  2785. + MSG(INIT, "Erase fail at block: 0x%x, update BMT success\n", mapped_block);
  2786. + return 0;
  2787. + } else {
  2788. + MSG(INIT, "Erase fail at block: 0x%x, update BMT fail\n", mapped_block);
  2789. + return NAND_STATUS_FAIL;
  2790. + }
  2791. +#else
  2792. + mtk_nand_block_markbad_hw(mtd, (page_in_block + mapped_block * page_per_block) << chip->page_shift);
  2793. + nand_bbt_set(mtd, page_in_block + mapped_block * page_per_block, 0x3);
  2794. + if (shift_on_bbt) {
  2795. + mapped_block = block_remap(mtd, block);
  2796. + if (mapped_block == -1)
  2797. + return NAND_STATUS_FAIL;
  2798. + if (nand_bbt_get(mtd, mapped_block << (chip->phys_erase_shift - chip->page_shift)) != 0x0)
  2799. + return NAND_STATUS_FAIL;
  2800. + } else
  2801. + return NAND_STATUS_FAIL;
  2802. +#endif
  2803. + } else
  2804. + break;
  2805. + } while(1);
  2806. +
  2807. + return 0;
  2808. +}
  2809. +
  2810. +static int
  2811. +mtk_nand_read_oob_raw(struct mtd_info *mtd, uint8_t * buf, int page_addr, int len)
  2812. +{
  2813. + struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  2814. + u32 col_addr = 0;
  2815. + u32 sector = 0;
  2816. + int res = 0;
  2817. + u32 colnob = 2, rawnob = devinfo.addr_cycle - 2;
  2818. + int randomread = 0;
  2819. + int read_len = 0;
  2820. + int sec_num = 1<<(chip->page_shift-9);
  2821. + int spare_per_sector = mtd->oobsize/sec_num;
  2822. +
  2823. + if (len > NAND_MAX_OOBSIZE || len % OOB_AVAI_PER_SECTOR || !buf) {
  2824. + printk(KERN_WARNING "[%s] invalid parameter, len: %d, buf: %p\n", __FUNCTION__, len, buf);
  2825. + return -EINVAL;
  2826. + }
  2827. + if (len > spare_per_sector)
  2828. + randomread = 1;
  2829. + if (!randomread || !(devinfo.advancedmode & RAMDOM_READ)) {
  2830. + while (len > 0) {
  2831. + read_len = min(len, spare_per_sector);
  2832. + col_addr = NAND_SECTOR_SIZE + sector * (NAND_SECTOR_SIZE + spare_per_sector); // TODO: Fix this hard-code 16
  2833. + if (!mtk_nand_ready_for_read(chip, page_addr, col_addr, false, NULL)) {
  2834. + printk(KERN_WARNING "mtk_nand_ready_for_read return failed\n");
  2835. + res = -EIO;
  2836. + goto error;
  2837. + }
  2838. + if (!mtk_nand_mcu_read_data(buf + spare_per_sector * sector, read_len)) {
  2839. + printk(KERN_WARNING "mtk_nand_mcu_read_data return failed\n");
  2840. + res = -EIO;
  2841. + goto error;
  2842. + }
  2843. + mtk_nand_check_RW_count(read_len);
  2844. + mtk_nand_stop_read();
  2845. + sector++;
  2846. + len -= read_len;
  2847. + }
  2848. + } else {
  2849. + col_addr = NAND_SECTOR_SIZE;
  2850. + if (chip->options & NAND_BUSWIDTH_16)
  2851. + col_addr /= 2;
  2852. + if (!mtk_nand_reset())
  2853. + goto error;
  2854. + mtk_nand_set_mode(0x6000);
  2855. + NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  2856. + DRV_WriteReg16(NFI_CON_REG16, 4 << CON_NFI_SEC_SHIFT);
  2857. +
  2858. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  2859. + NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2860. +
  2861. + mtk_nand_set_autoformat(false);
  2862. +
  2863. + if (!mtk_nand_set_command(NAND_CMD_READ0))
  2864. + goto error;
  2865. + //1 FIXED ME: For Any Kind of AddrCycle
  2866. + if (!mtk_nand_set_address(col_addr, page_addr, colnob, rawnob))
  2867. + goto error;
  2868. + if (!mtk_nand_set_command(NAND_CMD_READSTART))
  2869. + goto error;
  2870. + if (!mtk_nand_status_ready(STA_NAND_BUSY))
  2871. + goto error;
  2872. + read_len = min(len, spare_per_sector);
  2873. + if (!mtk_nand_mcu_read_data(buf + spare_per_sector * sector, read_len)) {
  2874. + printk(KERN_WARNING "mtk_nand_mcu_read_data return failed first 16\n");
  2875. + res = -EIO;
  2876. + goto error;
  2877. + }
  2878. + sector++;
  2879. + len -= read_len;
  2880. + mtk_nand_stop_read();
  2881. + while (len > 0) {
  2882. + read_len = min(len, spare_per_sector);
  2883. + if (!mtk_nand_set_command(0x05))
  2884. + goto error;
  2885. + col_addr = NAND_SECTOR_SIZE + sector * (NAND_SECTOR_SIZE + spare_per_sector);
  2886. + if (chip->options & NAND_BUSWIDTH_16)
  2887. + col_addr /= 2;
  2888. + DRV_WriteReg32(NFI_COLADDR_REG32, col_addr);
  2889. + DRV_WriteReg16(NFI_ADDRNOB_REG16, 2);
  2890. + DRV_WriteReg16(NFI_CON_REG16, 4 << CON_NFI_SEC_SHIFT);
  2891. + if (!mtk_nand_status_ready(STA_ADDR_STATE))
  2892. + goto error;
  2893. + if (!mtk_nand_set_command(0xE0))
  2894. + goto error;
  2895. + if (!mtk_nand_status_ready(STA_NAND_BUSY))
  2896. + goto error;
  2897. + if (!mtk_nand_mcu_read_data(buf + spare_per_sector * sector, read_len)) {
  2898. + printk(KERN_WARNING "mtk_nand_mcu_read_data return failed first 16\n");
  2899. + res = -EIO;
  2900. + goto error;
  2901. + }
  2902. + mtk_nand_stop_read();
  2903. + sector++;
  2904. + len -= read_len;
  2905. + }
  2906. + }
  2907. +error:
  2908. + NFI_CLN_REG16(NFI_CON_REG16, CON_NFI_BRD);
  2909. + return res;
  2910. +}
  2911. +
  2912. +static int
  2913. +mtk_nand_write_oob_raw(struct mtd_info *mtd, const uint8_t * buf, int page_addr, int len)
  2914. +{
  2915. + struct nand_chip *chip = mtd->priv;
  2916. + u32 col_addr = 0;
  2917. + u32 sector = 0;
  2918. + int write_len = 0;
  2919. + int status;
  2920. + int sec_num = 1<<(chip->page_shift-9);
  2921. + int spare_per_sector = mtd->oobsize/sec_num;
  2922. +
  2923. + if (len > NAND_MAX_OOBSIZE || len % OOB_AVAI_PER_SECTOR || !buf) {
  2924. + printk(KERN_WARNING "[%s] invalid parameter, len: %d, buf: %p\n", __FUNCTION__, len, buf);
  2925. + return -EINVAL;
  2926. + }
  2927. +
  2928. + while (len > 0) {
  2929. + write_len = min(len, spare_per_sector);
  2930. + col_addr = sector * (NAND_SECTOR_SIZE + spare_per_sector) + NAND_SECTOR_SIZE;
  2931. + if (!mtk_nand_ready_for_write(chip, page_addr, col_addr, false, NULL))
  2932. + return -EIO;
  2933. + if (!mtk_nand_mcu_write_data(mtd, buf + sector * spare_per_sector, write_len))
  2934. + return -EIO;
  2935. + (void)mtk_nand_check_RW_count(write_len);
  2936. + NFI_CLN_REG16(NFI_CON_REG16, CON_NFI_BWR);
  2937. + (void)mtk_nand_set_command(NAND_CMD_PAGEPROG);
  2938. + while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY)
  2939. + ;
  2940. + status = chip->waitfunc(mtd, chip);
  2941. + if (status & NAND_STATUS_FAIL) {
  2942. + printk(KERN_INFO "status: %d\n", status);
  2943. + return -EIO;
  2944. + }
  2945. + len -= write_len;
  2946. + sector++;
  2947. + }
  2948. +
  2949. + return 0;
  2950. +}
  2951. +
  2952. +static int
  2953. +mtk_nand_write_oob_hw(struct mtd_info *mtd, struct nand_chip *chip, int page)
  2954. +{
  2955. + int i, iter;
  2956. + int sec_num = 1<<(chip->page_shift-9);
  2957. + int spare_per_sector = mtd->oobsize/sec_num;
  2958. +
  2959. + memcpy(local_oob_buf, chip->oob_poi, mtd->oobsize);
  2960. +
  2961. + // copy ecc data
  2962. + for (i = 0; i < layout->eccbytes; i++) {
  2963. + iter = (i / (spare_per_sector-OOB_AVAI_PER_SECTOR)) * spare_per_sector + OOB_AVAI_PER_SECTOR + i % (spare_per_sector-OOB_AVAI_PER_SECTOR);
  2964. + local_oob_buf[iter] = chip->oob_poi[layout->eccpos[i]];
  2965. + }
  2966. +
  2967. + // copy FDM data
  2968. + for (i = 0; i < sec_num; i++)
  2969. + memcpy(&local_oob_buf[i * spare_per_sector], &chip->oob_poi[i * OOB_AVAI_PER_SECTOR], OOB_AVAI_PER_SECTOR);
  2970. +
  2971. + return mtk_nand_write_oob_raw(mtd, local_oob_buf, page, mtd->oobsize);
  2972. +}
  2973. +
  2974. +static int mtk_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  2975. +{
  2976. + int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2977. + int block = page / page_per_block;
  2978. + u16 page_in_block = page % page_per_block;
  2979. + int mapped_block = block;
  2980. +
  2981. +#if defined(MTK_NAND_BMT)
  2982. + mapped_block = get_mapping_block_index(block);
  2983. + // write bad index into oob
  2984. + if (mapped_block != block)
  2985. + set_bad_index_to_oob(chip->oob_poi, block);
  2986. + else
  2987. + set_bad_index_to_oob(chip->oob_poi, FAKE_INDEX);
  2988. +#else
  2989. + if (shift_on_bbt)
  2990. + {
  2991. + mapped_block = block_remap(mtd, block);
  2992. + if (mapped_block == -1)
  2993. + return NAND_STATUS_FAIL;
  2994. + if (nand_bbt_get(mtd, mapped_block << (chip->phys_erase_shift - chip->page_shift)) != 0x0)
  2995. + return NAND_STATUS_FAIL;
  2996. + }
  2997. +#endif
  2998. + do {
  2999. + if (mtk_nand_write_oob_hw(mtd, chip, page_in_block + mapped_block * page_per_block /* page */)) {
  3000. + MSG(INIT, "write oob fail at block: 0x%x, page: 0x%x\n", mapped_block, page_in_block);
  3001. +#if defined(MTK_NAND_BMT)
  3002. + if (update_bmt((page_in_block + mapped_block * page_per_block) << chip->page_shift,
  3003. + UPDATE_WRITE_FAIL, NULL, chip->oob_poi))
  3004. + {
  3005. + MSG(INIT, "Update BMT success\n");
  3006. + return 0;
  3007. + } else {
  3008. + MSG(INIT, "Update BMT fail\n");
  3009. + return -EIO;
  3010. + }
  3011. +#else
  3012. + mtk_nand_block_markbad_hw(mtd, (page_in_block + mapped_block * page_per_block) << chip->page_shift);
  3013. + nand_bbt_set(mtd, page_in_block + mapped_block * page_per_block, 0x3);
  3014. + if (shift_on_bbt) {
  3015. + mapped_block = block_remap(mtd, mapped_block);
  3016. + if (mapped_block == -1)
  3017. + return NAND_STATUS_FAIL;
  3018. + if (nand_bbt_get(mtd, mapped_block << (chip->phys_erase_shift - chip->page_shift)) != 0x0)
  3019. + return NAND_STATUS_FAIL;
  3020. + } else {
  3021. + return NAND_STATUS_FAIL;
  3022. + }
  3023. +#endif
  3024. + } else
  3025. + break;
  3026. + } while (1);
  3027. +
  3028. + return 0;
  3029. +}
  3030. +
  3031. +int
  3032. +mtk_nand_block_markbad_hw(struct mtd_info *mtd, loff_t offset)
  3033. +{
  3034. + struct nand_chip *chip = mtd->priv;
  3035. + int block = (int)offset >> chip->phys_erase_shift;
  3036. + int page = block * (1 << (chip->phys_erase_shift - chip->page_shift));
  3037. + u8 buf[8];
  3038. +
  3039. + memset(buf, 0xFF, 8);
  3040. + buf[0] = 0;
  3041. + return mtk_nand_write_oob_raw(mtd, buf, page, 8);
  3042. +}
  3043. +
  3044. +static int
  3045. +mtk_nand_block_markbad(struct mtd_info *mtd, loff_t offset)
  3046. +{
  3047. + struct nand_chip *chip = mtd->priv;
  3048. + int block = (int)offset >> chip->phys_erase_shift;
  3049. + int ret;
  3050. + int mapped_block = block;
  3051. +
  3052. + nand_get_device(chip, mtd, FL_WRITING);
  3053. +
  3054. +#if defined(MTK_NAND_BMT)
  3055. + mapped_block = get_mapping_block_index(block);
  3056. + ret = mtk_nand_block_markbad_hw(mtd, mapped_block << chip->phys_erase_shift);
  3057. +#else
  3058. + if (shift_on_bbt) {
  3059. + mapped_block = block_remap(mtd, block);
  3060. + if (mapped_block == -1) {
  3061. + printk("NAND mark bad failed\n");
  3062. + nand_release_device(mtd);
  3063. + return NAND_STATUS_FAIL;
  3064. + }
  3065. + }
  3066. + ret = mtk_nand_block_markbad_hw(mtd, mapped_block << chip->phys_erase_shift);
  3067. +#endif
  3068. + nand_release_device(mtd);
  3069. +
  3070. + return ret;
  3071. +}
  3072. +
  3073. +int
  3074. +mtk_nand_read_oob_hw(struct mtd_info *mtd, struct nand_chip *chip, int page)
  3075. +{
  3076. + int i;
  3077. + u8 iter = 0;
  3078. +
  3079. + int sec_num = 1<<(chip->page_shift-9);
  3080. + int spare_per_sector = mtd->oobsize/sec_num;
  3081. +
  3082. + if (mtk_nand_read_oob_raw(mtd, chip->oob_poi, page, mtd->oobsize)) {
  3083. + printk(KERN_ERR "[%s]mtk_nand_read_oob_raw return failed\n", __FUNCTION__);
  3084. + return -EIO;
  3085. + }
  3086. +
  3087. + // adjust to ecc physical layout to memory layout
  3088. + /*********************************************************/
  3089. + /* FDM0 | ECC0 | FDM1 | ECC1 | FDM2 | ECC2 | FDM3 | ECC3 */
  3090. + /* 8B | 8B | 8B | 8B | 8B | 8B | 8B | 8B */
  3091. + /*********************************************************/
  3092. +
  3093. + memcpy(local_oob_buf, chip->oob_poi, mtd->oobsize);
  3094. + // copy ecc data
  3095. + for (i = 0; i < layout->eccbytes; i++) {
  3096. + iter = (i / (spare_per_sector-OOB_AVAI_PER_SECTOR)) * spare_per_sector + OOB_AVAI_PER_SECTOR + i % (spare_per_sector-OOB_AVAI_PER_SECTOR);
  3097. + chip->oob_poi[layout->eccpos[i]] = local_oob_buf[iter];
  3098. + }
  3099. +
  3100. + // copy FDM data
  3101. + for (i = 0; i < sec_num; i++) {
  3102. + memcpy(&chip->oob_poi[i * OOB_AVAI_PER_SECTOR], &local_oob_buf[i * spare_per_sector], OOB_AVAI_PER_SECTOR);
  3103. + }
  3104. +
  3105. + return 0;
  3106. +}
  3107. +
  3108. +static int
  3109. +mtk_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  3110. +{
  3111. + int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  3112. + int block = page / page_per_block;
  3113. + u16 page_in_block = page % page_per_block;
  3114. + int mapped_block = block;
  3115. +
  3116. +#if defined (MTK_NAND_BMT)
  3117. + mapped_block = get_mapping_block_index(block);
  3118. + mtk_nand_read_oob_hw(mtd, chip, page_in_block + mapped_block * page_per_block);
  3119. +#else
  3120. + if (shift_on_bbt) {
  3121. + mapped_block = block_remap(mtd, block);
  3122. + if (mapped_block == -1)
  3123. + return NAND_STATUS_FAIL;
  3124. + // allow to read oob even if the block is bad
  3125. + }
  3126. + if (mtk_nand_read_oob_hw(mtd, chip, page_in_block + mapped_block * page_per_block)!=0)
  3127. + return -1;
  3128. +#endif
  3129. + return 0;
  3130. +}
  3131. +
  3132. +int
  3133. +mtk_nand_block_bad_hw(struct mtd_info *mtd, loff_t ofs)
  3134. +{
  3135. + struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  3136. + int page_addr = (int)(ofs >> chip->page_shift);
  3137. + unsigned int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  3138. + unsigned char oob_buf[8];
  3139. +
  3140. + page_addr &= ~(page_per_block - 1);
  3141. + if (mtk_nand_read_oob_raw(mtd, oob_buf, page_addr, sizeof(oob_buf))) {
  3142. + printk(KERN_WARNING "mtk_nand_read_oob_raw return error\n");
  3143. + return 1;
  3144. + }
  3145. +
  3146. + if (oob_buf[0] != 0xff) {
  3147. + printk(KERN_WARNING "Bad block detected at 0x%x, oob_buf[0] is 0x%x\n", page_addr, oob_buf[0]);
  3148. + // dump_nfi();
  3149. + return 1;
  3150. + }
  3151. +
  3152. + return 0;
  3153. +}
  3154. +
  3155. +static int
  3156. +mtk_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
  3157. +{
  3158. + struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  3159. + int block = (int)ofs >> chip->phys_erase_shift;
  3160. + int mapped_block = block;
  3161. + int ret;
  3162. +
  3163. +#if defined(MTK_NAND_BMT)
  3164. + mapped_block = get_mapping_block_index(block);
  3165. +#else
  3166. + if (shift_on_bbt) {
  3167. + mapped_block = block_remap(mtd, block);
  3168. + }
  3169. +#endif
  3170. +
  3171. + ret = mtk_nand_block_bad_hw(mtd, mapped_block << chip->phys_erase_shift);
  3172. +#if defined (MTK_NAND_BMT)
  3173. + if (ret) {
  3174. + MSG(INIT, "Unmapped bad block: 0x%x\n", mapped_block);
  3175. + if (update_bmt(mapped_block << chip->phys_erase_shift, UPDATE_UNMAPPED_BLOCK, NULL, NULL)) {
  3176. + MSG(INIT, "Update BMT success\n");
  3177. + ret = 0;
  3178. + } else {
  3179. + MSG(INIT, "Update BMT fail\n");
  3180. + ret = 1;
  3181. + }
  3182. + }
  3183. +#endif
  3184. +
  3185. + return ret;
  3186. +}
  3187. +
  3188. +#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  3189. +char gacBuf[4096 + 288];
  3190. +
  3191. +static int
  3192. +mtk_nand_verify_buf(struct mtd_info *mtd, const uint8_t * buf, int len)
  3193. +{
  3194. + struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  3195. + struct NAND_CMD *pkCMD = &g_kCMD;
  3196. + u32 u4PageSize = mtd->writesize;
  3197. + u32 *pSrc, *pDst;
  3198. + int i;
  3199. +
  3200. + mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, gacBuf, gacBuf + u4PageSize);
  3201. +
  3202. + pSrc = (u32 *) buf;
  3203. + pDst = (u32 *) gacBuf;
  3204. + len = len / sizeof(u32);
  3205. + for (i = 0; i < len; ++i) {
  3206. + if (*pSrc != *pDst) {
  3207. + MSG(VERIFY, "mtk_nand_verify_buf page fail at page %d\n", pkCMD->u4RowAddr);
  3208. + return -1;
  3209. + }
  3210. + pSrc++;
  3211. + pDst++;
  3212. + }
  3213. +
  3214. + pSrc = (u32 *) chip->oob_poi;
  3215. + pDst = (u32 *) (gacBuf + u4PageSize);
  3216. +
  3217. + if ((pSrc[0] != pDst[0]) || (pSrc[1] != pDst[1]) || (pSrc[2] != pDst[2]) || (pSrc[3] != pDst[3]) || (pSrc[4] != pDst[4]) || (pSrc[5] != pDst[5])) {
  3218. + // TODO: Ask Designer Why?
  3219. + //(pSrc[6] != pDst[6]) || (pSrc[7] != pDst[7]))
  3220. + MSG(VERIFY, "mtk_nand_verify_buf oob fail at page %d\n", pkCMD->u4RowAddr);
  3221. + MSG(VERIFY, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", pSrc[0], pSrc[1], pSrc[2], pSrc[3], pSrc[4], pSrc[5], pSrc[6], pSrc[7]);
  3222. + MSG(VERIFY, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", pDst[0], pDst[1], pDst[2], pDst[3], pDst[4], pDst[5], pDst[6], pDst[7]);
  3223. + return -1;
  3224. + }
  3225. + return 0;
  3226. +}
  3227. +#endif
  3228. +
  3229. +static void
  3230. +mtk_nand_init_hw(struct mtk_nand_host *host) {
  3231. + struct mtk_nand_host_hw *hw = host->hw;
  3232. + u32 data;
  3233. +
  3234. + data = DRV_Reg32(RALINK_SYSCTL_BASE+0x60);
  3235. + data &= ~((0x3<<18)|(0x3<<16));
  3236. + data |= ((0x2<<18) |(0x2<<16));
  3237. + DRV_WriteReg32(RALINK_SYSCTL_BASE+0x60, data);
  3238. +
  3239. + MSG(INIT, "Enable NFI Clock\n");
  3240. + nand_enable_clock();
  3241. +
  3242. + g_bInitDone = false;
  3243. + g_kCMD.u4OOBRowAddr = (u32) - 1;
  3244. +
  3245. + /* Set default NFI access timing control */
  3246. + DRV_WriteReg32(NFI_ACCCON_REG32, hw->nfi_access_timing);
  3247. + DRV_WriteReg16(NFI_CNFG_REG16, 0);
  3248. + DRV_WriteReg16(NFI_PAGEFMT_REG16, 0);
  3249. +
  3250. + /* Reset the state machine and data FIFO, because flushing FIFO */
  3251. + (void)mtk_nand_reset();
  3252. +
  3253. + /* Set the ECC engine */
  3254. + if (hw->nand_ecc_mode == NAND_ECC_HW) {
  3255. + MSG(INIT, "%s : Use HW ECC\n", MODULE_NAME);
  3256. + if (g_bHwEcc)
  3257. + NFI_SET_REG32(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  3258. + ECC_Config(host->hw,4);
  3259. + mtk_nand_configure_fdm(8);
  3260. + mtk_nand_configure_lock();
  3261. + }
  3262. +
  3263. + NFI_SET_REG16(NFI_IOCON_REG16, 0x47);
  3264. +}
  3265. +
  3266. +static int mtk_nand_dev_ready(struct mtd_info *mtd)
  3267. +{
  3268. + return !(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY);
  3269. +}
  3270. +
  3271. +#define FACT_BBT_BLOCK_NUM 32 // use the latest 32 BLOCK for factory bbt table
  3272. +#define FACT_BBT_OOB_SIGNATURE 1
  3273. +#define FACT_BBT_SIGNATURE_LEN 7
  3274. +const u8 oob_signature[] = "mtknand";
  3275. +static u8 *fact_bbt = 0;
  3276. +static u32 bbt_size = 0;
  3277. +
  3278. +static int
  3279. +read_fact_bbt(struct mtd_info *mtd, unsigned int page)
  3280. +{
  3281. + struct nand_chip *chip = mtd->priv;
  3282. +
  3283. + // read oob
  3284. + if (mtk_nand_read_oob_hw(mtd, chip, page)==0)
  3285. + {
  3286. + if (chip->oob_poi[nand_badblock_offset] != 0xFF)
  3287. + {
  3288. + printk("Bad Block on Page %x\n", page);
  3289. + return -1;
  3290. + }
  3291. + if (memcmp(&chip->oob_poi[FACT_BBT_OOB_SIGNATURE], oob_signature, FACT_BBT_SIGNATURE_LEN) != 0)
  3292. + {
  3293. + printk("compare signature failed %x\n", page);
  3294. + return -1;
  3295. + }
  3296. + if (mtk_nand_exec_read_page(mtd, page, mtd->writesize, chip->buffers->databuf, chip->oob_poi))
  3297. + {
  3298. + printk("Signature matched and data read!\n");
  3299. + memcpy(fact_bbt, chip->buffers->databuf, (bbt_size <= mtd->writesize)? bbt_size:mtd->writesize);
  3300. + return 0;
  3301. + }
  3302. +
  3303. + }
  3304. + printk("failed at page %x\n", page);
  3305. + return -1;
  3306. +}
  3307. +
  3308. +static int
  3309. +load_fact_bbt(struct mtd_info *mtd)
  3310. +{
  3311. + struct nand_chip *chip = mtd->priv;
  3312. + int i;
  3313. + u32 total_block;
  3314. +
  3315. + total_block = 1 << (chip->chip_shift - chip->phys_erase_shift);
  3316. + bbt_size = total_block >> 2;
  3317. +
  3318. + if ((!fact_bbt) && (bbt_size))
  3319. + fact_bbt = (u8 *)kmalloc(bbt_size, GFP_KERNEL);
  3320. + if (!fact_bbt)
  3321. + return -1;
  3322. +
  3323. + for (i = total_block - 1; i >= (total_block - FACT_BBT_BLOCK_NUM); i--)
  3324. + {
  3325. + if (read_fact_bbt(mtd, i << (chip->phys_erase_shift - chip->page_shift)) == 0)
  3326. + {
  3327. + printk("load_fact_bbt success %d\n", i);
  3328. + return 0;
  3329. + }
  3330. +
  3331. + }
  3332. + printk("load_fact_bbt failed\n");
  3333. + return -1;
  3334. +}
  3335. +
  3336. +static int oob_mtk_ooblayout_ecc(struct mtd_info *mtd, int section,
  3337. + struct mtd_oob_region *oobregion)
  3338. +{
  3339. + oobregion->length = 8;
  3340. + oobregion->offset = layout->eccpos[section * 8];
  3341. +
  3342. + return 0;
  3343. +}
  3344. +
  3345. +static int oob_mtk_ooblayout_free(struct mtd_info *mtd, int section,
  3346. + struct mtd_oob_region *oobregion)
  3347. +{
  3348. + if (section >= (layout->eccbytes / 8)) {
  3349. + return -ERANGE;
  3350. + }
  3351. + oobregion->offset = layout->oobfree[section].offset;
  3352. + oobregion->length = layout->oobfree[section].length;
  3353. +
  3354. + return 0;
  3355. +}
  3356. +
  3357. +
  3358. +static const struct mtd_ooblayout_ops oob_mtk_ops = {
  3359. + .ecc = oob_mtk_ooblayout_ecc,
  3360. + .free = oob_mtk_ooblayout_free,
  3361. +};
  3362. +
  3363. +static int
  3364. +mtk_nand_probe(struct platform_device *pdev)
  3365. +{
  3366. + struct mtd_part_parser_data ppdata;
  3367. + struct mtk_nand_host_hw *hw;
  3368. + struct nand_chip *nand_chip;
  3369. + struct mtd_info *mtd;
  3370. + u8 ext_id1, ext_id2, ext_id3;
  3371. + int err = 0;
  3372. + int id;
  3373. + u32 ext_id;
  3374. + int i;
  3375. + u32 data;
  3376. +
  3377. + data = DRV_Reg32(RALINK_SYSCTL_BASE+0x60);
  3378. + data &= ~((0x3<<18)|(0x3<<16));
  3379. + data |= ((0x2<<18) |(0x2<<16));
  3380. + DRV_WriteReg32(RALINK_SYSCTL_BASE+0x60, data);
  3381. +
  3382. + hw = &mt7621_nand_hw;
  3383. + BUG_ON(!hw);
  3384. + /* Allocate memory for the device structure (and zero it) */
  3385. + host = kzalloc(sizeof(struct mtk_nand_host), GFP_KERNEL);
  3386. + if (!host) {
  3387. + MSG(INIT, "mtk_nand: failed to allocate device structure.\n");
  3388. + return -ENOMEM;
  3389. + }
  3390. +
  3391. + host->hw = hw;
  3392. +
  3393. + /* init mtd data structure */
  3394. + nand_chip = &host->nand_chip;
  3395. + nand_chip->priv = host; /* link the private data structures */
  3396. +
  3397. + mtd = host->mtd = &nand_chip->mtd;
  3398. + mtd->priv = nand_chip;
  3399. + mtd->owner = THIS_MODULE;
  3400. + mtd->name = "MT7621-NAND";
  3401. +
  3402. + hw->nand_ecc_mode = NAND_ECC_HW;
  3403. +
  3404. + /* Set address of NAND IO lines */
  3405. + nand_chip->IO_ADDR_R = (void __iomem *)NFI_DATAR_REG32;
  3406. + nand_chip->IO_ADDR_W = (void __iomem *)NFI_DATAW_REG32;
  3407. + nand_chip->chip_delay = 20; /* 20us command delay time */
  3408. + nand_chip->ecc.mode = hw->nand_ecc_mode; /* enable ECC */
  3409. + nand_chip->ecc.strength = 1;
  3410. + nand_chip->read_byte = mtk_nand_read_byte;
  3411. + nand_chip->read_buf = mtk_nand_read_buf;
  3412. + nand_chip->write_buf = mtk_nand_write_buf;
  3413. +#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  3414. + nand_chip->verify_buf = mtk_nand_verify_buf;
  3415. +#endif
  3416. + nand_chip->select_chip = mtk_nand_select_chip;
  3417. + nand_chip->dev_ready = mtk_nand_dev_ready;
  3418. + nand_chip->cmdfunc = mtk_nand_command_bp;
  3419. + nand_chip->ecc.read_page = mtk_nand_read_page_hwecc;
  3420. + nand_chip->ecc.write_page = mtk_nand_write_page_hwecc;
  3421. +
  3422. + mtd_set_ooblayout(mtd, &oob_mtk_ops);
  3423. + nand_chip->ecc.size = hw->nand_ecc_size; //2048
  3424. + nand_chip->ecc.bytes = hw->nand_ecc_bytes; //32
  3425. +
  3426. + // For BMT, we need to revise driver architecture
  3427. + nand_chip->write_page = mtk_nand_write_page;
  3428. + nand_chip->ecc.write_oob = mtk_nand_write_oob;
  3429. + nand_chip->block_markbad = mtk_nand_block_markbad; // need to add nand_get_device()/nand_release_device().
  3430. + nand_chip->read_page = mtk_nand_read_page;
  3431. + nand_chip->ecc.read_oob = mtk_nand_read_oob;
  3432. + nand_chip->block_bad = mtk_nand_block_bad;
  3433. + nand_chip->cmd_ctrl = mtk_nfc_cmd_ctrl;
  3434. +
  3435. + //Qwert:Add for Uboot
  3436. + mtk_nand_init_hw(host);
  3437. + /* Select the device */
  3438. + nand_chip->select_chip(mtd, NFI_DEFAULT_CS);
  3439. +
  3440. + /*
  3441. + * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  3442. + * after power-up
  3443. + */
  3444. + nand_chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3445. +
  3446. + memset(&devinfo, 0 , sizeof(flashdev_info));
  3447. +
  3448. + /* Send the command for reading device ID */
  3449. +
  3450. + nand_chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3451. +
  3452. + /* Read manufacturer and device IDs */
  3453. + manu_id = nand_chip->read_byte(mtd);
  3454. + dev_id = nand_chip->read_byte(mtd);
  3455. + id = dev_id | (manu_id << 8);
  3456. + ext_id1 = nand_chip->read_byte(mtd);
  3457. + ext_id2 = nand_chip->read_byte(mtd);
  3458. + ext_id3 = nand_chip->read_byte(mtd);
  3459. + ext_id = ext_id1 << 16 | ext_id2 << 8 | ext_id3;
  3460. + if (!get_device_info(id, ext_id, &devinfo)) {
  3461. + u32 chip_mode = RALINK_REG(RALINK_SYSCTL_BASE+0x010)&0x0F;
  3462. + MSG(INIT, "Not Support this Device! \r\n");
  3463. + memset(&devinfo, 0 , sizeof(flashdev_info));
  3464. + MSG(INIT, "chip_mode=%08X\n",chip_mode);
  3465. +
  3466. + /* apply bootstrap first */
  3467. + devinfo.addr_cycle = 5;
  3468. + devinfo.iowidth = 8;
  3469. +
  3470. + switch (chip_mode) {
  3471. + case 10:
  3472. + devinfo.pagesize = 2048;
  3473. + devinfo.sparesize = 128;
  3474. + devinfo.totalsize = 128;
  3475. + devinfo.blocksize = 128;
  3476. + break;
  3477. + case 11:
  3478. + devinfo.pagesize = 4096;
  3479. + devinfo.sparesize = 128;
  3480. + devinfo.totalsize = 1024;
  3481. + devinfo.blocksize = 256;
  3482. + break;
  3483. + case 12:
  3484. + devinfo.pagesize = 4096;
  3485. + devinfo.sparesize = 224;
  3486. + devinfo.totalsize = 2048;
  3487. + devinfo.blocksize = 512;
  3488. + break;
  3489. + default:
  3490. + case 1:
  3491. + devinfo.pagesize = 2048;
  3492. + devinfo.sparesize = 64;
  3493. + devinfo.totalsize = 128;
  3494. + devinfo.blocksize = 128;
  3495. + break;
  3496. + }
  3497. +
  3498. + devinfo.timmingsetting = NFI_DEFAULT_ACCESS_TIMING;
  3499. + devinfo.devciename[0] = 'U';
  3500. + devinfo.advancedmode = 0;
  3501. + }
  3502. + mtd->writesize = devinfo.pagesize;
  3503. + mtd->erasesize = (devinfo.blocksize<<10);
  3504. + mtd->oobsize = devinfo.sparesize;
  3505. +
  3506. + nand_chip->chipsize = (devinfo.totalsize<<20);
  3507. + nand_chip->page_shift = ffs(mtd->writesize) - 1;
  3508. + nand_chip->pagemask = (nand_chip->chipsize >> nand_chip->page_shift) - 1;
  3509. + nand_chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
  3510. + nand_chip->chip_shift = ffs(nand_chip->chipsize) - 1;//0x1C;//ffs(nand_chip->chipsize) - 1;
  3511. + nand_chip->cmd_ctrl = mtk_nfc_cmd_ctrl;
  3512. +
  3513. + if (devinfo.pagesize == 4096)
  3514. + layout = &nand_oob_128;
  3515. + else if (devinfo.pagesize == 2048)
  3516. + layout = &nand_oob_64;
  3517. + else if (devinfo.pagesize == 512)
  3518. + layout = &nand_oob_16;
  3519. +
  3520. + layout->eccbytes = devinfo.sparesize-OOB_AVAI_PER_SECTOR*(devinfo.pagesize/NAND_SECTOR_SIZE);
  3521. + for (i = 0; i < layout->eccbytes; i++)
  3522. + layout->eccpos[i]=OOB_AVAI_PER_SECTOR*(devinfo.pagesize/NAND_SECTOR_SIZE)+i;
  3523. +
  3524. + MSG(INIT, "Support this Device in MTK table! %x \r\n", id);
  3525. + hw->nfi_bus_width = devinfo.iowidth;
  3526. + DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.timmingsetting);
  3527. +
  3528. + /* 16-bit bus width */
  3529. + if (hw->nfi_bus_width == 16) {
  3530. + MSG(INIT, "%s : Set the 16-bit I/O settings!\n", MODULE_NAME);
  3531. + nand_chip->options |= NAND_BUSWIDTH_16;
  3532. + }
  3533. + mtd->oobsize = devinfo.sparesize;
  3534. + hw->nfi_cs_num = 1;
  3535. +
  3536. + nand_chip->options |= NAND_USE_BOUNCE_BUFFER;
  3537. + nand_chip->buf_align = 16;
  3538. +
  3539. + /* Scan to find existance of the device */
  3540. + if (nand_scan(mtd, hw->nfi_cs_num)) {
  3541. + MSG(INIT, "%s : nand_scan fail.\n", MODULE_NAME);
  3542. + err = -ENXIO;
  3543. + goto out;
  3544. + }
  3545. +
  3546. + nand_chip->erase = mtk_nand_erase;
  3547. +
  3548. + g_page_size = mtd->writesize;
  3549. + platform_set_drvdata(pdev, host);
  3550. + if (hw->nfi_bus_width == 16) {
  3551. + NFI_SET_REG16(NFI_PAGEFMT_REG16, PAGEFMT_DBYTE_EN);
  3552. + }
  3553. +
  3554. + nand_chip->select_chip(mtd, 0);
  3555. +#if defined(MTK_NAND_BMT)
  3556. + nand_chip->chipsize -= (BMT_POOL_SIZE) << nand_chip->phys_erase_shift;
  3557. +#endif
  3558. + mtd->size = nand_chip->chipsize;
  3559. +
  3560. + CFG_BLOCKSIZE = mtd->erasesize;
  3561. +
  3562. +#if defined(MTK_NAND_BMT)
  3563. + if (!g_bmt) {
  3564. + if (!(g_bmt = init_bmt(nand_chip, BMT_POOL_SIZE))) {
  3565. + MSG(INIT, "Error: init bmt failed\n");
  3566. + return 0;
  3567. + }
  3568. + }
  3569. +#endif
  3570. +
  3571. + nand_set_flash_node(nand_chip, pdev->dev.of_node);
  3572. + err = mtd_device_parse_register(mtd, probe_types, &ppdata,
  3573. + NULL, 0);
  3574. + if (!err) {
  3575. + MSG(INIT, "[mtk_nand] probe successfully!\n");
  3576. + nand_disable_clock();
  3577. + shift_on_bbt = 1;
  3578. + if (load_fact_bbt(mtd) == 0) {
  3579. + int i;
  3580. + for (i = 0; i < 0x100; i++)
  3581. + nand_chip->bbt[i] |= fact_bbt[i];
  3582. + }
  3583. +
  3584. + return err;
  3585. + }
  3586. +
  3587. +out:
  3588. + MSG(INIT, "[NFI] mtk_nand_probe fail, err = %d!\n", err);
  3589. + nand_release(mtd);
  3590. + platform_set_drvdata(pdev, NULL);
  3591. + kfree(host);
  3592. + nand_disable_clock();
  3593. + return err;
  3594. +}
  3595. +
  3596. +static int
  3597. +mtk_nand_remove(struct platform_device *pdev)
  3598. +{
  3599. + struct mtk_nand_host *host = platform_get_drvdata(pdev);
  3600. + struct mtd_info *mtd = host->mtd;
  3601. + struct nand_chip *nand_chip = &host->nand_chip;
  3602. +
  3603. + nand_release(mtd);
  3604. + kfree(host);
  3605. + nand_disable_clock();
  3606. +
  3607. + return 0;
  3608. +}
  3609. +
  3610. +static const struct of_device_id mt7621_nand_match[] = {
  3611. + { .compatible = "mtk,mt7621-nand" },
  3612. + {},
  3613. +};
  3614. +MODULE_DEVICE_TABLE(of, mt7621_nand_match);
  3615. +
  3616. +static struct platform_driver mtk_nand_driver = {
  3617. + .probe = mtk_nand_probe,
  3618. + .remove = mtk_nand_remove,
  3619. + .driver = {
  3620. + .name = "MT7621-NAND",
  3621. + .owner = THIS_MODULE,
  3622. + .of_match_table = mt7621_nand_match,
  3623. + },
  3624. +};
  3625. +
  3626. +static int __init
  3627. +mtk_nand_init(void)
  3628. +{
  3629. + printk("MediaTek Nand driver init, version %s\n", VERSION);
  3630. +
  3631. + return platform_driver_register(&mtk_nand_driver);
  3632. +}
  3633. +
  3634. +static void __exit
  3635. +mtk_nand_exit(void)
  3636. +{
  3637. + platform_driver_unregister(&mtk_nand_driver);
  3638. +}
  3639. +
  3640. +module_init(mtk_nand_init);
  3641. +module_exit(mtk_nand_exit);
  3642. +MODULE_LICENSE("GPL");
  3643. --- /dev/null
  3644. +++ b/drivers/mtd/nand/mtk_nand2.h
  3645. @@ -0,0 +1,452 @@
  3646. +#ifndef __MTK_NAND_H
  3647. +#define __MTK_NAND_H
  3648. +
  3649. +#define RALINK_NAND_CTRL_BASE 0xBE003000
  3650. +#define RALINK_SYSCTL_BASE 0xBE000000
  3651. +#define RALINK_NANDECC_CTRL_BASE 0xBE003800
  3652. +/*******************************************************************************
  3653. + * NFI Register Definition
  3654. + *******************************************************************************/
  3655. +
  3656. +#define NFI_CNFG_REG16 ((volatile P_U16)(NFI_BASE+0x0000))
  3657. +#define NFI_PAGEFMT_REG16 ((volatile P_U16)(NFI_BASE+0x0004))
  3658. +#define NFI_CON_REG16 ((volatile P_U16)(NFI_BASE+0x0008))
  3659. +#define NFI_ACCCON_REG32 ((volatile P_U32)(NFI_BASE+0x000C))
  3660. +#define NFI_INTR_EN_REG16 ((volatile P_U16)(NFI_BASE+0x0010))
  3661. +#define NFI_INTR_REG16 ((volatile P_U16)(NFI_BASE+0x0014))
  3662. +
  3663. +#define NFI_CMD_REG16 ((volatile P_U16)(NFI_BASE+0x0020))
  3664. +
  3665. +#define NFI_ADDRNOB_REG16 ((volatile P_U16)(NFI_BASE+0x0030))
  3666. +#define NFI_COLADDR_REG32 ((volatile P_U32)(NFI_BASE+0x0034))
  3667. +#define NFI_ROWADDR_REG32 ((volatile P_U32)(NFI_BASE+0x0038))
  3668. +
  3669. +#define NFI_STRDATA_REG16 ((volatile P_U16)(NFI_BASE+0x0040))
  3670. +
  3671. +#define NFI_DATAW_REG32 ((volatile P_U32)(NFI_BASE+0x0050))
  3672. +#define NFI_DATAR_REG32 ((volatile P_U32)(NFI_BASE+0x0054))
  3673. +#define NFI_PIO_DIRDY_REG16 ((volatile P_U16)(NFI_BASE+0x0058))
  3674. +
  3675. +#define NFI_STA_REG32 ((volatile P_U32)(NFI_BASE+0x0060))
  3676. +#define NFI_FIFOSTA_REG16 ((volatile P_U16)(NFI_BASE+0x0064))
  3677. +#define NFI_LOCKSTA_REG16 ((volatile P_U16)(NFI_BASE+0x0068))
  3678. +
  3679. +#define NFI_ADDRCNTR_REG16 ((volatile P_U16)(NFI_BASE+0x0070))
  3680. +
  3681. +#define NFI_STRADDR_REG32 ((volatile P_U32)(NFI_BASE+0x0080))
  3682. +#define NFI_BYTELEN_REG16 ((volatile P_U16)(NFI_BASE+0x0084))
  3683. +
  3684. +#define NFI_CSEL_REG16 ((volatile P_U16)(NFI_BASE+0x0090))
  3685. +#define NFI_IOCON_REG16 ((volatile P_U16)(NFI_BASE+0x0094))
  3686. +
  3687. +#define NFI_FDM0L_REG32 ((volatile P_U32)(NFI_BASE+0x00A0))
  3688. +#define NFI_FDM0M_REG32 ((volatile P_U32)(NFI_BASE+0x00A4))
  3689. +
  3690. +#define NFI_LOCK_REG16 ((volatile P_U16)(NFI_BASE+0x0100))
  3691. +#define NFI_LOCKCON_REG32 ((volatile P_U32)(NFI_BASE+0x0104))
  3692. +#define NFI_LOCKANOB_REG16 ((volatile P_U16)(NFI_BASE+0x0108))
  3693. +#define NFI_LOCK00ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0110))
  3694. +#define NFI_LOCK00FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0114))
  3695. +#define NFI_LOCK01ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0118))
  3696. +#define NFI_LOCK01FMT_REG32 ((volatile P_U32)(NFI_BASE+0x011C))
  3697. +#define NFI_LOCK02ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0120))
  3698. +#define NFI_LOCK02FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0124))
  3699. +#define NFI_LOCK03ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0128))
  3700. +#define NFI_LOCK03FMT_REG32 ((volatile P_U32)(NFI_BASE+0x012C))
  3701. +#define NFI_LOCK04ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0130))
  3702. +#define NFI_LOCK04FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0134))
  3703. +#define NFI_LOCK05ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0138))
  3704. +#define NFI_LOCK05FMT_REG32 ((volatile P_U32)(NFI_BASE+0x013C))
  3705. +#define NFI_LOCK06ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0140))
  3706. +#define NFI_LOCK06FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0144))
  3707. +#define NFI_LOCK07ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0148))
  3708. +#define NFI_LOCK07FMT_REG32 ((volatile P_U32)(NFI_BASE+0x014C))
  3709. +#define NFI_LOCK08ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0150))
  3710. +#define NFI_LOCK08FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0154))
  3711. +#define NFI_LOCK09ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0158))
  3712. +#define NFI_LOCK09FMT_REG32 ((volatile P_U32)(NFI_BASE+0x015C))
  3713. +#define NFI_LOCK10ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0160))
  3714. +#define NFI_LOCK10FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0164))
  3715. +#define NFI_LOCK11ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0168))
  3716. +#define NFI_LOCK11FMT_REG32 ((volatile P_U32)(NFI_BASE+0x016C))
  3717. +#define NFI_LOCK12ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0170))
  3718. +#define NFI_LOCK12FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0174))
  3719. +#define NFI_LOCK13ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0178))
  3720. +#define NFI_LOCK13FMT_REG32 ((volatile P_U32)(NFI_BASE+0x017C))
  3721. +#define NFI_LOCK14ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0180))
  3722. +#define NFI_LOCK14FMT_REG32 ((volatile P_U32)(NFI_BASE+0x0184))
  3723. +#define NFI_LOCK15ADD_REG32 ((volatile P_U32)(NFI_BASE+0x0188))
  3724. +#define NFI_LOCK15FMT_REG32 ((volatile P_U32)(NFI_BASE+0x018C))
  3725. +
  3726. +#define NFI_FIFODATA0_REG32 ((volatile P_U32)(NFI_BASE+0x0190))
  3727. +#define NFI_FIFODATA1_REG32 ((volatile P_U32)(NFI_BASE+0x0194))
  3728. +#define NFI_FIFODATA2_REG32 ((volatile P_U32)(NFI_BASE+0x0198))
  3729. +#define NFI_FIFODATA3_REG32 ((volatile P_U32)(NFI_BASE+0x019C))
  3730. +#define NFI_MASTERSTA_REG16 ((volatile P_U16)(NFI_BASE+0x0210))
  3731. +
  3732. +
  3733. +/*******************************************************************************
  3734. + * NFI Register Field Definition
  3735. + *******************************************************************************/
  3736. +
  3737. +/* NFI_CNFG */
  3738. +#define CNFG_AHB (0x0001)
  3739. +#define CNFG_READ_EN (0x0002)
  3740. +#define CNFG_DMA_BURST_EN (0x0004)
  3741. +#define CNFG_BYTE_RW (0x0040)
  3742. +#define CNFG_HW_ECC_EN (0x0100)
  3743. +#define CNFG_AUTO_FMT_EN (0x0200)
  3744. +#define CNFG_OP_IDLE (0x0000)
  3745. +#define CNFG_OP_READ (0x1000)
  3746. +#define CNFG_OP_SRD (0x2000)
  3747. +#define CNFG_OP_PRGM (0x3000)
  3748. +#define CNFG_OP_ERASE (0x4000)
  3749. +#define CNFG_OP_RESET (0x5000)
  3750. +#define CNFG_OP_CUST (0x6000)
  3751. +#define CNFG_OP_MODE_MASK (0x7000)
  3752. +#define CNFG_OP_MODE_SHIFT (12)
  3753. +
  3754. +/* NFI_PAGEFMT */
  3755. +#define PAGEFMT_512 (0x0000)
  3756. +#define PAGEFMT_2K (0x0001)
  3757. +#define PAGEFMT_4K (0x0002)
  3758. +
  3759. +#define PAGEFMT_PAGE_MASK (0x0003)
  3760. +
  3761. +#define PAGEFMT_DBYTE_EN (0x0008)
  3762. +
  3763. +#define PAGEFMT_SPARE_16 (0x0000)
  3764. +#define PAGEFMT_SPARE_26 (0x0001)
  3765. +#define PAGEFMT_SPARE_27 (0x0002)
  3766. +#define PAGEFMT_SPARE_28 (0x0003)
  3767. +#define PAGEFMT_SPARE_MASK (0x0030)
  3768. +#define PAGEFMT_SPARE_SHIFT (4)
  3769. +
  3770. +#define PAGEFMT_FDM_MASK (0x0F00)
  3771. +#define PAGEFMT_FDM_SHIFT (8)
  3772. +
  3773. +#define PAGEFMT_FDM_ECC_MASK (0xF000)
  3774. +#define PAGEFMT_FDM_ECC_SHIFT (12)
  3775. +
  3776. +/* NFI_CON */
  3777. +#define CON_FIFO_FLUSH (0x0001)
  3778. +#define CON_NFI_RST (0x0002)
  3779. +#define CON_NFI_SRD (0x0010)
  3780. +
  3781. +#define CON_NFI_NOB_MASK (0x0060)
  3782. +#define CON_NFI_NOB_SHIFT (5)
  3783. +
  3784. +#define CON_NFI_BRD (0x0100)
  3785. +#define CON_NFI_BWR (0x0200)
  3786. +
  3787. +#define CON_NFI_SEC_MASK (0xF000)
  3788. +#define CON_NFI_SEC_SHIFT (12)
  3789. +
  3790. +/* NFI_ACCCON */
  3791. +#define ACCCON_SETTING ()
  3792. +
  3793. +/* NFI_INTR_EN */
  3794. +#define INTR_RD_DONE_EN (0x0001)
  3795. +#define INTR_WR_DONE_EN (0x0002)
  3796. +#define INTR_RST_DONE_EN (0x0004)
  3797. +#define INTR_ERASE_DONE_EN (0x0008)
  3798. +#define INTR_BSY_RTN_EN (0x0010)
  3799. +#define INTR_ACC_LOCK_EN (0x0020)
  3800. +#define INTR_AHB_DONE_EN (0x0040)
  3801. +#define INTR_ALL_INTR_DE (0x0000)
  3802. +#define INTR_ALL_INTR_EN (0x007F)
  3803. +
  3804. +/* NFI_INTR */
  3805. +#define INTR_RD_DONE (0x0001)
  3806. +#define INTR_WR_DONE (0x0002)
  3807. +#define INTR_RST_DONE (0x0004)
  3808. +#define INTR_ERASE_DONE (0x0008)
  3809. +#define INTR_BSY_RTN (0x0010)
  3810. +#define INTR_ACC_LOCK (0x0020)
  3811. +#define INTR_AHB_DONE (0x0040)
  3812. +
  3813. +/* NFI_ADDRNOB */
  3814. +#define ADDR_COL_NOB_MASK (0x0003)
  3815. +#define ADDR_COL_NOB_SHIFT (0)
  3816. +#define ADDR_ROW_NOB_MASK (0x0030)
  3817. +#define ADDR_ROW_NOB_SHIFT (4)
  3818. +
  3819. +/* NFI_STA */
  3820. +#define STA_READ_EMPTY (0x00001000)
  3821. +#define STA_ACC_LOCK (0x00000010)
  3822. +#define STA_CMD_STATE (0x00000001)
  3823. +#define STA_ADDR_STATE (0x00000002)
  3824. +#define STA_DATAR_STATE (0x00000004)
  3825. +#define STA_DATAW_STATE (0x00000008)
  3826. +
  3827. +#define STA_NAND_FSM_MASK (0x1F000000)
  3828. +#define STA_NAND_BUSY (0x00000100)
  3829. +#define STA_NAND_BUSY_RETURN (0x00000200)
  3830. +#define STA_NFI_FSM_MASK (0x000F0000)
  3831. +#define STA_NFI_OP_MASK (0x0000000F)
  3832. +
  3833. +/* NFI_FIFOSTA */
  3834. +#define FIFO_RD_EMPTY (0x0040)
  3835. +#define FIFO_RD_FULL (0x0080)
  3836. +#define FIFO_WR_FULL (0x8000)
  3837. +#define FIFO_WR_EMPTY (0x4000)
  3838. +#define FIFO_RD_REMAIN(x) (0x1F&(x))
  3839. +#define FIFO_WR_REMAIN(x) ((0x1F00&(x))>>8)
  3840. +
  3841. +/* NFI_ADDRCNTR */
  3842. +#define ADDRCNTR_CNTR(x) ((0xF000&(x))>>12)
  3843. +#define ADDRCNTR_OFFSET(x) (0x03FF&(x))
  3844. +
  3845. +/* NFI_LOCK */
  3846. +#define NFI_LOCK_ON (0x0001)
  3847. +
  3848. +/* NFI_LOCKANOB */
  3849. +#define PROG_RADD_NOB_MASK (0x7000)
  3850. +#define PROG_RADD_NOB_SHIFT (12)
  3851. +#define PROG_CADD_NOB_MASK (0x0300)
  3852. +#define PROG_CADD_NOB_SHIFT (8)
  3853. +#define ERASE_RADD_NOB_MASK (0x0070)
  3854. +#define ERASE_RADD_NOB_SHIFT (4)
  3855. +#define ERASE_CADD_NOB_MASK (0x0007)
  3856. +#define ERASE_CADD_NOB_SHIFT (0)
  3857. +
  3858. +/*******************************************************************************
  3859. + * ECC Register Definition
  3860. + *******************************************************************************/
  3861. +
  3862. +#define ECC_ENCCON_REG16 ((volatile P_U16)(NFIECC_BASE+0x0000))
  3863. +#define ECC_ENCCNFG_REG32 ((volatile P_U32)(NFIECC_BASE+0x0004))
  3864. +#define ECC_ENCDIADDR_REG32 ((volatile P_U32)(NFIECC_BASE+0x0008))
  3865. +#define ECC_ENCIDLE_REG32 ((volatile P_U32)(NFIECC_BASE+0x000C))
  3866. +#define ECC_ENCPAR0_REG32 ((volatile P_U32)(NFIECC_BASE+0x0010))
  3867. +#define ECC_ENCPAR1_REG32 ((volatile P_U32)(NFIECC_BASE+0x0014))
  3868. +#define ECC_ENCPAR2_REG32 ((volatile P_U32)(NFIECC_BASE+0x0018))
  3869. +#define ECC_ENCPAR3_REG32 ((volatile P_U32)(NFIECC_BASE+0x001C))
  3870. +#define ECC_ENCPAR4_REG32 ((volatile P_U32)(NFIECC_BASE+0x0020))
  3871. +#define ECC_ENCSTA_REG32 ((volatile P_U32)(NFIECC_BASE+0x0024))
  3872. +#define ECC_ENCIRQEN_REG16 ((volatile P_U16)(NFIECC_BASE+0x0028))
  3873. +#define ECC_ENCIRQSTA_REG16 ((volatile P_U16)(NFIECC_BASE+0x002C))
  3874. +
  3875. +#define ECC_DECCON_REG16 ((volatile P_U16)(NFIECC_BASE+0x0100))
  3876. +#define ECC_DECCNFG_REG32 ((volatile P_U32)(NFIECC_BASE+0x0104))
  3877. +#define ECC_DECDIADDR_REG32 ((volatile P_U32)(NFIECC_BASE+0x0108))
  3878. +#define ECC_DECIDLE_REG16 ((volatile P_U16)(NFIECC_BASE+0x010C))
  3879. +#define ECC_DECFER_REG16 ((volatile P_U16)(NFIECC_BASE+0x0110))
  3880. +#define ECC_DECENUM_REG32 ((volatile P_U32)(NFIECC_BASE+0x0114))
  3881. +#define ECC_DECDONE_REG16 ((volatile P_U16)(NFIECC_BASE+0x0118))
  3882. +#define ECC_DECEL0_REG32 ((volatile P_U32)(NFIECC_BASE+0x011C))
  3883. +#define ECC_DECEL1_REG32 ((volatile P_U32)(NFIECC_BASE+0x0120))
  3884. +#define ECC_DECEL2_REG32 ((volatile P_U32)(NFIECC_BASE+0x0124))
  3885. +#define ECC_DECEL3_REG32 ((volatile P_U32)(NFIECC_BASE+0x0128))
  3886. +#define ECC_DECEL4_REG32 ((volatile P_U32)(NFIECC_BASE+0x012C))
  3887. +#define ECC_DECEL5_REG32 ((volatile P_U32)(NFIECC_BASE+0x0130))
  3888. +#define ECC_DECIRQEN_REG16 ((volatile P_U16)(NFIECC_BASE+0x0134))
  3889. +#define ECC_DECIRQSTA_REG16 ((volatile P_U16)(NFIECC_BASE+0x0138))
  3890. +#define ECC_FDMADDR_REG32 ((volatile P_U32)(NFIECC_BASE+0x013C))
  3891. +#define ECC_DECFSM_REG32 ((volatile P_U32)(NFIECC_BASE+0x0140))
  3892. +#define ECC_SYNSTA_REG32 ((volatile P_U32)(NFIECC_BASE+0x0144))
  3893. +#define ECC_DECNFIDI_REG32 ((volatile P_U32)(NFIECC_BASE+0x0148))
  3894. +#define ECC_SYN0_REG32 ((volatile P_U32)(NFIECC_BASE+0x014C))
  3895. +
  3896. +/*******************************************************************************
  3897. + * ECC register definition
  3898. + *******************************************************************************/
  3899. +/* ECC_ENCON */
  3900. +#define ENC_EN (0x0001)
  3901. +#define ENC_DE (0x0000)
  3902. +
  3903. +/* ECC_ENCCNFG */
  3904. +#define ECC_CNFG_ECC4 (0x0000)
  3905. +#define ECC_CNFG_ECC6 (0x0001)
  3906. +#define ECC_CNFG_ECC8 (0x0002)
  3907. +#define ECC_CNFG_ECC10 (0x0003)
  3908. +#define ECC_CNFG_ECC12 (0x0004)
  3909. +#define ECC_CNFG_ECC_MASK (0x00000007)
  3910. +
  3911. +#define ENC_CNFG_NFI (0x0010)
  3912. +#define ENC_CNFG_MODE_MASK (0x0010)
  3913. +
  3914. +#define ENC_CNFG_META6 (0x10300000)
  3915. +#define ENC_CNFG_META8 (0x10400000)
  3916. +
  3917. +#define ENC_CNFG_MSG_MASK (0x1FFF0000)
  3918. +#define ENC_CNFG_MSG_SHIFT (0x10)
  3919. +
  3920. +/* ECC_ENCIDLE */
  3921. +#define ENC_IDLE (0x0001)
  3922. +
  3923. +/* ECC_ENCSTA */
  3924. +#define STA_FSM (0x001F)
  3925. +#define STA_COUNT_PS (0xFF10)
  3926. +#define STA_COUNT_MS (0x3FFF0000)
  3927. +
  3928. +/* ECC_ENCIRQEN */
  3929. +#define ENC_IRQEN (0x0001)
  3930. +
  3931. +/* ECC_ENCIRQSTA */
  3932. +#define ENC_IRQSTA (0x0001)
  3933. +
  3934. +/* ECC_DECCON */
  3935. +#define DEC_EN (0x0001)
  3936. +#define DEC_DE (0x0000)
  3937. +
  3938. +/* ECC_ENCCNFG */
  3939. +#define DEC_CNFG_ECC4 (0x0000)
  3940. +//#define DEC_CNFG_ECC6 (0x0001)
  3941. +//#define DEC_CNFG_ECC12 (0x0002)
  3942. +#define DEC_CNFG_NFI (0x0010)
  3943. +//#define DEC_CNFG_META6 (0x10300000)
  3944. +//#define DEC_CNFG_META8 (0x10400000)
  3945. +
  3946. +#define DEC_CNFG_FER (0x01000)
  3947. +#define DEC_CNFG_EL (0x02000)
  3948. +#define DEC_CNFG_CORRECT (0x03000)
  3949. +#define DEC_CNFG_TYPE_MASK (0x03000)
  3950. +
  3951. +#define DEC_CNFG_EMPTY_EN (0x80000000)
  3952. +
  3953. +#define DEC_CNFG_CODE_MASK (0x1FFF0000)
  3954. +#define DEC_CNFG_CODE_SHIFT (0x10)
  3955. +
  3956. +/* ECC_DECIDLE */
  3957. +#define DEC_IDLE (0x0001)
  3958. +
  3959. +/* ECC_DECFER */
  3960. +#define DEC_FER0 (0x0001)
  3961. +#define DEC_FER1 (0x0002)
  3962. +#define DEC_FER2 (0x0004)
  3963. +#define DEC_FER3 (0x0008)
  3964. +#define DEC_FER4 (0x0010)
  3965. +#define DEC_FER5 (0x0020)
  3966. +#define DEC_FER6 (0x0040)
  3967. +#define DEC_FER7 (0x0080)
  3968. +
  3969. +/* ECC_DECENUM */
  3970. +#define ERR_NUM0 (0x0000000F)
  3971. +#define ERR_NUM1 (0x000000F0)
  3972. +#define ERR_NUM2 (0x00000F00)
  3973. +#define ERR_NUM3 (0x0000F000)
  3974. +#define ERR_NUM4 (0x000F0000)
  3975. +#define ERR_NUM5 (0x00F00000)
  3976. +#define ERR_NUM6 (0x0F000000)
  3977. +#define ERR_NUM7 (0xF0000000)
  3978. +
  3979. +/* ECC_DECDONE */
  3980. +#define DEC_DONE0 (0x0001)
  3981. +#define DEC_DONE1 (0x0002)
  3982. +#define DEC_DONE2 (0x0004)
  3983. +#define DEC_DONE3 (0x0008)
  3984. +#define DEC_DONE4 (0x0010)
  3985. +#define DEC_DONE5 (0x0020)
  3986. +#define DEC_DONE6 (0x0040)
  3987. +#define DEC_DONE7 (0x0080)
  3988. +
  3989. +/* ECC_DECIRQEN */
  3990. +#define DEC_IRQEN (0x0001)
  3991. +
  3992. +/* ECC_DECIRQSTA */
  3993. +#define DEC_IRQSTA (0x0001)
  3994. +
  3995. +#define CHIPVER_ECO_1 (0x8a00)
  3996. +#define CHIPVER_ECO_2 (0x8a01)
  3997. +
  3998. +//#define NAND_PFM
  3999. +
  4000. +/*******************************************************************************
  4001. + * Data Structure Definition
  4002. + *******************************************************************************/
  4003. +struct mtk_nand_host
  4004. +{
  4005. + struct nand_chip nand_chip;
  4006. + struct mtd_info *mtd;
  4007. + struct mtk_nand_host_hw *hw;
  4008. +};
  4009. +
  4010. +struct NAND_CMD
  4011. +{
  4012. + u32 u4ColAddr;
  4013. + u32 u4RowAddr;
  4014. + u32 u4OOBRowAddr;
  4015. + u8 au1OOB[288];
  4016. + u8* pDataBuf;
  4017. +#ifdef NAND_PFM
  4018. + u32 pureReadOOB;
  4019. + u32 pureReadOOBNum;
  4020. +#endif
  4021. +};
  4022. +
  4023. +/*
  4024. + * ECC layout control structure. Exported to userspace for
  4025. + * diagnosis and to allow creation of raw images
  4026. +struct nand_ecclayout {
  4027. + uint32_t eccbytes;
  4028. + uint32_t eccpos[64];
  4029. + uint32_t oobavail;
  4030. + struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES];
  4031. +};
  4032. +*/
  4033. +#define __DEBUG_NAND 1 /* Debug information on/off */
  4034. +
  4035. +/* Debug message event */
  4036. +#define DBG_EVT_NONE 0x00000000 /* No event */
  4037. +#define DBG_EVT_INIT 0x00000001 /* Initial related event */
  4038. +#define DBG_EVT_VERIFY 0x00000002 /* Verify buffer related event */
  4039. +#define DBG_EVT_PERFORMANCE 0x00000004 /* Performance related event */
  4040. +#define DBG_EVT_READ 0x00000008 /* Read related event */
  4041. +#define DBG_EVT_WRITE 0x00000010 /* Write related event */
  4042. +#define DBG_EVT_ERASE 0x00000020 /* Erase related event */
  4043. +#define DBG_EVT_BADBLOCK 0x00000040 /* Badblock related event */
  4044. +#define DBG_EVT_POWERCTL 0x00000080 /* Suspend/Resume related event */
  4045. +
  4046. +#define DBG_EVT_ALL 0xffffffff
  4047. +
  4048. +#define DBG_EVT_MASK (DBG_EVT_INIT)
  4049. +
  4050. +#if __DEBUG_NAND
  4051. +#define MSG(evt, fmt, args...) \
  4052. +do { \
  4053. + if ((DBG_EVT_##evt) & DBG_EVT_MASK) { \
  4054. + printk(fmt, ##args); \
  4055. + } \
  4056. +} while(0)
  4057. +
  4058. +#define MSG_FUNC_ENTRY(f) MSG(FUC, "<FUN_ENT>: %s\n", __FUNCTION__)
  4059. +#else
  4060. +#define MSG(evt, fmt, args...) do{}while(0)
  4061. +#define MSG_FUNC_ENTRY(f) do{}while(0)
  4062. +#endif
  4063. +
  4064. +#define RAMDOM_READ 1<<0
  4065. +#define CACHE_READ 1<<1
  4066. +
  4067. +typedef struct
  4068. +{
  4069. + u16 id; //deviceid+menuid
  4070. + u32 ext_id;
  4071. + u8 addr_cycle;
  4072. + u8 iowidth;
  4073. + u16 totalsize;
  4074. + u16 blocksize;
  4075. + u16 pagesize;
  4076. + u16 sparesize;
  4077. + u32 timmingsetting;
  4078. + char devciename[14];
  4079. + u32 advancedmode; //
  4080. +}flashdev_info,*pflashdev_info;
  4081. +
  4082. +/* NAND driver */
  4083. +#if 0
  4084. +struct mtk_nand_host_hw {
  4085. + unsigned int nfi_bus_width; /* NFI_BUS_WIDTH */
  4086. + unsigned int nfi_access_timing; /* NFI_ACCESS_TIMING */
  4087. + unsigned int nfi_cs_num; /* NFI_CS_NUM */
  4088. + unsigned int nand_sec_size; /* NAND_SECTOR_SIZE */
  4089. + unsigned int nand_sec_shift; /* NAND_SECTOR_SHIFT */
  4090. + unsigned int nand_ecc_size;
  4091. + unsigned int nand_ecc_bytes;
  4092. + unsigned int nand_ecc_mode;
  4093. +};
  4094. +extern struct mtk_nand_host_hw mt7621_nand_hw;
  4095. +extern u32 CFG_BLOCKSIZE;
  4096. +#endif
  4097. +#endif
  4098. --- a/drivers/mtd/nand/nand_base.c
  4099. +++ b/drivers/mtd/nand/nand_base.c
  4100. @@ -48,7 +48,7 @@
  4101. #include <linux/mtd/partitions.h>
  4102. #include <linux/of.h>
  4103. -static int nand_get_device(struct mtd_info *mtd, int new_state);
  4104. +int nand_get_device(struct mtd_info *mtd, int new_state);
  4105. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  4106. struct mtd_oob_ops *ops);
  4107. @@ -240,7 +240,7 @@ static int check_offs_len(struct mtd_inf
  4108. *
  4109. * Release chip lock and wake up anyone waiting on the device.
  4110. */
  4111. -static void nand_release_device(struct mtd_info *mtd)
  4112. +void nand_release_device(struct mtd_info *mtd)
  4113. {
  4114. struct nand_chip *chip = mtd_to_nand(mtd);
  4115. @@ -968,7 +968,7 @@ static void panic_nand_get_device(struct
  4116. *
  4117. * Get the device and lock it for exclusive access
  4118. */
  4119. -static int
  4120. +int
  4121. nand_get_device(struct mtd_info *mtd, int new_state)
  4122. {
  4123. struct nand_chip *chip = mtd_to_nand(mtd);
  4124. --- /dev/null
  4125. +++ b/drivers/mtd/nand/nand_def.h
  4126. @@ -0,0 +1,123 @@
  4127. +#ifndef __NAND_DEF_H__
  4128. +#define __NAND_DEF_H__
  4129. +
  4130. +#define VERSION "v2.1 Fix AHB virt2phys error"
  4131. +#define MODULE_NAME "# MTK NAND #"
  4132. +#define PROCNAME "driver/nand"
  4133. +
  4134. +#undef TESTTIME
  4135. +//#define __UBOOT_NAND__ 1
  4136. +#define __KERNEL_NAND__ 1
  4137. +//#define __PRELOADER_NAND__ 1
  4138. +//#define PMT 1
  4139. +//#define _MTK_NAND_DUMMY_DRIVER
  4140. +//#define CONFIG_BADBLOCK_CHECK 1
  4141. +//#ifdef CONFIG_BADBLOCK_CHECK
  4142. +//#define MTK_NAND_BMT 1
  4143. +//#endif
  4144. +#define ECC_ENABLE 1
  4145. +#define MANUAL_CORRECT 1
  4146. +//#define __INTERNAL_USE_AHB_MODE__ (0)
  4147. +#define SKIP_BAD_BLOCK
  4148. +#define FACT_BBT
  4149. +
  4150. +#ifndef NAND_OTP_SUPPORT
  4151. +#define NAND_OTP_SUPPORT 0
  4152. +#endif
  4153. +
  4154. +/*******************************************************************************
  4155. + * Macro definition
  4156. + *******************************************************************************/
  4157. +//#define NFI_SET_REG32(reg, value) (DRV_WriteReg32(reg, DRV_Reg32(reg) | (value)))
  4158. +//#define NFI_SET_REG16(reg, value) (DRV_WriteReg16(reg, DRV_Reg16(reg) | (value)))
  4159. +//#define NFI_CLN_REG32(reg, value) (DRV_WriteReg32(reg, DRV_Reg32(reg) & (~(value))))
  4160. +//#define NFI_CLN_REG16(reg, value) (DRV_WriteReg16(reg, DRV_Reg16(reg) & (~(value))))
  4161. +
  4162. +#if defined (__KERNEL_NAND__)
  4163. +#define NFI_SET_REG32(reg, value) \
  4164. +do { \
  4165. + g_value = (DRV_Reg32(reg) | (value));\
  4166. + DRV_WriteReg32(reg, g_value); \
  4167. +} while(0)
  4168. +
  4169. +#define NFI_SET_REG16(reg, value) \
  4170. +do { \
  4171. + g_value = (DRV_Reg16(reg) | (value));\
  4172. + DRV_WriteReg16(reg, g_value); \
  4173. +} while(0)
  4174. +
  4175. +#define NFI_CLN_REG32(reg, value) \
  4176. +do { \
  4177. + g_value = (DRV_Reg32(reg) & (~(value)));\
  4178. + DRV_WriteReg32(reg, g_value); \
  4179. +} while(0)
  4180. +
  4181. +#define NFI_CLN_REG16(reg, value) \
  4182. +do { \
  4183. + g_value = (DRV_Reg16(reg) & (~(value)));\
  4184. + DRV_WriteReg16(reg, g_value); \
  4185. +} while(0)
  4186. +#endif
  4187. +
  4188. +#define NFI_WAIT_STATE_DONE(state) do{;}while (__raw_readl(NFI_STA_REG32) & state)
  4189. +#define NFI_WAIT_TO_READY() do{;}while (!(__raw_readl(NFI_STA_REG32) & STA_BUSY2READY))
  4190. +
  4191. +
  4192. +#define NAND_SECTOR_SIZE (512)
  4193. +#define OOB_PER_SECTOR (16)
  4194. +#define OOB_AVAI_PER_SECTOR (8)
  4195. +
  4196. +#ifndef PART_SIZE_BMTPOOL
  4197. +#define BMT_POOL_SIZE (80)
  4198. +#else
  4199. +#define BMT_POOL_SIZE (PART_SIZE_BMTPOOL)
  4200. +#endif
  4201. +
  4202. +#define PMT_POOL_SIZE (2)
  4203. +
  4204. +#define TIMEOUT_1 0x1fff
  4205. +#define TIMEOUT_2 0x8ff
  4206. +#define TIMEOUT_3 0xffff
  4207. +#define TIMEOUT_4 0xffff//5000 //PIO
  4208. +
  4209. +
  4210. +/* temporarity definiation */
  4211. +#if !defined (__KERNEL_NAND__)
  4212. +#define KERN_INFO
  4213. +#define KERN_WARNING
  4214. +#define KERN_ERR
  4215. +#define PAGE_SIZE (4096)
  4216. +#endif
  4217. +#define AddStorageTrace //AddStorageTrace
  4218. +#define STORAGE_LOGGER_MSG_NAND 0
  4219. +#define NFI_BASE RALINK_NAND_CTRL_BASE
  4220. +#define NFIECC_BASE RALINK_NANDECC_CTRL_BASE
  4221. +
  4222. +#ifdef __INTERNAL_USE_AHB_MODE__
  4223. +#define MT65xx_POLARITY_LOW 0
  4224. +#define MT65XX_PDN_PERI_NFI 0
  4225. +#define MT65xx_EDGE_SENSITIVE 0
  4226. +#define MT6575_NFI_IRQ_ID (58)
  4227. +#endif
  4228. +
  4229. +#if defined (__KERNEL_NAND__)
  4230. +#define RALINK_REG(x) (*((volatile u32 *)(x)))
  4231. +#define __virt_to_phys(x) virt_to_phys((volatile void*)x)
  4232. +#else
  4233. +#define CONFIG_MTD_NAND_VERIFY_WRITE (1)
  4234. +#define printk printf
  4235. +#define ra_dbg printf
  4236. +#define BUG() //BUG()
  4237. +#define BUG_ON(x) //BUG_ON()
  4238. +#define NUM_PARTITIONS 1
  4239. +#endif
  4240. +
  4241. +#define NFI_DEFAULT_ACCESS_TIMING (0x30C77fff) //(0x44333)
  4242. +
  4243. +//uboot only support 1 cs
  4244. +#define NFI_CS_NUM (1)
  4245. +#define NFI_DEFAULT_CS (0)
  4246. +
  4247. +#include "mt6575_typedefs.h"
  4248. +
  4249. +#endif /* __NAND_DEF_H__ */
  4250. --- /dev/null
  4251. +++ b/drivers/mtd/nand/nand_device_list.h
  4252. @@ -0,0 +1,59 @@
  4253. +/* Copyright Statement:
  4254. + *
  4255. + * This software/firmware and related documentation ("MediaTek Software") are
  4256. + * protected under relevant copyright laws. The information contained herein
  4257. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  4258. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  4259. + * any reproduction, modification, use or disclosure of MediaTek Software,
  4260. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  4261. + */
  4262. +/* MediaTek Inc. (C) 2010. All rights reserved.
  4263. + *
  4264. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  4265. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  4266. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  4267. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  4268. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  4269. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  4270. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  4271. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  4272. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  4273. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  4274. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  4275. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  4276. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  4277. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  4278. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  4279. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  4280. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  4281. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  4282. + *
  4283. + * The following software/firmware and/or related documentation ("MediaTek Software")
  4284. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  4285. + * applicable license agreements with MediaTek Inc.
  4286. + */
  4287. +
  4288. +#ifndef __NAND_DEVICE_LIST_H__
  4289. +#define __NAND_DEVICE_LIST_H__
  4290. +
  4291. +static const flashdev_info gen_FlashTable[]={
  4292. + {0x20BC, 0x105554, 5, 16, 512, 128, 2048, 64, 0x1123, "EHD013151MA_5", 0},
  4293. + {0xECBC, 0x005554, 5, 16, 512, 128, 2048, 64, 0x1123, "K524G2GACB_A0", 0},
  4294. + {0x2CBC, 0x905556, 5, 16, 512, 128, 2048, 64, 0x21044333, "MT29C4G96MAZA", 0},
  4295. + {0x2CDA, 0x909506, 5, 8, 256, 128, 2048, 64, 0x30C77fff, "MT29F2G08ABAE", 0},
  4296. + {0xADBC, 0x905554, 5, 16, 512, 128, 2048, 64, 0x10801011, "H9DA4GH4JJAMC", 0},
  4297. + {0x01F1, 0x801D01, 4, 8, 128, 128, 2048, 64, 0x30C77fff, "S34ML01G100TF", 0},
  4298. + {0x92F1, 0x8095FF, 4, 8, 128, 128, 2048, 64, 0x30C77fff, "F59L1G81A", 0},
  4299. + {0xC8D1, 0x809540, 4, 8, 128, 128, 2048, 64, 0x30C77fff, "F59L1G81MA", 0},
  4300. + {0xC8DA, 0x909544, 5, 8, 256, 128, 2048, 64, 0x30C77fff, "F59L2G81A", 0},
  4301. + {0xC8DC, 0x909554, 5, 8, 512, 128, 2048, 64, 0x30C77fff, "F59L4G81A", 0},
  4302. + {0xECD3, 0x519558, 5, 8, 1024, 128, 2048, 64, 0x44333, "K9K8G8000", 0},
  4303. + {0xC2F1, 0x801DC2, 4, 8, 128, 128, 2048, 64, 0x30C77fff, "MX30LF1G08AA", 0},
  4304. + {0x98D3, 0x902676, 5, 8, 1024, 256, 4096, 224, 0x00C25332, "TC58NVG3S0F", 0},
  4305. + {0x01DA, 0x909546, 5, 8, 256, 128, 2048, 128, 0x30C77fff, "S34ML02G200TF", 0},
  4306. + {0x01DC, 0x909556, 5, 8, 512, 128, 2048, 128, 0x30C77fff, "S34ML04G200TF", 0},
  4307. + {0x0000, 0x000000, 0, 0, 0, 0, 0, 0, 0, "xxxxxxxxxx", 0},
  4308. +};
  4309. +
  4310. +
  4311. +#endif
  4312. --- /dev/null
  4313. +++ b/drivers/mtd/nand/partition.h
  4314. @@ -0,0 +1,115 @@
  4315. +/* Copyright Statement:
  4316. + *
  4317. + * This software/firmware and related documentation ("MediaTek Software") are
  4318. + * protected under relevant copyright laws. The information contained herein
  4319. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  4320. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  4321. + * any reproduction, modification, use or disclosure of MediaTek Software,
  4322. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  4323. + */
  4324. +/* MediaTek Inc. (C) 2010. All rights reserved.
  4325. + *
  4326. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  4327. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  4328. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  4329. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  4330. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  4331. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  4332. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  4333. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  4334. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  4335. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  4336. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  4337. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  4338. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  4339. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  4340. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  4341. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  4342. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  4343. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  4344. + *
  4345. + * The following software/firmware and/or related documentation ("MediaTek Software")
  4346. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  4347. + * applicable license agreements with MediaTek Inc.
  4348. + */
  4349. +
  4350. +#include <linux/mtd/mtd.h>
  4351. +#include <linux/mtd/rawnand.h>
  4352. +#include <linux/mtd/partitions.h>
  4353. +
  4354. +#define RECONFIG_PARTITION_SIZE 1
  4355. +
  4356. +#define MTD_BOOT_PART_SIZE 0x80000
  4357. +#define MTD_CONFIG_PART_SIZE 0x20000
  4358. +#define MTD_FACTORY_PART_SIZE 0x20000
  4359. +
  4360. +extern unsigned int CFG_BLOCKSIZE;
  4361. +#define LARGE_MTD_BOOT_PART_SIZE (CFG_BLOCKSIZE<<2)
  4362. +#define LARGE_MTD_CONFIG_PART_SIZE (CFG_BLOCKSIZE<<2)
  4363. +#define LARGE_MTD_FACTORY_PART_SIZE (CFG_BLOCKSIZE<<1)
  4364. +
  4365. +/*=======================================================================*/
  4366. +/* NAND PARTITION Mapping */
  4367. +/*=======================================================================*/
  4368. +//#ifdef CONFIG_MTD_PARTITIONS
  4369. +static struct mtd_partition g_pasStatic_Partition[] = {
  4370. + {
  4371. + name: "ALL",
  4372. + size: MTDPART_SIZ_FULL,
  4373. + offset: 0,
  4374. + },
  4375. + /* Put your own partition definitions here */
  4376. + {
  4377. + name: "Bootloader",
  4378. + size: MTD_BOOT_PART_SIZE,
  4379. + offset: 0,
  4380. + }, {
  4381. + name: "Config",
  4382. + size: MTD_CONFIG_PART_SIZE,
  4383. + offset: MTDPART_OFS_APPEND
  4384. + }, {
  4385. + name: "Factory",
  4386. + size: MTD_FACTORY_PART_SIZE,
  4387. + offset: MTDPART_OFS_APPEND
  4388. +#ifdef CONFIG_RT2880_ROOTFS_IN_FLASH
  4389. + }, {
  4390. + name: "Kernel",
  4391. + size: MTD_KERN_PART_SIZE,
  4392. + offset: MTDPART_OFS_APPEND,
  4393. + }, {
  4394. + name: "RootFS",
  4395. + size: MTD_ROOTFS_PART_SIZE,
  4396. + offset: MTDPART_OFS_APPEND,
  4397. +#ifdef CONFIG_ROOTFS_IN_FLASH_NO_PADDING
  4398. + }, {
  4399. + name: "Kernel_RootFS",
  4400. + size: MTD_KERN_PART_SIZE + MTD_ROOTFS_PART_SIZE,
  4401. + offset: MTD_BOOT_PART_SIZE + MTD_CONFIG_PART_SIZE + MTD_FACTORY_PART_SIZE,
  4402. +#endif
  4403. +#else //CONFIG_RT2880_ROOTFS_IN_RAM
  4404. + }, {
  4405. + name: "Kernel",
  4406. + size: 0x10000,
  4407. + offset: MTDPART_OFS_APPEND,
  4408. +#endif
  4409. +#ifdef CONFIG_DUAL_IMAGE
  4410. + }, {
  4411. + name: "Kernel2",
  4412. + size: MTD_KERN2_PART_SIZE,
  4413. + offset: MTD_KERN2_PART_OFFSET,
  4414. +#ifdef CONFIG_RT2880_ROOTFS_IN_FLASH
  4415. + }, {
  4416. + name: "RootFS2",
  4417. + size: MTD_ROOTFS2_PART_SIZE,
  4418. + offset: MTD_ROOTFS2_PART_OFFSET,
  4419. +#endif
  4420. +#endif
  4421. + }
  4422. +
  4423. +};
  4424. +
  4425. +#define NUM_PARTITIONS ARRAY_SIZE(g_pasStatic_Partition)
  4426. +extern int part_num; // = NUM_PARTITIONS;
  4427. +//#endif
  4428. +#undef RECONFIG_PARTITION_SIZE
  4429. +