nand_base.c 120 KB

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