nand_base.c 112 KB

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