onenand_base.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright © 2005-2009 Samsung Electronics
  5. * Copyright © 2007 Nokia Corporation
  6. *
  7. * Kyungmin Park <kyungmin.park@samsung.com>
  8. *
  9. * Credits:
  10. * Adrian Hunter <ext-adrian.hunter@nokia.com>:
  11. * auto-placement support, read-while load support, various fixes
  12. *
  13. * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
  14. * Flex-OneNAND support
  15. * Amul Kumar Saha <amul.saha at samsung.com>
  16. * OTP support
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/onenand.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <asm/io.h>
  35. /*
  36. * Multiblock erase if number of blocks to erase is 2 or more.
  37. * Maximum number of blocks for simultaneous erase is 64.
  38. */
  39. #define MB_ERASE_MIN_BLK_COUNT 2
  40. #define MB_ERASE_MAX_BLK_COUNT 64
  41. /* Default Flex-OneNAND boundary and lock respectively */
  42. static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
  43. module_param_array(flex_bdry, int, NULL, 0400);
  44. MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
  45. "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
  46. "DIE_BDRY: SLC boundary of the die"
  47. "LOCK: Locking information for SLC boundary"
  48. " : 0->Set boundary in unlocked status"
  49. " : 1->Set boundary in locked status");
  50. /* Default OneNAND/Flex-OneNAND OTP options*/
  51. static int otp;
  52. module_param(otp, int, 0400);
  53. MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
  54. "Syntax : otp=LOCK_TYPE"
  55. "LOCK_TYPE : Keys issued, for specific OTP Lock type"
  56. " : 0 -> Default (No Blocks Locked)"
  57. " : 1 -> OTP Block lock"
  58. " : 2 -> 1st Block lock"
  59. " : 3 -> BOTH OTP Block and 1st Block lock");
  60. /*
  61. * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
  62. * For now, we expose only 64 out of 80 ecc bytes
  63. */
  64. static struct nand_ecclayout flexonenand_oob_128 = {
  65. .eccbytes = 64,
  66. .eccpos = {
  67. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  68. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  69. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  70. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  71. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  72. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  73. 102, 103, 104, 105
  74. },
  75. .oobfree = {
  76. {2, 4}, {18, 4}, {34, 4}, {50, 4},
  77. {66, 4}, {82, 4}, {98, 4}, {114, 4}
  78. }
  79. };
  80. /*
  81. * onenand_oob_128 - oob info for OneNAND with 4KB page
  82. *
  83. * Based on specification:
  84. * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
  85. *
  86. * For eccpos we expose only 64 bytes out of 72 (see struct nand_ecclayout)
  87. *
  88. * oobfree uses the spare area fields marked as
  89. * "Managed by internal ECC logic for Logical Sector Number area"
  90. */
  91. static struct nand_ecclayout onenand_oob_128 = {
  92. .eccbytes = 64,
  93. .eccpos = {
  94. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  95. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  96. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  97. 55, 56, 57, 58, 59, 60, 61, 62, 63,
  98. 71, 72, 73, 74, 75, 76, 77, 78, 79,
  99. 87, 88, 89, 90, 91, 92, 93, 94, 95,
  100. 103, 104, 105, 106, 107, 108, 109, 110, 111,
  101. 119
  102. },
  103. .oobfree = {
  104. {2, 3}, {18, 3}, {34, 3}, {50, 3},
  105. {66, 3}, {82, 3}, {98, 3}, {114, 3}
  106. }
  107. };
  108. /**
  109. * onenand_oob_64 - oob info for large (2KB) page
  110. */
  111. static struct nand_ecclayout onenand_oob_64 = {
  112. .eccbytes = 20,
  113. .eccpos = {
  114. 8, 9, 10, 11, 12,
  115. 24, 25, 26, 27, 28,
  116. 40, 41, 42, 43, 44,
  117. 56, 57, 58, 59, 60,
  118. },
  119. .oobfree = {
  120. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  121. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  122. }
  123. };
  124. /**
  125. * onenand_oob_32 - oob info for middle (1KB) page
  126. */
  127. static struct nand_ecclayout onenand_oob_32 = {
  128. .eccbytes = 10,
  129. .eccpos = {
  130. 8, 9, 10, 11, 12,
  131. 24, 25, 26, 27, 28,
  132. },
  133. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  134. };
  135. static const unsigned char ffchars[] = {
  136. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  137. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  138. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  139. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  140. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  141. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  142. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  143. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  144. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  145. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
  146. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  147. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
  148. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  149. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
  150. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  151. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
  152. };
  153. /**
  154. * onenand_readw - [OneNAND Interface] Read OneNAND register
  155. * @param addr address to read
  156. *
  157. * Read OneNAND register
  158. */
  159. static unsigned short onenand_readw(void __iomem *addr)
  160. {
  161. return readw(addr);
  162. }
  163. /**
  164. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  165. * @param value value to write
  166. * @param addr address to write
  167. *
  168. * Write OneNAND register with value
  169. */
  170. static void onenand_writew(unsigned short value, void __iomem *addr)
  171. {
  172. writew(value, addr);
  173. }
  174. /**
  175. * onenand_block_address - [DEFAULT] Get block address
  176. * @param this onenand chip data structure
  177. * @param block the block
  178. * @return translated block address if DDP, otherwise same
  179. *
  180. * Setup Start Address 1 Register (F100h)
  181. */
  182. static int onenand_block_address(struct onenand_chip *this, int block)
  183. {
  184. /* Device Flash Core select, NAND Flash Block Address */
  185. if (block & this->density_mask)
  186. return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
  187. return block;
  188. }
  189. /**
  190. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  191. * @param this onenand chip data structure
  192. * @param block the block
  193. * @return set DBS value if DDP, otherwise 0
  194. *
  195. * Setup Start Address 2 Register (F101h) for DDP
  196. */
  197. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  198. {
  199. /* Device BufferRAM Select */
  200. if (block & this->density_mask)
  201. return ONENAND_DDP_CHIP1;
  202. return ONENAND_DDP_CHIP0;
  203. }
  204. /**
  205. * onenand_page_address - [DEFAULT] Get page address
  206. * @param page the page address
  207. * @param sector the sector address
  208. * @return combined page and sector address
  209. *
  210. * Setup Start Address 8 Register (F107h)
  211. */
  212. static int onenand_page_address(int page, int sector)
  213. {
  214. /* Flash Page Address, Flash Sector Address */
  215. int fpa, fsa;
  216. fpa = page & ONENAND_FPA_MASK;
  217. fsa = sector & ONENAND_FSA_MASK;
  218. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  219. }
  220. /**
  221. * onenand_buffer_address - [DEFAULT] Get buffer address
  222. * @param dataram1 DataRAM index
  223. * @param sectors the sector address
  224. * @param count the number of sectors
  225. * @return the start buffer value
  226. *
  227. * Setup Start Buffer Register (F200h)
  228. */
  229. static int onenand_buffer_address(int dataram1, int sectors, int count)
  230. {
  231. int bsa, bsc;
  232. /* BufferRAM Sector Address */
  233. bsa = sectors & ONENAND_BSA_MASK;
  234. if (dataram1)
  235. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  236. else
  237. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  238. /* BufferRAM Sector Count */
  239. bsc = count & ONENAND_BSC_MASK;
  240. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  241. }
  242. /**
  243. * flexonenand_block- For given address return block number
  244. * @param this - OneNAND device structure
  245. * @param addr - Address for which block number is needed
  246. */
  247. static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
  248. {
  249. unsigned boundary, blk, die = 0;
  250. if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
  251. die = 1;
  252. addr -= this->diesize[0];
  253. }
  254. boundary = this->boundary[die];
  255. blk = addr >> (this->erase_shift - 1);
  256. if (blk > boundary)
  257. blk = (blk + boundary + 1) >> 1;
  258. blk += die ? this->density_mask : 0;
  259. return blk;
  260. }
  261. inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
  262. {
  263. if (!FLEXONENAND(this))
  264. return addr >> this->erase_shift;
  265. return flexonenand_block(this, addr);
  266. }
  267. /**
  268. * flexonenand_addr - Return address of the block
  269. * @this: OneNAND device structure
  270. * @block: Block number on Flex-OneNAND
  271. *
  272. * Return address of the block
  273. */
  274. static loff_t flexonenand_addr(struct onenand_chip *this, int block)
  275. {
  276. loff_t ofs = 0;
  277. int die = 0, boundary;
  278. if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
  279. block -= this->density_mask;
  280. die = 1;
  281. ofs = this->diesize[0];
  282. }
  283. boundary = this->boundary[die];
  284. ofs += (loff_t)block << (this->erase_shift - 1);
  285. if (block > (boundary + 1))
  286. ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
  287. return ofs;
  288. }
  289. loff_t onenand_addr(struct onenand_chip *this, int block)
  290. {
  291. if (!FLEXONENAND(this))
  292. return (loff_t)block << this->erase_shift;
  293. return flexonenand_addr(this, block);
  294. }
  295. EXPORT_SYMBOL(onenand_addr);
  296. /**
  297. * onenand_get_density - [DEFAULT] Get OneNAND density
  298. * @param dev_id OneNAND device ID
  299. *
  300. * Get OneNAND density from device ID
  301. */
  302. static inline int onenand_get_density(int dev_id)
  303. {
  304. int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  305. return (density & ONENAND_DEVICE_DENSITY_MASK);
  306. }
  307. /**
  308. * flexonenand_region - [Flex-OneNAND] Return erase region of addr
  309. * @param mtd MTD device structure
  310. * @param addr address whose erase region needs to be identified
  311. */
  312. int flexonenand_region(struct mtd_info *mtd, loff_t addr)
  313. {
  314. int i;
  315. for (i = 0; i < mtd->numeraseregions; i++)
  316. if (addr < mtd->eraseregions[i].offset)
  317. break;
  318. return i - 1;
  319. }
  320. EXPORT_SYMBOL(flexonenand_region);
  321. /**
  322. * onenand_command - [DEFAULT] Send command to OneNAND device
  323. * @param mtd MTD device structure
  324. * @param cmd the command to be sent
  325. * @param addr offset to read from or write to
  326. * @param len number of bytes to read or write
  327. *
  328. * Send command to OneNAND device. This function is used for middle/large page
  329. * devices (1KB/2KB Bytes per page)
  330. */
  331. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  332. {
  333. struct onenand_chip *this = mtd->priv;
  334. int value, block, page;
  335. /* Address translation */
  336. switch (cmd) {
  337. case ONENAND_CMD_UNLOCK:
  338. case ONENAND_CMD_LOCK:
  339. case ONENAND_CMD_LOCK_TIGHT:
  340. case ONENAND_CMD_UNLOCK_ALL:
  341. block = -1;
  342. page = -1;
  343. break;
  344. case FLEXONENAND_CMD_PI_ACCESS:
  345. /* addr contains die index */
  346. block = addr * this->density_mask;
  347. page = -1;
  348. break;
  349. case ONENAND_CMD_ERASE:
  350. case ONENAND_CMD_MULTIBLOCK_ERASE:
  351. case ONENAND_CMD_ERASE_VERIFY:
  352. case ONENAND_CMD_BUFFERRAM:
  353. case ONENAND_CMD_OTP_ACCESS:
  354. block = onenand_block(this, addr);
  355. page = -1;
  356. break;
  357. case FLEXONENAND_CMD_READ_PI:
  358. cmd = ONENAND_CMD_READ;
  359. block = addr * this->density_mask;
  360. page = 0;
  361. break;
  362. default:
  363. block = onenand_block(this, addr);
  364. if (FLEXONENAND(this))
  365. page = (int) (addr - onenand_addr(this, block))>>\
  366. this->page_shift;
  367. else
  368. page = (int) (addr >> this->page_shift);
  369. if (ONENAND_IS_2PLANE(this)) {
  370. /* Make the even block number */
  371. block &= ~1;
  372. /* Is it the odd plane? */
  373. if (addr & this->writesize)
  374. block++;
  375. page >>= 1;
  376. }
  377. page &= this->page_mask;
  378. break;
  379. }
  380. /* NOTE: The setting order of the registers is very important! */
  381. if (cmd == ONENAND_CMD_BUFFERRAM) {
  382. /* Select DataRAM for DDP */
  383. value = onenand_bufferram_address(this, block);
  384. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  385. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
  386. /* It is always BufferRAM0 */
  387. ONENAND_SET_BUFFERRAM0(this);
  388. else
  389. /* Switch to the next data buffer */
  390. ONENAND_SET_NEXT_BUFFERRAM(this);
  391. return 0;
  392. }
  393. if (block != -1) {
  394. /* Write 'DFS, FBA' of Flash */
  395. value = onenand_block_address(this, block);
  396. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  397. /* Select DataRAM for DDP */
  398. value = onenand_bufferram_address(this, block);
  399. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  400. }
  401. if (page != -1) {
  402. /* Now we use page size operation */
  403. int sectors = 0, count = 0;
  404. int dataram;
  405. switch (cmd) {
  406. case FLEXONENAND_CMD_RECOVER_LSB:
  407. case ONENAND_CMD_READ:
  408. case ONENAND_CMD_READOOB:
  409. if (ONENAND_IS_4KB_PAGE(this))
  410. /* It is always BufferRAM0 */
  411. dataram = ONENAND_SET_BUFFERRAM0(this);
  412. else
  413. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  414. break;
  415. default:
  416. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  417. cmd = ONENAND_CMD_2X_PROG;
  418. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  419. break;
  420. }
  421. /* Write 'FPA, FSA' of Flash */
  422. value = onenand_page_address(page, sectors);
  423. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  424. /* Write 'BSA, BSC' of DataRAM */
  425. value = onenand_buffer_address(dataram, sectors, count);
  426. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  427. }
  428. /* Interrupt clear */
  429. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  430. /* Write command */
  431. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  432. return 0;
  433. }
  434. /**
  435. * onenand_read_ecc - return ecc status
  436. * @param this onenand chip structure
  437. */
  438. static inline int onenand_read_ecc(struct onenand_chip *this)
  439. {
  440. int ecc, i, result = 0;
  441. if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
  442. return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  443. for (i = 0; i < 4; i++) {
  444. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
  445. if (likely(!ecc))
  446. continue;
  447. if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
  448. return ONENAND_ECC_2BIT_ALL;
  449. else
  450. result = ONENAND_ECC_1BIT_ALL;
  451. }
  452. return result;
  453. }
  454. /**
  455. * onenand_wait - [DEFAULT] wait until the command is done
  456. * @param mtd MTD device structure
  457. * @param state state to select the max. timeout value
  458. *
  459. * Wait for command done. This applies to all OneNAND command
  460. * Read can take up to 30us, erase up to 2ms and program up to 350us
  461. * according to general OneNAND specs
  462. */
  463. static int onenand_wait(struct mtd_info *mtd, int state)
  464. {
  465. struct onenand_chip * this = mtd->priv;
  466. unsigned long timeout;
  467. unsigned int flags = ONENAND_INT_MASTER;
  468. unsigned int interrupt = 0;
  469. unsigned int ctrl;
  470. /* The 20 msec is enough */
  471. timeout = jiffies + msecs_to_jiffies(20);
  472. while (time_before(jiffies, timeout)) {
  473. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  474. if (interrupt & flags)
  475. break;
  476. if (state != FL_READING && state != FL_PREPARING_ERASE)
  477. cond_resched();
  478. }
  479. /* To get correct interrupt status in timeout case */
  480. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  481. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  482. /*
  483. * In the Spec. it checks the controller status first
  484. * However if you get the correct information in case of
  485. * power off recovery (POR) test, it should read ECC status first
  486. */
  487. if (interrupt & ONENAND_INT_READ) {
  488. int ecc = onenand_read_ecc(this);
  489. if (ecc) {
  490. if (ecc & ONENAND_ECC_2BIT_ALL) {
  491. printk(KERN_ERR "%s: ECC error = 0x%04x\n",
  492. __func__, ecc);
  493. mtd->ecc_stats.failed++;
  494. return -EBADMSG;
  495. } else if (ecc & ONENAND_ECC_1BIT_ALL) {
  496. printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
  497. __func__, ecc);
  498. mtd->ecc_stats.corrected++;
  499. }
  500. }
  501. } else if (state == FL_READING) {
  502. printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
  503. __func__, ctrl, interrupt);
  504. return -EIO;
  505. }
  506. if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
  507. printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
  508. __func__, ctrl, interrupt);
  509. return -EIO;
  510. }
  511. if (!(interrupt & ONENAND_INT_MASTER)) {
  512. printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
  513. __func__, ctrl, interrupt);
  514. return -EIO;
  515. }
  516. /* If there's controller error, it's a real error */
  517. if (ctrl & ONENAND_CTRL_ERROR) {
  518. printk(KERN_ERR "%s: controller error = 0x%04x\n",
  519. __func__, ctrl);
  520. if (ctrl & ONENAND_CTRL_LOCK)
  521. printk(KERN_ERR "%s: it's locked error.\n", __func__);
  522. return -EIO;
  523. }
  524. return 0;
  525. }
  526. /*
  527. * onenand_interrupt - [DEFAULT] onenand interrupt handler
  528. * @param irq onenand interrupt number
  529. * @param dev_id interrupt data
  530. *
  531. * complete the work
  532. */
  533. static irqreturn_t onenand_interrupt(int irq, void *data)
  534. {
  535. struct onenand_chip *this = data;
  536. /* To handle shared interrupt */
  537. if (!this->complete.done)
  538. complete(&this->complete);
  539. return IRQ_HANDLED;
  540. }
  541. /*
  542. * onenand_interrupt_wait - [DEFAULT] wait until the command is done
  543. * @param mtd MTD device structure
  544. * @param state state to select the max. timeout value
  545. *
  546. * Wait for command done.
  547. */
  548. static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
  549. {
  550. struct onenand_chip *this = mtd->priv;
  551. wait_for_completion(&this->complete);
  552. return onenand_wait(mtd, state);
  553. }
  554. /*
  555. * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
  556. * @param mtd MTD device structure
  557. * @param state state to select the max. timeout value
  558. *
  559. * Try interrupt based wait (It is used one-time)
  560. */
  561. static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
  562. {
  563. struct onenand_chip *this = mtd->priv;
  564. unsigned long remain, timeout;
  565. /* We use interrupt wait first */
  566. this->wait = onenand_interrupt_wait;
  567. timeout = msecs_to_jiffies(100);
  568. remain = wait_for_completion_timeout(&this->complete, timeout);
  569. if (!remain) {
  570. printk(KERN_INFO "OneNAND: There's no interrupt. "
  571. "We use the normal wait\n");
  572. /* Release the irq */
  573. free_irq(this->irq, this);
  574. this->wait = onenand_wait;
  575. }
  576. return onenand_wait(mtd, state);
  577. }
  578. /*
  579. * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
  580. * @param mtd MTD device structure
  581. *
  582. * There's two method to wait onenand work
  583. * 1. polling - read interrupt status register
  584. * 2. interrupt - use the kernel interrupt method
  585. */
  586. static void onenand_setup_wait(struct mtd_info *mtd)
  587. {
  588. struct onenand_chip *this = mtd->priv;
  589. int syscfg;
  590. init_completion(&this->complete);
  591. if (this->irq <= 0) {
  592. this->wait = onenand_wait;
  593. return;
  594. }
  595. if (request_irq(this->irq, &onenand_interrupt,
  596. IRQF_SHARED, "onenand", this)) {
  597. /* If we can't get irq, use the normal wait */
  598. this->wait = onenand_wait;
  599. return;
  600. }
  601. /* Enable interrupt */
  602. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  603. syscfg |= ONENAND_SYS_CFG1_IOBE;
  604. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  605. this->wait = onenand_try_interrupt_wait;
  606. }
  607. /**
  608. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  609. * @param mtd MTD data structure
  610. * @param area BufferRAM area
  611. * @return offset given area
  612. *
  613. * Return BufferRAM offset given area
  614. */
  615. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  616. {
  617. struct onenand_chip *this = mtd->priv;
  618. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  619. /* Note: the 'this->writesize' is a real page size */
  620. if (area == ONENAND_DATARAM)
  621. return this->writesize;
  622. if (area == ONENAND_SPARERAM)
  623. return mtd->oobsize;
  624. }
  625. return 0;
  626. }
  627. /**
  628. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  629. * @param mtd MTD data structure
  630. * @param area BufferRAM area
  631. * @param buffer the databuffer to put/get data
  632. * @param offset offset to read from or write to
  633. * @param count number of bytes to read/write
  634. *
  635. * Read the BufferRAM area
  636. */
  637. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  638. unsigned char *buffer, int offset, size_t count)
  639. {
  640. struct onenand_chip *this = mtd->priv;
  641. void __iomem *bufferram;
  642. bufferram = this->base + area;
  643. bufferram += onenand_bufferram_offset(mtd, area);
  644. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  645. unsigned short word;
  646. /* Align with word(16-bit) size */
  647. count--;
  648. /* Read word and save byte */
  649. word = this->read_word(bufferram + offset + count);
  650. buffer[count] = (word & 0xff);
  651. }
  652. memcpy(buffer, bufferram + offset, count);
  653. return 0;
  654. }
  655. /**
  656. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  657. * @param mtd MTD data structure
  658. * @param area BufferRAM area
  659. * @param buffer the databuffer to put/get data
  660. * @param offset offset to read from or write to
  661. * @param count number of bytes to read/write
  662. *
  663. * Read the BufferRAM area with Sync. Burst Mode
  664. */
  665. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  666. unsigned char *buffer, int offset, size_t count)
  667. {
  668. struct onenand_chip *this = mtd->priv;
  669. void __iomem *bufferram;
  670. bufferram = this->base + area;
  671. bufferram += onenand_bufferram_offset(mtd, area);
  672. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  673. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  674. unsigned short word;
  675. /* Align with word(16-bit) size */
  676. count--;
  677. /* Read word and save byte */
  678. word = this->read_word(bufferram + offset + count);
  679. buffer[count] = (word & 0xff);
  680. }
  681. memcpy(buffer, bufferram + offset, count);
  682. this->mmcontrol(mtd, 0);
  683. return 0;
  684. }
  685. /**
  686. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  687. * @param mtd MTD data structure
  688. * @param area BufferRAM area
  689. * @param buffer the databuffer to put/get data
  690. * @param offset offset to read from or write to
  691. * @param count number of bytes to read/write
  692. *
  693. * Write the BufferRAM area
  694. */
  695. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  696. const unsigned char *buffer, int offset, size_t count)
  697. {
  698. struct onenand_chip *this = mtd->priv;
  699. void __iomem *bufferram;
  700. bufferram = this->base + area;
  701. bufferram += onenand_bufferram_offset(mtd, area);
  702. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  703. unsigned short word;
  704. int byte_offset;
  705. /* Align with word(16-bit) size */
  706. count--;
  707. /* Calculate byte access offset */
  708. byte_offset = offset + count;
  709. /* Read word and save byte */
  710. word = this->read_word(bufferram + byte_offset);
  711. word = (word & ~0xff) | buffer[count];
  712. this->write_word(word, bufferram + byte_offset);
  713. }
  714. memcpy(bufferram + offset, buffer, count);
  715. return 0;
  716. }
  717. /**
  718. * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
  719. * @param mtd MTD data structure
  720. * @param addr address to check
  721. * @return blockpage address
  722. *
  723. * Get blockpage address at 2x program mode
  724. */
  725. static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
  726. {
  727. struct onenand_chip *this = mtd->priv;
  728. int blockpage, block, page;
  729. /* Calculate the even block number */
  730. block = (int) (addr >> this->erase_shift) & ~1;
  731. /* Is it the odd plane? */
  732. if (addr & this->writesize)
  733. block++;
  734. page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
  735. blockpage = (block << 7) | page;
  736. return blockpage;
  737. }
  738. /**
  739. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  740. * @param mtd MTD data structure
  741. * @param addr address to check
  742. * @return 1 if there are valid data, otherwise 0
  743. *
  744. * Check bufferram if there is data we required
  745. */
  746. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  747. {
  748. struct onenand_chip *this = mtd->priv;
  749. int blockpage, found = 0;
  750. unsigned int i;
  751. if (ONENAND_IS_2PLANE(this))
  752. blockpage = onenand_get_2x_blockpage(mtd, addr);
  753. else
  754. blockpage = (int) (addr >> this->page_shift);
  755. /* Is there valid data? */
  756. i = ONENAND_CURRENT_BUFFERRAM(this);
  757. if (this->bufferram[i].blockpage == blockpage)
  758. found = 1;
  759. else {
  760. /* Check another BufferRAM */
  761. i = ONENAND_NEXT_BUFFERRAM(this);
  762. if (this->bufferram[i].blockpage == blockpage) {
  763. ONENAND_SET_NEXT_BUFFERRAM(this);
  764. found = 1;
  765. }
  766. }
  767. if (found && ONENAND_IS_DDP(this)) {
  768. /* Select DataRAM for DDP */
  769. int block = onenand_block(this, addr);
  770. int value = onenand_bufferram_address(this, block);
  771. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  772. }
  773. return found;
  774. }
  775. /**
  776. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  777. * @param mtd MTD data structure
  778. * @param addr address to update
  779. * @param valid valid flag
  780. *
  781. * Update BufferRAM information
  782. */
  783. static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  784. int valid)
  785. {
  786. struct onenand_chip *this = mtd->priv;
  787. int blockpage;
  788. unsigned int i;
  789. if (ONENAND_IS_2PLANE(this))
  790. blockpage = onenand_get_2x_blockpage(mtd, addr);
  791. else
  792. blockpage = (int) (addr >> this->page_shift);
  793. /* Invalidate another BufferRAM */
  794. i = ONENAND_NEXT_BUFFERRAM(this);
  795. if (this->bufferram[i].blockpage == blockpage)
  796. this->bufferram[i].blockpage = -1;
  797. /* Update BufferRAM */
  798. i = ONENAND_CURRENT_BUFFERRAM(this);
  799. if (valid)
  800. this->bufferram[i].blockpage = blockpage;
  801. else
  802. this->bufferram[i].blockpage = -1;
  803. }
  804. /**
  805. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  806. * @param mtd MTD data structure
  807. * @param addr start address to invalidate
  808. * @param len length to invalidate
  809. *
  810. * Invalidate BufferRAM information
  811. */
  812. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  813. unsigned int len)
  814. {
  815. struct onenand_chip *this = mtd->priv;
  816. int i;
  817. loff_t end_addr = addr + len;
  818. /* Invalidate BufferRAM */
  819. for (i = 0; i < MAX_BUFFERRAM; i++) {
  820. loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
  821. if (buf_addr >= addr && buf_addr < end_addr)
  822. this->bufferram[i].blockpage = -1;
  823. }
  824. }
  825. /**
  826. * onenand_get_device - [GENERIC] Get chip for selected access
  827. * @param mtd MTD device structure
  828. * @param new_state the state which is requested
  829. *
  830. * Get the device and lock it for exclusive access
  831. */
  832. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  833. {
  834. struct onenand_chip *this = mtd->priv;
  835. DECLARE_WAITQUEUE(wait, current);
  836. /*
  837. * Grab the lock and see if the device is available
  838. */
  839. while (1) {
  840. spin_lock(&this->chip_lock);
  841. if (this->state == FL_READY) {
  842. this->state = new_state;
  843. spin_unlock(&this->chip_lock);
  844. if (new_state != FL_PM_SUSPENDED && this->enable)
  845. this->enable(mtd);
  846. break;
  847. }
  848. if (new_state == FL_PM_SUSPENDED) {
  849. spin_unlock(&this->chip_lock);
  850. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  851. }
  852. set_current_state(TASK_UNINTERRUPTIBLE);
  853. add_wait_queue(&this->wq, &wait);
  854. spin_unlock(&this->chip_lock);
  855. schedule();
  856. remove_wait_queue(&this->wq, &wait);
  857. }
  858. return 0;
  859. }
  860. /**
  861. * onenand_release_device - [GENERIC] release chip
  862. * @param mtd MTD device structure
  863. *
  864. * Deselect, release chip lock and wake up anyone waiting on the device
  865. */
  866. static void onenand_release_device(struct mtd_info *mtd)
  867. {
  868. struct onenand_chip *this = mtd->priv;
  869. if (this->state != FL_PM_SUSPENDED && this->disable)
  870. this->disable(mtd);
  871. /* Release the chip */
  872. spin_lock(&this->chip_lock);
  873. this->state = FL_READY;
  874. wake_up(&this->wq);
  875. spin_unlock(&this->chip_lock);
  876. }
  877. /**
  878. * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
  879. * @param mtd MTD device structure
  880. * @param buf destination address
  881. * @param column oob offset to read from
  882. * @param thislen oob length to read
  883. */
  884. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
  885. int thislen)
  886. {
  887. struct onenand_chip *this = mtd->priv;
  888. struct nand_oobfree *free;
  889. int readcol = column;
  890. int readend = column + thislen;
  891. int lastgap = 0;
  892. unsigned int i;
  893. uint8_t *oob_buf = this->oob_buf;
  894. free = this->ecclayout->oobfree;
  895. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  896. if (readcol >= lastgap)
  897. readcol += free->offset - lastgap;
  898. if (readend >= lastgap)
  899. readend += free->offset - lastgap;
  900. lastgap = free->offset + free->length;
  901. }
  902. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  903. free = this->ecclayout->oobfree;
  904. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  905. int free_end = free->offset + free->length;
  906. if (free->offset < readend && free_end > readcol) {
  907. int st = max_t(int,free->offset,readcol);
  908. int ed = min_t(int,free_end,readend);
  909. int n = ed - st;
  910. memcpy(buf, oob_buf + st, n);
  911. buf += n;
  912. } else if (column == 0)
  913. break;
  914. }
  915. return 0;
  916. }
  917. /**
  918. * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
  919. * @param mtd MTD device structure
  920. * @param addr address to recover
  921. * @param status return value from onenand_wait / onenand_bbt_wait
  922. *
  923. * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
  924. * lower page address and MSB page has higher page address in paired pages.
  925. * If power off occurs during MSB page program, the paired LSB page data can
  926. * become corrupt. LSB page recovery read is a way to read LSB page though page
  927. * data are corrupted. When uncorrectable error occurs as a result of LSB page
  928. * read after power up, issue LSB page recovery read.
  929. */
  930. static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
  931. {
  932. struct onenand_chip *this = mtd->priv;
  933. int i;
  934. /* Recovery is only for Flex-OneNAND */
  935. if (!FLEXONENAND(this))
  936. return status;
  937. /* check if we failed due to uncorrectable error */
  938. if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
  939. return status;
  940. /* check if address lies in MLC region */
  941. i = flexonenand_region(mtd, addr);
  942. if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
  943. return status;
  944. /* We are attempting to reread, so decrement stats.failed
  945. * which was incremented by onenand_wait due to read failure
  946. */
  947. printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
  948. __func__);
  949. mtd->ecc_stats.failed--;
  950. /* Issue the LSB page recovery command */
  951. this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
  952. return this->wait(mtd, FL_READING);
  953. }
  954. /**
  955. * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
  956. * @param mtd MTD device structure
  957. * @param from offset to read from
  958. * @param ops: oob operation description structure
  959. *
  960. * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
  961. * So, read-while-load is not present.
  962. */
  963. static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  964. struct mtd_oob_ops *ops)
  965. {
  966. struct onenand_chip *this = mtd->priv;
  967. struct mtd_ecc_stats stats;
  968. size_t len = ops->len;
  969. size_t ooblen = ops->ooblen;
  970. u_char *buf = ops->datbuf;
  971. u_char *oobbuf = ops->oobbuf;
  972. int read = 0, column, thislen;
  973. int oobread = 0, oobcolumn, thisooblen, oobsize;
  974. int ret = 0;
  975. int writesize = this->writesize;
  976. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  977. (int)len);
  978. if (ops->mode == MTD_OPS_AUTO_OOB)
  979. oobsize = this->ecclayout->oobavail;
  980. else
  981. oobsize = mtd->oobsize;
  982. oobcolumn = from & (mtd->oobsize - 1);
  983. /* Do not allow reads past end of device */
  984. if (from + len > mtd->size) {
  985. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  986. __func__);
  987. ops->retlen = 0;
  988. ops->oobretlen = 0;
  989. return -EINVAL;
  990. }
  991. stats = mtd->ecc_stats;
  992. while (read < len) {
  993. cond_resched();
  994. thislen = min_t(int, writesize, len - read);
  995. column = from & (writesize - 1);
  996. if (column + thislen > writesize)
  997. thislen = writesize - column;
  998. if (!onenand_check_bufferram(mtd, from)) {
  999. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1000. ret = this->wait(mtd, FL_READING);
  1001. if (unlikely(ret))
  1002. ret = onenand_recover_lsb(mtd, from, ret);
  1003. onenand_update_bufferram(mtd, from, !ret);
  1004. if (mtd_is_eccerr(ret))
  1005. ret = 0;
  1006. if (ret)
  1007. break;
  1008. }
  1009. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1010. if (oobbuf) {
  1011. thisooblen = oobsize - oobcolumn;
  1012. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1013. if (ops->mode == MTD_OPS_AUTO_OOB)
  1014. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1015. else
  1016. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1017. oobread += thisooblen;
  1018. oobbuf += thisooblen;
  1019. oobcolumn = 0;
  1020. }
  1021. read += thislen;
  1022. if (read == len)
  1023. break;
  1024. from += thislen;
  1025. buf += thislen;
  1026. }
  1027. /*
  1028. * Return success, if no ECC failures, else -EBADMSG
  1029. * fs driver will take care of that, because
  1030. * retlen == desired len and result == -EBADMSG
  1031. */
  1032. ops->retlen = read;
  1033. ops->oobretlen = oobread;
  1034. if (ret)
  1035. return ret;
  1036. if (mtd->ecc_stats.failed - stats.failed)
  1037. return -EBADMSG;
  1038. /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
  1039. return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
  1040. }
  1041. /**
  1042. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  1043. * @param mtd MTD device structure
  1044. * @param from offset to read from
  1045. * @param ops: oob operation description structure
  1046. *
  1047. * OneNAND read main and/or out-of-band data
  1048. */
  1049. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  1050. struct mtd_oob_ops *ops)
  1051. {
  1052. struct onenand_chip *this = mtd->priv;
  1053. struct mtd_ecc_stats stats;
  1054. size_t len = ops->len;
  1055. size_t ooblen = ops->ooblen;
  1056. u_char *buf = ops->datbuf;
  1057. u_char *oobbuf = ops->oobbuf;
  1058. int read = 0, column, thislen;
  1059. int oobread = 0, oobcolumn, thisooblen, oobsize;
  1060. int ret = 0, boundary = 0;
  1061. int writesize = this->writesize;
  1062. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  1063. (int)len);
  1064. if (ops->mode == MTD_OPS_AUTO_OOB)
  1065. oobsize = this->ecclayout->oobavail;
  1066. else
  1067. oobsize = mtd->oobsize;
  1068. oobcolumn = from & (mtd->oobsize - 1);
  1069. /* Do not allow reads past end of device */
  1070. if ((from + len) > mtd->size) {
  1071. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1072. __func__);
  1073. ops->retlen = 0;
  1074. ops->oobretlen = 0;
  1075. return -EINVAL;
  1076. }
  1077. stats = mtd->ecc_stats;
  1078. /* Read-while-load method */
  1079. /* Do first load to bufferRAM */
  1080. if (read < len) {
  1081. if (!onenand_check_bufferram(mtd, from)) {
  1082. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1083. ret = this->wait(mtd, FL_READING);
  1084. onenand_update_bufferram(mtd, from, !ret);
  1085. if (mtd_is_eccerr(ret))
  1086. ret = 0;
  1087. }
  1088. }
  1089. thislen = min_t(int, writesize, len - read);
  1090. column = from & (writesize - 1);
  1091. if (column + thislen > writesize)
  1092. thislen = writesize - column;
  1093. while (!ret) {
  1094. /* If there is more to load then start next load */
  1095. from += thislen;
  1096. if (read + thislen < len) {
  1097. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1098. /*
  1099. * Chip boundary handling in DDP
  1100. * Now we issued chip 1 read and pointed chip 1
  1101. * bufferram so we have to point chip 0 bufferram.
  1102. */
  1103. if (ONENAND_IS_DDP(this) &&
  1104. unlikely(from == (this->chipsize >> 1))) {
  1105. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  1106. boundary = 1;
  1107. } else
  1108. boundary = 0;
  1109. ONENAND_SET_PREV_BUFFERRAM(this);
  1110. }
  1111. /* While load is going, read from last bufferRAM */
  1112. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1113. /* Read oob area if needed */
  1114. if (oobbuf) {
  1115. thisooblen = oobsize - oobcolumn;
  1116. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1117. if (ops->mode == MTD_OPS_AUTO_OOB)
  1118. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1119. else
  1120. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1121. oobread += thisooblen;
  1122. oobbuf += thisooblen;
  1123. oobcolumn = 0;
  1124. }
  1125. /* See if we are done */
  1126. read += thislen;
  1127. if (read == len)
  1128. break;
  1129. /* Set up for next read from bufferRAM */
  1130. if (unlikely(boundary))
  1131. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  1132. ONENAND_SET_NEXT_BUFFERRAM(this);
  1133. buf += thislen;
  1134. thislen = min_t(int, writesize, len - read);
  1135. column = 0;
  1136. cond_resched();
  1137. /* Now wait for load */
  1138. ret = this->wait(mtd, FL_READING);
  1139. onenand_update_bufferram(mtd, from, !ret);
  1140. if (mtd_is_eccerr(ret))
  1141. ret = 0;
  1142. }
  1143. /*
  1144. * Return success, if no ECC failures, else -EBADMSG
  1145. * fs driver will take care of that, because
  1146. * retlen == desired len and result == -EBADMSG
  1147. */
  1148. ops->retlen = read;
  1149. ops->oobretlen = oobread;
  1150. if (ret)
  1151. return ret;
  1152. if (mtd->ecc_stats.failed - stats.failed)
  1153. return -EBADMSG;
  1154. /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
  1155. return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
  1156. }
  1157. /**
  1158. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  1159. * @param mtd MTD device structure
  1160. * @param from offset to read from
  1161. * @param ops: oob operation description structure
  1162. *
  1163. * OneNAND read out-of-band data from the spare area
  1164. */
  1165. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  1166. struct mtd_oob_ops *ops)
  1167. {
  1168. struct onenand_chip *this = mtd->priv;
  1169. struct mtd_ecc_stats stats;
  1170. int read = 0, thislen, column, oobsize;
  1171. size_t len = ops->ooblen;
  1172. unsigned int mode = ops->mode;
  1173. u_char *buf = ops->oobbuf;
  1174. int ret = 0, readcmd;
  1175. from += ops->ooboffs;
  1176. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  1177. (int)len);
  1178. /* Initialize return length value */
  1179. ops->oobretlen = 0;
  1180. if (mode == MTD_OPS_AUTO_OOB)
  1181. oobsize = this->ecclayout->oobavail;
  1182. else
  1183. oobsize = mtd->oobsize;
  1184. column = from & (mtd->oobsize - 1);
  1185. if (unlikely(column >= oobsize)) {
  1186. printk(KERN_ERR "%s: Attempted to start read outside oob\n",
  1187. __func__);
  1188. return -EINVAL;
  1189. }
  1190. /* Do not allow reads past end of device */
  1191. if (unlikely(from >= mtd->size ||
  1192. column + len > ((mtd->size >> this->page_shift) -
  1193. (from >> this->page_shift)) * oobsize)) {
  1194. printk(KERN_ERR "%s: Attempted to read beyond end of device\n",
  1195. __func__);
  1196. return -EINVAL;
  1197. }
  1198. stats = mtd->ecc_stats;
  1199. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1200. while (read < len) {
  1201. cond_resched();
  1202. thislen = oobsize - column;
  1203. thislen = min_t(int, thislen, len);
  1204. this->command(mtd, readcmd, from, mtd->oobsize);
  1205. onenand_update_bufferram(mtd, from, 0);
  1206. ret = this->wait(mtd, FL_READING);
  1207. if (unlikely(ret))
  1208. ret = onenand_recover_lsb(mtd, from, ret);
  1209. if (ret && !mtd_is_eccerr(ret)) {
  1210. printk(KERN_ERR "%s: read failed = 0x%x\n",
  1211. __func__, ret);
  1212. break;
  1213. }
  1214. if (mode == MTD_OPS_AUTO_OOB)
  1215. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  1216. else
  1217. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1218. read += thislen;
  1219. if (read == len)
  1220. break;
  1221. buf += thislen;
  1222. /* Read more? */
  1223. if (read < len) {
  1224. /* Page size */
  1225. from += mtd->writesize;
  1226. column = 0;
  1227. }
  1228. }
  1229. ops->oobretlen = read;
  1230. if (ret)
  1231. return ret;
  1232. if (mtd->ecc_stats.failed - stats.failed)
  1233. return -EBADMSG;
  1234. return 0;
  1235. }
  1236. /**
  1237. * onenand_read - [MTD Interface] Read data from flash
  1238. * @param mtd MTD device structure
  1239. * @param from offset to read from
  1240. * @param len number of bytes to read
  1241. * @param retlen pointer to variable to store the number of read bytes
  1242. * @param buf the databuffer to put data
  1243. *
  1244. * Read with ecc
  1245. */
  1246. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1247. size_t *retlen, u_char *buf)
  1248. {
  1249. struct onenand_chip *this = mtd->priv;
  1250. struct mtd_oob_ops ops = {
  1251. .len = len,
  1252. .ooblen = 0,
  1253. .datbuf = buf,
  1254. .oobbuf = NULL,
  1255. };
  1256. int ret;
  1257. onenand_get_device(mtd, FL_READING);
  1258. ret = ONENAND_IS_4KB_PAGE(this) ?
  1259. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  1260. onenand_read_ops_nolock(mtd, from, &ops);
  1261. onenand_release_device(mtd);
  1262. *retlen = ops.retlen;
  1263. return ret;
  1264. }
  1265. /**
  1266. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  1267. * @param mtd: MTD device structure
  1268. * @param from: offset to read from
  1269. * @param ops: oob operation description structure
  1270. * Read main and/or out-of-band
  1271. */
  1272. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  1273. struct mtd_oob_ops *ops)
  1274. {
  1275. struct onenand_chip *this = mtd->priv;
  1276. int ret;
  1277. switch (ops->mode) {
  1278. case MTD_OPS_PLACE_OOB:
  1279. case MTD_OPS_AUTO_OOB:
  1280. break;
  1281. case MTD_OPS_RAW:
  1282. /* Not implemented yet */
  1283. default:
  1284. return -EINVAL;
  1285. }
  1286. onenand_get_device(mtd, FL_READING);
  1287. if (ops->datbuf)
  1288. ret = ONENAND_IS_4KB_PAGE(this) ?
  1289. onenand_mlc_read_ops_nolock(mtd, from, ops) :
  1290. onenand_read_ops_nolock(mtd, from, ops);
  1291. else
  1292. ret = onenand_read_oob_nolock(mtd, from, ops);
  1293. onenand_release_device(mtd);
  1294. return ret;
  1295. }
  1296. /**
  1297. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  1298. * @param mtd MTD device structure
  1299. * @param state state to select the max. timeout value
  1300. *
  1301. * Wait for command done.
  1302. */
  1303. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  1304. {
  1305. struct onenand_chip *this = mtd->priv;
  1306. unsigned long timeout;
  1307. unsigned int interrupt, ctrl, ecc, addr1, addr8;
  1308. /* The 20 msec is enough */
  1309. timeout = jiffies + msecs_to_jiffies(20);
  1310. while (time_before(jiffies, timeout)) {
  1311. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1312. if (interrupt & ONENAND_INT_MASTER)
  1313. break;
  1314. }
  1315. /* To get correct interrupt status in timeout case */
  1316. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1317. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  1318. addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
  1319. addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
  1320. if (interrupt & ONENAND_INT_READ) {
  1321. ecc = onenand_read_ecc(this);
  1322. if (ecc & ONENAND_ECC_2BIT_ALL) {
  1323. printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
  1324. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1325. __func__, ecc, ctrl, interrupt, addr1, addr8);
  1326. return ONENAND_BBT_READ_ECC_ERROR;
  1327. }
  1328. } else {
  1329. printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
  1330. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1331. __func__, ctrl, interrupt, addr1, addr8);
  1332. return ONENAND_BBT_READ_FATAL_ERROR;
  1333. }
  1334. /* Initial bad block case: 0x2400 or 0x0400 */
  1335. if (ctrl & ONENAND_CTRL_ERROR) {
  1336. printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
  1337. "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
  1338. return ONENAND_BBT_READ_ERROR;
  1339. }
  1340. return 0;
  1341. }
  1342. /**
  1343. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1344. * @param mtd MTD device structure
  1345. * @param from offset to read from
  1346. * @param ops oob operation description structure
  1347. *
  1348. * OneNAND read out-of-band data from the spare area for bbt scan
  1349. */
  1350. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1351. struct mtd_oob_ops *ops)
  1352. {
  1353. struct onenand_chip *this = mtd->priv;
  1354. int read = 0, thislen, column;
  1355. int ret = 0, readcmd;
  1356. size_t len = ops->ooblen;
  1357. u_char *buf = ops->oobbuf;
  1358. pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
  1359. len);
  1360. /* Initialize return value */
  1361. ops->oobretlen = 0;
  1362. /* Do not allow reads past end of device */
  1363. if (unlikely((from + len) > mtd->size)) {
  1364. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1365. __func__);
  1366. return ONENAND_BBT_READ_FATAL_ERROR;
  1367. }
  1368. /* Grab the lock and see if the device is available */
  1369. onenand_get_device(mtd, FL_READING);
  1370. column = from & (mtd->oobsize - 1);
  1371. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1372. while (read < len) {
  1373. cond_resched();
  1374. thislen = mtd->oobsize - column;
  1375. thislen = min_t(int, thislen, len);
  1376. this->command(mtd, readcmd, from, mtd->oobsize);
  1377. onenand_update_bufferram(mtd, from, 0);
  1378. ret = this->bbt_wait(mtd, FL_READING);
  1379. if (unlikely(ret))
  1380. ret = onenand_recover_lsb(mtd, from, ret);
  1381. if (ret)
  1382. break;
  1383. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1384. read += thislen;
  1385. if (read == len)
  1386. break;
  1387. buf += thislen;
  1388. /* Read more? */
  1389. if (read < len) {
  1390. /* Update Page size */
  1391. from += this->writesize;
  1392. column = 0;
  1393. }
  1394. }
  1395. /* Deselect and wake up anyone waiting on the device */
  1396. onenand_release_device(mtd);
  1397. ops->oobretlen = read;
  1398. return ret;
  1399. }
  1400. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1401. /**
  1402. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1403. * @param mtd MTD device structure
  1404. * @param buf the databuffer to verify
  1405. * @param to offset to read from
  1406. */
  1407. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1408. {
  1409. struct onenand_chip *this = mtd->priv;
  1410. u_char *oob_buf = this->oob_buf;
  1411. int status, i, readcmd;
  1412. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1413. this->command(mtd, readcmd, to, mtd->oobsize);
  1414. onenand_update_bufferram(mtd, to, 0);
  1415. status = this->wait(mtd, FL_READING);
  1416. if (status)
  1417. return status;
  1418. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  1419. for (i = 0; i < mtd->oobsize; i++)
  1420. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  1421. return -EBADMSG;
  1422. return 0;
  1423. }
  1424. /**
  1425. * onenand_verify - [GENERIC] verify the chip contents after a write
  1426. * @param mtd MTD device structure
  1427. * @param buf the databuffer to verify
  1428. * @param addr offset to read from
  1429. * @param len number of bytes to read and compare
  1430. */
  1431. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1432. {
  1433. struct onenand_chip *this = mtd->priv;
  1434. int ret = 0;
  1435. int thislen, column;
  1436. column = addr & (this->writesize - 1);
  1437. while (len != 0) {
  1438. thislen = min_t(int, this->writesize - column, len);
  1439. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1440. onenand_update_bufferram(mtd, addr, 0);
  1441. ret = this->wait(mtd, FL_READING);
  1442. if (ret)
  1443. return ret;
  1444. onenand_update_bufferram(mtd, addr, 1);
  1445. this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
  1446. if (memcmp(buf, this->verify_buf + column, thislen))
  1447. return -EBADMSG;
  1448. len -= thislen;
  1449. buf += thislen;
  1450. addr += thislen;
  1451. column = 0;
  1452. }
  1453. return 0;
  1454. }
  1455. #else
  1456. #define onenand_verify(...) (0)
  1457. #define onenand_verify_oob(...) (0)
  1458. #endif
  1459. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1460. static void onenand_panic_wait(struct mtd_info *mtd)
  1461. {
  1462. struct onenand_chip *this = mtd->priv;
  1463. unsigned int interrupt;
  1464. int i;
  1465. for (i = 0; i < 2000; i++) {
  1466. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1467. if (interrupt & ONENAND_INT_MASTER)
  1468. break;
  1469. udelay(10);
  1470. }
  1471. }
  1472. /**
  1473. * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
  1474. * @param mtd MTD device structure
  1475. * @param to offset to write to
  1476. * @param len number of bytes to write
  1477. * @param retlen pointer to variable to store the number of written bytes
  1478. * @param buf the data to write
  1479. *
  1480. * Write with ECC
  1481. */
  1482. static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1483. size_t *retlen, const u_char *buf)
  1484. {
  1485. struct onenand_chip *this = mtd->priv;
  1486. int column, subpage;
  1487. int written = 0;
  1488. int ret = 0;
  1489. if (this->state == FL_PM_SUSPENDED)
  1490. return -EBUSY;
  1491. /* Wait for any existing operation to clear */
  1492. onenand_panic_wait(mtd);
  1493. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1494. (int)len);
  1495. /* Reject writes, which are not page aligned */
  1496. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1497. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1498. __func__);
  1499. return -EINVAL;
  1500. }
  1501. column = to & (mtd->writesize - 1);
  1502. /* Loop until all data write */
  1503. while (written < len) {
  1504. int thislen = min_t(int, mtd->writesize - column, len - written);
  1505. u_char *wbuf = (u_char *) buf;
  1506. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1507. /* Partial page write */
  1508. subpage = thislen < mtd->writesize;
  1509. if (subpage) {
  1510. memset(this->page_buf, 0xff, mtd->writesize);
  1511. memcpy(this->page_buf + column, buf, thislen);
  1512. wbuf = this->page_buf;
  1513. }
  1514. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1515. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  1516. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1517. onenand_panic_wait(mtd);
  1518. /* In partial page write we don't update bufferram */
  1519. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1520. if (ONENAND_IS_2PLANE(this)) {
  1521. ONENAND_SET_BUFFERRAM1(this);
  1522. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1523. }
  1524. if (ret) {
  1525. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1526. break;
  1527. }
  1528. written += thislen;
  1529. if (written == len)
  1530. break;
  1531. column = 0;
  1532. to += thislen;
  1533. buf += thislen;
  1534. }
  1535. *retlen = written;
  1536. return ret;
  1537. }
  1538. /**
  1539. * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
  1540. * @param mtd MTD device structure
  1541. * @param oob_buf oob buffer
  1542. * @param buf source address
  1543. * @param column oob offset to write to
  1544. * @param thislen oob length to write
  1545. */
  1546. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1547. const u_char *buf, int column, int thislen)
  1548. {
  1549. struct onenand_chip *this = mtd->priv;
  1550. struct nand_oobfree *free;
  1551. int writecol = column;
  1552. int writeend = column + thislen;
  1553. int lastgap = 0;
  1554. unsigned int i;
  1555. free = this->ecclayout->oobfree;
  1556. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1557. if (writecol >= lastgap)
  1558. writecol += free->offset - lastgap;
  1559. if (writeend >= lastgap)
  1560. writeend += free->offset - lastgap;
  1561. lastgap = free->offset + free->length;
  1562. }
  1563. free = this->ecclayout->oobfree;
  1564. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1565. int free_end = free->offset + free->length;
  1566. if (free->offset < writeend && free_end > writecol) {
  1567. int st = max_t(int,free->offset,writecol);
  1568. int ed = min_t(int,free_end,writeend);
  1569. int n = ed - st;
  1570. memcpy(oob_buf + st, buf, n);
  1571. buf += n;
  1572. } else if (column == 0)
  1573. break;
  1574. }
  1575. return 0;
  1576. }
  1577. /**
  1578. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1579. * @param mtd MTD device structure
  1580. * @param to offset to write to
  1581. * @param ops oob operation description structure
  1582. *
  1583. * Write main and/or oob with ECC
  1584. */
  1585. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1586. struct mtd_oob_ops *ops)
  1587. {
  1588. struct onenand_chip *this = mtd->priv;
  1589. int written = 0, column, thislen = 0, subpage = 0;
  1590. int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
  1591. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1592. size_t len = ops->len;
  1593. size_t ooblen = ops->ooblen;
  1594. const u_char *buf = ops->datbuf;
  1595. const u_char *oob = ops->oobbuf;
  1596. u_char *oobbuf;
  1597. int ret = 0, cmd;
  1598. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1599. (int)len);
  1600. /* Initialize retlen, in case of early exit */
  1601. ops->retlen = 0;
  1602. ops->oobretlen = 0;
  1603. /* Reject writes, which are not page aligned */
  1604. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1605. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1606. __func__);
  1607. return -EINVAL;
  1608. }
  1609. /* Check zero length */
  1610. if (!len)
  1611. return 0;
  1612. if (ops->mode == MTD_OPS_AUTO_OOB)
  1613. oobsize = this->ecclayout->oobavail;
  1614. else
  1615. oobsize = mtd->oobsize;
  1616. oobcolumn = to & (mtd->oobsize - 1);
  1617. column = to & (mtd->writesize - 1);
  1618. /* Loop until all data write */
  1619. while (1) {
  1620. if (written < len) {
  1621. u_char *wbuf = (u_char *) buf;
  1622. thislen = min_t(int, mtd->writesize - column, len - written);
  1623. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1624. cond_resched();
  1625. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1626. /* Partial page write */
  1627. subpage = thislen < mtd->writesize;
  1628. if (subpage) {
  1629. memset(this->page_buf, 0xff, mtd->writesize);
  1630. memcpy(this->page_buf + column, buf, thislen);
  1631. wbuf = this->page_buf;
  1632. }
  1633. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1634. if (oob) {
  1635. oobbuf = this->oob_buf;
  1636. /* We send data to spare ram with oobsize
  1637. * to prevent byte access */
  1638. memset(oobbuf, 0xff, mtd->oobsize);
  1639. if (ops->mode == MTD_OPS_AUTO_OOB)
  1640. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1641. else
  1642. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1643. oobwritten += thisooblen;
  1644. oob += thisooblen;
  1645. oobcolumn = 0;
  1646. } else
  1647. oobbuf = (u_char *) ffchars;
  1648. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1649. } else
  1650. ONENAND_SET_NEXT_BUFFERRAM(this);
  1651. /*
  1652. * 2 PLANE, MLC, and Flex-OneNAND do not support
  1653. * write-while-program feature.
  1654. */
  1655. if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
  1656. ONENAND_SET_PREV_BUFFERRAM(this);
  1657. ret = this->wait(mtd, FL_WRITING);
  1658. /* In partial page write we don't update bufferram */
  1659. onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
  1660. if (ret) {
  1661. written -= prevlen;
  1662. printk(KERN_ERR "%s: write failed %d\n",
  1663. __func__, ret);
  1664. break;
  1665. }
  1666. if (written == len) {
  1667. /* Only check verify write turn on */
  1668. ret = onenand_verify(mtd, buf - len, to - len, len);
  1669. if (ret)
  1670. printk(KERN_ERR "%s: verify failed %d\n",
  1671. __func__, ret);
  1672. break;
  1673. }
  1674. ONENAND_SET_NEXT_BUFFERRAM(this);
  1675. }
  1676. this->ongoing = 0;
  1677. cmd = ONENAND_CMD_PROG;
  1678. /* Exclude 1st OTP and OTP blocks for cache program feature */
  1679. if (ONENAND_IS_CACHE_PROGRAM(this) &&
  1680. likely(onenand_block(this, to) != 0) &&
  1681. ONENAND_IS_4KB_PAGE(this) &&
  1682. ((written + thislen) < len)) {
  1683. cmd = ONENAND_CMD_2X_CACHE_PROG;
  1684. this->ongoing = 1;
  1685. }
  1686. this->command(mtd, cmd, to, mtd->writesize);
  1687. /*
  1688. * 2 PLANE, MLC, and Flex-OneNAND wait here
  1689. */
  1690. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
  1691. ret = this->wait(mtd, FL_WRITING);
  1692. /* In partial page write we don't update bufferram */
  1693. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1694. if (ret) {
  1695. printk(KERN_ERR "%s: write failed %d\n",
  1696. __func__, ret);
  1697. break;
  1698. }
  1699. /* Only check verify write turn on */
  1700. ret = onenand_verify(mtd, buf, to, thislen);
  1701. if (ret) {
  1702. printk(KERN_ERR "%s: verify failed %d\n",
  1703. __func__, ret);
  1704. break;
  1705. }
  1706. written += thislen;
  1707. if (written == len)
  1708. break;
  1709. } else
  1710. written += thislen;
  1711. column = 0;
  1712. prev_subpage = subpage;
  1713. prev = to;
  1714. prevlen = thislen;
  1715. to += thislen;
  1716. buf += thislen;
  1717. first = 0;
  1718. }
  1719. /* In error case, clear all bufferrams */
  1720. if (written != len)
  1721. onenand_invalidate_bufferram(mtd, 0, -1);
  1722. ops->retlen = written;
  1723. ops->oobretlen = oobwritten;
  1724. return ret;
  1725. }
  1726. /**
  1727. * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
  1728. * @param mtd MTD device structure
  1729. * @param to offset to write to
  1730. * @param len number of bytes to write
  1731. * @param retlen pointer to variable to store the number of written bytes
  1732. * @param buf the data to write
  1733. * @param mode operation mode
  1734. *
  1735. * OneNAND write out-of-band
  1736. */
  1737. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1738. struct mtd_oob_ops *ops)
  1739. {
  1740. struct onenand_chip *this = mtd->priv;
  1741. int column, ret = 0, oobsize;
  1742. int written = 0, oobcmd;
  1743. u_char *oobbuf;
  1744. size_t len = ops->ooblen;
  1745. const u_char *buf = ops->oobbuf;
  1746. unsigned int mode = ops->mode;
  1747. to += ops->ooboffs;
  1748. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1749. (int)len);
  1750. /* Initialize retlen, in case of early exit */
  1751. ops->oobretlen = 0;
  1752. if (mode == MTD_OPS_AUTO_OOB)
  1753. oobsize = this->ecclayout->oobavail;
  1754. else
  1755. oobsize = mtd->oobsize;
  1756. column = to & (mtd->oobsize - 1);
  1757. if (unlikely(column >= oobsize)) {
  1758. printk(KERN_ERR "%s: Attempted to start write outside oob\n",
  1759. __func__);
  1760. return -EINVAL;
  1761. }
  1762. /* For compatibility with NAND: Do not allow write past end of page */
  1763. if (unlikely(column + len > oobsize)) {
  1764. printk(KERN_ERR "%s: Attempt to write past end of page\n",
  1765. __func__);
  1766. return -EINVAL;
  1767. }
  1768. /* Do not allow reads past end of device */
  1769. if (unlikely(to >= mtd->size ||
  1770. column + len > ((mtd->size >> this->page_shift) -
  1771. (to >> this->page_shift)) * oobsize)) {
  1772. printk(KERN_ERR "%s: Attempted to write past end of device\n",
  1773. __func__);
  1774. return -EINVAL;
  1775. }
  1776. oobbuf = this->oob_buf;
  1777. oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1778. /* Loop until all data write */
  1779. while (written < len) {
  1780. int thislen = min_t(int, oobsize, len - written);
  1781. cond_resched();
  1782. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1783. /* We send data to spare ram with oobsize
  1784. * to prevent byte access */
  1785. memset(oobbuf, 0xff, mtd->oobsize);
  1786. if (mode == MTD_OPS_AUTO_OOB)
  1787. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1788. else
  1789. memcpy(oobbuf + column, buf, thislen);
  1790. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1791. if (ONENAND_IS_4KB_PAGE(this)) {
  1792. /* Set main area of DataRAM to 0xff*/
  1793. memset(this->page_buf, 0xff, mtd->writesize);
  1794. this->write_bufferram(mtd, ONENAND_DATARAM,
  1795. this->page_buf, 0, mtd->writesize);
  1796. }
  1797. this->command(mtd, oobcmd, to, mtd->oobsize);
  1798. onenand_update_bufferram(mtd, to, 0);
  1799. if (ONENAND_IS_2PLANE(this)) {
  1800. ONENAND_SET_BUFFERRAM1(this);
  1801. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1802. }
  1803. ret = this->wait(mtd, FL_WRITING);
  1804. if (ret) {
  1805. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1806. break;
  1807. }
  1808. ret = onenand_verify_oob(mtd, oobbuf, to);
  1809. if (ret) {
  1810. printk(KERN_ERR "%s: verify failed %d\n",
  1811. __func__, ret);
  1812. break;
  1813. }
  1814. written += thislen;
  1815. if (written == len)
  1816. break;
  1817. to += mtd->writesize;
  1818. buf += thislen;
  1819. column = 0;
  1820. }
  1821. ops->oobretlen = written;
  1822. return ret;
  1823. }
  1824. /**
  1825. * onenand_write - [MTD Interface] write buffer to FLASH
  1826. * @param mtd MTD device structure
  1827. * @param to offset to write to
  1828. * @param len number of bytes to write
  1829. * @param retlen pointer to variable to store the number of written bytes
  1830. * @param buf the data to write
  1831. *
  1832. * Write with ECC
  1833. */
  1834. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1835. size_t *retlen, const u_char *buf)
  1836. {
  1837. struct mtd_oob_ops ops = {
  1838. .len = len,
  1839. .ooblen = 0,
  1840. .datbuf = (u_char *) buf,
  1841. .oobbuf = NULL,
  1842. };
  1843. int ret;
  1844. onenand_get_device(mtd, FL_WRITING);
  1845. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1846. onenand_release_device(mtd);
  1847. *retlen = ops.retlen;
  1848. return ret;
  1849. }
  1850. /**
  1851. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1852. * @param mtd: MTD device structure
  1853. * @param to: offset to write
  1854. * @param ops: oob operation description structure
  1855. */
  1856. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1857. struct mtd_oob_ops *ops)
  1858. {
  1859. int ret;
  1860. switch (ops->mode) {
  1861. case MTD_OPS_PLACE_OOB:
  1862. case MTD_OPS_AUTO_OOB:
  1863. break;
  1864. case MTD_OPS_RAW:
  1865. /* Not implemented yet */
  1866. default:
  1867. return -EINVAL;
  1868. }
  1869. onenand_get_device(mtd, FL_WRITING);
  1870. if (ops->datbuf)
  1871. ret = onenand_write_ops_nolock(mtd, to, ops);
  1872. else
  1873. ret = onenand_write_oob_nolock(mtd, to, ops);
  1874. onenand_release_device(mtd);
  1875. return ret;
  1876. }
  1877. /**
  1878. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1879. * @param mtd MTD device structure
  1880. * @param ofs offset from device start
  1881. * @param allowbbt 1, if its allowed to access the bbt area
  1882. *
  1883. * Check, if the block is bad. Either by reading the bad block table or
  1884. * calling of the scan function.
  1885. */
  1886. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1887. {
  1888. struct onenand_chip *this = mtd->priv;
  1889. struct bbm_info *bbm = this->bbm;
  1890. /* Return info from the table */
  1891. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1892. }
  1893. static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
  1894. struct erase_info *instr)
  1895. {
  1896. struct onenand_chip *this = mtd->priv;
  1897. loff_t addr = instr->addr;
  1898. int len = instr->len;
  1899. unsigned int block_size = (1 << this->erase_shift);
  1900. int ret = 0;
  1901. while (len) {
  1902. this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
  1903. ret = this->wait(mtd, FL_VERIFYING_ERASE);
  1904. if (ret) {
  1905. printk(KERN_ERR "%s: Failed verify, block %d\n",
  1906. __func__, onenand_block(this, addr));
  1907. instr->state = MTD_ERASE_FAILED;
  1908. instr->fail_addr = addr;
  1909. return -1;
  1910. }
  1911. len -= block_size;
  1912. addr += block_size;
  1913. }
  1914. return 0;
  1915. }
  1916. /**
  1917. * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
  1918. * @param mtd MTD device structure
  1919. * @param instr erase instruction
  1920. * @param region erase region
  1921. *
  1922. * Erase one or more blocks up to 64 block at a time
  1923. */
  1924. static int onenand_multiblock_erase(struct mtd_info *mtd,
  1925. struct erase_info *instr,
  1926. unsigned int block_size)
  1927. {
  1928. struct onenand_chip *this = mtd->priv;
  1929. loff_t addr = instr->addr;
  1930. int len = instr->len;
  1931. int eb_count = 0;
  1932. int ret = 0;
  1933. int bdry_block = 0;
  1934. instr->state = MTD_ERASING;
  1935. if (ONENAND_IS_DDP(this)) {
  1936. loff_t bdry_addr = this->chipsize >> 1;
  1937. if (addr < bdry_addr && (addr + len) > bdry_addr)
  1938. bdry_block = bdry_addr >> this->erase_shift;
  1939. }
  1940. /* Pre-check bbs */
  1941. while (len) {
  1942. /* Check if we have a bad block, we do not erase bad blocks */
  1943. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  1944. printk(KERN_WARNING "%s: attempt to erase a bad block "
  1945. "at addr 0x%012llx\n",
  1946. __func__, (unsigned long long) addr);
  1947. instr->state = MTD_ERASE_FAILED;
  1948. return -EIO;
  1949. }
  1950. len -= block_size;
  1951. addr += block_size;
  1952. }
  1953. len = instr->len;
  1954. addr = instr->addr;
  1955. /* loop over 64 eb batches */
  1956. while (len) {
  1957. struct erase_info verify_instr = *instr;
  1958. int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
  1959. verify_instr.addr = addr;
  1960. verify_instr.len = 0;
  1961. /* do not cross chip boundary */
  1962. if (bdry_block) {
  1963. int this_block = (addr >> this->erase_shift);
  1964. if (this_block < bdry_block) {
  1965. max_eb_count = min(max_eb_count,
  1966. (bdry_block - this_block));
  1967. }
  1968. }
  1969. eb_count = 0;
  1970. while (len > block_size && eb_count < (max_eb_count - 1)) {
  1971. this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
  1972. addr, block_size);
  1973. onenand_invalidate_bufferram(mtd, addr, block_size);
  1974. ret = this->wait(mtd, FL_PREPARING_ERASE);
  1975. if (ret) {
  1976. printk(KERN_ERR "%s: Failed multiblock erase, "
  1977. "block %d\n", __func__,
  1978. onenand_block(this, addr));
  1979. instr->state = MTD_ERASE_FAILED;
  1980. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1981. return -EIO;
  1982. }
  1983. len -= block_size;
  1984. addr += block_size;
  1985. eb_count++;
  1986. }
  1987. /* last block of 64-eb series */
  1988. cond_resched();
  1989. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1990. onenand_invalidate_bufferram(mtd, addr, block_size);
  1991. ret = this->wait(mtd, FL_ERASING);
  1992. /* Check if it is write protected */
  1993. if (ret) {
  1994. printk(KERN_ERR "%s: Failed erase, block %d\n",
  1995. __func__, onenand_block(this, addr));
  1996. instr->state = MTD_ERASE_FAILED;
  1997. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1998. return -EIO;
  1999. }
  2000. len -= block_size;
  2001. addr += block_size;
  2002. eb_count++;
  2003. /* verify */
  2004. verify_instr.len = eb_count * block_size;
  2005. if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
  2006. instr->state = verify_instr.state;
  2007. instr->fail_addr = verify_instr.fail_addr;
  2008. return -EIO;
  2009. }
  2010. }
  2011. return 0;
  2012. }
  2013. /**
  2014. * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
  2015. * @param mtd MTD device structure
  2016. * @param instr erase instruction
  2017. * @param region erase region
  2018. * @param block_size erase block size
  2019. *
  2020. * Erase one or more blocks one block at a time
  2021. */
  2022. static int onenand_block_by_block_erase(struct mtd_info *mtd,
  2023. struct erase_info *instr,
  2024. struct mtd_erase_region_info *region,
  2025. unsigned int block_size)
  2026. {
  2027. struct onenand_chip *this = mtd->priv;
  2028. loff_t addr = instr->addr;
  2029. int len = instr->len;
  2030. loff_t region_end = 0;
  2031. int ret = 0;
  2032. if (region) {
  2033. /* region is set for Flex-OneNAND */
  2034. region_end = region->offset + region->erasesize * region->numblocks;
  2035. }
  2036. instr->state = MTD_ERASING;
  2037. /* Loop through the blocks */
  2038. while (len) {
  2039. cond_resched();
  2040. /* Check if we have a bad block, we do not erase bad blocks */
  2041. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  2042. printk(KERN_WARNING "%s: attempt to erase a bad block "
  2043. "at addr 0x%012llx\n",
  2044. __func__, (unsigned long long) addr);
  2045. instr->state = MTD_ERASE_FAILED;
  2046. return -EIO;
  2047. }
  2048. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  2049. onenand_invalidate_bufferram(mtd, addr, block_size);
  2050. ret = this->wait(mtd, FL_ERASING);
  2051. /* Check, if it is write protected */
  2052. if (ret) {
  2053. printk(KERN_ERR "%s: Failed erase, block %d\n",
  2054. __func__, onenand_block(this, addr));
  2055. instr->state = MTD_ERASE_FAILED;
  2056. instr->fail_addr = addr;
  2057. return -EIO;
  2058. }
  2059. len -= block_size;
  2060. addr += block_size;
  2061. if (region && addr == region_end) {
  2062. if (!len)
  2063. break;
  2064. region++;
  2065. block_size = region->erasesize;
  2066. region_end = region->offset + region->erasesize * region->numblocks;
  2067. if (len & (block_size - 1)) {
  2068. /* FIXME: This should be handled at MTD partitioning level. */
  2069. printk(KERN_ERR "%s: Unaligned address\n",
  2070. __func__);
  2071. return -EIO;
  2072. }
  2073. }
  2074. }
  2075. return 0;
  2076. }
  2077. /**
  2078. * onenand_erase - [MTD Interface] erase block(s)
  2079. * @param mtd MTD device structure
  2080. * @param instr erase instruction
  2081. *
  2082. * Erase one or more blocks
  2083. */
  2084. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2085. {
  2086. struct onenand_chip *this = mtd->priv;
  2087. unsigned int block_size;
  2088. loff_t addr = instr->addr;
  2089. loff_t len = instr->len;
  2090. int ret = 0;
  2091. struct mtd_erase_region_info *region = NULL;
  2092. loff_t region_offset = 0;
  2093. pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
  2094. (unsigned long long)instr->addr,
  2095. (unsigned long long)instr->len);
  2096. if (FLEXONENAND(this)) {
  2097. /* Find the eraseregion of this address */
  2098. int i = flexonenand_region(mtd, addr);
  2099. region = &mtd->eraseregions[i];
  2100. block_size = region->erasesize;
  2101. /* Start address within region must align on block boundary.
  2102. * Erase region's start offset is always block start address.
  2103. */
  2104. region_offset = region->offset;
  2105. } else
  2106. block_size = 1 << this->erase_shift;
  2107. /* Start address must align on block boundary */
  2108. if (unlikely((addr - region_offset) & (block_size - 1))) {
  2109. printk(KERN_ERR "%s: Unaligned address\n", __func__);
  2110. return -EINVAL;
  2111. }
  2112. /* Length must align on block boundary */
  2113. if (unlikely(len & (block_size - 1))) {
  2114. printk(KERN_ERR "%s: Length not block aligned\n", __func__);
  2115. return -EINVAL;
  2116. }
  2117. /* Grab the lock and see if the device is available */
  2118. onenand_get_device(mtd, FL_ERASING);
  2119. if (ONENAND_IS_4KB_PAGE(this) || region ||
  2120. instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
  2121. /* region is set for Flex-OneNAND (no mb erase) */
  2122. ret = onenand_block_by_block_erase(mtd, instr,
  2123. region, block_size);
  2124. } else {
  2125. ret = onenand_multiblock_erase(mtd, instr, block_size);
  2126. }
  2127. /* Deselect and wake up anyone waiting on the device */
  2128. onenand_release_device(mtd);
  2129. /* Do call back function */
  2130. if (!ret) {
  2131. instr->state = MTD_ERASE_DONE;
  2132. mtd_erase_callback(instr);
  2133. }
  2134. return ret;
  2135. }
  2136. /**
  2137. * onenand_sync - [MTD Interface] sync
  2138. * @param mtd MTD device structure
  2139. *
  2140. * Sync is actually a wait for chip ready function
  2141. */
  2142. static void onenand_sync(struct mtd_info *mtd)
  2143. {
  2144. pr_debug("%s: called\n", __func__);
  2145. /* Grab the lock and see if the device is available */
  2146. onenand_get_device(mtd, FL_SYNCING);
  2147. /* Release it and go back */
  2148. onenand_release_device(mtd);
  2149. }
  2150. /**
  2151. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  2152. * @param mtd MTD device structure
  2153. * @param ofs offset relative to mtd start
  2154. *
  2155. * Check whether the block is bad
  2156. */
  2157. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  2158. {
  2159. int ret;
  2160. /* Check for invalid offset */
  2161. if (ofs > mtd->size)
  2162. return -EINVAL;
  2163. onenand_get_device(mtd, FL_READING);
  2164. ret = onenand_block_isbad_nolock(mtd, ofs, 0);
  2165. onenand_release_device(mtd);
  2166. return ret;
  2167. }
  2168. /**
  2169. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  2170. * @param mtd MTD device structure
  2171. * @param ofs offset from device start
  2172. *
  2173. * This is the default implementation, which can be overridden by
  2174. * a hardware specific driver.
  2175. */
  2176. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2177. {
  2178. struct onenand_chip *this = mtd->priv;
  2179. struct bbm_info *bbm = this->bbm;
  2180. u_char buf[2] = {0, 0};
  2181. struct mtd_oob_ops ops = {
  2182. .mode = MTD_OPS_PLACE_OOB,
  2183. .ooblen = 2,
  2184. .oobbuf = buf,
  2185. .ooboffs = 0,
  2186. };
  2187. int block;
  2188. /* Get block number */
  2189. block = onenand_block(this, ofs);
  2190. if (bbm->bbt)
  2191. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  2192. /* We write two bytes, so we don't have to mess with 16-bit access */
  2193. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  2194. /* FIXME : What to do when marking SLC block in partition
  2195. * with MLC erasesize? For now, it is not advisable to
  2196. * create partitions containing both SLC and MLC regions.
  2197. */
  2198. return onenand_write_oob_nolock(mtd, ofs, &ops);
  2199. }
  2200. /**
  2201. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  2202. * @param mtd MTD device structure
  2203. * @param ofs offset relative to mtd start
  2204. *
  2205. * Mark the block as bad
  2206. */
  2207. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2208. {
  2209. int ret;
  2210. ret = onenand_block_isbad(mtd, ofs);
  2211. if (ret) {
  2212. /* If it was bad already, return success and do nothing */
  2213. if (ret > 0)
  2214. return 0;
  2215. return ret;
  2216. }
  2217. onenand_get_device(mtd, FL_WRITING);
  2218. ret = mtd_block_markbad(mtd, ofs);
  2219. onenand_release_device(mtd);
  2220. return ret;
  2221. }
  2222. /**
  2223. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  2224. * @param mtd MTD device structure
  2225. * @param ofs offset relative to mtd start
  2226. * @param len number of bytes to lock or unlock
  2227. * @param cmd lock or unlock command
  2228. *
  2229. * Lock or unlock one or more blocks
  2230. */
  2231. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  2232. {
  2233. struct onenand_chip *this = mtd->priv;
  2234. int start, end, block, value, status;
  2235. int wp_status_mask;
  2236. start = onenand_block(this, ofs);
  2237. end = onenand_block(this, ofs + len) - 1;
  2238. if (cmd == ONENAND_CMD_LOCK)
  2239. wp_status_mask = ONENAND_WP_LS;
  2240. else
  2241. wp_status_mask = ONENAND_WP_US;
  2242. /* Continuous lock scheme */
  2243. if (this->options & ONENAND_HAS_CONT_LOCK) {
  2244. /* Set start block address */
  2245. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2246. /* Set end block address */
  2247. this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  2248. /* Write lock command */
  2249. this->command(mtd, cmd, 0, 0);
  2250. /* There's no return value */
  2251. this->wait(mtd, FL_LOCKING);
  2252. /* Sanity check */
  2253. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2254. & ONENAND_CTRL_ONGO)
  2255. continue;
  2256. /* Check lock status */
  2257. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2258. if (!(status & wp_status_mask))
  2259. printk(KERN_ERR "%s: wp status = 0x%x\n",
  2260. __func__, status);
  2261. return 0;
  2262. }
  2263. /* Block lock scheme */
  2264. for (block = start; block < end + 1; block++) {
  2265. /* Set block address */
  2266. value = onenand_block_address(this, block);
  2267. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2268. /* Select DataRAM for DDP */
  2269. value = onenand_bufferram_address(this, block);
  2270. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2271. /* Set start block address */
  2272. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2273. /* Write lock command */
  2274. this->command(mtd, cmd, 0, 0);
  2275. /* There's no return value */
  2276. this->wait(mtd, FL_LOCKING);
  2277. /* Sanity check */
  2278. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2279. & ONENAND_CTRL_ONGO)
  2280. continue;
  2281. /* Check lock status */
  2282. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2283. if (!(status & wp_status_mask))
  2284. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2285. __func__, block, status);
  2286. }
  2287. return 0;
  2288. }
  2289. /**
  2290. * onenand_lock - [MTD Interface] Lock block(s)
  2291. * @param mtd MTD device structure
  2292. * @param ofs offset relative to mtd start
  2293. * @param len number of bytes to unlock
  2294. *
  2295. * Lock one or more blocks
  2296. */
  2297. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2298. {
  2299. int ret;
  2300. onenand_get_device(mtd, FL_LOCKING);
  2301. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  2302. onenand_release_device(mtd);
  2303. return ret;
  2304. }
  2305. /**
  2306. * onenand_unlock - [MTD Interface] Unlock block(s)
  2307. * @param mtd MTD device structure
  2308. * @param ofs offset relative to mtd start
  2309. * @param len number of bytes to unlock
  2310. *
  2311. * Unlock one or more blocks
  2312. */
  2313. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2314. {
  2315. int ret;
  2316. onenand_get_device(mtd, FL_LOCKING);
  2317. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2318. onenand_release_device(mtd);
  2319. return ret;
  2320. }
  2321. /**
  2322. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  2323. * @param this onenand chip data structure
  2324. *
  2325. * Check lock status
  2326. */
  2327. static int onenand_check_lock_status(struct onenand_chip *this)
  2328. {
  2329. unsigned int value, block, status;
  2330. unsigned int end;
  2331. end = this->chipsize >> this->erase_shift;
  2332. for (block = 0; block < end; block++) {
  2333. /* Set block address */
  2334. value = onenand_block_address(this, block);
  2335. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2336. /* Select DataRAM for DDP */
  2337. value = onenand_bufferram_address(this, block);
  2338. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2339. /* Set start block address */
  2340. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2341. /* Check lock status */
  2342. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2343. if (!(status & ONENAND_WP_US)) {
  2344. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2345. __func__, block, status);
  2346. return 0;
  2347. }
  2348. }
  2349. return 1;
  2350. }
  2351. /**
  2352. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  2353. * @param mtd MTD device structure
  2354. *
  2355. * Unlock all blocks
  2356. */
  2357. static void onenand_unlock_all(struct mtd_info *mtd)
  2358. {
  2359. struct onenand_chip *this = mtd->priv;
  2360. loff_t ofs = 0;
  2361. loff_t len = mtd->size;
  2362. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  2363. /* Set start block address */
  2364. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2365. /* Write unlock command */
  2366. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  2367. /* There's no return value */
  2368. this->wait(mtd, FL_LOCKING);
  2369. /* Sanity check */
  2370. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2371. & ONENAND_CTRL_ONGO)
  2372. continue;
  2373. /* Don't check lock status */
  2374. if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
  2375. return;
  2376. /* Check lock status */
  2377. if (onenand_check_lock_status(this))
  2378. return;
  2379. /* Workaround for all block unlock in DDP */
  2380. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  2381. /* All blocks on another chip */
  2382. ofs = this->chipsize >> 1;
  2383. len = this->chipsize >> 1;
  2384. }
  2385. }
  2386. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2387. }
  2388. #ifdef CONFIG_MTD_ONENAND_OTP
  2389. /**
  2390. * onenand_otp_command - Send OTP specific command to OneNAND device
  2391. * @param mtd MTD device structure
  2392. * @param cmd the command to be sent
  2393. * @param addr offset to read from or write to
  2394. * @param len number of bytes to read or write
  2395. */
  2396. static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
  2397. size_t len)
  2398. {
  2399. struct onenand_chip *this = mtd->priv;
  2400. int value, block, page;
  2401. /* Address translation */
  2402. switch (cmd) {
  2403. case ONENAND_CMD_OTP_ACCESS:
  2404. block = (int) (addr >> this->erase_shift);
  2405. page = -1;
  2406. break;
  2407. default:
  2408. block = (int) (addr >> this->erase_shift);
  2409. page = (int) (addr >> this->page_shift);
  2410. if (ONENAND_IS_2PLANE(this)) {
  2411. /* Make the even block number */
  2412. block &= ~1;
  2413. /* Is it the odd plane? */
  2414. if (addr & this->writesize)
  2415. block++;
  2416. page >>= 1;
  2417. }
  2418. page &= this->page_mask;
  2419. break;
  2420. }
  2421. if (block != -1) {
  2422. /* Write 'DFS, FBA' of Flash */
  2423. value = onenand_block_address(this, block);
  2424. this->write_word(value, this->base +
  2425. ONENAND_REG_START_ADDRESS1);
  2426. }
  2427. if (page != -1) {
  2428. /* Now we use page size operation */
  2429. int sectors = 4, count = 4;
  2430. int dataram;
  2431. switch (cmd) {
  2432. default:
  2433. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  2434. cmd = ONENAND_CMD_2X_PROG;
  2435. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  2436. break;
  2437. }
  2438. /* Write 'FPA, FSA' of Flash */
  2439. value = onenand_page_address(page, sectors);
  2440. this->write_word(value, this->base +
  2441. ONENAND_REG_START_ADDRESS8);
  2442. /* Write 'BSA, BSC' of DataRAM */
  2443. value = onenand_buffer_address(dataram, sectors, count);
  2444. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  2445. }
  2446. /* Interrupt clear */
  2447. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  2448. /* Write command */
  2449. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  2450. return 0;
  2451. }
  2452. /**
  2453. * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
  2454. * @param mtd MTD device structure
  2455. * @param to offset to write to
  2456. * @param len number of bytes to write
  2457. * @param retlen pointer to variable to store the number of written bytes
  2458. * @param buf the data to write
  2459. *
  2460. * OneNAND write out-of-band only for OTP
  2461. */
  2462. static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  2463. struct mtd_oob_ops *ops)
  2464. {
  2465. struct onenand_chip *this = mtd->priv;
  2466. int column, ret = 0, oobsize;
  2467. int written = 0;
  2468. u_char *oobbuf;
  2469. size_t len = ops->ooblen;
  2470. const u_char *buf = ops->oobbuf;
  2471. int block, value, status;
  2472. to += ops->ooboffs;
  2473. /* Initialize retlen, in case of early exit */
  2474. ops->oobretlen = 0;
  2475. oobsize = mtd->oobsize;
  2476. column = to & (mtd->oobsize - 1);
  2477. oobbuf = this->oob_buf;
  2478. /* Loop until all data write */
  2479. while (written < len) {
  2480. int thislen = min_t(int, oobsize, len - written);
  2481. cond_resched();
  2482. block = (int) (to >> this->erase_shift);
  2483. /*
  2484. * Write 'DFS, FBA' of Flash
  2485. * Add: F100h DQ=DFS, FBA
  2486. */
  2487. value = onenand_block_address(this, block);
  2488. this->write_word(value, this->base +
  2489. ONENAND_REG_START_ADDRESS1);
  2490. /*
  2491. * Select DataRAM for DDP
  2492. * Add: F101h DQ=DBS
  2493. */
  2494. value = onenand_bufferram_address(this, block);
  2495. this->write_word(value, this->base +
  2496. ONENAND_REG_START_ADDRESS2);
  2497. ONENAND_SET_NEXT_BUFFERRAM(this);
  2498. /*
  2499. * Enter OTP access mode
  2500. */
  2501. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2502. this->wait(mtd, FL_OTPING);
  2503. /* We send data to spare ram with oobsize
  2504. * to prevent byte access */
  2505. memcpy(oobbuf + column, buf, thislen);
  2506. /*
  2507. * Write Data into DataRAM
  2508. * Add: 8th Word
  2509. * in sector0/spare/page0
  2510. * DQ=XXFCh
  2511. */
  2512. this->write_bufferram(mtd, ONENAND_SPARERAM,
  2513. oobbuf, 0, mtd->oobsize);
  2514. onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  2515. onenand_update_bufferram(mtd, to, 0);
  2516. if (ONENAND_IS_2PLANE(this)) {
  2517. ONENAND_SET_BUFFERRAM1(this);
  2518. onenand_update_bufferram(mtd, to + this->writesize, 0);
  2519. }
  2520. ret = this->wait(mtd, FL_WRITING);
  2521. if (ret) {
  2522. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  2523. break;
  2524. }
  2525. /* Exit OTP access mode */
  2526. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2527. this->wait(mtd, FL_RESETING);
  2528. status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  2529. status &= 0x60;
  2530. if (status == 0x60) {
  2531. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2532. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2533. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2534. } else if (status == 0x20) {
  2535. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2536. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2537. printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
  2538. } else if (status == 0x40) {
  2539. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2540. printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
  2541. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2542. } else {
  2543. printk(KERN_DEBUG "Reboot to check\n");
  2544. }
  2545. written += thislen;
  2546. if (written == len)
  2547. break;
  2548. to += mtd->writesize;
  2549. buf += thislen;
  2550. column = 0;
  2551. }
  2552. ops->oobretlen = written;
  2553. return ret;
  2554. }
  2555. /* Internal OTP operation */
  2556. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  2557. size_t *retlen, u_char *buf);
  2558. /**
  2559. * do_otp_read - [DEFAULT] Read OTP block area
  2560. * @param mtd MTD device structure
  2561. * @param from The offset to read
  2562. * @param len number of bytes to read
  2563. * @param retlen pointer to variable to store the number of readbytes
  2564. * @param buf the databuffer to put/get data
  2565. *
  2566. * Read OTP block area.
  2567. */
  2568. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  2569. size_t *retlen, u_char *buf)
  2570. {
  2571. struct onenand_chip *this = mtd->priv;
  2572. struct mtd_oob_ops ops = {
  2573. .len = len,
  2574. .ooblen = 0,
  2575. .datbuf = buf,
  2576. .oobbuf = NULL,
  2577. };
  2578. int ret;
  2579. /* Enter OTP access mode */
  2580. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2581. this->wait(mtd, FL_OTPING);
  2582. ret = ONENAND_IS_4KB_PAGE(this) ?
  2583. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  2584. onenand_read_ops_nolock(mtd, from, &ops);
  2585. /* Exit OTP access mode */
  2586. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2587. this->wait(mtd, FL_RESETING);
  2588. return ret;
  2589. }
  2590. /**
  2591. * do_otp_write - [DEFAULT] Write OTP block area
  2592. * @param mtd MTD device structure
  2593. * @param to The offset to write
  2594. * @param len number of bytes to write
  2595. * @param retlen pointer to variable to store the number of write bytes
  2596. * @param buf the databuffer to put/get data
  2597. *
  2598. * Write OTP block area.
  2599. */
  2600. static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  2601. size_t *retlen, u_char *buf)
  2602. {
  2603. struct onenand_chip *this = mtd->priv;
  2604. unsigned char *pbuf = buf;
  2605. int ret;
  2606. struct mtd_oob_ops ops;
  2607. /* Force buffer page aligned */
  2608. if (len < mtd->writesize) {
  2609. memcpy(this->page_buf, buf, len);
  2610. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  2611. pbuf = this->page_buf;
  2612. len = mtd->writesize;
  2613. }
  2614. /* Enter OTP access mode */
  2615. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2616. this->wait(mtd, FL_OTPING);
  2617. ops.len = len;
  2618. ops.ooblen = 0;
  2619. ops.datbuf = pbuf;
  2620. ops.oobbuf = NULL;
  2621. ret = onenand_write_ops_nolock(mtd, to, &ops);
  2622. *retlen = ops.retlen;
  2623. /* Exit OTP access mode */
  2624. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2625. this->wait(mtd, FL_RESETING);
  2626. return ret;
  2627. }
  2628. /**
  2629. * do_otp_lock - [DEFAULT] Lock OTP block area
  2630. * @param mtd MTD device structure
  2631. * @param from The offset to lock
  2632. * @param len number of bytes to lock
  2633. * @param retlen pointer to variable to store the number of lock bytes
  2634. * @param buf the databuffer to put/get data
  2635. *
  2636. * Lock OTP block area.
  2637. */
  2638. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  2639. size_t *retlen, u_char *buf)
  2640. {
  2641. struct onenand_chip *this = mtd->priv;
  2642. struct mtd_oob_ops ops;
  2643. int ret;
  2644. if (FLEXONENAND(this)) {
  2645. /* Enter OTP access mode */
  2646. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2647. this->wait(mtd, FL_OTPING);
  2648. /*
  2649. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2650. * main area of page 49.
  2651. */
  2652. ops.len = mtd->writesize;
  2653. ops.ooblen = 0;
  2654. ops.datbuf = buf;
  2655. ops.oobbuf = NULL;
  2656. ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
  2657. *retlen = ops.retlen;
  2658. /* Exit OTP access mode */
  2659. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2660. this->wait(mtd, FL_RESETING);
  2661. } else {
  2662. ops.mode = MTD_OPS_PLACE_OOB;
  2663. ops.ooblen = len;
  2664. ops.oobbuf = buf;
  2665. ops.ooboffs = 0;
  2666. ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
  2667. *retlen = ops.oobretlen;
  2668. }
  2669. return ret;
  2670. }
  2671. /**
  2672. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  2673. * @param mtd MTD device structure
  2674. * @param from The offset to read/write
  2675. * @param len number of bytes to read/write
  2676. * @param retlen pointer to variable to store the number of read bytes
  2677. * @param buf the databuffer to put/get data
  2678. * @param action do given action
  2679. * @param mode specify user and factory
  2680. *
  2681. * Handle OTP operation.
  2682. */
  2683. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  2684. size_t *retlen, u_char *buf,
  2685. otp_op_t action, int mode)
  2686. {
  2687. struct onenand_chip *this = mtd->priv;
  2688. int otp_pages;
  2689. int density;
  2690. int ret = 0;
  2691. *retlen = 0;
  2692. density = onenand_get_density(this->device_id);
  2693. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  2694. otp_pages = 20;
  2695. else
  2696. otp_pages = 50;
  2697. if (mode == MTD_OTP_FACTORY) {
  2698. from += mtd->writesize * otp_pages;
  2699. otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
  2700. }
  2701. /* Check User/Factory boundary */
  2702. if (mode == MTD_OTP_USER) {
  2703. if (mtd->writesize * otp_pages < from + len)
  2704. return 0;
  2705. } else {
  2706. if (mtd->writesize * otp_pages < len)
  2707. return 0;
  2708. }
  2709. onenand_get_device(mtd, FL_OTPING);
  2710. while (len > 0 && otp_pages > 0) {
  2711. if (!action) { /* OTP Info functions */
  2712. struct otp_info *otpinfo;
  2713. len -= sizeof(struct otp_info);
  2714. if (len <= 0) {
  2715. ret = -ENOSPC;
  2716. break;
  2717. }
  2718. otpinfo = (struct otp_info *) buf;
  2719. otpinfo->start = from;
  2720. otpinfo->length = mtd->writesize;
  2721. otpinfo->locked = 0;
  2722. from += mtd->writesize;
  2723. buf += sizeof(struct otp_info);
  2724. *retlen += sizeof(struct otp_info);
  2725. } else {
  2726. size_t tmp_retlen;
  2727. ret = action(mtd, from, len, &tmp_retlen, buf);
  2728. buf += tmp_retlen;
  2729. len -= tmp_retlen;
  2730. *retlen += tmp_retlen;
  2731. if (ret)
  2732. break;
  2733. }
  2734. otp_pages--;
  2735. }
  2736. onenand_release_device(mtd);
  2737. return ret;
  2738. }
  2739. /**
  2740. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  2741. * @param mtd MTD device structure
  2742. * @param buf the databuffer to put/get data
  2743. * @param len number of bytes to read
  2744. *
  2745. * Read factory OTP info.
  2746. */
  2747. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  2748. struct otp_info *buf, size_t len)
  2749. {
  2750. size_t retlen;
  2751. int ret;
  2752. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  2753. return ret ? : retlen;
  2754. }
  2755. /**
  2756. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  2757. * @param mtd MTD device structure
  2758. * @param from The offset to read
  2759. * @param len number of bytes to read
  2760. * @param retlen pointer to variable to store the number of read bytes
  2761. * @param buf the databuffer to put/get data
  2762. *
  2763. * Read factory OTP area.
  2764. */
  2765. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  2766. size_t len, size_t *retlen, u_char *buf)
  2767. {
  2768. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  2769. }
  2770. /**
  2771. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  2772. * @param mtd MTD device structure
  2773. * @param buf the databuffer to put/get data
  2774. * @param len number of bytes to read
  2775. *
  2776. * Read user OTP info.
  2777. */
  2778. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  2779. struct otp_info *buf, size_t len)
  2780. {
  2781. size_t retlen;
  2782. int ret;
  2783. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  2784. return ret ? : retlen;
  2785. }
  2786. /**
  2787. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  2788. * @param mtd MTD device structure
  2789. * @param from The offset to read
  2790. * @param len number of bytes to read
  2791. * @param retlen pointer to variable to store the number of read bytes
  2792. * @param buf the databuffer to put/get data
  2793. *
  2794. * Read user OTP area.
  2795. */
  2796. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2797. size_t len, size_t *retlen, u_char *buf)
  2798. {
  2799. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  2800. }
  2801. /**
  2802. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  2803. * @param mtd MTD device structure
  2804. * @param from The offset to write
  2805. * @param len number of bytes to write
  2806. * @param retlen pointer to variable to store the number of write bytes
  2807. * @param buf the databuffer to put/get data
  2808. *
  2809. * Write user OTP area.
  2810. */
  2811. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2812. size_t len, size_t *retlen, u_char *buf)
  2813. {
  2814. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  2815. }
  2816. /**
  2817. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  2818. * @param mtd MTD device structure
  2819. * @param from The offset to lock
  2820. * @param len number of bytes to unlock
  2821. *
  2822. * Write lock mark on spare area in page 0 in OTP block
  2823. */
  2824. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2825. size_t len)
  2826. {
  2827. struct onenand_chip *this = mtd->priv;
  2828. u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
  2829. size_t retlen;
  2830. int ret;
  2831. unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
  2832. memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
  2833. : mtd->oobsize);
  2834. /*
  2835. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  2836. * We write 16 bytes spare area instead of 2 bytes.
  2837. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2838. * main area of page 49.
  2839. */
  2840. from = 0;
  2841. len = FLEXONENAND(this) ? mtd->writesize : 16;
  2842. /*
  2843. * Note: OTP lock operation
  2844. * OTP block : 0xXXFC XX 1111 1100
  2845. * 1st block : 0xXXF3 (If chip support) XX 1111 0011
  2846. * Both : 0xXXF0 (If chip support) XX 1111 0000
  2847. */
  2848. if (FLEXONENAND(this))
  2849. otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
  2850. /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
  2851. if (otp == 1)
  2852. buf[otp_lock_offset] = 0xFC;
  2853. else if (otp == 2)
  2854. buf[otp_lock_offset] = 0xF3;
  2855. else if (otp == 3)
  2856. buf[otp_lock_offset] = 0xF0;
  2857. else if (otp != 0)
  2858. printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
  2859. ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
  2860. return ret ? : retlen;
  2861. }
  2862. #endif /* CONFIG_MTD_ONENAND_OTP */
  2863. /**
  2864. * onenand_check_features - Check and set OneNAND features
  2865. * @param mtd MTD data structure
  2866. *
  2867. * Check and set OneNAND features
  2868. * - lock scheme
  2869. * - two plane
  2870. */
  2871. static void onenand_check_features(struct mtd_info *mtd)
  2872. {
  2873. struct onenand_chip *this = mtd->priv;
  2874. unsigned int density, process, numbufs;
  2875. /* Lock scheme depends on density and process */
  2876. density = onenand_get_density(this->device_id);
  2877. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  2878. numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
  2879. /* Lock scheme */
  2880. switch (density) {
  2881. case ONENAND_DEVICE_DENSITY_4Gb:
  2882. if (ONENAND_IS_DDP(this))
  2883. this->options |= ONENAND_HAS_2PLANE;
  2884. else if (numbufs == 1) {
  2885. this->options |= ONENAND_HAS_4KB_PAGE;
  2886. this->options |= ONENAND_HAS_CACHE_PROGRAM;
  2887. /*
  2888. * There are two different 4KiB pagesize chips
  2889. * and no way to detect it by H/W config values.
  2890. *
  2891. * To detect the correct NOP for each chips,
  2892. * It should check the version ID as workaround.
  2893. *
  2894. * Now it has as following
  2895. * KFM4G16Q4M has NOP 4 with version ID 0x0131
  2896. * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
  2897. */
  2898. if ((this->version_id & 0xf) == 0xe)
  2899. this->options |= ONENAND_HAS_NOP_1;
  2900. }
  2901. case ONENAND_DEVICE_DENSITY_2Gb:
  2902. /* 2Gb DDP does not have 2 plane */
  2903. if (!ONENAND_IS_DDP(this))
  2904. this->options |= ONENAND_HAS_2PLANE;
  2905. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2906. case ONENAND_DEVICE_DENSITY_1Gb:
  2907. /* A-Die has all block unlock */
  2908. if (process)
  2909. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2910. break;
  2911. default:
  2912. /* Some OneNAND has continuous lock scheme */
  2913. if (!process)
  2914. this->options |= ONENAND_HAS_CONT_LOCK;
  2915. break;
  2916. }
  2917. /* The MLC has 4KiB pagesize. */
  2918. if (ONENAND_IS_MLC(this))
  2919. this->options |= ONENAND_HAS_4KB_PAGE;
  2920. if (ONENAND_IS_4KB_PAGE(this))
  2921. this->options &= ~ONENAND_HAS_2PLANE;
  2922. if (FLEXONENAND(this)) {
  2923. this->options &= ~ONENAND_HAS_CONT_LOCK;
  2924. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2925. }
  2926. if (this->options & ONENAND_HAS_CONT_LOCK)
  2927. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  2928. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  2929. printk(KERN_DEBUG "Chip support all block unlock\n");
  2930. if (this->options & ONENAND_HAS_2PLANE)
  2931. printk(KERN_DEBUG "Chip has 2 plane\n");
  2932. if (this->options & ONENAND_HAS_4KB_PAGE)
  2933. printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
  2934. if (this->options & ONENAND_HAS_CACHE_PROGRAM)
  2935. printk(KERN_DEBUG "Chip has cache program feature\n");
  2936. }
  2937. /**
  2938. * onenand_print_device_info - Print device & version ID
  2939. * @param device device ID
  2940. * @param version version ID
  2941. *
  2942. * Print device & version ID
  2943. */
  2944. static void onenand_print_device_info(int device, int version)
  2945. {
  2946. int vcc, demuxed, ddp, density, flexonenand;
  2947. vcc = device & ONENAND_DEVICE_VCC_MASK;
  2948. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  2949. ddp = device & ONENAND_DEVICE_IS_DDP;
  2950. density = onenand_get_density(device);
  2951. flexonenand = device & DEVICE_IS_FLEXONENAND;
  2952. printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  2953. demuxed ? "" : "Muxed ",
  2954. flexonenand ? "Flex-" : "",
  2955. ddp ? "(DDP)" : "",
  2956. (16 << density),
  2957. vcc ? "2.65/3.3" : "1.8",
  2958. device);
  2959. printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
  2960. }
  2961. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  2962. {ONENAND_MFR_SAMSUNG, "Samsung"},
  2963. {ONENAND_MFR_NUMONYX, "Numonyx"},
  2964. };
  2965. /**
  2966. * onenand_check_maf - Check manufacturer ID
  2967. * @param manuf manufacturer ID
  2968. *
  2969. * Check manufacturer ID
  2970. */
  2971. static int onenand_check_maf(int manuf)
  2972. {
  2973. int size = ARRAY_SIZE(onenand_manuf_ids);
  2974. char *name;
  2975. int i;
  2976. for (i = 0; i < size; i++)
  2977. if (manuf == onenand_manuf_ids[i].id)
  2978. break;
  2979. if (i < size)
  2980. name = onenand_manuf_ids[i].name;
  2981. else
  2982. name = "Unknown";
  2983. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2984. return (i == size);
  2985. }
  2986. /**
  2987. * flexonenand_get_boundary - Reads the SLC boundary
  2988. * @param onenand_info - onenand info structure
  2989. **/
  2990. static int flexonenand_get_boundary(struct mtd_info *mtd)
  2991. {
  2992. struct onenand_chip *this = mtd->priv;
  2993. unsigned die, bdry;
  2994. int ret, syscfg, locked;
  2995. /* Disable ECC */
  2996. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2997. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  2998. for (die = 0; die < this->dies; die++) {
  2999. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  3000. this->wait(mtd, FL_SYNCING);
  3001. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3002. ret = this->wait(mtd, FL_READING);
  3003. bdry = this->read_word(this->base + ONENAND_DATARAM);
  3004. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  3005. locked = 0;
  3006. else
  3007. locked = 1;
  3008. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  3009. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  3010. ret = this->wait(mtd, FL_RESETING);
  3011. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  3012. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  3013. }
  3014. /* Enable ECC */
  3015. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3016. return 0;
  3017. }
  3018. /**
  3019. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  3020. * boundary[], diesize[], mtd->size, mtd->erasesize
  3021. * @param mtd - MTD device structure
  3022. */
  3023. static void flexonenand_get_size(struct mtd_info *mtd)
  3024. {
  3025. struct onenand_chip *this = mtd->priv;
  3026. int die, i, eraseshift, density;
  3027. int blksperdie, maxbdry;
  3028. loff_t ofs;
  3029. density = onenand_get_density(this->device_id);
  3030. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  3031. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3032. maxbdry = blksperdie - 1;
  3033. eraseshift = this->erase_shift - 1;
  3034. mtd->numeraseregions = this->dies << 1;
  3035. /* This fills up the device boundary */
  3036. flexonenand_get_boundary(mtd);
  3037. die = ofs = 0;
  3038. i = -1;
  3039. for (; die < this->dies; die++) {
  3040. if (!die || this->boundary[die-1] != maxbdry) {
  3041. i++;
  3042. mtd->eraseregions[i].offset = ofs;
  3043. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3044. mtd->eraseregions[i].numblocks =
  3045. this->boundary[die] + 1;
  3046. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3047. eraseshift++;
  3048. } else {
  3049. mtd->numeraseregions -= 1;
  3050. mtd->eraseregions[i].numblocks +=
  3051. this->boundary[die] + 1;
  3052. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  3053. }
  3054. if (this->boundary[die] != maxbdry) {
  3055. i++;
  3056. mtd->eraseregions[i].offset = ofs;
  3057. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3058. mtd->eraseregions[i].numblocks = maxbdry ^
  3059. this->boundary[die];
  3060. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3061. eraseshift--;
  3062. } else
  3063. mtd->numeraseregions -= 1;
  3064. }
  3065. /* Expose MLC erase size except when all blocks are SLC */
  3066. mtd->erasesize = 1 << this->erase_shift;
  3067. if (mtd->numeraseregions == 1)
  3068. mtd->erasesize >>= 1;
  3069. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  3070. for (i = 0; i < mtd->numeraseregions; i++)
  3071. printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
  3072. " numblocks: %04u]\n",
  3073. (unsigned int) mtd->eraseregions[i].offset,
  3074. mtd->eraseregions[i].erasesize,
  3075. mtd->eraseregions[i].numblocks);
  3076. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  3077. this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
  3078. this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
  3079. << (this->erase_shift - 1);
  3080. mtd->size += this->diesize[die];
  3081. }
  3082. }
  3083. /**
  3084. * flexonenand_check_blocks_erased - Check if blocks are erased
  3085. * @param mtd_info - mtd info structure
  3086. * @param start - first erase block to check
  3087. * @param end - last erase block to check
  3088. *
  3089. * Converting an unerased block from MLC to SLC
  3090. * causes byte values to change. Since both data and its ECC
  3091. * have changed, reads on the block give uncorrectable error.
  3092. * This might lead to the block being detected as bad.
  3093. *
  3094. * Avoid this by ensuring that the block to be converted is
  3095. * erased.
  3096. */
  3097. static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
  3098. {
  3099. struct onenand_chip *this = mtd->priv;
  3100. int i, ret;
  3101. int block;
  3102. struct mtd_oob_ops ops = {
  3103. .mode = MTD_OPS_PLACE_OOB,
  3104. .ooboffs = 0,
  3105. .ooblen = mtd->oobsize,
  3106. .datbuf = NULL,
  3107. .oobbuf = this->oob_buf,
  3108. };
  3109. loff_t addr;
  3110. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  3111. for (block = start; block <= end; block++) {
  3112. addr = flexonenand_addr(this, block);
  3113. if (onenand_block_isbad_nolock(mtd, addr, 0))
  3114. continue;
  3115. /*
  3116. * Since main area write results in ECC write to spare,
  3117. * it is sufficient to check only ECC bytes for change.
  3118. */
  3119. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  3120. if (ret)
  3121. return ret;
  3122. for (i = 0; i < mtd->oobsize; i++)
  3123. if (this->oob_buf[i] != 0xff)
  3124. break;
  3125. if (i != mtd->oobsize) {
  3126. printk(KERN_WARNING "%s: Block %d not erased.\n",
  3127. __func__, block);
  3128. return 1;
  3129. }
  3130. }
  3131. return 0;
  3132. }
  3133. /**
  3134. * flexonenand_set_boundary - Writes the SLC boundary
  3135. * @param mtd - mtd info structure
  3136. */
  3137. static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  3138. int boundary, int lock)
  3139. {
  3140. struct onenand_chip *this = mtd->priv;
  3141. int ret, density, blksperdie, old, new, thisboundary;
  3142. loff_t addr;
  3143. /* Change only once for SDP Flex-OneNAND */
  3144. if (die && (!ONENAND_IS_DDP(this)))
  3145. return 0;
  3146. /* boundary value of -1 indicates no required change */
  3147. if (boundary < 0 || boundary == this->boundary[die])
  3148. return 0;
  3149. density = onenand_get_density(this->device_id);
  3150. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  3151. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3152. if (boundary >= blksperdie) {
  3153. printk(KERN_ERR "%s: Invalid boundary value. "
  3154. "Boundary not changed.\n", __func__);
  3155. return -EINVAL;
  3156. }
  3157. /* Check if converting blocks are erased */
  3158. old = this->boundary[die] + (die * this->density_mask);
  3159. new = boundary + (die * this->density_mask);
  3160. ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
  3161. if (ret) {
  3162. printk(KERN_ERR "%s: Please erase blocks "
  3163. "before boundary change\n", __func__);
  3164. return ret;
  3165. }
  3166. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  3167. this->wait(mtd, FL_SYNCING);
  3168. /* Check is boundary is locked */
  3169. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3170. ret = this->wait(mtd, FL_READING);
  3171. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  3172. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  3173. printk(KERN_ERR "%s: boundary locked\n", __func__);
  3174. ret = 1;
  3175. goto out;
  3176. }
  3177. printk(KERN_INFO "Changing die %d boundary: %d%s\n",
  3178. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  3179. addr = die ? this->diesize[0] : 0;
  3180. boundary &= FLEXONENAND_PI_MASK;
  3181. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  3182. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  3183. ret = this->wait(mtd, FL_ERASING);
  3184. if (ret) {
  3185. printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
  3186. __func__, die);
  3187. goto out;
  3188. }
  3189. this->write_word(boundary, this->base + ONENAND_DATARAM);
  3190. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  3191. ret = this->wait(mtd, FL_WRITING);
  3192. if (ret) {
  3193. printk(KERN_ERR "%s: Failed PI write for Die %d\n",
  3194. __func__, die);
  3195. goto out;
  3196. }
  3197. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  3198. ret = this->wait(mtd, FL_WRITING);
  3199. out:
  3200. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  3201. this->wait(mtd, FL_RESETING);
  3202. if (!ret)
  3203. /* Recalculate device size on boundary change*/
  3204. flexonenand_get_size(mtd);
  3205. return ret;
  3206. }
  3207. /**
  3208. * onenand_chip_probe - [OneNAND Interface] The generic chip probe
  3209. * @param mtd MTD device structure
  3210. *
  3211. * OneNAND detection method:
  3212. * Compare the values from command with ones from register
  3213. */
  3214. static int onenand_chip_probe(struct mtd_info *mtd)
  3215. {
  3216. struct onenand_chip *this = mtd->priv;
  3217. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  3218. int syscfg;
  3219. /* Save system configuration 1 */
  3220. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  3221. /* Clear Sync. Burst Read mode to read BootRAM */
  3222. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
  3223. /* Send the command for reading device ID from BootRAM */
  3224. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  3225. /* Read manufacturer and device IDs from BootRAM */
  3226. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  3227. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  3228. /* Reset OneNAND to read default register values */
  3229. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  3230. /* Wait reset */
  3231. this->wait(mtd, FL_RESETING);
  3232. /* Restore system configuration 1 */
  3233. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3234. /* Check manufacturer ID */
  3235. if (onenand_check_maf(bram_maf_id))
  3236. return -ENXIO;
  3237. /* Read manufacturer and device IDs from Register */
  3238. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3239. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3240. /* Check OneNAND device */
  3241. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  3242. return -ENXIO;
  3243. return 0;
  3244. }
  3245. /**
  3246. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  3247. * @param mtd MTD device structure
  3248. */
  3249. static int onenand_probe(struct mtd_info *mtd)
  3250. {
  3251. struct onenand_chip *this = mtd->priv;
  3252. int maf_id, dev_id, ver_id;
  3253. int density;
  3254. int ret;
  3255. ret = this->chip_probe(mtd);
  3256. if (ret)
  3257. return ret;
  3258. /* Read manufacturer and device IDs from Register */
  3259. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3260. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3261. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  3262. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  3263. /* Flash device information */
  3264. onenand_print_device_info(dev_id, ver_id);
  3265. this->device_id = dev_id;
  3266. this->version_id = ver_id;
  3267. /* Check OneNAND features */
  3268. onenand_check_features(mtd);
  3269. density = onenand_get_density(dev_id);
  3270. if (FLEXONENAND(this)) {
  3271. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  3272. /* Maximum possible erase regions */
  3273. mtd->numeraseregions = this->dies << 1;
  3274. mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
  3275. * (this->dies << 1), GFP_KERNEL);
  3276. if (!mtd->eraseregions)
  3277. return -ENOMEM;
  3278. }
  3279. /*
  3280. * For Flex-OneNAND, chipsize represents maximum possible device size.
  3281. * mtd->size represents the actual device size.
  3282. */
  3283. this->chipsize = (16 << density) << 20;
  3284. /* OneNAND page size & block size */
  3285. /* The data buffer size is equal to page size */
  3286. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  3287. /* We use the full BufferRAM */
  3288. if (ONENAND_IS_4KB_PAGE(this))
  3289. mtd->writesize <<= 1;
  3290. mtd->oobsize = mtd->writesize >> 5;
  3291. /* Pages per a block are always 64 in OneNAND */
  3292. mtd->erasesize = mtd->writesize << 6;
  3293. /*
  3294. * Flex-OneNAND SLC area has 64 pages per block.
  3295. * Flex-OneNAND MLC area has 128 pages per block.
  3296. * Expose MLC erase size to find erase_shift and page_mask.
  3297. */
  3298. if (FLEXONENAND(this))
  3299. mtd->erasesize <<= 1;
  3300. this->erase_shift = ffs(mtd->erasesize) - 1;
  3301. this->page_shift = ffs(mtd->writesize) - 1;
  3302. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  3303. /* Set density mask. it is used for DDP */
  3304. if (ONENAND_IS_DDP(this))
  3305. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  3306. /* It's real page size */
  3307. this->writesize = mtd->writesize;
  3308. /* REVISIT: Multichip handling */
  3309. if (FLEXONENAND(this))
  3310. flexonenand_get_size(mtd);
  3311. else
  3312. mtd->size = this->chipsize;
  3313. /*
  3314. * We emulate the 4KiB page and 256KiB erase block size
  3315. * But oobsize is still 64 bytes.
  3316. * It is only valid if you turn on 2X program support,
  3317. * Otherwise it will be ignored by compiler.
  3318. */
  3319. if (ONENAND_IS_2PLANE(this)) {
  3320. mtd->writesize <<= 1;
  3321. mtd->erasesize <<= 1;
  3322. }
  3323. return 0;
  3324. }
  3325. /**
  3326. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  3327. * @param mtd MTD device structure
  3328. */
  3329. static int onenand_suspend(struct mtd_info *mtd)
  3330. {
  3331. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  3332. }
  3333. /**
  3334. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  3335. * @param mtd MTD device structure
  3336. */
  3337. static void onenand_resume(struct mtd_info *mtd)
  3338. {
  3339. struct onenand_chip *this = mtd->priv;
  3340. if (this->state == FL_PM_SUSPENDED)
  3341. onenand_release_device(mtd);
  3342. else
  3343. printk(KERN_ERR "%s: resume() called for the chip which is not "
  3344. "in suspended state\n", __func__);
  3345. }
  3346. /**
  3347. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  3348. * @param mtd MTD device structure
  3349. * @param maxchips Number of chips to scan for
  3350. *
  3351. * This fills out all the not initialized function pointers
  3352. * with the defaults.
  3353. * The flash ID is read and the mtd/chip structures are
  3354. * filled with the appropriate values.
  3355. */
  3356. int onenand_scan(struct mtd_info *mtd, int maxchips)
  3357. {
  3358. int i, ret;
  3359. struct onenand_chip *this = mtd->priv;
  3360. if (!this->read_word)
  3361. this->read_word = onenand_readw;
  3362. if (!this->write_word)
  3363. this->write_word = onenand_writew;
  3364. if (!this->command)
  3365. this->command = onenand_command;
  3366. if (!this->wait)
  3367. onenand_setup_wait(mtd);
  3368. if (!this->bbt_wait)
  3369. this->bbt_wait = onenand_bbt_wait;
  3370. if (!this->unlock_all)
  3371. this->unlock_all = onenand_unlock_all;
  3372. if (!this->chip_probe)
  3373. this->chip_probe = onenand_chip_probe;
  3374. if (!this->read_bufferram)
  3375. this->read_bufferram = onenand_read_bufferram;
  3376. if (!this->write_bufferram)
  3377. this->write_bufferram = onenand_write_bufferram;
  3378. if (!this->block_markbad)
  3379. this->block_markbad = onenand_default_block_markbad;
  3380. if (!this->scan_bbt)
  3381. this->scan_bbt = onenand_default_bbt;
  3382. if (onenand_probe(mtd))
  3383. return -ENXIO;
  3384. /* Set Sync. Burst Read after probing */
  3385. if (this->mmcontrol) {
  3386. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  3387. this->read_bufferram = onenand_sync_read_bufferram;
  3388. }
  3389. /* Allocate buffers, if necessary */
  3390. if (!this->page_buf) {
  3391. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3392. if (!this->page_buf) {
  3393. printk(KERN_ERR "%s: Can't allocate page_buf\n",
  3394. __func__);
  3395. return -ENOMEM;
  3396. }
  3397. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3398. this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3399. if (!this->verify_buf) {
  3400. kfree(this->page_buf);
  3401. return -ENOMEM;
  3402. }
  3403. #endif
  3404. this->options |= ONENAND_PAGEBUF_ALLOC;
  3405. }
  3406. if (!this->oob_buf) {
  3407. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  3408. if (!this->oob_buf) {
  3409. printk(KERN_ERR "%s: Can't allocate oob_buf\n",
  3410. __func__);
  3411. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3412. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  3413. kfree(this->page_buf);
  3414. }
  3415. return -ENOMEM;
  3416. }
  3417. this->options |= ONENAND_OOBBUF_ALLOC;
  3418. }
  3419. this->state = FL_READY;
  3420. init_waitqueue_head(&this->wq);
  3421. spin_lock_init(&this->chip_lock);
  3422. /*
  3423. * Allow subpage writes up to oobsize.
  3424. */
  3425. switch (mtd->oobsize) {
  3426. case 128:
  3427. if (FLEXONENAND(this)) {
  3428. this->ecclayout = &flexonenand_oob_128;
  3429. mtd->subpage_sft = 0;
  3430. } else {
  3431. this->ecclayout = &onenand_oob_128;
  3432. mtd->subpage_sft = 2;
  3433. }
  3434. if (ONENAND_IS_NOP_1(this))
  3435. mtd->subpage_sft = 0;
  3436. break;
  3437. case 64:
  3438. this->ecclayout = &onenand_oob_64;
  3439. mtd->subpage_sft = 2;
  3440. break;
  3441. case 32:
  3442. this->ecclayout = &onenand_oob_32;
  3443. mtd->subpage_sft = 1;
  3444. break;
  3445. default:
  3446. printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
  3447. __func__, mtd->oobsize);
  3448. mtd->subpage_sft = 0;
  3449. /* To prevent kernel oops */
  3450. this->ecclayout = &onenand_oob_32;
  3451. break;
  3452. }
  3453. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3454. /*
  3455. * The number of bytes available for a client to place data into
  3456. * the out of band area
  3457. */
  3458. this->ecclayout->oobavail = 0;
  3459. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  3460. this->ecclayout->oobfree[i].length; i++)
  3461. this->ecclayout->oobavail +=
  3462. this->ecclayout->oobfree[i].length;
  3463. mtd->oobavail = this->ecclayout->oobavail;
  3464. mtd->ecclayout = this->ecclayout;
  3465. mtd->ecc_strength = 1;
  3466. /* Fill in remaining MTD driver data */
  3467. mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
  3468. mtd->flags = MTD_CAP_NANDFLASH;
  3469. mtd->_erase = onenand_erase;
  3470. mtd->_point = NULL;
  3471. mtd->_unpoint = NULL;
  3472. mtd->_read = onenand_read;
  3473. mtd->_write = onenand_write;
  3474. mtd->_read_oob = onenand_read_oob;
  3475. mtd->_write_oob = onenand_write_oob;
  3476. mtd->_panic_write = onenand_panic_write;
  3477. #ifdef CONFIG_MTD_ONENAND_OTP
  3478. mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
  3479. mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
  3480. mtd->_get_user_prot_info = onenand_get_user_prot_info;
  3481. mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
  3482. mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
  3483. mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
  3484. #endif
  3485. mtd->_sync = onenand_sync;
  3486. mtd->_lock = onenand_lock;
  3487. mtd->_unlock = onenand_unlock;
  3488. mtd->_suspend = onenand_suspend;
  3489. mtd->_resume = onenand_resume;
  3490. mtd->_block_isbad = onenand_block_isbad;
  3491. mtd->_block_markbad = onenand_block_markbad;
  3492. mtd->owner = THIS_MODULE;
  3493. mtd->writebufsize = mtd->writesize;
  3494. /* Unlock whole block */
  3495. if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
  3496. this->unlock_all(mtd);
  3497. ret = this->scan_bbt(mtd);
  3498. if ((!FLEXONENAND(this)) || ret)
  3499. return ret;
  3500. /* Change Flex-OneNAND boundaries if required */
  3501. for (i = 0; i < MAX_DIES; i++)
  3502. flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
  3503. flex_bdry[(2 * i) + 1]);
  3504. return 0;
  3505. }
  3506. /**
  3507. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  3508. * @param mtd MTD device structure
  3509. */
  3510. void onenand_release(struct mtd_info *mtd)
  3511. {
  3512. struct onenand_chip *this = mtd->priv;
  3513. /* Deregister partitions */
  3514. mtd_device_unregister(mtd);
  3515. /* Free bad block table memory, if allocated */
  3516. if (this->bbm) {
  3517. struct bbm_info *bbm = this->bbm;
  3518. kfree(bbm->bbt);
  3519. kfree(this->bbm);
  3520. }
  3521. /* Buffers allocated by onenand_scan */
  3522. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3523. kfree(this->page_buf);
  3524. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3525. kfree(this->verify_buf);
  3526. #endif
  3527. }
  3528. if (this->options & ONENAND_OOBBUF_ALLOC)
  3529. kfree(this->oob_buf);
  3530. kfree(mtd->eraseregions);
  3531. }
  3532. EXPORT_SYMBOL_GPL(onenand_scan);
  3533. EXPORT_SYMBOL_GPL(onenand_release);
  3534. MODULE_LICENSE("GPL");
  3535. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  3536. MODULE_DESCRIPTION("Generic OneNAND flash driver code");