nand_base.c 97 KB

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