onenand_base.c 104 KB

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