nand_base.c 111 KB

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