nand_base.c 118 KB

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