nand_base.c 111 KB

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