onenand_base.c 94 KB

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