nand_base.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346
  1. /*
  2. * drivers/mtd/nand.c
  3. *
  4. * Overview:
  5. * This is the generic MTD driver for NAND flash devices. It should be
  6. * capable of working with almost all NAND chips currently available.
  7. *
  8. * Additional technical information is available on
  9. * http://www.linux-mtd.infradead.org/doc/nand.html
  10. *
  11. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  12. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  13. *
  14. * Credits:
  15. * David Woodhouse for adding multichip support
  16. *
  17. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  18. * rework for 2K page size chips
  19. *
  20. * TODO:
  21. * Enable cached programming for 2k page size chips
  22. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  23. * if we have HW ECC support.
  24. * BBT table is not serialized, has to be fixed
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License version 2 as
  28. * published by the Free Software Foundation.
  29. *
  30. */
  31. #ifndef __UBOOT__
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/module.h>
  34. #include <linux/delay.h>
  35. #include <linux/errno.h>
  36. #include <linux/err.h>
  37. #include <linux/sched.h>
  38. #include <linux/slab.h>
  39. #include <linux/types.h>
  40. #include <linux/mtd/mtd.h>
  41. #include <linux/mtd/nand.h>
  42. #include <linux/mtd/nand_ecc.h>
  43. #include <linux/mtd/nand_bch.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/bitops.h>
  46. #include <linux/leds.h>
  47. #include <linux/io.h>
  48. #include <linux/mtd/partitions.h>
  49. #else
  50. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  51. #include <common.h>
  52. #include <malloc.h>
  53. #include <watchdog.h>
  54. #include <linux/err.h>
  55. #include <linux/compat.h>
  56. #include <linux/mtd/mtd.h>
  57. #include <linux/mtd/nand.h>
  58. #include <linux/mtd/nand_ecc.h>
  59. #include <linux/mtd/nand_bch.h>
  60. #ifdef CONFIG_MTD_PARTITIONS
  61. #include <linux/mtd/partitions.h>
  62. #endif
  63. #include <asm/io.h>
  64. #include <asm/errno.h>
  65. /*
  66. * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
  67. * a flash. NAND flash is initialized prior to interrupts so standard timers
  68. * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
  69. * which is greater than (max NAND reset time / NAND status read time).
  70. * A conservative default of 200000 (500 us / 25 ns) is used as a default.
  71. */
  72. #ifndef CONFIG_SYS_NAND_RESET_CNT
  73. #define CONFIG_SYS_NAND_RESET_CNT 200000
  74. #endif
  75. static bool is_module_text_address(unsigned long addr) {return 0;}
  76. #endif
  77. /* Define default oob placement schemes for large and small page devices */
  78. static struct nand_ecclayout nand_oob_8 = {
  79. .eccbytes = 3,
  80. .eccpos = {0, 1, 2},
  81. .oobfree = {
  82. {.offset = 3,
  83. .length = 2},
  84. {.offset = 6,
  85. .length = 2} }
  86. };
  87. static struct nand_ecclayout nand_oob_16 = {
  88. .eccbytes = 6,
  89. .eccpos = {0, 1, 2, 3, 6, 7},
  90. .oobfree = {
  91. {.offset = 8,
  92. . length = 8} }
  93. };
  94. static struct nand_ecclayout nand_oob_64 = {
  95. .eccbytes = 24,
  96. .eccpos = {
  97. 40, 41, 42, 43, 44, 45, 46, 47,
  98. 48, 49, 50, 51, 52, 53, 54, 55,
  99. 56, 57, 58, 59, 60, 61, 62, 63},
  100. .oobfree = {
  101. {.offset = 2,
  102. .length = 38} }
  103. };
  104. static struct nand_ecclayout nand_oob_128 = {
  105. .eccbytes = 48,
  106. .eccpos = {
  107. 80, 81, 82, 83, 84, 85, 86, 87,
  108. 88, 89, 90, 91, 92, 93, 94, 95,
  109. 96, 97, 98, 99, 100, 101, 102, 103,
  110. 104, 105, 106, 107, 108, 109, 110, 111,
  111. 112, 113, 114, 115, 116, 117, 118, 119,
  112. 120, 121, 122, 123, 124, 125, 126, 127},
  113. .oobfree = {
  114. {.offset = 2,
  115. .length = 78} }
  116. };
  117. static int nand_get_device(struct mtd_info *mtd, int new_state);
  118. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  119. struct mtd_oob_ops *ops);
  120. /*
  121. * For devices which display every fart in the system on a separate LED. Is
  122. * compiled away when LED support is disabled.
  123. */
  124. DEFINE_LED_TRIGGER(nand_led_trigger);
  125. static int check_offs_len(struct mtd_info *mtd,
  126. loff_t ofs, uint64_t len)
  127. {
  128. struct nand_chip *chip = mtd->priv;
  129. int ret = 0;
  130. /* Start address must align on block boundary */
  131. if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
  132. pr_debug("%s: unaligned address\n", __func__);
  133. ret = -EINVAL;
  134. }
  135. /* Length must align on block boundary */
  136. if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
  137. pr_debug("%s: length not block aligned\n", __func__);
  138. ret = -EINVAL;
  139. }
  140. return ret;
  141. }
  142. /**
  143. * nand_release_device - [GENERIC] release chip
  144. * @mtd: MTD device structure
  145. *
  146. * Release chip lock and wake up anyone waiting on the device.
  147. */
  148. static void nand_release_device(struct mtd_info *mtd)
  149. {
  150. struct nand_chip *chip = mtd->priv;
  151. #ifndef __UBOOT__
  152. /* Release the controller and the chip */
  153. spin_lock(&chip->controller->lock);
  154. chip->controller->active = NULL;
  155. chip->state = FL_READY;
  156. wake_up(&chip->controller->wq);
  157. spin_unlock(&chip->controller->lock);
  158. #else
  159. /* De-select the NAND device */
  160. chip->select_chip(mtd, -1);
  161. #endif
  162. }
  163. /**
  164. * nand_read_byte - [DEFAULT] read one byte from the chip
  165. * @mtd: MTD device structure
  166. *
  167. * Default read function for 8bit buswidth
  168. */
  169. #ifndef __UBOOT__
  170. static uint8_t nand_read_byte(struct mtd_info *mtd)
  171. #else
  172. uint8_t nand_read_byte(struct mtd_info *mtd)
  173. #endif
  174. {
  175. struct nand_chip *chip = mtd->priv;
  176. return readb(chip->IO_ADDR_R);
  177. }
  178. /**
  179. * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  180. * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  181. * @mtd: MTD device structure
  182. *
  183. * Default read function for 16bit buswidth with endianness conversion.
  184. *
  185. */
  186. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  187. {
  188. struct nand_chip *chip = mtd->priv;
  189. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  190. }
  191. /**
  192. * nand_read_word - [DEFAULT] read one word from the chip
  193. * @mtd: MTD device structure
  194. *
  195. * Default read function for 16bit buswidth without endianness conversion.
  196. */
  197. static u16 nand_read_word(struct mtd_info *mtd)
  198. {
  199. struct nand_chip *chip = mtd->priv;
  200. return readw(chip->IO_ADDR_R);
  201. }
  202. /**
  203. * nand_select_chip - [DEFAULT] control CE line
  204. * @mtd: MTD device structure
  205. * @chipnr: chipnumber to select, -1 for deselect
  206. *
  207. * Default select function for 1 chip devices.
  208. */
  209. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  210. {
  211. struct nand_chip *chip = mtd->priv;
  212. switch (chipnr) {
  213. case -1:
  214. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  215. break;
  216. case 0:
  217. break;
  218. default:
  219. BUG();
  220. }
  221. }
  222. /**
  223. * nand_write_byte - [DEFAULT] write single byte to chip
  224. * @mtd: MTD device structure
  225. * @byte: value to write
  226. *
  227. * Default function to write a byte to I/O[7:0]
  228. */
  229. static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
  230. {
  231. struct nand_chip *chip = mtd->priv;
  232. chip->write_buf(mtd, &byte, 1);
  233. }
  234. /**
  235. * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
  236. * @mtd: MTD device structure
  237. * @byte: value to write
  238. *
  239. * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
  240. */
  241. static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
  242. {
  243. struct nand_chip *chip = mtd->priv;
  244. uint16_t word = byte;
  245. /*
  246. * It's not entirely clear what should happen to I/O[15:8] when writing
  247. * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
  248. *
  249. * When the host supports a 16-bit bus width, only data is
  250. * transferred at the 16-bit width. All address and command line
  251. * transfers shall use only the lower 8-bits of the data bus. During
  252. * command transfers, the host may place any value on the upper
  253. * 8-bits of the data bus. During address transfers, the host shall
  254. * set the upper 8-bits of the data bus to 00h.
  255. *
  256. * One user of the write_byte callback is nand_onfi_set_features. The
  257. * four parameters are specified to be written to I/O[7:0], but this is
  258. * neither an address nor a command transfer. Let's assume a 0 on the
  259. * upper I/O lines is OK.
  260. */
  261. chip->write_buf(mtd, (uint8_t *)&word, 2);
  262. }
  263. #if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
  264. static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
  265. {
  266. int i;
  267. for (i = 0; i < len; i++)
  268. writeb(buf[i], addr);
  269. }
  270. static void ioread8_rep(void *addr, uint8_t *buf, int len)
  271. {
  272. int i;
  273. for (i = 0; i < len; i++)
  274. buf[i] = readb(addr);
  275. }
  276. static void ioread16_rep(void *addr, void *buf, int len)
  277. {
  278. int i;
  279. u16 *p = (u16 *) buf;
  280. for (i = 0; i < len; i++)
  281. p[i] = readw(addr);
  282. }
  283. static void iowrite16_rep(void *addr, void *buf, int len)
  284. {
  285. int i;
  286. u16 *p = (u16 *) buf;
  287. for (i = 0; i < len; i++)
  288. writew(p[i], addr);
  289. }
  290. #endif
  291. /**
  292. * nand_write_buf - [DEFAULT] write buffer to chip
  293. * @mtd: MTD device structure
  294. * @buf: data buffer
  295. * @len: number of bytes to write
  296. *
  297. * Default write function for 8bit buswidth.
  298. */
  299. #ifndef __UBOOT__
  300. static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  301. #else
  302. void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  303. #endif
  304. {
  305. struct nand_chip *chip = mtd->priv;
  306. iowrite8_rep(chip->IO_ADDR_W, buf, len);
  307. }
  308. /**
  309. * nand_read_buf - [DEFAULT] read chip data into buffer
  310. * @mtd: MTD device structure
  311. * @buf: buffer to store date
  312. * @len: number of bytes to read
  313. *
  314. * Default read function for 8bit buswidth.
  315. */
  316. #ifndef __UBOOT__
  317. static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  318. #else
  319. void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  320. #endif
  321. {
  322. struct nand_chip *chip = mtd->priv;
  323. ioread8_rep(chip->IO_ADDR_R, buf, len);
  324. }
  325. /**
  326. * nand_write_buf16 - [DEFAULT] write buffer to chip
  327. * @mtd: MTD device structure
  328. * @buf: data buffer
  329. * @len: number of bytes to write
  330. *
  331. * Default write function for 16bit buswidth.
  332. */
  333. #ifndef __UBOOT__
  334. static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  335. #else
  336. void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  337. #endif
  338. {
  339. struct nand_chip *chip = mtd->priv;
  340. u16 *p = (u16 *) buf;
  341. iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
  342. }
  343. /**
  344. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  345. * @mtd: MTD device structure
  346. * @buf: buffer to store date
  347. * @len: number of bytes to read
  348. *
  349. * Default read function for 16bit buswidth.
  350. */
  351. #ifndef __UBOOT__
  352. static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  353. #else
  354. void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  355. #endif
  356. {
  357. struct nand_chip *chip = mtd->priv;
  358. u16 *p = (u16 *) buf;
  359. ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
  360. }
  361. /**
  362. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  363. * @mtd: MTD device structure
  364. * @ofs: offset from device start
  365. * @getchip: 0, if the chip is already selected
  366. *
  367. * Check, if the block is bad.
  368. */
  369. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  370. {
  371. int page, chipnr, res = 0, i = 0;
  372. struct nand_chip *chip = mtd->priv;
  373. u16 bad;
  374. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  375. ofs += mtd->erasesize - mtd->writesize;
  376. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  377. if (getchip) {
  378. chipnr = (int)(ofs >> chip->chip_shift);
  379. nand_get_device(mtd, FL_READING);
  380. /* Select the NAND device */
  381. chip->select_chip(mtd, chipnr);
  382. }
  383. do {
  384. if (chip->options & NAND_BUSWIDTH_16) {
  385. chip->cmdfunc(mtd, NAND_CMD_READOOB,
  386. chip->badblockpos & 0xFE, page);
  387. bad = cpu_to_le16(chip->read_word(mtd));
  388. if (chip->badblockpos & 0x1)
  389. bad >>= 8;
  390. else
  391. bad &= 0xFF;
  392. } else {
  393. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
  394. page);
  395. bad = chip->read_byte(mtd);
  396. }
  397. if (likely(chip->badblockbits == 8))
  398. res = bad != 0xFF;
  399. else
  400. res = hweight8(bad) < chip->badblockbits;
  401. ofs += mtd->writesize;
  402. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  403. i++;
  404. } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
  405. if (getchip) {
  406. chip->select_chip(mtd, -1);
  407. nand_release_device(mtd);
  408. }
  409. return res;
  410. }
  411. /**
  412. * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
  413. * @mtd: MTD device structure
  414. * @ofs: offset from device start
  415. *
  416. * This is the default implementation, which can be overridden by a hardware
  417. * specific driver. It provides the details for writing a bad block marker to a
  418. * block.
  419. */
  420. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  421. {
  422. struct nand_chip *chip = mtd->priv;
  423. struct mtd_oob_ops ops;
  424. uint8_t buf[2] = { 0, 0 };
  425. int ret = 0, res, i = 0;
  426. ops.datbuf = NULL;
  427. ops.oobbuf = buf;
  428. ops.ooboffs = chip->badblockpos;
  429. if (chip->options & NAND_BUSWIDTH_16) {
  430. ops.ooboffs &= ~0x01;
  431. ops.len = ops.ooblen = 2;
  432. } else {
  433. ops.len = ops.ooblen = 1;
  434. }
  435. ops.mode = MTD_OPS_PLACE_OOB;
  436. /* Write to first/last page(s) if necessary */
  437. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  438. ofs += mtd->erasesize - mtd->writesize;
  439. do {
  440. res = nand_do_write_oob(mtd, ofs, &ops);
  441. if (!ret)
  442. ret = res;
  443. i++;
  444. ofs += mtd->writesize;
  445. } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
  446. return ret;
  447. }
  448. /**
  449. * nand_block_markbad_lowlevel - mark a block bad
  450. * @mtd: MTD device structure
  451. * @ofs: offset from device start
  452. *
  453. * This function performs the generic NAND bad block marking steps (i.e., bad
  454. * block table(s) and/or marker(s)). We only allow the hardware driver to
  455. * specify how to write bad block markers to OOB (chip->block_markbad).
  456. *
  457. * We try operations in the following order:
  458. * (1) erase the affected block, to allow OOB marker to be written cleanly
  459. * (2) write bad block marker to OOB area of affected block (unless flag
  460. * NAND_BBT_NO_OOB_BBM is present)
  461. * (3) update the BBT
  462. * Note that we retain the first error encountered in (2) or (3), finish the
  463. * procedures, and dump the error in the end.
  464. */
  465. static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
  466. {
  467. struct nand_chip *chip = mtd->priv;
  468. int res, ret = 0;
  469. if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
  470. struct erase_info einfo;
  471. /* Attempt erase before marking OOB */
  472. memset(&einfo, 0, sizeof(einfo));
  473. einfo.mtd = mtd;
  474. einfo.addr = ofs;
  475. einfo.len = 1ULL << chip->phys_erase_shift;
  476. nand_erase_nand(mtd, &einfo, 0);
  477. /* Write bad block marker to OOB */
  478. nand_get_device(mtd, FL_WRITING);
  479. ret = chip->block_markbad(mtd, ofs);
  480. nand_release_device(mtd);
  481. }
  482. /* Mark block bad in BBT */
  483. if (chip->bbt) {
  484. res = nand_markbad_bbt(mtd, ofs);
  485. if (!ret)
  486. ret = res;
  487. }
  488. if (!ret)
  489. mtd->ecc_stats.badblocks++;
  490. return ret;
  491. }
  492. /**
  493. * nand_check_wp - [GENERIC] check if the chip is write protected
  494. * @mtd: MTD device structure
  495. *
  496. * Check, if the device is write protected. The function expects, that the
  497. * device is already selected.
  498. */
  499. static int nand_check_wp(struct mtd_info *mtd)
  500. {
  501. struct nand_chip *chip = mtd->priv;
  502. /* Broken xD cards report WP despite being writable */
  503. if (chip->options & NAND_BROKEN_XD)
  504. return 0;
  505. /* Check the WP bit */
  506. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  507. return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  508. }
  509. /**
  510. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  511. * @mtd: MTD device structure
  512. * @ofs: offset from device start
  513. * @getchip: 0, if the chip is already selected
  514. * @allowbbt: 1, if its allowed to access the bbt area
  515. *
  516. * Check, if the block is bad. Either by reading the bad block table or
  517. * calling of the scan function.
  518. */
  519. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
  520. int allowbbt)
  521. {
  522. struct nand_chip *chip = mtd->priv;
  523. if (!(chip->options & NAND_SKIP_BBTSCAN) &&
  524. !(chip->options & NAND_BBT_SCANNED)) {
  525. chip->options |= NAND_BBT_SCANNED;
  526. chip->scan_bbt(mtd);
  527. }
  528. if (!chip->bbt)
  529. return chip->block_bad(mtd, ofs, getchip);
  530. /* Return info from the table */
  531. return nand_isbad_bbt(mtd, ofs, allowbbt);
  532. }
  533. #ifndef __UBOOT__
  534. /**
  535. * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
  536. * @mtd: MTD device structure
  537. * @timeo: Timeout
  538. *
  539. * Helper function for nand_wait_ready used when needing to wait in interrupt
  540. * context.
  541. */
  542. static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
  543. {
  544. struct nand_chip *chip = mtd->priv;
  545. int i;
  546. /* Wait for the device to get ready */
  547. for (i = 0; i < timeo; i++) {
  548. if (chip->dev_ready(mtd))
  549. break;
  550. touch_softlockup_watchdog();
  551. mdelay(1);
  552. }
  553. }
  554. #endif
  555. /* Wait for the ready pin, after a command. The timeout is caught later. */
  556. void nand_wait_ready(struct mtd_info *mtd)
  557. {
  558. struct nand_chip *chip = mtd->priv;
  559. #ifndef __UBOOT__
  560. unsigned long timeo = jiffies + msecs_to_jiffies(20);
  561. /* 400ms timeout */
  562. if (in_interrupt() || oops_in_progress)
  563. return panic_nand_wait_ready(mtd, 400);
  564. led_trigger_event(nand_led_trigger, LED_FULL);
  565. /* Wait until command is processed or timeout occurs */
  566. do {
  567. if (chip->dev_ready(mtd))
  568. break;
  569. touch_softlockup_watchdog();
  570. } while (time_before(jiffies, timeo));
  571. led_trigger_event(nand_led_trigger, LED_OFF);
  572. #else
  573. u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
  574. u32 time_start;
  575. time_start = get_timer(0);
  576. /* Wait until command is processed or timeout occurs */
  577. while (get_timer(time_start) < timeo) {
  578. if (chip->dev_ready)
  579. if (chip->dev_ready(mtd))
  580. break;
  581. }
  582. #endif
  583. }
  584. EXPORT_SYMBOL_GPL(nand_wait_ready);
  585. /**
  586. * nand_command - [DEFAULT] Send command to NAND device
  587. * @mtd: MTD device structure
  588. * @command: the command to be sent
  589. * @column: the column address for this command, -1 if none
  590. * @page_addr: the page address for this command, -1 if none
  591. *
  592. * Send command to NAND device. This function is used for small page devices
  593. * (512 Bytes per page).
  594. */
  595. static void nand_command(struct mtd_info *mtd, unsigned int command,
  596. int column, int page_addr)
  597. {
  598. register struct nand_chip *chip = mtd->priv;
  599. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  600. uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
  601. /* Write out the command to the device */
  602. if (command == NAND_CMD_SEQIN) {
  603. int readcmd;
  604. if (column >= mtd->writesize) {
  605. /* OOB area */
  606. column -= mtd->writesize;
  607. readcmd = NAND_CMD_READOOB;
  608. } else if (column < 256) {
  609. /* First 256 bytes --> READ0 */
  610. readcmd = NAND_CMD_READ0;
  611. } else {
  612. column -= 256;
  613. readcmd = NAND_CMD_READ1;
  614. }
  615. chip->cmd_ctrl(mtd, readcmd, ctrl);
  616. ctrl &= ~NAND_CTRL_CHANGE;
  617. }
  618. chip->cmd_ctrl(mtd, command, ctrl);
  619. /* Address cycle, when necessary */
  620. ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
  621. /* Serially input address */
  622. if (column != -1) {
  623. /* Adjust columns for 16 bit buswidth */
  624. if (chip->options & NAND_BUSWIDTH_16 &&
  625. !nand_opcode_8bits(command))
  626. column >>= 1;
  627. chip->cmd_ctrl(mtd, column, ctrl);
  628. ctrl &= ~NAND_CTRL_CHANGE;
  629. }
  630. if (page_addr != -1) {
  631. chip->cmd_ctrl(mtd, page_addr, ctrl);
  632. ctrl &= ~NAND_CTRL_CHANGE;
  633. chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
  634. /* One more address cycle for devices > 32MiB */
  635. if (chip->chipsize > (32 << 20))
  636. chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
  637. }
  638. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  639. /*
  640. * Program and erase have their own busy handlers status and sequential
  641. * in needs no delay
  642. */
  643. switch (command) {
  644. case NAND_CMD_PAGEPROG:
  645. case NAND_CMD_ERASE1:
  646. case NAND_CMD_ERASE2:
  647. case NAND_CMD_SEQIN:
  648. case NAND_CMD_STATUS:
  649. return;
  650. case NAND_CMD_RESET:
  651. if (chip->dev_ready)
  652. break;
  653. udelay(chip->chip_delay);
  654. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  655. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  656. chip->cmd_ctrl(mtd,
  657. NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  658. while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
  659. (rst_sts_cnt--));
  660. return;
  661. /* This applies to read commands */
  662. default:
  663. /*
  664. * If we don't have access to the busy pin, we apply the given
  665. * command delay
  666. */
  667. if (!chip->dev_ready) {
  668. udelay(chip->chip_delay);
  669. return;
  670. }
  671. }
  672. /*
  673. * Apply this short delay always to ensure that we do wait tWB in
  674. * any case on any machine.
  675. */
  676. ndelay(100);
  677. nand_wait_ready(mtd);
  678. }
  679. /**
  680. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  681. * @mtd: MTD device structure
  682. * @command: the command to be sent
  683. * @column: the column address for this command, -1 if none
  684. * @page_addr: the page address for this command, -1 if none
  685. *
  686. * Send command to NAND device. This is the version for the new large page
  687. * devices. We don't have the separate regions as we have in the small page
  688. * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
  689. */
  690. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  691. int column, int page_addr)
  692. {
  693. register struct nand_chip *chip = mtd->priv;
  694. uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
  695. /* Emulate NAND_CMD_READOOB */
  696. if (command == NAND_CMD_READOOB) {
  697. column += mtd->writesize;
  698. command = NAND_CMD_READ0;
  699. }
  700. /* Command latch cycle */
  701. chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  702. if (column != -1 || page_addr != -1) {
  703. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  704. /* Serially input address */
  705. if (column != -1) {
  706. /* Adjust columns for 16 bit buswidth */
  707. if (chip->options & NAND_BUSWIDTH_16 &&
  708. !nand_opcode_8bits(command))
  709. column >>= 1;
  710. chip->cmd_ctrl(mtd, column, ctrl);
  711. ctrl &= ~NAND_CTRL_CHANGE;
  712. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  713. }
  714. if (page_addr != -1) {
  715. chip->cmd_ctrl(mtd, page_addr, ctrl);
  716. chip->cmd_ctrl(mtd, page_addr >> 8,
  717. NAND_NCE | NAND_ALE);
  718. /* One more address cycle for devices > 128MiB */
  719. if (chip->chipsize > (128 << 20))
  720. chip->cmd_ctrl(mtd, page_addr >> 16,
  721. NAND_NCE | NAND_ALE);
  722. }
  723. }
  724. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  725. /*
  726. * Program and erase have their own busy handlers status, sequential
  727. * in, and deplete1 need no delay.
  728. */
  729. switch (command) {
  730. case NAND_CMD_CACHEDPROG:
  731. case NAND_CMD_PAGEPROG:
  732. case NAND_CMD_ERASE1:
  733. case NAND_CMD_ERASE2:
  734. case NAND_CMD_SEQIN:
  735. case NAND_CMD_RNDIN:
  736. case NAND_CMD_STATUS:
  737. return;
  738. case NAND_CMD_RESET:
  739. if (chip->dev_ready)
  740. break;
  741. udelay(chip->chip_delay);
  742. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  743. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  744. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  745. NAND_NCE | NAND_CTRL_CHANGE);
  746. while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
  747. (rst_sts_cnt--));
  748. return;
  749. case NAND_CMD_RNDOUT:
  750. /* No ready / busy check necessary */
  751. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  752. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  753. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  754. NAND_NCE | NAND_CTRL_CHANGE);
  755. return;
  756. case NAND_CMD_READ0:
  757. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  758. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  759. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  760. NAND_NCE | NAND_CTRL_CHANGE);
  761. /* This applies to read commands */
  762. default:
  763. /*
  764. * If we don't have access to the busy pin, we apply the given
  765. * command delay.
  766. */
  767. if (!chip->dev_ready) {
  768. udelay(chip->chip_delay);
  769. return;
  770. }
  771. }
  772. /*
  773. * Apply this short delay always to ensure that we do wait tWB in
  774. * any case on any machine.
  775. */
  776. ndelay(100);
  777. nand_wait_ready(mtd);
  778. }
  779. /**
  780. * panic_nand_get_device - [GENERIC] Get chip for selected access
  781. * @chip: the nand chip descriptor
  782. * @mtd: MTD device structure
  783. * @new_state: the state which is requested
  784. *
  785. * Used when in panic, no locks are taken.
  786. */
  787. static void panic_nand_get_device(struct nand_chip *chip,
  788. struct mtd_info *mtd, int new_state)
  789. {
  790. /* Hardware controller shared among independent devices */
  791. chip->controller->active = chip;
  792. chip->state = new_state;
  793. }
  794. /**
  795. * nand_get_device - [GENERIC] Get chip for selected access
  796. * @mtd: MTD device structure
  797. * @new_state: the state which is requested
  798. *
  799. * Get the device and lock it for exclusive access
  800. */
  801. static int
  802. nand_get_device(struct mtd_info *mtd, int new_state)
  803. {
  804. struct nand_chip *chip = mtd->priv;
  805. #ifndef __UBOOT__
  806. spinlock_t *lock = &chip->controller->lock;
  807. wait_queue_head_t *wq = &chip->controller->wq;
  808. DECLARE_WAITQUEUE(wait, current);
  809. retry:
  810. spin_lock(lock);
  811. /* Hardware controller shared among independent devices */
  812. if (!chip->controller->active)
  813. chip->controller->active = chip;
  814. if (chip->controller->active == chip && chip->state == FL_READY) {
  815. chip->state = new_state;
  816. spin_unlock(lock);
  817. return 0;
  818. }
  819. if (new_state == FL_PM_SUSPENDED) {
  820. if (chip->controller->active->state == FL_PM_SUSPENDED) {
  821. chip->state = FL_PM_SUSPENDED;
  822. spin_unlock(lock);
  823. return 0;
  824. }
  825. }
  826. set_current_state(TASK_UNINTERRUPTIBLE);
  827. add_wait_queue(wq, &wait);
  828. spin_unlock(lock);
  829. schedule();
  830. remove_wait_queue(wq, &wait);
  831. goto retry;
  832. #else
  833. chip->state = new_state;
  834. return 0;
  835. #endif
  836. }
  837. /**
  838. * panic_nand_wait - [GENERIC] wait until the command is done
  839. * @mtd: MTD device structure
  840. * @chip: NAND chip structure
  841. * @timeo: timeout
  842. *
  843. * Wait for command done. This is a helper function for nand_wait used when
  844. * we are in interrupt context. May happen when in panic and trying to write
  845. * an oops through mtdoops.
  846. */
  847. static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
  848. unsigned long timeo)
  849. {
  850. int i;
  851. for (i = 0; i < timeo; i++) {
  852. if (chip->dev_ready) {
  853. if (chip->dev_ready(mtd))
  854. break;
  855. } else {
  856. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  857. break;
  858. }
  859. mdelay(1);
  860. }
  861. }
  862. /**
  863. * nand_wait - [DEFAULT] wait until the command is done
  864. * @mtd: MTD device structure
  865. * @chip: NAND chip structure
  866. *
  867. * Wait for command done. This applies to erase and program only. Erase can
  868. * take up to 400ms and program up to 20ms according to general NAND and
  869. * SmartMedia specs.
  870. */
  871. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  872. {
  873. int status, state = chip->state;
  874. unsigned long timeo = (state == FL_ERASING ? 400 : 20);
  875. led_trigger_event(nand_led_trigger, LED_FULL);
  876. /*
  877. * Apply this short delay always to ensure that we do wait tWB in any
  878. * case on any machine.
  879. */
  880. ndelay(100);
  881. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  882. #ifndef __UBOOT__
  883. if (in_interrupt() || oops_in_progress)
  884. panic_nand_wait(mtd, chip, timeo);
  885. else {
  886. timeo = jiffies + msecs_to_jiffies(timeo);
  887. while (time_before(jiffies, timeo)) {
  888. if (chip->dev_ready) {
  889. if (chip->dev_ready(mtd))
  890. break;
  891. } else {
  892. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  893. break;
  894. }
  895. cond_resched();
  896. }
  897. }
  898. #else
  899. u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
  900. u32 time_start;
  901. time_start = get_timer(0);
  902. while (get_timer(time_start) < timer) {
  903. if (chip->dev_ready) {
  904. if (chip->dev_ready(mtd))
  905. break;
  906. } else {
  907. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  908. break;
  909. }
  910. }
  911. #endif
  912. led_trigger_event(nand_led_trigger, LED_OFF);
  913. status = (int)chip->read_byte(mtd);
  914. /* This can happen if in case of timeout or buggy dev_ready */
  915. WARN_ON(!(status & NAND_STATUS_READY));
  916. return status;
  917. }
  918. #ifndef __UBOOT__
  919. /**
  920. * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
  921. * @mtd: mtd info
  922. * @ofs: offset to start unlock from
  923. * @len: length to unlock
  924. * @invert: when = 0, unlock the range of blocks within the lower and
  925. * upper boundary address
  926. * when = 1, unlock the range of blocks outside the boundaries
  927. * of the lower and upper boundary address
  928. *
  929. * Returs unlock status.
  930. */
  931. static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
  932. uint64_t len, int invert)
  933. {
  934. int ret = 0;
  935. int status, page;
  936. struct nand_chip *chip = mtd->priv;
  937. /* Submit address of first page to unlock */
  938. page = ofs >> chip->page_shift;
  939. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  940. /* Submit address of last page to unlock */
  941. page = (ofs + len) >> chip->page_shift;
  942. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
  943. (page | invert) & chip->pagemask);
  944. /* Call wait ready function */
  945. status = chip->waitfunc(mtd, chip);
  946. /* See if device thinks it succeeded */
  947. if (status & NAND_STATUS_FAIL) {
  948. pr_debug("%s: error status = 0x%08x\n",
  949. __func__, status);
  950. ret = -EIO;
  951. }
  952. return ret;
  953. }
  954. /**
  955. * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
  956. * @mtd: mtd info
  957. * @ofs: offset to start unlock from
  958. * @len: length to unlock
  959. *
  960. * Returns unlock status.
  961. */
  962. int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  963. {
  964. int ret = 0;
  965. int chipnr;
  966. struct nand_chip *chip = mtd->priv;
  967. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  968. __func__, (unsigned long long)ofs, len);
  969. if (check_offs_len(mtd, ofs, len))
  970. ret = -EINVAL;
  971. /* Align to last block address if size addresses end of the device */
  972. if (ofs + len == mtd->size)
  973. len -= mtd->erasesize;
  974. nand_get_device(mtd, FL_UNLOCKING);
  975. /* Shift to get chip number */
  976. chipnr = ofs >> chip->chip_shift;
  977. chip->select_chip(mtd, chipnr);
  978. /* Check, if it is write protected */
  979. if (nand_check_wp(mtd)) {
  980. pr_debug("%s: device is write protected!\n",
  981. __func__);
  982. ret = -EIO;
  983. goto out;
  984. }
  985. ret = __nand_unlock(mtd, ofs, len, 0);
  986. out:
  987. chip->select_chip(mtd, -1);
  988. nand_release_device(mtd);
  989. return ret;
  990. }
  991. EXPORT_SYMBOL(nand_unlock);
  992. /**
  993. * nand_lock - [REPLACEABLE] locks all blocks present in the device
  994. * @mtd: mtd info
  995. * @ofs: offset to start unlock from
  996. * @len: length to unlock
  997. *
  998. * This feature is not supported in many NAND parts. 'Micron' NAND parts do
  999. * have this feature, but it allows only to lock all blocks, not for specified
  1000. * range for block. Implementing 'lock' feature by making use of 'unlock', for
  1001. * now.
  1002. *
  1003. * Returns lock status.
  1004. */
  1005. int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1006. {
  1007. int ret = 0;
  1008. int chipnr, status, page;
  1009. struct nand_chip *chip = mtd->priv;
  1010. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  1011. __func__, (unsigned long long)ofs, len);
  1012. if (check_offs_len(mtd, ofs, len))
  1013. ret = -EINVAL;
  1014. nand_get_device(mtd, FL_LOCKING);
  1015. /* Shift to get chip number */
  1016. chipnr = ofs >> chip->chip_shift;
  1017. chip->select_chip(mtd, chipnr);
  1018. /* Check, if it is write protected */
  1019. if (nand_check_wp(mtd)) {
  1020. pr_debug("%s: device is write protected!\n",
  1021. __func__);
  1022. status = MTD_ERASE_FAILED;
  1023. ret = -EIO;
  1024. goto out;
  1025. }
  1026. /* Submit address of first page to lock */
  1027. page = ofs >> chip->page_shift;
  1028. chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
  1029. /* Call wait ready function */
  1030. status = chip->waitfunc(mtd, chip);
  1031. /* See if device thinks it succeeded */
  1032. if (status & NAND_STATUS_FAIL) {
  1033. pr_debug("%s: error status = 0x%08x\n",
  1034. __func__, status);
  1035. ret = -EIO;
  1036. goto out;
  1037. }
  1038. ret = __nand_unlock(mtd, ofs, len, 0x1);
  1039. out:
  1040. chip->select_chip(mtd, -1);
  1041. nand_release_device(mtd);
  1042. return ret;
  1043. }
  1044. EXPORT_SYMBOL(nand_lock);
  1045. #endif
  1046. /**
  1047. * nand_read_page_raw - [INTERN] read raw page data without ecc
  1048. * @mtd: mtd info structure
  1049. * @chip: nand chip info structure
  1050. * @buf: buffer to store read data
  1051. * @oob_required: caller requires OOB data read to chip->oob_poi
  1052. * @page: page number to read
  1053. *
  1054. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1055. */
  1056. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1057. uint8_t *buf, int oob_required, int page)
  1058. {
  1059. chip->read_buf(mtd, buf, mtd->writesize);
  1060. if (oob_required)
  1061. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1062. return 0;
  1063. }
  1064. /**
  1065. * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
  1066. * @mtd: mtd info structure
  1067. * @chip: nand chip info structure
  1068. * @buf: buffer to store read data
  1069. * @oob_required: caller requires OOB data read to chip->oob_poi
  1070. * @page: page number to read
  1071. *
  1072. * We need a special oob layout and handling even when OOB isn't used.
  1073. */
  1074. static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
  1075. struct nand_chip *chip, uint8_t *buf,
  1076. int oob_required, int page)
  1077. {
  1078. int eccsize = chip->ecc.size;
  1079. int eccbytes = chip->ecc.bytes;
  1080. uint8_t *oob = chip->oob_poi;
  1081. int steps, size;
  1082. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1083. chip->read_buf(mtd, buf, eccsize);
  1084. buf += eccsize;
  1085. if (chip->ecc.prepad) {
  1086. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1087. oob += chip->ecc.prepad;
  1088. }
  1089. chip->read_buf(mtd, oob, eccbytes);
  1090. oob += eccbytes;
  1091. if (chip->ecc.postpad) {
  1092. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1093. oob += chip->ecc.postpad;
  1094. }
  1095. }
  1096. size = mtd->oobsize - (oob - chip->oob_poi);
  1097. if (size)
  1098. chip->read_buf(mtd, oob, size);
  1099. return 0;
  1100. }
  1101. /**
  1102. * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
  1103. * @mtd: mtd info structure
  1104. * @chip: nand chip info structure
  1105. * @buf: buffer to store read data
  1106. * @oob_required: caller requires OOB data read to chip->oob_poi
  1107. * @page: page number to read
  1108. */
  1109. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1110. uint8_t *buf, int oob_required, int page)
  1111. {
  1112. int i, eccsize = chip->ecc.size;
  1113. int eccbytes = chip->ecc.bytes;
  1114. int eccsteps = chip->ecc.steps;
  1115. uint8_t *p = buf;
  1116. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1117. uint8_t *ecc_code = chip->buffers->ecccode;
  1118. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1119. unsigned int max_bitflips = 0;
  1120. chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
  1121. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1122. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1123. for (i = 0; i < chip->ecc.total; i++)
  1124. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1125. eccsteps = chip->ecc.steps;
  1126. p = buf;
  1127. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1128. int stat;
  1129. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1130. if (stat < 0) {
  1131. mtd->ecc_stats.failed++;
  1132. } else {
  1133. mtd->ecc_stats.corrected += stat;
  1134. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1135. }
  1136. }
  1137. return max_bitflips;
  1138. }
  1139. /**
  1140. * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
  1141. * @mtd: mtd info structure
  1142. * @chip: nand chip info structure
  1143. * @data_offs: offset of requested data within the page
  1144. * @readlen: data length
  1145. * @bufpoi: buffer to store read data
  1146. * @page: page number to read
  1147. */
  1148. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1149. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
  1150. int page)
  1151. {
  1152. int start_step, end_step, num_steps;
  1153. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1154. uint8_t *p;
  1155. int data_col_addr, i, gaps = 0;
  1156. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1157. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1158. int index;
  1159. unsigned int max_bitflips = 0;
  1160. /* Column address within the page aligned to ECC size (256bytes) */
  1161. start_step = data_offs / chip->ecc.size;
  1162. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1163. num_steps = end_step - start_step + 1;
  1164. index = start_step * chip->ecc.bytes;
  1165. /* Data size aligned to ECC ecc.size */
  1166. datafrag_len = num_steps * chip->ecc.size;
  1167. eccfrag_len = num_steps * chip->ecc.bytes;
  1168. data_col_addr = start_step * chip->ecc.size;
  1169. /* If we read not a page aligned data */
  1170. if (data_col_addr != 0)
  1171. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1172. p = bufpoi + data_col_addr;
  1173. chip->read_buf(mtd, p, datafrag_len);
  1174. /* Calculate ECC */
  1175. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1176. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1177. /*
  1178. * The performance is faster if we position offsets according to
  1179. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1180. */
  1181. for (i = 0; i < eccfrag_len - 1; i++) {
  1182. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  1183. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  1184. gaps = 1;
  1185. break;
  1186. }
  1187. }
  1188. if (gaps) {
  1189. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1190. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1191. } else {
  1192. /*
  1193. * Send the command to read the particular ECC bytes take care
  1194. * about buswidth alignment in read_buf.
  1195. */
  1196. aligned_pos = eccpos[index] & ~(busw - 1);
  1197. aligned_len = eccfrag_len;
  1198. if (eccpos[index] & (busw - 1))
  1199. aligned_len++;
  1200. if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1201. aligned_len++;
  1202. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1203. mtd->writesize + aligned_pos, -1);
  1204. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1205. }
  1206. for (i = 0; i < eccfrag_len; i++)
  1207. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1208. p = bufpoi + data_col_addr;
  1209. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1210. int stat;
  1211. stat = chip->ecc.correct(mtd, p,
  1212. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1213. if (stat < 0) {
  1214. mtd->ecc_stats.failed++;
  1215. } else {
  1216. mtd->ecc_stats.corrected += stat;
  1217. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1218. }
  1219. }
  1220. return max_bitflips;
  1221. }
  1222. /**
  1223. * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
  1224. * @mtd: mtd info structure
  1225. * @chip: nand chip info structure
  1226. * @buf: buffer to store read data
  1227. * @oob_required: caller requires OOB data read to chip->oob_poi
  1228. * @page: page number to read
  1229. *
  1230. * Not for syndrome calculating ECC controllers which need a special oob layout.
  1231. */
  1232. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1233. uint8_t *buf, int oob_required, int page)
  1234. {
  1235. int i, eccsize = chip->ecc.size;
  1236. int eccbytes = chip->ecc.bytes;
  1237. int eccsteps = chip->ecc.steps;
  1238. uint8_t *p = buf;
  1239. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1240. uint8_t *ecc_code = chip->buffers->ecccode;
  1241. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1242. unsigned int max_bitflips = 0;
  1243. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1244. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1245. chip->read_buf(mtd, p, eccsize);
  1246. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1247. }
  1248. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1249. for (i = 0; i < chip->ecc.total; i++)
  1250. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1251. eccsteps = chip->ecc.steps;
  1252. p = buf;
  1253. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1254. int stat;
  1255. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1256. if (stat < 0) {
  1257. mtd->ecc_stats.failed++;
  1258. } else {
  1259. mtd->ecc_stats.corrected += stat;
  1260. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1261. }
  1262. }
  1263. return max_bitflips;
  1264. }
  1265. /**
  1266. * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
  1267. * @mtd: mtd info structure
  1268. * @chip: nand chip info structure
  1269. * @buf: buffer to store read data
  1270. * @oob_required: caller requires OOB data read to chip->oob_poi
  1271. * @page: page number to read
  1272. *
  1273. * Hardware ECC for large page chips, require OOB to be read first. For this
  1274. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1275. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1276. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1277. * the data area, by overwriting the NAND manufacturer bad block markings.
  1278. */
  1279. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1280. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  1281. {
  1282. int i, eccsize = chip->ecc.size;
  1283. int eccbytes = chip->ecc.bytes;
  1284. int eccsteps = chip->ecc.steps;
  1285. uint8_t *p = buf;
  1286. uint8_t *ecc_code = chip->buffers->ecccode;
  1287. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1288. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1289. unsigned int max_bitflips = 0;
  1290. /* Read the OOB area first */
  1291. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1292. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1293. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1294. for (i = 0; i < chip->ecc.total; i++)
  1295. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1296. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1297. int stat;
  1298. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1299. chip->read_buf(mtd, p, eccsize);
  1300. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1301. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1302. if (stat < 0) {
  1303. mtd->ecc_stats.failed++;
  1304. } else {
  1305. mtd->ecc_stats.corrected += stat;
  1306. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1307. }
  1308. }
  1309. return max_bitflips;
  1310. }
  1311. /**
  1312. * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
  1313. * @mtd: mtd info structure
  1314. * @chip: nand chip info structure
  1315. * @buf: buffer to store read data
  1316. * @oob_required: caller requires OOB data read to chip->oob_poi
  1317. * @page: page number to read
  1318. *
  1319. * The hw generator calculates the error syndrome automatically. Therefore we
  1320. * need a special oob layout and handling.
  1321. */
  1322. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1323. uint8_t *buf, int oob_required, int page)
  1324. {
  1325. int i, eccsize = chip->ecc.size;
  1326. int eccbytes = chip->ecc.bytes;
  1327. int eccsteps = chip->ecc.steps;
  1328. uint8_t *p = buf;
  1329. uint8_t *oob = chip->oob_poi;
  1330. unsigned int max_bitflips = 0;
  1331. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1332. int stat;
  1333. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1334. chip->read_buf(mtd, p, eccsize);
  1335. if (chip->ecc.prepad) {
  1336. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1337. oob += chip->ecc.prepad;
  1338. }
  1339. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1340. chip->read_buf(mtd, oob, eccbytes);
  1341. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1342. if (stat < 0) {
  1343. mtd->ecc_stats.failed++;
  1344. } else {
  1345. mtd->ecc_stats.corrected += stat;
  1346. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1347. }
  1348. oob += eccbytes;
  1349. if (chip->ecc.postpad) {
  1350. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1351. oob += chip->ecc.postpad;
  1352. }
  1353. }
  1354. /* Calculate remaining oob bytes */
  1355. i = mtd->oobsize - (oob - chip->oob_poi);
  1356. if (i)
  1357. chip->read_buf(mtd, oob, i);
  1358. return max_bitflips;
  1359. }
  1360. /**
  1361. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  1362. * @chip: nand chip structure
  1363. * @oob: oob destination address
  1364. * @ops: oob ops structure
  1365. * @len: size of oob to transfer
  1366. */
  1367. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1368. struct mtd_oob_ops *ops, size_t len)
  1369. {
  1370. switch (ops->mode) {
  1371. case MTD_OPS_PLACE_OOB:
  1372. case MTD_OPS_RAW:
  1373. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1374. return oob + len;
  1375. case MTD_OPS_AUTO_OOB: {
  1376. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1377. uint32_t boffs = 0, roffs = ops->ooboffs;
  1378. size_t bytes = 0;
  1379. for (; free->length && len; free++, len -= bytes) {
  1380. /* Read request not from offset 0? */
  1381. if (unlikely(roffs)) {
  1382. if (roffs >= free->length) {
  1383. roffs -= free->length;
  1384. continue;
  1385. }
  1386. boffs = free->offset + roffs;
  1387. bytes = min_t(size_t, len,
  1388. (free->length - roffs));
  1389. roffs = 0;
  1390. } else {
  1391. bytes = min_t(size_t, len, free->length);
  1392. boffs = free->offset;
  1393. }
  1394. memcpy(oob, chip->oob_poi + boffs, bytes);
  1395. oob += bytes;
  1396. }
  1397. return oob;
  1398. }
  1399. default:
  1400. BUG();
  1401. }
  1402. return NULL;
  1403. }
  1404. /**
  1405. * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
  1406. * @mtd: MTD device structure
  1407. * @retry_mode: the retry mode to use
  1408. *
  1409. * Some vendors supply a special command to shift the Vt threshold, to be used
  1410. * when there are too many bitflips in a page (i.e., ECC error). After setting
  1411. * a new threshold, the host should retry reading the page.
  1412. */
  1413. static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
  1414. {
  1415. struct nand_chip *chip = mtd->priv;
  1416. pr_debug("setting READ RETRY mode %d\n", retry_mode);
  1417. if (retry_mode >= chip->read_retries)
  1418. return -EINVAL;
  1419. if (!chip->setup_read_retry)
  1420. return -EOPNOTSUPP;
  1421. return chip->setup_read_retry(mtd, retry_mode);
  1422. }
  1423. /**
  1424. * nand_do_read_ops - [INTERN] Read data with ECC
  1425. * @mtd: MTD device structure
  1426. * @from: offset to read from
  1427. * @ops: oob ops structure
  1428. *
  1429. * Internal function. Called with chip held.
  1430. */
  1431. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1432. struct mtd_oob_ops *ops)
  1433. {
  1434. int chipnr, page, realpage, col, bytes, aligned, oob_required;
  1435. struct nand_chip *chip = mtd->priv;
  1436. int ret = 0;
  1437. uint32_t readlen = ops->len;
  1438. uint32_t oobreadlen = ops->ooblen;
  1439. uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
  1440. mtd->oobavail : mtd->oobsize;
  1441. uint8_t *bufpoi, *oob, *buf;
  1442. unsigned int max_bitflips = 0;
  1443. int retry_mode = 0;
  1444. bool ecc_fail = false;
  1445. chipnr = (int)(from >> chip->chip_shift);
  1446. chip->select_chip(mtd, chipnr);
  1447. realpage = (int)(from >> chip->page_shift);
  1448. page = realpage & chip->pagemask;
  1449. col = (int)(from & (mtd->writesize - 1));
  1450. buf = ops->datbuf;
  1451. oob = ops->oobbuf;
  1452. oob_required = oob ? 1 : 0;
  1453. while (1) {
  1454. unsigned int ecc_failures = mtd->ecc_stats.failed;
  1455. WATCHDOG_RESET();
  1456. bytes = min(mtd->writesize - col, readlen);
  1457. aligned = (bytes == mtd->writesize);
  1458. /* Is the current page in the buffer? */
  1459. if (realpage != chip->pagebuf || oob) {
  1460. bufpoi = aligned ? buf : chip->buffers->databuf;
  1461. read_retry:
  1462. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1463. /*
  1464. * Now read the page into the buffer. Absent an error,
  1465. * the read methods return max bitflips per ecc step.
  1466. */
  1467. if (unlikely(ops->mode == MTD_OPS_RAW))
  1468. ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
  1469. oob_required,
  1470. page);
  1471. else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
  1472. !oob)
  1473. ret = chip->ecc.read_subpage(mtd, chip,
  1474. col, bytes, bufpoi,
  1475. page);
  1476. else
  1477. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1478. oob_required, page);
  1479. if (ret < 0) {
  1480. if (!aligned)
  1481. /* Invalidate page cache */
  1482. chip->pagebuf = -1;
  1483. break;
  1484. }
  1485. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  1486. /* Transfer not aligned data */
  1487. if (!aligned) {
  1488. if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
  1489. !(mtd->ecc_stats.failed - ecc_failures) &&
  1490. (ops->mode != MTD_OPS_RAW)) {
  1491. chip->pagebuf = realpage;
  1492. chip->pagebuf_bitflips = ret;
  1493. } else {
  1494. /* Invalidate page cache */
  1495. chip->pagebuf = -1;
  1496. }
  1497. memcpy(buf, chip->buffers->databuf + col, bytes);
  1498. }
  1499. if (unlikely(oob)) {
  1500. int toread = min(oobreadlen, max_oobsize);
  1501. if (toread) {
  1502. oob = nand_transfer_oob(chip,
  1503. oob, ops, toread);
  1504. oobreadlen -= toread;
  1505. }
  1506. }
  1507. if (chip->options & NAND_NEED_READRDY) {
  1508. /* Apply delay or wait for ready/busy pin */
  1509. if (!chip->dev_ready)
  1510. udelay(chip->chip_delay);
  1511. else
  1512. nand_wait_ready(mtd);
  1513. }
  1514. if (mtd->ecc_stats.failed - ecc_failures) {
  1515. if (retry_mode + 1 < chip->read_retries) {
  1516. retry_mode++;
  1517. ret = nand_setup_read_retry(mtd,
  1518. retry_mode);
  1519. if (ret < 0)
  1520. break;
  1521. /* Reset failures; retry */
  1522. mtd->ecc_stats.failed = ecc_failures;
  1523. goto read_retry;
  1524. } else {
  1525. /* No more retry modes; real failure */
  1526. ecc_fail = true;
  1527. }
  1528. }
  1529. buf += bytes;
  1530. } else {
  1531. memcpy(buf, chip->buffers->databuf + col, bytes);
  1532. buf += bytes;
  1533. max_bitflips = max_t(unsigned int, max_bitflips,
  1534. chip->pagebuf_bitflips);
  1535. }
  1536. readlen -= bytes;
  1537. /* Reset to retry mode 0 */
  1538. if (retry_mode) {
  1539. ret = nand_setup_read_retry(mtd, 0);
  1540. if (ret < 0)
  1541. break;
  1542. retry_mode = 0;
  1543. }
  1544. if (!readlen)
  1545. break;
  1546. /* For subsequent reads align to page boundary */
  1547. col = 0;
  1548. /* Increment page address */
  1549. realpage++;
  1550. page = realpage & chip->pagemask;
  1551. /* Check, if we cross a chip boundary */
  1552. if (!page) {
  1553. chipnr++;
  1554. chip->select_chip(mtd, -1);
  1555. chip->select_chip(mtd, chipnr);
  1556. }
  1557. }
  1558. chip->select_chip(mtd, -1);
  1559. ops->retlen = ops->len - (size_t) readlen;
  1560. if (oob)
  1561. ops->oobretlen = ops->ooblen - oobreadlen;
  1562. if (ret < 0)
  1563. return ret;
  1564. if (ecc_fail)
  1565. return -EBADMSG;
  1566. return max_bitflips;
  1567. }
  1568. /**
  1569. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1570. * @mtd: MTD device structure
  1571. * @from: offset to read from
  1572. * @len: number of bytes to read
  1573. * @retlen: pointer to variable to store the number of read bytes
  1574. * @buf: the databuffer to put data
  1575. *
  1576. * Get hold of the chip and call nand_do_read.
  1577. */
  1578. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1579. size_t *retlen, uint8_t *buf)
  1580. {
  1581. struct mtd_oob_ops ops;
  1582. int ret;
  1583. nand_get_device(mtd, FL_READING);
  1584. ops.len = len;
  1585. ops.datbuf = buf;
  1586. ops.oobbuf = NULL;
  1587. ops.mode = MTD_OPS_PLACE_OOB;
  1588. ret = nand_do_read_ops(mtd, from, &ops);
  1589. *retlen = ops.retlen;
  1590. nand_release_device(mtd);
  1591. return ret;
  1592. }
  1593. /**
  1594. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1595. * @mtd: mtd info structure
  1596. * @chip: nand chip info structure
  1597. * @page: page number to read
  1598. */
  1599. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1600. int page)
  1601. {
  1602. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1603. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1604. return 0;
  1605. }
  1606. /**
  1607. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1608. * with syndromes
  1609. * @mtd: mtd info structure
  1610. * @chip: nand chip info structure
  1611. * @page: page number to read
  1612. */
  1613. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1614. int page)
  1615. {
  1616. uint8_t *buf = chip->oob_poi;
  1617. int length = mtd->oobsize;
  1618. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1619. int eccsize = chip->ecc.size;
  1620. uint8_t *bufpoi = buf;
  1621. int i, toread, sndrnd = 0, pos;
  1622. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1623. for (i = 0; i < chip->ecc.steps; i++) {
  1624. if (sndrnd) {
  1625. pos = eccsize + i * (eccsize + chunk);
  1626. if (mtd->writesize > 512)
  1627. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1628. else
  1629. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1630. } else
  1631. sndrnd = 1;
  1632. toread = min_t(int, length, chunk);
  1633. chip->read_buf(mtd, bufpoi, toread);
  1634. bufpoi += toread;
  1635. length -= toread;
  1636. }
  1637. if (length > 0)
  1638. chip->read_buf(mtd, bufpoi, length);
  1639. return 0;
  1640. }
  1641. /**
  1642. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1643. * @mtd: mtd info structure
  1644. * @chip: nand chip info structure
  1645. * @page: page number to write
  1646. */
  1647. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1648. int page)
  1649. {
  1650. int status = 0;
  1651. const uint8_t *buf = chip->oob_poi;
  1652. int length = mtd->oobsize;
  1653. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1654. chip->write_buf(mtd, buf, length);
  1655. /* Send command to program the OOB data */
  1656. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1657. status = chip->waitfunc(mtd, chip);
  1658. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1659. }
  1660. /**
  1661. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1662. * with syndrome - only for large page flash
  1663. * @mtd: mtd info structure
  1664. * @chip: nand chip info structure
  1665. * @page: page number to write
  1666. */
  1667. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1668. struct nand_chip *chip, int page)
  1669. {
  1670. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1671. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1672. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1673. const uint8_t *bufpoi = chip->oob_poi;
  1674. /*
  1675. * data-ecc-data-ecc ... ecc-oob
  1676. * or
  1677. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1678. */
  1679. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1680. pos = steps * (eccsize + chunk);
  1681. steps = 0;
  1682. } else
  1683. pos = eccsize;
  1684. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1685. for (i = 0; i < steps; i++) {
  1686. if (sndcmd) {
  1687. if (mtd->writesize <= 512) {
  1688. uint32_t fill = 0xFFFFFFFF;
  1689. len = eccsize;
  1690. while (len > 0) {
  1691. int num = min_t(int, len, 4);
  1692. chip->write_buf(mtd, (uint8_t *)&fill,
  1693. num);
  1694. len -= num;
  1695. }
  1696. } else {
  1697. pos = eccsize + i * (eccsize + chunk);
  1698. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1699. }
  1700. } else
  1701. sndcmd = 1;
  1702. len = min_t(int, length, chunk);
  1703. chip->write_buf(mtd, bufpoi, len);
  1704. bufpoi += len;
  1705. length -= len;
  1706. }
  1707. if (length > 0)
  1708. chip->write_buf(mtd, bufpoi, length);
  1709. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1710. status = chip->waitfunc(mtd, chip);
  1711. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1712. }
  1713. /**
  1714. * nand_do_read_oob - [INTERN] NAND read out-of-band
  1715. * @mtd: MTD device structure
  1716. * @from: offset to read from
  1717. * @ops: oob operations description structure
  1718. *
  1719. * NAND read out-of-band data from the spare area.
  1720. */
  1721. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1722. struct mtd_oob_ops *ops)
  1723. {
  1724. int page, realpage, chipnr;
  1725. struct nand_chip *chip = mtd->priv;
  1726. struct mtd_ecc_stats stats;
  1727. int readlen = ops->ooblen;
  1728. int len;
  1729. uint8_t *buf = ops->oobbuf;
  1730. int ret = 0;
  1731. pr_debug("%s: from = 0x%08Lx, len = %i\n",
  1732. __func__, (unsigned long long)from, readlen);
  1733. stats = mtd->ecc_stats;
  1734. if (ops->mode == MTD_OPS_AUTO_OOB)
  1735. len = chip->ecc.layout->oobavail;
  1736. else
  1737. len = mtd->oobsize;
  1738. if (unlikely(ops->ooboffs >= len)) {
  1739. pr_debug("%s: attempt to start read outside oob\n",
  1740. __func__);
  1741. return -EINVAL;
  1742. }
  1743. /* Do not allow reads past end of device */
  1744. if (unlikely(from >= mtd->size ||
  1745. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1746. (from >> chip->page_shift)) * len)) {
  1747. pr_debug("%s: attempt to read beyond end of device\n",
  1748. __func__);
  1749. return -EINVAL;
  1750. }
  1751. chipnr = (int)(from >> chip->chip_shift);
  1752. chip->select_chip(mtd, chipnr);
  1753. /* Shift to get page */
  1754. realpage = (int)(from >> chip->page_shift);
  1755. page = realpage & chip->pagemask;
  1756. while (1) {
  1757. WATCHDOG_RESET();
  1758. if (ops->mode == MTD_OPS_RAW)
  1759. ret = chip->ecc.read_oob_raw(mtd, chip, page);
  1760. else
  1761. ret = chip->ecc.read_oob(mtd, chip, page);
  1762. if (ret < 0)
  1763. break;
  1764. len = min(len, readlen);
  1765. buf = nand_transfer_oob(chip, buf, ops, len);
  1766. if (chip->options & NAND_NEED_READRDY) {
  1767. /* Apply delay or wait for ready/busy pin */
  1768. if (!chip->dev_ready)
  1769. udelay(chip->chip_delay);
  1770. else
  1771. nand_wait_ready(mtd);
  1772. }
  1773. readlen -= len;
  1774. if (!readlen)
  1775. break;
  1776. /* Increment page address */
  1777. realpage++;
  1778. page = realpage & chip->pagemask;
  1779. /* Check, if we cross a chip boundary */
  1780. if (!page) {
  1781. chipnr++;
  1782. chip->select_chip(mtd, -1);
  1783. chip->select_chip(mtd, chipnr);
  1784. }
  1785. }
  1786. chip->select_chip(mtd, -1);
  1787. ops->oobretlen = ops->ooblen - readlen;
  1788. if (ret < 0)
  1789. return ret;
  1790. if (mtd->ecc_stats.failed - stats.failed)
  1791. return -EBADMSG;
  1792. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1793. }
  1794. /**
  1795. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1796. * @mtd: MTD device structure
  1797. * @from: offset to read from
  1798. * @ops: oob operation description structure
  1799. *
  1800. * NAND read data and/or out-of-band data.
  1801. */
  1802. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1803. struct mtd_oob_ops *ops)
  1804. {
  1805. int ret = -ENOTSUPP;
  1806. ops->retlen = 0;
  1807. /* Do not allow reads past end of device */
  1808. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1809. pr_debug("%s: attempt to read beyond end of device\n",
  1810. __func__);
  1811. return -EINVAL;
  1812. }
  1813. nand_get_device(mtd, FL_READING);
  1814. switch (ops->mode) {
  1815. case MTD_OPS_PLACE_OOB:
  1816. case MTD_OPS_AUTO_OOB:
  1817. case MTD_OPS_RAW:
  1818. break;
  1819. default:
  1820. goto out;
  1821. }
  1822. if (!ops->datbuf)
  1823. ret = nand_do_read_oob(mtd, from, ops);
  1824. else
  1825. ret = nand_do_read_ops(mtd, from, ops);
  1826. out:
  1827. nand_release_device(mtd);
  1828. return ret;
  1829. }
  1830. /**
  1831. * nand_write_page_raw - [INTERN] raw page write function
  1832. * @mtd: mtd info structure
  1833. * @chip: nand chip info structure
  1834. * @buf: data buffer
  1835. * @oob_required: must write chip->oob_poi to OOB
  1836. *
  1837. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1838. */
  1839. static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1840. const uint8_t *buf, int oob_required)
  1841. {
  1842. chip->write_buf(mtd, buf, mtd->writesize);
  1843. if (oob_required)
  1844. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1845. return 0;
  1846. }
  1847. /**
  1848. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  1849. * @mtd: mtd info structure
  1850. * @chip: nand chip info structure
  1851. * @buf: data buffer
  1852. * @oob_required: must write chip->oob_poi to OOB
  1853. *
  1854. * We need a special oob layout and handling even when ECC isn't checked.
  1855. */
  1856. static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1857. struct nand_chip *chip,
  1858. const uint8_t *buf, int oob_required)
  1859. {
  1860. int eccsize = chip->ecc.size;
  1861. int eccbytes = chip->ecc.bytes;
  1862. uint8_t *oob = chip->oob_poi;
  1863. int steps, size;
  1864. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1865. chip->write_buf(mtd, buf, eccsize);
  1866. buf += eccsize;
  1867. if (chip->ecc.prepad) {
  1868. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1869. oob += chip->ecc.prepad;
  1870. }
  1871. chip->write_buf(mtd, oob, eccbytes);
  1872. oob += eccbytes;
  1873. if (chip->ecc.postpad) {
  1874. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1875. oob += chip->ecc.postpad;
  1876. }
  1877. }
  1878. size = mtd->oobsize - (oob - chip->oob_poi);
  1879. if (size)
  1880. chip->write_buf(mtd, oob, size);
  1881. return 0;
  1882. }
  1883. /**
  1884. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  1885. * @mtd: mtd info structure
  1886. * @chip: nand chip info structure
  1887. * @buf: data buffer
  1888. * @oob_required: must write chip->oob_poi to OOB
  1889. */
  1890. static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1891. const uint8_t *buf, int oob_required)
  1892. {
  1893. int i, eccsize = chip->ecc.size;
  1894. int eccbytes = chip->ecc.bytes;
  1895. int eccsteps = chip->ecc.steps;
  1896. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1897. const uint8_t *p = buf;
  1898. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1899. /* Software ECC calculation */
  1900. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1901. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1902. for (i = 0; i < chip->ecc.total; i++)
  1903. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1904. return chip->ecc.write_page_raw(mtd, chip, buf, 1);
  1905. }
  1906. /**
  1907. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  1908. * @mtd: mtd info structure
  1909. * @chip: nand chip info structure
  1910. * @buf: data buffer
  1911. * @oob_required: must write chip->oob_poi to OOB
  1912. */
  1913. static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1914. const uint8_t *buf, int oob_required)
  1915. {
  1916. int i, eccsize = chip->ecc.size;
  1917. int eccbytes = chip->ecc.bytes;
  1918. int eccsteps = chip->ecc.steps;
  1919. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1920. const uint8_t *p = buf;
  1921. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1922. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1923. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1924. chip->write_buf(mtd, p, eccsize);
  1925. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1926. }
  1927. for (i = 0; i < chip->ecc.total; i++)
  1928. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1929. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1930. return 0;
  1931. }
  1932. /**
  1933. * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
  1934. * @mtd: mtd info structure
  1935. * @chip: nand chip info structure
  1936. * @offset: column address of subpage within the page
  1937. * @data_len: data length
  1938. * @buf: data buffer
  1939. * @oob_required: must write chip->oob_poi to OOB
  1940. */
  1941. static int nand_write_subpage_hwecc(struct mtd_info *mtd,
  1942. struct nand_chip *chip, uint32_t offset,
  1943. uint32_t data_len, const uint8_t *buf,
  1944. int oob_required)
  1945. {
  1946. uint8_t *oob_buf = chip->oob_poi;
  1947. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1948. int ecc_size = chip->ecc.size;
  1949. int ecc_bytes = chip->ecc.bytes;
  1950. int ecc_steps = chip->ecc.steps;
  1951. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1952. uint32_t start_step = offset / ecc_size;
  1953. uint32_t end_step = (offset + data_len - 1) / ecc_size;
  1954. int oob_bytes = mtd->oobsize / ecc_steps;
  1955. int step, i;
  1956. for (step = 0; step < ecc_steps; step++) {
  1957. /* configure controller for WRITE access */
  1958. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1959. /* write data (untouched subpages already masked by 0xFF) */
  1960. chip->write_buf(mtd, buf, ecc_size);
  1961. /* mask ECC of un-touched subpages by padding 0xFF */
  1962. if ((step < start_step) || (step > end_step))
  1963. memset(ecc_calc, 0xff, ecc_bytes);
  1964. else
  1965. chip->ecc.calculate(mtd, buf, ecc_calc);
  1966. /* mask OOB of un-touched subpages by padding 0xFF */
  1967. /* if oob_required, preserve OOB metadata of written subpage */
  1968. if (!oob_required || (step < start_step) || (step > end_step))
  1969. memset(oob_buf, 0xff, oob_bytes);
  1970. buf += ecc_size;
  1971. ecc_calc += ecc_bytes;
  1972. oob_buf += oob_bytes;
  1973. }
  1974. /* copy calculated ECC for whole page to chip->buffer->oob */
  1975. /* this include masked-value(0xFF) for unwritten subpages */
  1976. ecc_calc = chip->buffers->ecccalc;
  1977. for (i = 0; i < chip->ecc.total; i++)
  1978. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1979. /* write OOB buffer to NAND device */
  1980. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1981. return 0;
  1982. }
  1983. /**
  1984. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  1985. * @mtd: mtd info structure
  1986. * @chip: nand chip info structure
  1987. * @buf: data buffer
  1988. * @oob_required: must write chip->oob_poi to OOB
  1989. *
  1990. * The hw generator calculates the error syndrome automatically. Therefore we
  1991. * need a special oob layout and handling.
  1992. */
  1993. static int nand_write_page_syndrome(struct mtd_info *mtd,
  1994. struct nand_chip *chip,
  1995. const uint8_t *buf, int oob_required)
  1996. {
  1997. int i, eccsize = chip->ecc.size;
  1998. int eccbytes = chip->ecc.bytes;
  1999. int eccsteps = chip->ecc.steps;
  2000. const uint8_t *p = buf;
  2001. uint8_t *oob = chip->oob_poi;
  2002. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  2003. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  2004. chip->write_buf(mtd, p, eccsize);
  2005. if (chip->ecc.prepad) {
  2006. chip->write_buf(mtd, oob, chip->ecc.prepad);
  2007. oob += chip->ecc.prepad;
  2008. }
  2009. chip->ecc.calculate(mtd, p, oob);
  2010. chip->write_buf(mtd, oob, eccbytes);
  2011. oob += eccbytes;
  2012. if (chip->ecc.postpad) {
  2013. chip->write_buf(mtd, oob, chip->ecc.postpad);
  2014. oob += chip->ecc.postpad;
  2015. }
  2016. }
  2017. /* Calculate remaining oob bytes */
  2018. i = mtd->oobsize - (oob - chip->oob_poi);
  2019. if (i)
  2020. chip->write_buf(mtd, oob, i);
  2021. return 0;
  2022. }
  2023. /**
  2024. * nand_write_page - [REPLACEABLE] write one page
  2025. * @mtd: MTD device structure
  2026. * @chip: NAND chip descriptor
  2027. * @offset: address offset within the page
  2028. * @data_len: length of actual data to be written
  2029. * @buf: the data to write
  2030. * @oob_required: must write chip->oob_poi to OOB
  2031. * @page: page number to write
  2032. * @cached: cached programming
  2033. * @raw: use _raw version of write_page
  2034. */
  2035. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  2036. uint32_t offset, int data_len, const uint8_t *buf,
  2037. int oob_required, int page, int cached, int raw)
  2038. {
  2039. int status, subpage;
  2040. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2041. chip->ecc.write_subpage)
  2042. subpage = offset || (data_len < mtd->writesize);
  2043. else
  2044. subpage = 0;
  2045. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  2046. if (unlikely(raw))
  2047. status = chip->ecc.write_page_raw(mtd, chip, buf,
  2048. oob_required);
  2049. else if (subpage)
  2050. status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
  2051. buf, oob_required);
  2052. else
  2053. status = chip->ecc.write_page(mtd, chip, buf, oob_required);
  2054. if (status < 0)
  2055. return status;
  2056. /*
  2057. * Cached progamming disabled for now. Not sure if it's worth the
  2058. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  2059. */
  2060. cached = 0;
  2061. if (!cached || !NAND_HAS_CACHEPROG(chip)) {
  2062. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  2063. status = chip->waitfunc(mtd, chip);
  2064. /*
  2065. * See if operation failed and additional status checks are
  2066. * available.
  2067. */
  2068. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2069. status = chip->errstat(mtd, chip, FL_WRITING, status,
  2070. page);
  2071. if (status & NAND_STATUS_FAIL)
  2072. return -EIO;
  2073. } else {
  2074. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  2075. status = chip->waitfunc(mtd, chip);
  2076. }
  2077. return 0;
  2078. }
  2079. /**
  2080. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  2081. * @mtd: MTD device structure
  2082. * @oob: oob data buffer
  2083. * @len: oob data write length
  2084. * @ops: oob ops structure
  2085. */
  2086. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  2087. struct mtd_oob_ops *ops)
  2088. {
  2089. struct nand_chip *chip = mtd->priv;
  2090. /*
  2091. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  2092. * data from a previous OOB read.
  2093. */
  2094. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2095. switch (ops->mode) {
  2096. case MTD_OPS_PLACE_OOB:
  2097. case MTD_OPS_RAW:
  2098. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  2099. return oob + len;
  2100. case MTD_OPS_AUTO_OOB: {
  2101. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  2102. uint32_t boffs = 0, woffs = ops->ooboffs;
  2103. size_t bytes = 0;
  2104. for (; free->length && len; free++, len -= bytes) {
  2105. /* Write request not from offset 0? */
  2106. if (unlikely(woffs)) {
  2107. if (woffs >= free->length) {
  2108. woffs -= free->length;
  2109. continue;
  2110. }
  2111. boffs = free->offset + woffs;
  2112. bytes = min_t(size_t, len,
  2113. (free->length - woffs));
  2114. woffs = 0;
  2115. } else {
  2116. bytes = min_t(size_t, len, free->length);
  2117. boffs = free->offset;
  2118. }
  2119. memcpy(chip->oob_poi + boffs, oob, bytes);
  2120. oob += bytes;
  2121. }
  2122. return oob;
  2123. }
  2124. default:
  2125. BUG();
  2126. }
  2127. return NULL;
  2128. }
  2129. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  2130. /**
  2131. * nand_do_write_ops - [INTERN] NAND write with ECC
  2132. * @mtd: MTD device structure
  2133. * @to: offset to write to
  2134. * @ops: oob operations description structure
  2135. *
  2136. * NAND write with ECC.
  2137. */
  2138. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  2139. struct mtd_oob_ops *ops)
  2140. {
  2141. int chipnr, realpage, page, blockmask, column;
  2142. struct nand_chip *chip = mtd->priv;
  2143. uint32_t writelen = ops->len;
  2144. uint32_t oobwritelen = ops->ooblen;
  2145. uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
  2146. mtd->oobavail : mtd->oobsize;
  2147. uint8_t *oob = ops->oobbuf;
  2148. uint8_t *buf = ops->datbuf;
  2149. int ret;
  2150. int oob_required = oob ? 1 : 0;
  2151. ops->retlen = 0;
  2152. if (!writelen)
  2153. return 0;
  2154. #ifndef __UBOOT__
  2155. /* Reject writes, which are not page aligned */
  2156. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  2157. #else
  2158. /* Reject writes, which are not page aligned */
  2159. if (NOTALIGNED(to)) {
  2160. #endif
  2161. pr_notice("%s: attempt to write non page aligned data\n",
  2162. __func__);
  2163. return -EINVAL;
  2164. }
  2165. column = to & (mtd->writesize - 1);
  2166. chipnr = (int)(to >> chip->chip_shift);
  2167. chip->select_chip(mtd, chipnr);
  2168. /* Check, if it is write protected */
  2169. if (nand_check_wp(mtd)) {
  2170. ret = -EIO;
  2171. goto err_out;
  2172. }
  2173. realpage = (int)(to >> chip->page_shift);
  2174. page = realpage & chip->pagemask;
  2175. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  2176. /* Invalidate the page cache, when we write to the cached page */
  2177. if (to <= (chip->pagebuf << chip->page_shift) &&
  2178. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  2179. chip->pagebuf = -1;
  2180. /* Don't allow multipage oob writes with offset */
  2181. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
  2182. ret = -EINVAL;
  2183. goto err_out;
  2184. }
  2185. while (1) {
  2186. int bytes = mtd->writesize;
  2187. int cached = writelen > bytes && page != blockmask;
  2188. uint8_t *wbuf = buf;
  2189. WATCHDOG_RESET();
  2190. /* Partial page write? */
  2191. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  2192. cached = 0;
  2193. bytes = min_t(int, bytes - column, (int) writelen);
  2194. chip->pagebuf = -1;
  2195. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  2196. memcpy(&chip->buffers->databuf[column], buf, bytes);
  2197. wbuf = chip->buffers->databuf;
  2198. }
  2199. if (unlikely(oob)) {
  2200. size_t len = min(oobwritelen, oobmaxlen);
  2201. oob = nand_fill_oob(mtd, oob, len, ops);
  2202. oobwritelen -= len;
  2203. } else {
  2204. /* We still need to erase leftover OOB data */
  2205. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2206. }
  2207. ret = chip->write_page(mtd, chip, column, bytes, wbuf,
  2208. oob_required, page, cached,
  2209. (ops->mode == MTD_OPS_RAW));
  2210. if (ret)
  2211. break;
  2212. writelen -= bytes;
  2213. if (!writelen)
  2214. break;
  2215. column = 0;
  2216. buf += bytes;
  2217. realpage++;
  2218. page = realpage & chip->pagemask;
  2219. /* Check, if we cross a chip boundary */
  2220. if (!page) {
  2221. chipnr++;
  2222. chip->select_chip(mtd, -1);
  2223. chip->select_chip(mtd, chipnr);
  2224. }
  2225. }
  2226. ops->retlen = ops->len - writelen;
  2227. if (unlikely(oob))
  2228. ops->oobretlen = ops->ooblen;
  2229. err_out:
  2230. chip->select_chip(mtd, -1);
  2231. return ret;
  2232. }
  2233. /**
  2234. * panic_nand_write - [MTD Interface] NAND write with ECC
  2235. * @mtd: MTD device structure
  2236. * @to: offset to write to
  2237. * @len: number of bytes to write
  2238. * @retlen: pointer to variable to store the number of written bytes
  2239. * @buf: the data to write
  2240. *
  2241. * NAND write with ECC. Used when performing writes in interrupt context, this
  2242. * may for example be called by mtdoops when writing an oops while in panic.
  2243. */
  2244. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2245. size_t *retlen, const uint8_t *buf)
  2246. {
  2247. struct nand_chip *chip = mtd->priv;
  2248. struct mtd_oob_ops ops;
  2249. int ret;
  2250. /* Wait for the device to get ready */
  2251. panic_nand_wait(mtd, chip, 400);
  2252. /* Grab the device */
  2253. panic_nand_get_device(chip, mtd, FL_WRITING);
  2254. ops.len = len;
  2255. ops.datbuf = (uint8_t *)buf;
  2256. ops.oobbuf = NULL;
  2257. ops.mode = MTD_OPS_PLACE_OOB;
  2258. ret = nand_do_write_ops(mtd, to, &ops);
  2259. *retlen = ops.retlen;
  2260. return ret;
  2261. }
  2262. /**
  2263. * nand_write - [MTD Interface] NAND write with ECC
  2264. * @mtd: MTD device structure
  2265. * @to: offset to write to
  2266. * @len: number of bytes to write
  2267. * @retlen: pointer to variable to store the number of written bytes
  2268. * @buf: the data to write
  2269. *
  2270. * NAND write with ECC.
  2271. */
  2272. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2273. size_t *retlen, const uint8_t *buf)
  2274. {
  2275. struct mtd_oob_ops ops;
  2276. int ret;
  2277. nand_get_device(mtd, FL_WRITING);
  2278. ops.len = len;
  2279. ops.datbuf = (uint8_t *)buf;
  2280. ops.oobbuf = NULL;
  2281. ops.mode = MTD_OPS_PLACE_OOB;
  2282. ret = nand_do_write_ops(mtd, to, &ops);
  2283. *retlen = ops.retlen;
  2284. nand_release_device(mtd);
  2285. return ret;
  2286. }
  2287. /**
  2288. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2289. * @mtd: MTD device structure
  2290. * @to: offset to write to
  2291. * @ops: oob operation description structure
  2292. *
  2293. * NAND write out-of-band.
  2294. */
  2295. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2296. struct mtd_oob_ops *ops)
  2297. {
  2298. int chipnr, page, status, len;
  2299. struct nand_chip *chip = mtd->priv;
  2300. pr_debug("%s: to = 0x%08x, len = %i\n",
  2301. __func__, (unsigned int)to, (int)ops->ooblen);
  2302. if (ops->mode == MTD_OPS_AUTO_OOB)
  2303. len = chip->ecc.layout->oobavail;
  2304. else
  2305. len = mtd->oobsize;
  2306. /* Do not allow write past end of page */
  2307. if ((ops->ooboffs + ops->ooblen) > len) {
  2308. pr_debug("%s: attempt to write past end of page\n",
  2309. __func__);
  2310. return -EINVAL;
  2311. }
  2312. if (unlikely(ops->ooboffs >= len)) {
  2313. pr_debug("%s: attempt to start write outside oob\n",
  2314. __func__);
  2315. return -EINVAL;
  2316. }
  2317. /* Do not allow write past end of device */
  2318. if (unlikely(to >= mtd->size ||
  2319. ops->ooboffs + ops->ooblen >
  2320. ((mtd->size >> chip->page_shift) -
  2321. (to >> chip->page_shift)) * len)) {
  2322. pr_debug("%s: attempt to write beyond end of device\n",
  2323. __func__);
  2324. return -EINVAL;
  2325. }
  2326. chipnr = (int)(to >> chip->chip_shift);
  2327. chip->select_chip(mtd, chipnr);
  2328. /* Shift to get page */
  2329. page = (int)(to >> chip->page_shift);
  2330. /*
  2331. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2332. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2333. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2334. * it in the doc2000 driver in August 1999. dwmw2.
  2335. */
  2336. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2337. /* Check, if it is write protected */
  2338. if (nand_check_wp(mtd)) {
  2339. chip->select_chip(mtd, -1);
  2340. return -EROFS;
  2341. }
  2342. /* Invalidate the page cache, if we write to the cached page */
  2343. if (page == chip->pagebuf)
  2344. chip->pagebuf = -1;
  2345. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2346. if (ops->mode == MTD_OPS_RAW)
  2347. status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
  2348. else
  2349. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2350. chip->select_chip(mtd, -1);
  2351. if (status)
  2352. return status;
  2353. ops->oobretlen = ops->ooblen;
  2354. return 0;
  2355. }
  2356. /**
  2357. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2358. * @mtd: MTD device structure
  2359. * @to: offset to write to
  2360. * @ops: oob operation description structure
  2361. */
  2362. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2363. struct mtd_oob_ops *ops)
  2364. {
  2365. int ret = -ENOTSUPP;
  2366. ops->retlen = 0;
  2367. /* Do not allow writes past end of device */
  2368. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2369. pr_debug("%s: attempt to write beyond end of device\n",
  2370. __func__);
  2371. return -EINVAL;
  2372. }
  2373. nand_get_device(mtd, FL_WRITING);
  2374. switch (ops->mode) {
  2375. case MTD_OPS_PLACE_OOB:
  2376. case MTD_OPS_AUTO_OOB:
  2377. case MTD_OPS_RAW:
  2378. break;
  2379. default:
  2380. goto out;
  2381. }
  2382. if (!ops->datbuf)
  2383. ret = nand_do_write_oob(mtd, to, ops);
  2384. else
  2385. ret = nand_do_write_ops(mtd, to, ops);
  2386. out:
  2387. nand_release_device(mtd);
  2388. return ret;
  2389. }
  2390. /**
  2391. * single_erase_cmd - [GENERIC] NAND standard block erase command function
  2392. * @mtd: MTD device structure
  2393. * @page: the page address of the block which will be erased
  2394. *
  2395. * Standard erase command for NAND chips.
  2396. */
  2397. static void single_erase_cmd(struct mtd_info *mtd, int page)
  2398. {
  2399. struct nand_chip *chip = mtd->priv;
  2400. /* Send commands to erase a block */
  2401. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2402. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2403. }
  2404. /**
  2405. * nand_erase - [MTD Interface] erase block(s)
  2406. * @mtd: MTD device structure
  2407. * @instr: erase instruction
  2408. *
  2409. * Erase one ore more blocks.
  2410. */
  2411. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2412. {
  2413. return nand_erase_nand(mtd, instr, 0);
  2414. }
  2415. /**
  2416. * nand_erase_nand - [INTERN] erase block(s)
  2417. * @mtd: MTD device structure
  2418. * @instr: erase instruction
  2419. * @allowbbt: allow erasing the bbt area
  2420. *
  2421. * Erase one ore more blocks.
  2422. */
  2423. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2424. int allowbbt)
  2425. {
  2426. int page, status, pages_per_block, ret, chipnr;
  2427. struct nand_chip *chip = mtd->priv;
  2428. loff_t len;
  2429. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2430. __func__, (unsigned long long)instr->addr,
  2431. (unsigned long long)instr->len);
  2432. if (check_offs_len(mtd, instr->addr, instr->len))
  2433. return -EINVAL;
  2434. /* Grab the lock and see if the device is available */
  2435. nand_get_device(mtd, FL_ERASING);
  2436. /* Shift to get first page */
  2437. page = (int)(instr->addr >> chip->page_shift);
  2438. chipnr = (int)(instr->addr >> chip->chip_shift);
  2439. /* Calculate pages in each block */
  2440. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2441. /* Select the NAND device */
  2442. chip->select_chip(mtd, chipnr);
  2443. /* Check, if it is write protected */
  2444. if (nand_check_wp(mtd)) {
  2445. pr_debug("%s: device is write protected!\n",
  2446. __func__);
  2447. instr->state = MTD_ERASE_FAILED;
  2448. goto erase_exit;
  2449. }
  2450. /* Loop through the pages */
  2451. len = instr->len;
  2452. instr->state = MTD_ERASING;
  2453. while (len) {
  2454. WATCHDOG_RESET();
  2455. /* Check if we have a bad block, we do not erase bad blocks! */
  2456. if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
  2457. chip->page_shift, 0, allowbbt)) {
  2458. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2459. __func__, page);
  2460. instr->state = MTD_ERASE_FAILED;
  2461. goto erase_exit;
  2462. }
  2463. /*
  2464. * Invalidate the page cache, if we erase the block which
  2465. * contains the current cached page.
  2466. */
  2467. if (page <= chip->pagebuf && chip->pagebuf <
  2468. (page + pages_per_block))
  2469. chip->pagebuf = -1;
  2470. chip->erase_cmd(mtd, page & chip->pagemask);
  2471. status = chip->waitfunc(mtd, chip);
  2472. /*
  2473. * See if operation failed and additional status checks are
  2474. * available
  2475. */
  2476. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2477. status = chip->errstat(mtd, chip, FL_ERASING,
  2478. status, page);
  2479. /* See if block erase succeeded */
  2480. if (status & NAND_STATUS_FAIL) {
  2481. pr_debug("%s: failed erase, page 0x%08x\n",
  2482. __func__, page);
  2483. instr->state = MTD_ERASE_FAILED;
  2484. instr->fail_addr =
  2485. ((loff_t)page << chip->page_shift);
  2486. goto erase_exit;
  2487. }
  2488. /* Increment page address and decrement length */
  2489. len -= (1ULL << chip->phys_erase_shift);
  2490. page += pages_per_block;
  2491. /* Check, if we cross a chip boundary */
  2492. if (len && !(page & chip->pagemask)) {
  2493. chipnr++;
  2494. chip->select_chip(mtd, -1);
  2495. chip->select_chip(mtd, chipnr);
  2496. }
  2497. }
  2498. instr->state = MTD_ERASE_DONE;
  2499. erase_exit:
  2500. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2501. /* Deselect and wake up anyone waiting on the device */
  2502. chip->select_chip(mtd, -1);
  2503. nand_release_device(mtd);
  2504. /* Do call back function */
  2505. if (!ret)
  2506. mtd_erase_callback(instr);
  2507. /* Return more or less happy */
  2508. return ret;
  2509. }
  2510. /**
  2511. * nand_sync - [MTD Interface] sync
  2512. * @mtd: MTD device structure
  2513. *
  2514. * Sync is actually a wait for chip ready function.
  2515. */
  2516. static void nand_sync(struct mtd_info *mtd)
  2517. {
  2518. pr_debug("%s: called\n", __func__);
  2519. /* Grab the lock and see if the device is available */
  2520. nand_get_device(mtd, FL_SYNCING);
  2521. /* Release it and go back */
  2522. nand_release_device(mtd);
  2523. }
  2524. /**
  2525. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2526. * @mtd: MTD device structure
  2527. * @offs: offset relative to mtd start
  2528. */
  2529. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2530. {
  2531. return nand_block_checkbad(mtd, offs, 1, 0);
  2532. }
  2533. /**
  2534. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2535. * @mtd: MTD device structure
  2536. * @ofs: offset relative to mtd start
  2537. */
  2538. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2539. {
  2540. int ret;
  2541. ret = nand_block_isbad(mtd, ofs);
  2542. if (ret) {
  2543. /* If it was bad already, return success and do nothing */
  2544. if (ret > 0)
  2545. return 0;
  2546. return ret;
  2547. }
  2548. return nand_block_markbad_lowlevel(mtd, ofs);
  2549. }
  2550. /**
  2551. * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
  2552. * @mtd: MTD device structure
  2553. * @chip: nand chip info structure
  2554. * @addr: feature address.
  2555. * @subfeature_param: the subfeature parameters, a four bytes array.
  2556. */
  2557. static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
  2558. int addr, uint8_t *subfeature_param)
  2559. {
  2560. int status;
  2561. int i;
  2562. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2563. if (!chip->onfi_version ||
  2564. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2565. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2566. return -EINVAL;
  2567. #endif
  2568. chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
  2569. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2570. chip->write_byte(mtd, subfeature_param[i]);
  2571. status = chip->waitfunc(mtd, chip);
  2572. if (status & NAND_STATUS_FAIL)
  2573. return -EIO;
  2574. return 0;
  2575. }
  2576. /**
  2577. * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
  2578. * @mtd: MTD device structure
  2579. * @chip: nand chip info structure
  2580. * @addr: feature address.
  2581. * @subfeature_param: the subfeature parameters, a four bytes array.
  2582. */
  2583. static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
  2584. int addr, uint8_t *subfeature_param)
  2585. {
  2586. int i;
  2587. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2588. if (!chip->onfi_version ||
  2589. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2590. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2591. return -EINVAL;
  2592. #endif
  2593. /* clear the sub feature parameters */
  2594. memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
  2595. chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
  2596. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2597. *subfeature_param++ = chip->read_byte(mtd);
  2598. return 0;
  2599. }
  2600. #ifndef __UBOOT__
  2601. /**
  2602. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2603. * @mtd: MTD device structure
  2604. */
  2605. static int nand_suspend(struct mtd_info *mtd)
  2606. {
  2607. return nand_get_device(mtd, FL_PM_SUSPENDED);
  2608. }
  2609. /**
  2610. * nand_resume - [MTD Interface] Resume the NAND flash
  2611. * @mtd: MTD device structure
  2612. */
  2613. static void nand_resume(struct mtd_info *mtd)
  2614. {
  2615. struct nand_chip *chip = mtd->priv;
  2616. if (chip->state == FL_PM_SUSPENDED)
  2617. nand_release_device(mtd);
  2618. else
  2619. pr_err("%s called for a chip which is not in suspended state\n",
  2620. __func__);
  2621. }
  2622. #endif
  2623. /* Set default functions */
  2624. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2625. {
  2626. /* check for proper chip_delay setup, set 20us if not */
  2627. if (!chip->chip_delay)
  2628. chip->chip_delay = 20;
  2629. /* check, if a user supplied command function given */
  2630. if (chip->cmdfunc == NULL)
  2631. chip->cmdfunc = nand_command;
  2632. /* check, if a user supplied wait function given */
  2633. if (chip->waitfunc == NULL)
  2634. chip->waitfunc = nand_wait;
  2635. if (!chip->select_chip)
  2636. chip->select_chip = nand_select_chip;
  2637. /* set for ONFI nand */
  2638. if (!chip->onfi_set_features)
  2639. chip->onfi_set_features = nand_onfi_set_features;
  2640. if (!chip->onfi_get_features)
  2641. chip->onfi_get_features = nand_onfi_get_features;
  2642. /* If called twice, pointers that depend on busw may need to be reset */
  2643. if (!chip->read_byte || chip->read_byte == nand_read_byte)
  2644. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2645. if (!chip->read_word)
  2646. chip->read_word = nand_read_word;
  2647. if (!chip->block_bad)
  2648. chip->block_bad = nand_block_bad;
  2649. if (!chip->block_markbad)
  2650. chip->block_markbad = nand_default_block_markbad;
  2651. if (!chip->write_buf || chip->write_buf == nand_write_buf)
  2652. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2653. if (!chip->write_byte || chip->write_byte == nand_write_byte)
  2654. chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
  2655. if (!chip->read_buf || chip->read_buf == nand_read_buf)
  2656. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2657. if (!chip->scan_bbt)
  2658. chip->scan_bbt = nand_default_bbt;
  2659. if (!chip->controller) {
  2660. chip->controller = &chip->hwcontrol;
  2661. spin_lock_init(&chip->controller->lock);
  2662. init_waitqueue_head(&chip->controller->wq);
  2663. }
  2664. }
  2665. /* Sanitize ONFI strings so we can safely print them */
  2666. #ifndef __UBOOT__
  2667. static void sanitize_string(uint8_t *s, size_t len)
  2668. #else
  2669. static void sanitize_string(char *s, size_t len)
  2670. #endif
  2671. {
  2672. ssize_t i;
  2673. /* Null terminate */
  2674. s[len - 1] = 0;
  2675. /* Remove non printable chars */
  2676. for (i = 0; i < len - 1; i++) {
  2677. if (s[i] < ' ' || s[i] > 127)
  2678. s[i] = '?';
  2679. }
  2680. /* Remove trailing spaces */
  2681. strim(s);
  2682. }
  2683. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2684. {
  2685. int i;
  2686. while (len--) {
  2687. crc ^= *p++ << 8;
  2688. for (i = 0; i < 8; i++)
  2689. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2690. }
  2691. return crc;
  2692. }
  2693. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2694. /* Parse the Extended Parameter Page. */
  2695. static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
  2696. struct nand_chip *chip, struct nand_onfi_params *p)
  2697. {
  2698. struct onfi_ext_param_page *ep;
  2699. struct onfi_ext_section *s;
  2700. struct onfi_ext_ecc_info *ecc;
  2701. uint8_t *cursor;
  2702. int ret = -EINVAL;
  2703. int len;
  2704. int i;
  2705. len = le16_to_cpu(p->ext_param_page_length) * 16;
  2706. ep = kmalloc(len, GFP_KERNEL);
  2707. if (!ep)
  2708. return -ENOMEM;
  2709. /* Send our own NAND_CMD_PARAM. */
  2710. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2711. /* Use the Change Read Column command to skip the ONFI param pages. */
  2712. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  2713. sizeof(*p) * p->num_of_param_pages , -1);
  2714. /* Read out the Extended Parameter Page. */
  2715. chip->read_buf(mtd, (uint8_t *)ep, len);
  2716. if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
  2717. != le16_to_cpu(ep->crc))) {
  2718. pr_debug("fail in the CRC.\n");
  2719. goto ext_out;
  2720. }
  2721. /*
  2722. * Check the signature.
  2723. * Do not strictly follow the ONFI spec, maybe changed in future.
  2724. */
  2725. #ifndef __UBOOT__
  2726. if (strncmp(ep->sig, "EPPS", 4)) {
  2727. #else
  2728. if (strncmp((char *)ep->sig, "EPPS", 4)) {
  2729. #endif
  2730. pr_debug("The signature is invalid.\n");
  2731. goto ext_out;
  2732. }
  2733. /* find the ECC section. */
  2734. cursor = (uint8_t *)(ep + 1);
  2735. for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
  2736. s = ep->sections + i;
  2737. if (s->type == ONFI_SECTION_TYPE_2)
  2738. break;
  2739. cursor += s->length * 16;
  2740. }
  2741. if (i == ONFI_EXT_SECTION_MAX) {
  2742. pr_debug("We can not find the ECC section.\n");
  2743. goto ext_out;
  2744. }
  2745. /* get the info we want. */
  2746. ecc = (struct onfi_ext_ecc_info *)cursor;
  2747. if (!ecc->codeword_size) {
  2748. pr_debug("Invalid codeword size\n");
  2749. goto ext_out;
  2750. }
  2751. chip->ecc_strength_ds = ecc->ecc_bits;
  2752. chip->ecc_step_ds = 1 << ecc->codeword_size;
  2753. ret = 0;
  2754. ext_out:
  2755. kfree(ep);
  2756. return ret;
  2757. }
  2758. static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
  2759. {
  2760. struct nand_chip *chip = mtd->priv;
  2761. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  2762. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  2763. feature);
  2764. }
  2765. /*
  2766. * Configure chip properties from Micron vendor-specific ONFI table
  2767. */
  2768. static void nand_onfi_detect_micron(struct nand_chip *chip,
  2769. struct nand_onfi_params *p)
  2770. {
  2771. struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
  2772. if (le16_to_cpu(p->vendor_revision) < 1)
  2773. return;
  2774. chip->read_retries = micron->read_retry_options;
  2775. chip->setup_read_retry = nand_setup_read_retry_micron;
  2776. }
  2777. /*
  2778. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2779. */
  2780. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2781. int *busw)
  2782. {
  2783. struct nand_onfi_params *p = &chip->onfi_params;
  2784. int i, j;
  2785. int val;
  2786. /* Try ONFI for unknown chip or LP */
  2787. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2788. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2789. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2790. return 0;
  2791. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2792. for (i = 0; i < 3; i++) {
  2793. for (j = 0; j < sizeof(*p); j++)
  2794. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  2795. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2796. le16_to_cpu(p->crc)) {
  2797. break;
  2798. }
  2799. }
  2800. if (i == 3) {
  2801. pr_err("Could not find valid ONFI parameter page; aborting\n");
  2802. return 0;
  2803. }
  2804. /* Check version */
  2805. val = le16_to_cpu(p->revision);
  2806. if (val & (1 << 5))
  2807. chip->onfi_version = 23;
  2808. else if (val & (1 << 4))
  2809. chip->onfi_version = 22;
  2810. else if (val & (1 << 3))
  2811. chip->onfi_version = 21;
  2812. else if (val & (1 << 2))
  2813. chip->onfi_version = 20;
  2814. else if (val & (1 << 1))
  2815. chip->onfi_version = 10;
  2816. if (!chip->onfi_version) {
  2817. pr_info("unsupported ONFI version: %d\n", val);
  2818. return 0;
  2819. }
  2820. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2821. sanitize_string(p->model, sizeof(p->model));
  2822. if (!mtd->name)
  2823. mtd->name = p->model;
  2824. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2825. /*
  2826. * pages_per_block and blocks_per_lun may not be a power-of-2 size
  2827. * (don't ask me who thought of this...). MTD assumes that these
  2828. * dimensions will be power-of-2, so just truncate the remaining area.
  2829. */
  2830. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2831. mtd->erasesize *= mtd->writesize;
  2832. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2833. /* See erasesize comment */
  2834. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2835. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2836. chip->bits_per_cell = p->bits_per_cell;
  2837. if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
  2838. *busw = NAND_BUSWIDTH_16;
  2839. else
  2840. *busw = 0;
  2841. if (p->ecc_bits != 0xff) {
  2842. chip->ecc_strength_ds = p->ecc_bits;
  2843. chip->ecc_step_ds = 512;
  2844. } else if (chip->onfi_version >= 21 &&
  2845. (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
  2846. /*
  2847. * The nand_flash_detect_ext_param_page() uses the
  2848. * Change Read Column command which maybe not supported
  2849. * by the chip->cmdfunc. So try to update the chip->cmdfunc
  2850. * now. We do not replace user supplied command function.
  2851. */
  2852. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2853. chip->cmdfunc = nand_command_lp;
  2854. /* The Extended Parameter Page is supported since ONFI 2.1. */
  2855. if (nand_flash_detect_ext_param_page(mtd, chip, p))
  2856. pr_warn("Failed to detect ONFI extended param page\n");
  2857. } else {
  2858. pr_warn("Could not retrieve ONFI ECC requirements\n");
  2859. }
  2860. if (p->jedec_id == NAND_MFR_MICRON)
  2861. nand_onfi_detect_micron(chip, p);
  2862. return 1;
  2863. }
  2864. #else
  2865. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2866. int *busw)
  2867. {
  2868. return 0;
  2869. }
  2870. #endif
  2871. /*
  2872. * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
  2873. */
  2874. static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
  2875. int *busw)
  2876. {
  2877. struct nand_jedec_params *p = &chip->jedec_params;
  2878. struct jedec_ecc_info *ecc;
  2879. int val;
  2880. int i, j;
  2881. /* Try JEDEC for unknown chip or LP */
  2882. chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
  2883. if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
  2884. chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
  2885. chip->read_byte(mtd) != 'C')
  2886. return 0;
  2887. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
  2888. for (i = 0; i < 3; i++) {
  2889. for (j = 0; j < sizeof(*p); j++)
  2890. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  2891. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
  2892. le16_to_cpu(p->crc))
  2893. break;
  2894. }
  2895. if (i == 3) {
  2896. pr_err("Could not find valid JEDEC parameter page; aborting\n");
  2897. return 0;
  2898. }
  2899. /* Check version */
  2900. val = le16_to_cpu(p->revision);
  2901. if (val & (1 << 2))
  2902. chip->jedec_version = 10;
  2903. else if (val & (1 << 1))
  2904. chip->jedec_version = 1; /* vendor specific version */
  2905. if (!chip->jedec_version) {
  2906. pr_info("unsupported JEDEC version: %d\n", val);
  2907. return 0;
  2908. }
  2909. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2910. sanitize_string(p->model, sizeof(p->model));
  2911. if (!mtd->name)
  2912. mtd->name = p->model;
  2913. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2914. /* Please reference to the comment for nand_flash_detect_onfi. */
  2915. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2916. mtd->erasesize *= mtd->writesize;
  2917. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2918. /* Please reference to the comment for nand_flash_detect_onfi. */
  2919. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2920. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2921. chip->bits_per_cell = p->bits_per_cell;
  2922. if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
  2923. *busw = NAND_BUSWIDTH_16;
  2924. else
  2925. *busw = 0;
  2926. /* ECC info */
  2927. ecc = &p->ecc_info[0];
  2928. if (ecc->codeword_size >= 9) {
  2929. chip->ecc_strength_ds = ecc->ecc_bits;
  2930. chip->ecc_step_ds = 1 << ecc->codeword_size;
  2931. } else {
  2932. pr_warn("Invalid codeword size\n");
  2933. }
  2934. return 1;
  2935. }
  2936. /*
  2937. * nand_id_has_period - Check if an ID string has a given wraparound period
  2938. * @id_data: the ID string
  2939. * @arrlen: the length of the @id_data array
  2940. * @period: the period of repitition
  2941. *
  2942. * Check if an ID string is repeated within a given sequence of bytes at
  2943. * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
  2944. * period of 3). This is a helper function for nand_id_len(). Returns non-zero
  2945. * if the repetition has a period of @period; otherwise, returns zero.
  2946. */
  2947. static int nand_id_has_period(u8 *id_data, int arrlen, int period)
  2948. {
  2949. int i, j;
  2950. for (i = 0; i < period; i++)
  2951. for (j = i + period; j < arrlen; j += period)
  2952. if (id_data[i] != id_data[j])
  2953. return 0;
  2954. return 1;
  2955. }
  2956. /*
  2957. * nand_id_len - Get the length of an ID string returned by CMD_READID
  2958. * @id_data: the ID string
  2959. * @arrlen: the length of the @id_data array
  2960. * Returns the length of the ID string, according to known wraparound/trailing
  2961. * zero patterns. If no pattern exists, returns the length of the array.
  2962. */
  2963. static int nand_id_len(u8 *id_data, int arrlen)
  2964. {
  2965. int last_nonzero, period;
  2966. /* Find last non-zero byte */
  2967. for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
  2968. if (id_data[last_nonzero])
  2969. break;
  2970. /* All zeros */
  2971. if (last_nonzero < 0)
  2972. return 0;
  2973. /* Calculate wraparound period */
  2974. for (period = 1; period < arrlen; period++)
  2975. if (nand_id_has_period(id_data, arrlen, period))
  2976. break;
  2977. /* There's a repeated pattern */
  2978. if (period < arrlen)
  2979. return period;
  2980. /* There are trailing zeros */
  2981. if (last_nonzero < arrlen - 1)
  2982. return last_nonzero + 1;
  2983. /* No pattern detected */
  2984. return arrlen;
  2985. }
  2986. /* Extract the bits of per cell from the 3rd byte of the extended ID */
  2987. static int nand_get_bits_per_cell(u8 cellinfo)
  2988. {
  2989. int bits;
  2990. bits = cellinfo & NAND_CI_CELLTYPE_MSK;
  2991. bits >>= NAND_CI_CELLTYPE_SHIFT;
  2992. return bits + 1;
  2993. }
  2994. /*
  2995. * Many new NAND share similar device ID codes, which represent the size of the
  2996. * chip. The rest of the parameters must be decoded according to generic or
  2997. * manufacturer-specific "extended ID" decoding patterns.
  2998. */
  2999. static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
  3000. u8 id_data[8], int *busw)
  3001. {
  3002. int extid, id_len;
  3003. /* The 3rd id byte holds MLC / multichip data */
  3004. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  3005. /* The 4th id byte is the important one */
  3006. extid = id_data[3];
  3007. id_len = nand_id_len(id_data, 8);
  3008. /*
  3009. * Field definitions are in the following datasheets:
  3010. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  3011. * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
  3012. * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
  3013. *
  3014. * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
  3015. * ID to decide what to do.
  3016. */
  3017. if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
  3018. !nand_is_slc(chip) && id_data[5] != 0x00) {
  3019. /* Calc pagesize */
  3020. mtd->writesize = 2048 << (extid & 0x03);
  3021. extid >>= 2;
  3022. /* Calc oobsize */
  3023. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  3024. case 1:
  3025. mtd->oobsize = 128;
  3026. break;
  3027. case 2:
  3028. mtd->oobsize = 218;
  3029. break;
  3030. case 3:
  3031. mtd->oobsize = 400;
  3032. break;
  3033. case 4:
  3034. mtd->oobsize = 436;
  3035. break;
  3036. case 5:
  3037. mtd->oobsize = 512;
  3038. break;
  3039. case 6:
  3040. mtd->oobsize = 640;
  3041. break;
  3042. case 7:
  3043. default: /* Other cases are "reserved" (unknown) */
  3044. mtd->oobsize = 1024;
  3045. break;
  3046. }
  3047. extid >>= 2;
  3048. /* Calc blocksize */
  3049. mtd->erasesize = (128 * 1024) <<
  3050. (((extid >> 1) & 0x04) | (extid & 0x03));
  3051. *busw = 0;
  3052. } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
  3053. !nand_is_slc(chip)) {
  3054. unsigned int tmp;
  3055. /* Calc pagesize */
  3056. mtd->writesize = 2048 << (extid & 0x03);
  3057. extid >>= 2;
  3058. /* Calc oobsize */
  3059. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  3060. case 0:
  3061. mtd->oobsize = 128;
  3062. break;
  3063. case 1:
  3064. mtd->oobsize = 224;
  3065. break;
  3066. case 2:
  3067. mtd->oobsize = 448;
  3068. break;
  3069. case 3:
  3070. mtd->oobsize = 64;
  3071. break;
  3072. case 4:
  3073. mtd->oobsize = 32;
  3074. break;
  3075. case 5:
  3076. mtd->oobsize = 16;
  3077. break;
  3078. default:
  3079. mtd->oobsize = 640;
  3080. break;
  3081. }
  3082. extid >>= 2;
  3083. /* Calc blocksize */
  3084. tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
  3085. if (tmp < 0x03)
  3086. mtd->erasesize = (128 * 1024) << tmp;
  3087. else if (tmp == 0x03)
  3088. mtd->erasesize = 768 * 1024;
  3089. else
  3090. mtd->erasesize = (64 * 1024) << tmp;
  3091. *busw = 0;
  3092. } else {
  3093. /* Calc pagesize */
  3094. mtd->writesize = 1024 << (extid & 0x03);
  3095. extid >>= 2;
  3096. /* Calc oobsize */
  3097. mtd->oobsize = (8 << (extid & 0x01)) *
  3098. (mtd->writesize >> 9);
  3099. extid >>= 2;
  3100. /* Calc blocksize. Blocksize is multiples of 64KiB */
  3101. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  3102. extid >>= 2;
  3103. /* Get buswidth information */
  3104. *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  3105. /*
  3106. * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
  3107. * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
  3108. * follows:
  3109. * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
  3110. * 110b -> 24nm
  3111. * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
  3112. */
  3113. if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
  3114. nand_is_slc(chip) &&
  3115. (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
  3116. !(id_data[4] & 0x80) /* !BENAND */) {
  3117. mtd->oobsize = 32 * mtd->writesize >> 9;
  3118. }
  3119. }
  3120. }
  3121. /*
  3122. * Old devices have chip data hardcoded in the device ID table. nand_decode_id
  3123. * decodes a matching ID table entry and assigns the MTD size parameters for
  3124. * the chip.
  3125. */
  3126. static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
  3127. struct nand_flash_dev *type, u8 id_data[8],
  3128. int *busw)
  3129. {
  3130. int maf_id = id_data[0];
  3131. mtd->erasesize = type->erasesize;
  3132. mtd->writesize = type->pagesize;
  3133. mtd->oobsize = mtd->writesize / 32;
  3134. *busw = type->options & NAND_BUSWIDTH_16;
  3135. /* All legacy ID NAND are small-page, SLC */
  3136. chip->bits_per_cell = 1;
  3137. /*
  3138. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  3139. * some Spansion chips have erasesize that conflicts with size
  3140. * listed in nand_ids table.
  3141. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  3142. */
  3143. if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
  3144. && id_data[6] == 0x00 && id_data[7] == 0x00
  3145. && mtd->writesize == 512) {
  3146. mtd->erasesize = 128 * 1024;
  3147. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  3148. }
  3149. }
  3150. /*
  3151. * Set the bad block marker/indicator (BBM/BBI) patterns according to some
  3152. * heuristic patterns using various detected parameters (e.g., manufacturer,
  3153. * page size, cell-type information).
  3154. */
  3155. static void nand_decode_bbm_options(struct mtd_info *mtd,
  3156. struct nand_chip *chip, u8 id_data[8])
  3157. {
  3158. int maf_id = id_data[0];
  3159. /* Set the bad block position */
  3160. if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
  3161. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  3162. else
  3163. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  3164. /*
  3165. * Bad block marker is stored in the last page of each block on Samsung
  3166. * and Hynix MLC devices; stored in first two pages of each block on
  3167. * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
  3168. * AMD/Spansion, and Macronix. All others scan only the first page.
  3169. */
  3170. if (!nand_is_slc(chip) &&
  3171. (maf_id == NAND_MFR_SAMSUNG ||
  3172. maf_id == NAND_MFR_HYNIX))
  3173. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  3174. else if ((nand_is_slc(chip) &&
  3175. (maf_id == NAND_MFR_SAMSUNG ||
  3176. maf_id == NAND_MFR_HYNIX ||
  3177. maf_id == NAND_MFR_TOSHIBA ||
  3178. maf_id == NAND_MFR_AMD ||
  3179. maf_id == NAND_MFR_MACRONIX)) ||
  3180. (mtd->writesize == 2048 &&
  3181. maf_id == NAND_MFR_MICRON))
  3182. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  3183. }
  3184. static inline bool is_full_id_nand(struct nand_flash_dev *type)
  3185. {
  3186. return type->id_len;
  3187. }
  3188. static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
  3189. struct nand_flash_dev *type, u8 *id_data, int *busw)
  3190. {
  3191. #ifndef __UBOOT__
  3192. if (!strncmp(type->id, id_data, type->id_len)) {
  3193. #else
  3194. if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
  3195. #endif
  3196. mtd->writesize = type->pagesize;
  3197. mtd->erasesize = type->erasesize;
  3198. mtd->oobsize = type->oobsize;
  3199. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  3200. chip->chipsize = (uint64_t)type->chipsize << 20;
  3201. chip->options |= type->options;
  3202. chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
  3203. chip->ecc_step_ds = NAND_ECC_STEP(type);
  3204. *busw = type->options & NAND_BUSWIDTH_16;
  3205. if (!mtd->name)
  3206. mtd->name = type->name;
  3207. return true;
  3208. }
  3209. return false;
  3210. }
  3211. /*
  3212. * Get the flash and manufacturer id and lookup if the type is supported.
  3213. */
  3214. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  3215. struct nand_chip *chip,
  3216. int *maf_id, int *dev_id,
  3217. struct nand_flash_dev *type)
  3218. {
  3219. int busw;
  3220. int i, maf_idx;
  3221. u8 id_data[8];
  3222. /* Select the device */
  3223. chip->select_chip(mtd, 0);
  3224. /*
  3225. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  3226. * after power-up.
  3227. */
  3228. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3229. /* Send the command for reading device ID */
  3230. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3231. /* Read manufacturer and device IDs */
  3232. *maf_id = chip->read_byte(mtd);
  3233. *dev_id = chip->read_byte(mtd);
  3234. /*
  3235. * Try again to make sure, as some systems the bus-hold or other
  3236. * interface concerns can cause random data which looks like a
  3237. * possibly credible NAND flash to appear. If the two results do
  3238. * not match, ignore the device completely.
  3239. */
  3240. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3241. /* Read entire ID string */
  3242. for (i = 0; i < 8; i++)
  3243. id_data[i] = chip->read_byte(mtd);
  3244. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  3245. pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
  3246. *maf_id, *dev_id, id_data[0], id_data[1]);
  3247. return ERR_PTR(-ENODEV);
  3248. }
  3249. if (!type)
  3250. type = nand_flash_ids;
  3251. for (; type->name != NULL; type++) {
  3252. if (is_full_id_nand(type)) {
  3253. if (find_full_id_nand(mtd, chip, type, id_data, &busw))
  3254. goto ident_done;
  3255. } else if (*dev_id == type->dev_id) {
  3256. break;
  3257. }
  3258. }
  3259. chip->onfi_version = 0;
  3260. if (!type->name || !type->pagesize) {
  3261. /* Check is chip is ONFI compliant */
  3262. if (nand_flash_detect_onfi(mtd, chip, &busw))
  3263. goto ident_done;
  3264. /* Check if the chip is JEDEC compliant */
  3265. if (nand_flash_detect_jedec(mtd, chip, &busw))
  3266. goto ident_done;
  3267. }
  3268. if (!type->name)
  3269. return ERR_PTR(-ENODEV);
  3270. if (!mtd->name)
  3271. mtd->name = type->name;
  3272. chip->chipsize = (uint64_t)type->chipsize << 20;
  3273. if (!type->pagesize && chip->init_size) {
  3274. /* Set the pagesize, oobsize, erasesize by the driver */
  3275. busw = chip->init_size(mtd, chip, id_data);
  3276. } else if (!type->pagesize) {
  3277. /* Decode parameters from extended ID */
  3278. nand_decode_ext_id(mtd, chip, id_data, &busw);
  3279. } else {
  3280. nand_decode_id(mtd, chip, type, id_data, &busw);
  3281. }
  3282. /* Get chip options */
  3283. chip->options |= type->options;
  3284. /*
  3285. * Check if chip is not a Samsung device. Do not clear the
  3286. * options for chips which do not have an extended id.
  3287. */
  3288. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  3289. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  3290. ident_done:
  3291. /* Try to identify manufacturer */
  3292. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  3293. if (nand_manuf_ids[maf_idx].id == *maf_id)
  3294. break;
  3295. }
  3296. if (chip->options & NAND_BUSWIDTH_AUTO) {
  3297. WARN_ON(chip->options & NAND_BUSWIDTH_16);
  3298. chip->options |= busw;
  3299. nand_set_defaults(chip, busw);
  3300. } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  3301. /*
  3302. * Check, if buswidth is correct. Hardware drivers should set
  3303. * chip correct!
  3304. */
  3305. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3306. *maf_id, *dev_id);
  3307. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
  3308. pr_warn("bus width %d instead %d bit\n",
  3309. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  3310. busw ? 16 : 8);
  3311. return ERR_PTR(-EINVAL);
  3312. }
  3313. nand_decode_bbm_options(mtd, chip, id_data);
  3314. /* Calculate the address shift from the page size */
  3315. chip->page_shift = ffs(mtd->writesize) - 1;
  3316. /* Convert chipsize to number of pages per chip -1 */
  3317. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  3318. chip->bbt_erase_shift = chip->phys_erase_shift =
  3319. ffs(mtd->erasesize) - 1;
  3320. if (chip->chipsize & 0xffffffff)
  3321. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  3322. else {
  3323. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  3324. chip->chip_shift += 32 - 1;
  3325. }
  3326. chip->badblockbits = 8;
  3327. chip->erase_cmd = single_erase_cmd;
  3328. /* Do not replace user supplied command function! */
  3329. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  3330. chip->cmdfunc = nand_command_lp;
  3331. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3332. *maf_id, *dev_id);
  3333. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  3334. if (chip->onfi_version)
  3335. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3336. chip->onfi_params.model);
  3337. else if (chip->jedec_version)
  3338. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3339. chip->jedec_params.model);
  3340. else
  3341. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3342. type->name);
  3343. #else
  3344. if (chip->jedec_version)
  3345. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3346. chip->jedec_params.model);
  3347. else
  3348. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3349. type->name);
  3350. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3351. type->name);
  3352. #endif
  3353. pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
  3354. (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
  3355. mtd->writesize, mtd->oobsize);
  3356. return type;
  3357. }
  3358. /**
  3359. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  3360. * @mtd: MTD device structure
  3361. * @maxchips: number of chips to scan for
  3362. * @table: alternative NAND ID table
  3363. *
  3364. * This is the first phase of the normal nand_scan() function. It reads the
  3365. * flash ID and sets up MTD fields accordingly.
  3366. *
  3367. * The mtd->owner field must be set to the module of the caller.
  3368. */
  3369. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  3370. struct nand_flash_dev *table)
  3371. {
  3372. int i, nand_maf_id, nand_dev_id;
  3373. struct nand_chip *chip = mtd->priv;
  3374. struct nand_flash_dev *type;
  3375. /* Set the default functions */
  3376. nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
  3377. /* Read the flash type */
  3378. type = nand_get_flash_type(mtd, chip, &nand_maf_id,
  3379. &nand_dev_id, table);
  3380. if (IS_ERR(type)) {
  3381. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  3382. pr_warn("No NAND device found\n");
  3383. chip->select_chip(mtd, -1);
  3384. return PTR_ERR(type);
  3385. }
  3386. chip->select_chip(mtd, -1);
  3387. /* Check for a chip array */
  3388. for (i = 1; i < maxchips; i++) {
  3389. chip->select_chip(mtd, i);
  3390. /* See comment in nand_get_flash_type for reset */
  3391. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3392. /* Send the command for reading device ID */
  3393. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3394. /* Read manufacturer and device IDs */
  3395. if (nand_maf_id != chip->read_byte(mtd) ||
  3396. nand_dev_id != chip->read_byte(mtd)) {
  3397. chip->select_chip(mtd, -1);
  3398. break;
  3399. }
  3400. chip->select_chip(mtd, -1);
  3401. }
  3402. #ifdef DEBUG
  3403. if (i > 1)
  3404. pr_info("%d chips detected\n", i);
  3405. #endif
  3406. /* Store the number of chips and calc total size for mtd */
  3407. chip->numchips = i;
  3408. mtd->size = i * chip->chipsize;
  3409. return 0;
  3410. }
  3411. EXPORT_SYMBOL(nand_scan_ident);
  3412. /**
  3413. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  3414. * @mtd: MTD device structure
  3415. *
  3416. * This is the second phase of the normal nand_scan() function. It fills out
  3417. * all the uninitialized function pointers with the defaults and scans for a
  3418. * bad block table if appropriate.
  3419. */
  3420. int nand_scan_tail(struct mtd_info *mtd)
  3421. {
  3422. int i;
  3423. struct nand_chip *chip = mtd->priv;
  3424. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3425. struct nand_buffers *nbuf;
  3426. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  3427. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  3428. !(chip->bbt_options & NAND_BBT_USE_FLASH));
  3429. if (!(chip->options & NAND_OWN_BUFFERS)) {
  3430. #ifndef __UBOOT__
  3431. nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
  3432. + mtd->oobsize * 3, GFP_KERNEL);
  3433. if (!nbuf)
  3434. return -ENOMEM;
  3435. nbuf->ecccalc = (uint8_t *)(nbuf + 1);
  3436. nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
  3437. nbuf->databuf = nbuf->ecccode + mtd->oobsize;
  3438. #else
  3439. nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
  3440. #endif
  3441. chip->buffers = nbuf;
  3442. } else {
  3443. if (!chip->buffers)
  3444. return -ENOMEM;
  3445. }
  3446. /* Set the internal oob buffer location, just after the page data */
  3447. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  3448. /*
  3449. * If no default placement scheme is given, select an appropriate one.
  3450. */
  3451. if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  3452. switch (mtd->oobsize) {
  3453. case 8:
  3454. ecc->layout = &nand_oob_8;
  3455. break;
  3456. case 16:
  3457. ecc->layout = &nand_oob_16;
  3458. break;
  3459. case 64:
  3460. ecc->layout = &nand_oob_64;
  3461. break;
  3462. case 128:
  3463. ecc->layout = &nand_oob_128;
  3464. break;
  3465. default:
  3466. pr_warn("No oob scheme defined for oobsize %d\n",
  3467. mtd->oobsize);
  3468. BUG();
  3469. }
  3470. }
  3471. if (!chip->write_page)
  3472. chip->write_page = nand_write_page;
  3473. /*
  3474. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  3475. * selected and we have 256 byte pagesize fallback to software ECC
  3476. */
  3477. switch (ecc->mode) {
  3478. case NAND_ECC_HW_OOB_FIRST:
  3479. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  3480. if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
  3481. pr_warn("No ECC functions supplied; "
  3482. "hardware ECC not possible\n");
  3483. BUG();
  3484. }
  3485. if (!ecc->read_page)
  3486. ecc->read_page = nand_read_page_hwecc_oob_first;
  3487. case NAND_ECC_HW:
  3488. /* Use standard hwecc read page function? */
  3489. if (!ecc->read_page)
  3490. ecc->read_page = nand_read_page_hwecc;
  3491. if (!ecc->write_page)
  3492. ecc->write_page = nand_write_page_hwecc;
  3493. if (!ecc->read_page_raw)
  3494. ecc->read_page_raw = nand_read_page_raw;
  3495. if (!ecc->write_page_raw)
  3496. ecc->write_page_raw = nand_write_page_raw;
  3497. if (!ecc->read_oob)
  3498. ecc->read_oob = nand_read_oob_std;
  3499. if (!ecc->write_oob)
  3500. ecc->write_oob = nand_write_oob_std;
  3501. if (!ecc->read_subpage)
  3502. ecc->read_subpage = nand_read_subpage;
  3503. if (!ecc->write_subpage)
  3504. ecc->write_subpage = nand_write_subpage_hwecc;
  3505. case NAND_ECC_HW_SYNDROME:
  3506. if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
  3507. (!ecc->read_page ||
  3508. ecc->read_page == nand_read_page_hwecc ||
  3509. !ecc->write_page ||
  3510. ecc->write_page == nand_write_page_hwecc)) {
  3511. pr_warn("No ECC functions supplied; "
  3512. "hardware ECC not possible\n");
  3513. BUG();
  3514. }
  3515. /* Use standard syndrome read/write page function? */
  3516. if (!ecc->read_page)
  3517. ecc->read_page = nand_read_page_syndrome;
  3518. if (!ecc->write_page)
  3519. ecc->write_page = nand_write_page_syndrome;
  3520. if (!ecc->read_page_raw)
  3521. ecc->read_page_raw = nand_read_page_raw_syndrome;
  3522. if (!ecc->write_page_raw)
  3523. ecc->write_page_raw = nand_write_page_raw_syndrome;
  3524. if (!ecc->read_oob)
  3525. ecc->read_oob = nand_read_oob_syndrome;
  3526. if (!ecc->write_oob)
  3527. ecc->write_oob = nand_write_oob_syndrome;
  3528. if (mtd->writesize >= ecc->size) {
  3529. if (!ecc->strength) {
  3530. pr_warn("Driver must set ecc.strength when using hardware ECC\n");
  3531. BUG();
  3532. }
  3533. break;
  3534. }
  3535. pr_warn("%d byte HW ECC not possible on "
  3536. "%d byte page size, fallback to SW ECC\n",
  3537. ecc->size, mtd->writesize);
  3538. ecc->mode = NAND_ECC_SOFT;
  3539. case NAND_ECC_SOFT:
  3540. ecc->calculate = nand_calculate_ecc;
  3541. ecc->correct = nand_correct_data;
  3542. ecc->read_page = nand_read_page_swecc;
  3543. ecc->read_subpage = nand_read_subpage;
  3544. ecc->write_page = nand_write_page_swecc;
  3545. ecc->read_page_raw = nand_read_page_raw;
  3546. ecc->write_page_raw = nand_write_page_raw;
  3547. ecc->read_oob = nand_read_oob_std;
  3548. ecc->write_oob = nand_write_oob_std;
  3549. if (!ecc->size)
  3550. ecc->size = 256;
  3551. ecc->bytes = 3;
  3552. ecc->strength = 1;
  3553. break;
  3554. case NAND_ECC_SOFT_BCH:
  3555. if (!mtd_nand_has_bch()) {
  3556. pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  3557. BUG();
  3558. }
  3559. ecc->calculate = nand_bch_calculate_ecc;
  3560. ecc->correct = nand_bch_correct_data;
  3561. ecc->read_page = nand_read_page_swecc;
  3562. ecc->read_subpage = nand_read_subpage;
  3563. ecc->write_page = nand_write_page_swecc;
  3564. ecc->read_page_raw = nand_read_page_raw;
  3565. ecc->write_page_raw = nand_write_page_raw;
  3566. ecc->read_oob = nand_read_oob_std;
  3567. ecc->write_oob = nand_write_oob_std;
  3568. /*
  3569. * Board driver should supply ecc.size and ecc.bytes values to
  3570. * select how many bits are correctable; see nand_bch_init()
  3571. * for details. Otherwise, default to 4 bits for large page
  3572. * devices.
  3573. */
  3574. if (!ecc->size && (mtd->oobsize >= 64)) {
  3575. ecc->size = 512;
  3576. ecc->bytes = 7;
  3577. }
  3578. ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
  3579. &ecc->layout);
  3580. if (!ecc->priv) {
  3581. pr_warn("BCH ECC initialization failed!\n");
  3582. BUG();
  3583. }
  3584. ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
  3585. break;
  3586. case NAND_ECC_NONE:
  3587. pr_warn("NAND_ECC_NONE selected by board driver. "
  3588. "This is not recommended!\n");
  3589. ecc->read_page = nand_read_page_raw;
  3590. ecc->write_page = nand_write_page_raw;
  3591. ecc->read_oob = nand_read_oob_std;
  3592. ecc->read_page_raw = nand_read_page_raw;
  3593. ecc->write_page_raw = nand_write_page_raw;
  3594. ecc->write_oob = nand_write_oob_std;
  3595. ecc->size = mtd->writesize;
  3596. ecc->bytes = 0;
  3597. ecc->strength = 0;
  3598. break;
  3599. default:
  3600. pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
  3601. BUG();
  3602. }
  3603. /* For many systems, the standard OOB write also works for raw */
  3604. if (!ecc->read_oob_raw)
  3605. ecc->read_oob_raw = ecc->read_oob;
  3606. if (!ecc->write_oob_raw)
  3607. ecc->write_oob_raw = ecc->write_oob;
  3608. /*
  3609. * The number of bytes available for a client to place data into
  3610. * the out of band area.
  3611. */
  3612. ecc->layout->oobavail = 0;
  3613. for (i = 0; ecc->layout->oobfree[i].length
  3614. && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
  3615. ecc->layout->oobavail += ecc->layout->oobfree[i].length;
  3616. mtd->oobavail = ecc->layout->oobavail;
  3617. /*
  3618. * Set the number of read / write steps for one page depending on ECC
  3619. * mode.
  3620. */
  3621. ecc->steps = mtd->writesize / ecc->size;
  3622. if (ecc->steps * ecc->size != mtd->writesize) {
  3623. pr_warn("Invalid ECC parameters\n");
  3624. BUG();
  3625. }
  3626. ecc->total = ecc->steps * ecc->bytes;
  3627. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  3628. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
  3629. switch (ecc->steps) {
  3630. case 2:
  3631. mtd->subpage_sft = 1;
  3632. break;
  3633. case 4:
  3634. case 8:
  3635. case 16:
  3636. mtd->subpage_sft = 2;
  3637. break;
  3638. }
  3639. }
  3640. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3641. /* Initialize state */
  3642. chip->state = FL_READY;
  3643. /* Invalidate the pagebuffer reference */
  3644. chip->pagebuf = -1;
  3645. /* Large page NAND with SOFT_ECC should support subpage reads */
  3646. if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
  3647. chip->options |= NAND_SUBPAGE_READ;
  3648. /* Fill in remaining MTD driver data */
  3649. mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
  3650. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  3651. MTD_CAP_NANDFLASH;
  3652. mtd->_erase = nand_erase;
  3653. #ifndef __UBOOT__
  3654. mtd->_point = NULL;
  3655. mtd->_unpoint = NULL;
  3656. #endif
  3657. mtd->_read = nand_read;
  3658. mtd->_write = nand_write;
  3659. mtd->_panic_write = panic_nand_write;
  3660. mtd->_read_oob = nand_read_oob;
  3661. mtd->_write_oob = nand_write_oob;
  3662. mtd->_sync = nand_sync;
  3663. mtd->_lock = NULL;
  3664. mtd->_unlock = NULL;
  3665. #ifndef __UBOOT__
  3666. mtd->_suspend = nand_suspend;
  3667. mtd->_resume = nand_resume;
  3668. #endif
  3669. mtd->_block_isbad = nand_block_isbad;
  3670. mtd->_block_markbad = nand_block_markbad;
  3671. mtd->writebufsize = mtd->writesize;
  3672. /* propagate ecc info to mtd_info */
  3673. mtd->ecclayout = ecc->layout;
  3674. mtd->ecc_strength = ecc->strength;
  3675. mtd->ecc_step_size = ecc->size;
  3676. /*
  3677. * Initialize bitflip_threshold to its default prior scan_bbt() call.
  3678. * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
  3679. * properly set.
  3680. */
  3681. if (!mtd->bitflip_threshold)
  3682. mtd->bitflip_threshold = mtd->ecc_strength;
  3683. return 0;
  3684. }
  3685. EXPORT_SYMBOL(nand_scan_tail);
  3686. /*
  3687. * is_module_text_address() isn't exported, and it's mostly a pointless
  3688. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  3689. * to call us from in-kernel code if the core NAND support is modular.
  3690. */
  3691. #ifdef MODULE
  3692. #define caller_is_module() (1)
  3693. #else
  3694. #define caller_is_module() \
  3695. is_module_text_address((unsigned long)__builtin_return_address(0))
  3696. #endif
  3697. /**
  3698. * nand_scan - [NAND Interface] Scan for the NAND device
  3699. * @mtd: MTD device structure
  3700. * @maxchips: number of chips to scan for
  3701. *
  3702. * This fills out all the uninitialized function pointers with the defaults.
  3703. * The flash ID is read and the mtd/chip structures are filled with the
  3704. * appropriate values. The mtd->owner field must be set to the module of the
  3705. * caller.
  3706. */
  3707. int nand_scan(struct mtd_info *mtd, int maxchips)
  3708. {
  3709. int ret;
  3710. /* Many callers got this wrong, so check for it for a while... */
  3711. if (!mtd->owner && caller_is_module()) {
  3712. pr_crit("%s called with NULL mtd->owner!\n", __func__);
  3713. BUG();
  3714. }
  3715. ret = nand_scan_ident(mtd, maxchips, NULL);
  3716. if (!ret)
  3717. ret = nand_scan_tail(mtd);
  3718. return ret;
  3719. }
  3720. EXPORT_SYMBOL(nand_scan);
  3721. #ifndef __UBOOT__
  3722. /**
  3723. * nand_release - [NAND Interface] Free resources held by the NAND device
  3724. * @mtd: MTD device structure
  3725. */
  3726. void nand_release(struct mtd_info *mtd)
  3727. {
  3728. struct nand_chip *chip = mtd->priv;
  3729. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  3730. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  3731. mtd_device_unregister(mtd);
  3732. /* Free bad block table memory */
  3733. kfree(chip->bbt);
  3734. if (!(chip->options & NAND_OWN_BUFFERS))
  3735. kfree(chip->buffers);
  3736. /* Free bad block descriptor memory */
  3737. if (chip->badblock_pattern && chip->badblock_pattern->options
  3738. & NAND_BBT_DYNAMICSTRUCT)
  3739. kfree(chip->badblock_pattern);
  3740. }
  3741. EXPORT_SYMBOL_GPL(nand_release);
  3742. static int __init nand_base_init(void)
  3743. {
  3744. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  3745. return 0;
  3746. }
  3747. static void __exit nand_base_exit(void)
  3748. {
  3749. led_trigger_unregister_simple(nand_led_trigger);
  3750. }
  3751. #endif
  3752. module_init(nand_base_init);
  3753. module_exit(nand_base_exit);
  3754. MODULE_LICENSE("GPL");
  3755. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3756. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3757. MODULE_DESCRIPTION("Generic NAND flash driver code");