nand_base.c 98 KB

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