onenand_base.c 94 KB

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