onenand_base.c 104 KB

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