nand_base.c 90 KB

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