onenand_base.c 100 KB

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