nand_base.c 91 KB

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