nand_base.c 92 KB

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