nand_base.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753
  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/tech/nand.html
  11. *
  12. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  13. * 2002 Thomas Gleixner (tglx@linutronix.de)
  14. *
  15. * 02-08-2004 tglx: support for strange chips, which cannot auto increment
  16. * pages on read / read_oob
  17. *
  18. * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes
  19. * pointed this out, as he marked an auto increment capable chip
  20. * as NOAUTOINCR in the board driver.
  21. * Make reads over block boundaries work too
  22. *
  23. * 04-14-2004 tglx: first working version for 2k page size chips
  24. *
  25. * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
  26. *
  27. * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared
  28. * among multiple independend devices. Suggestions and initial patch
  29. * from Ben Dooks <ben-mtd@fluff.org>
  30. *
  31. * 12-05-2004 dmarlin: add workaround for Renesas AG-AND chips "disturb" issue.
  32. * Basically, any block not rewritten may lose data when surrounding blocks
  33. * are rewritten many times. JFFS2 ensures this doesn't happen for blocks
  34. * it uses, but the Bad Block Table(s) may not be rewritten. To ensure they
  35. * do not lose data, force them to be rewritten when some of the surrounding
  36. * blocks are erased. Rather than tracking a specific nearby block (which
  37. * could itself go bad), use a page address 'mask' to select several blocks
  38. * in the same area, and rewrite the BBT when any of them are erased.
  39. *
  40. * 01-03-2005 dmarlin: added support for the device recovery command sequence for Renesas
  41. * AG-AND chips. If there was a sudden loss of power during an erase operation,
  42. * a "device recovery" operation must be performed when power is restored
  43. * to ensure correct operation.
  44. *
  45. * 01-20-2005 dmarlin: added support for optional hardware specific callback routine to
  46. * perform extra error status checks on erase and write failures. This required
  47. * adding a wrapper function for nand_read_ecc.
  48. *
  49. * 08-20-2005 vwool: suspend/resume added
  50. *
  51. * Credits:
  52. * David Woodhouse for adding multichip support
  53. *
  54. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  55. * rework for 2K page size chips
  56. *
  57. * TODO:
  58. * Enable cached programming for 2k page size chips
  59. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  60. * if we have HW ecc support.
  61. * The AG-AND chips have nice features for speed improvement,
  62. * which are not supported yet. Read / program 4 pages in one go.
  63. *
  64. * $Id: nand_base.c,v 1.150 2005/09/15 13:58:48 vwool Exp $
  65. *
  66. * This program is free software; you can redistribute it and/or modify
  67. * it under the terms of the GNU General Public License version 2 as
  68. * published by the Free Software Foundation.
  69. *
  70. */
  71. #include <linux/module.h>
  72. #include <linux/delay.h>
  73. #include <linux/errno.h>
  74. #include <linux/sched.h>
  75. #include <linux/slab.h>
  76. #include <linux/types.h>
  77. #include <linux/mtd/mtd.h>
  78. #include <linux/mtd/nand.h>
  79. #include <linux/mtd/nand_ecc.h>
  80. #include <linux/mtd/compatmac.h>
  81. #include <linux/interrupt.h>
  82. #include <linux/bitops.h>
  83. #include <linux/leds.h>
  84. #include <asm/io.h>
  85. #ifdef CONFIG_MTD_PARTITIONS
  86. #include <linux/mtd/partitions.h>
  87. #endif
  88. /* Define default oob placement schemes for large and small page devices */
  89. static struct nand_oobinfo nand_oob_8 = {
  90. .useecc = MTD_NANDECC_AUTOPLACE,
  91. .eccbytes = 3,
  92. .eccpos = {0, 1, 2},
  93. .oobfree = {{3, 2}, {6, 2}}
  94. };
  95. static struct nand_oobinfo nand_oob_16 = {
  96. .useecc = MTD_NANDECC_AUTOPLACE,
  97. .eccbytes = 6,
  98. .eccpos = {0, 1, 2, 3, 6, 7},
  99. .oobfree = {{8, 8}}
  100. };
  101. static struct nand_oobinfo nand_oob_64 = {
  102. .useecc = MTD_NANDECC_AUTOPLACE,
  103. .eccbytes = 24,
  104. .eccpos = {
  105. 40, 41, 42, 43, 44, 45, 46, 47,
  106. 48, 49, 50, 51, 52, 53, 54, 55,
  107. 56, 57, 58, 59, 60, 61, 62, 63},
  108. .oobfree = {{2, 38}}
  109. };
  110. /* This is used for padding purposes in nand_write_oob */
  111. static u_char ffchars[] = {
  112. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  113. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  114. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  115. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  116. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  117. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  118. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  119. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  120. };
  121. /*
  122. * NAND low-level MTD interface functions
  123. */
  124. static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
  125. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
  126. static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
  127. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
  128. static int nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  129. size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
  130. static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
  131. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
  132. static int nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  133. size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
  134. static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
  135. static int nand_writev(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
  136. static int nand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
  137. unsigned long count, loff_t to, size_t *retlen, u_char *eccbuf,
  138. struct nand_oobinfo *oobsel);
  139. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr);
  140. static void nand_sync(struct mtd_info *mtd);
  141. /* Some internal functions */
  142. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this, int page, u_char * oob_buf,
  143. struct nand_oobinfo *oobsel, int mode);
  144. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  145. static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
  146. u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
  147. #else
  148. #define nand_verify_pages(...) (0)
  149. #endif
  150. static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state);
  151. /**
  152. * nand_release_device - [GENERIC] release chip
  153. * @mtd: MTD device structure
  154. *
  155. * Deselect, release chip lock and wake up anyone waiting on the device
  156. */
  157. static void nand_release_device(struct mtd_info *mtd)
  158. {
  159. struct nand_chip *this = mtd->priv;
  160. /* De-select the NAND device */
  161. this->select_chip(mtd, -1);
  162. /* Release the controller and the chip */
  163. spin_lock(&this->controller->lock);
  164. this->controller->active = NULL;
  165. this->state = FL_READY;
  166. wake_up(&this->controller->wq);
  167. spin_unlock(&this->controller->lock);
  168. }
  169. /**
  170. * nand_read_byte - [DEFAULT] read one byte from the chip
  171. * @mtd: MTD device structure
  172. *
  173. * Default read function for 8bit buswith
  174. */
  175. static u_char nand_read_byte(struct mtd_info *mtd)
  176. {
  177. struct nand_chip *this = mtd->priv;
  178. return readb(this->IO_ADDR_R);
  179. }
  180. /**
  181. * nand_write_byte - [DEFAULT] write one byte to the chip
  182. * @mtd: MTD device structure
  183. * @byte: pointer to data byte to write
  184. *
  185. * Default write function for 8it buswith
  186. */
  187. static void nand_write_byte(struct mtd_info *mtd, u_char byte)
  188. {
  189. struct nand_chip *this = mtd->priv;
  190. writeb(byte, this->IO_ADDR_W);
  191. }
  192. /**
  193. * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
  194. * @mtd: MTD device structure
  195. *
  196. * Default read function for 16bit buswith with
  197. * endianess conversion
  198. */
  199. static u_char nand_read_byte16(struct mtd_info *mtd)
  200. {
  201. struct nand_chip *this = mtd->priv;
  202. return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
  203. }
  204. /**
  205. * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
  206. * @mtd: MTD device structure
  207. * @byte: pointer to data byte to write
  208. *
  209. * Default write function for 16bit buswith with
  210. * endianess conversion
  211. */
  212. static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
  213. {
  214. struct nand_chip *this = mtd->priv;
  215. writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
  216. }
  217. /**
  218. * nand_read_word - [DEFAULT] read one word from the chip
  219. * @mtd: MTD device structure
  220. *
  221. * Default read function for 16bit buswith without
  222. * endianess conversion
  223. */
  224. static u16 nand_read_word(struct mtd_info *mtd)
  225. {
  226. struct nand_chip *this = mtd->priv;
  227. return readw(this->IO_ADDR_R);
  228. }
  229. /**
  230. * nand_write_word - [DEFAULT] write one word to the chip
  231. * @mtd: MTD device structure
  232. * @word: data word to write
  233. *
  234. * Default write function for 16bit buswith without
  235. * endianess conversion
  236. */
  237. static void nand_write_word(struct mtd_info *mtd, u16 word)
  238. {
  239. struct nand_chip *this = mtd->priv;
  240. writew(word, this->IO_ADDR_W);
  241. }
  242. /**
  243. * nand_select_chip - [DEFAULT] control CE line
  244. * @mtd: MTD device structure
  245. * @chip: chipnumber to select, -1 for deselect
  246. *
  247. * Default select function for 1 chip devices.
  248. */
  249. static void nand_select_chip(struct mtd_info *mtd, int chip)
  250. {
  251. struct nand_chip *this = mtd->priv;
  252. switch (chip) {
  253. case -1:
  254. this->hwcontrol(mtd, NAND_CTL_CLRNCE);
  255. break;
  256. case 0:
  257. this->hwcontrol(mtd, NAND_CTL_SETNCE);
  258. break;
  259. default:
  260. BUG();
  261. }
  262. }
  263. /**
  264. * nand_write_buf - [DEFAULT] write buffer to chip
  265. * @mtd: MTD device structure
  266. * @buf: data buffer
  267. * @len: number of bytes to write
  268. *
  269. * Default write function for 8bit buswith
  270. */
  271. static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  272. {
  273. int i;
  274. struct nand_chip *this = mtd->priv;
  275. for (i = 0; i < len; i++)
  276. writeb(buf[i], this->IO_ADDR_W);
  277. }
  278. /**
  279. * nand_read_buf - [DEFAULT] read chip data into buffer
  280. * @mtd: MTD device structure
  281. * @buf: buffer to store date
  282. * @len: number of bytes to read
  283. *
  284. * Default read function for 8bit buswith
  285. */
  286. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  287. {
  288. int i;
  289. struct nand_chip *this = mtd->priv;
  290. for (i = 0; i < len; i++)
  291. buf[i] = readb(this->IO_ADDR_R);
  292. }
  293. /**
  294. * nand_verify_buf - [DEFAULT] Verify chip data against buffer
  295. * @mtd: MTD device structure
  296. * @buf: buffer containing the data to compare
  297. * @len: number of bytes to compare
  298. *
  299. * Default verify function for 8bit buswith
  300. */
  301. static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  302. {
  303. int i;
  304. struct nand_chip *this = mtd->priv;
  305. for (i = 0; i < len; i++)
  306. if (buf[i] != readb(this->IO_ADDR_R))
  307. return -EFAULT;
  308. return 0;
  309. }
  310. /**
  311. * nand_write_buf16 - [DEFAULT] write buffer to chip
  312. * @mtd: MTD device structure
  313. * @buf: data buffer
  314. * @len: number of bytes to write
  315. *
  316. * Default write function for 16bit buswith
  317. */
  318. static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  319. {
  320. int i;
  321. struct nand_chip *this = mtd->priv;
  322. u16 *p = (u16 *) buf;
  323. len >>= 1;
  324. for (i = 0; i < len; i++)
  325. writew(p[i], this->IO_ADDR_W);
  326. }
  327. /**
  328. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  329. * @mtd: MTD device structure
  330. * @buf: buffer to store date
  331. * @len: number of bytes to read
  332. *
  333. * Default read function for 16bit buswith
  334. */
  335. static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  336. {
  337. int i;
  338. struct nand_chip *this = mtd->priv;
  339. u16 *p = (u16 *) buf;
  340. len >>= 1;
  341. for (i = 0; i < len; i++)
  342. p[i] = readw(this->IO_ADDR_R);
  343. }
  344. /**
  345. * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
  346. * @mtd: MTD device structure
  347. * @buf: buffer containing the data to compare
  348. * @len: number of bytes to compare
  349. *
  350. * Default verify function for 16bit buswith
  351. */
  352. static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  353. {
  354. int i;
  355. struct nand_chip *this = mtd->priv;
  356. u16 *p = (u16 *) buf;
  357. len >>= 1;
  358. for (i = 0; i < len; i++)
  359. if (p[i] != readw(this->IO_ADDR_R))
  360. return -EFAULT;
  361. return 0;
  362. }
  363. /**
  364. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  365. * @mtd: MTD device structure
  366. * @ofs: offset from device start
  367. * @getchip: 0, if the chip is already selected
  368. *
  369. * Check, if the block is bad.
  370. */
  371. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  372. {
  373. int page, chipnr, res = 0;
  374. struct nand_chip *this = mtd->priv;
  375. u16 bad;
  376. if (getchip) {
  377. page = (int)(ofs >> this->page_shift);
  378. chipnr = (int)(ofs >> this->chip_shift);
  379. /* Grab the lock and see if the device is available */
  380. nand_get_device(this, mtd, FL_READING);
  381. /* Select the NAND device */
  382. this->select_chip(mtd, chipnr);
  383. } else
  384. page = (int)ofs;
  385. if (this->options & NAND_BUSWIDTH_16) {
  386. this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
  387. bad = cpu_to_le16(this->read_word(mtd));
  388. if (this->badblockpos & 0x1)
  389. bad >>= 8;
  390. if ((bad & 0xFF) != 0xff)
  391. res = 1;
  392. } else {
  393. this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
  394. if (this->read_byte(mtd) != 0xff)
  395. res = 1;
  396. }
  397. if (getchip) {
  398. /* Deselect and wake up anyone waiting on the device */
  399. nand_release_device(mtd);
  400. }
  401. return res;
  402. }
  403. /**
  404. * nand_default_block_markbad - [DEFAULT] mark a block bad
  405. * @mtd: MTD device structure
  406. * @ofs: offset from device start
  407. *
  408. * This is the default implementation, which can be overridden by
  409. * a hardware specific driver.
  410. */
  411. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  412. {
  413. struct nand_chip *this = mtd->priv;
  414. u_char buf[2] = { 0, 0 };
  415. size_t retlen;
  416. int block;
  417. /* Get block number */
  418. block = ((int)ofs) >> this->bbt_erase_shift;
  419. if (this->bbt)
  420. this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  421. /* Do we have a flash based bad block table ? */
  422. if (this->options & NAND_USE_FLASH_BBT)
  423. return nand_update_bbt(mtd, ofs);
  424. /* We write two bytes, so we dont have to mess with 16 bit access */
  425. ofs += mtd->oobsize + (this->badblockpos & ~0x01);
  426. return nand_write_oob(mtd, ofs, 2, &retlen, buf);
  427. }
  428. /**
  429. * nand_check_wp - [GENERIC] check if the chip is write protected
  430. * @mtd: MTD device structure
  431. * Check, if the device is write protected
  432. *
  433. * The function expects, that the device is already selected
  434. */
  435. static int nand_check_wp(struct mtd_info *mtd)
  436. {
  437. struct nand_chip *this = mtd->priv;
  438. /* Check the WP bit */
  439. this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  440. return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  441. }
  442. /**
  443. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  444. * @mtd: MTD device structure
  445. * @ofs: offset from device start
  446. * @getchip: 0, if the chip is already selected
  447. * @allowbbt: 1, if its allowed to access the bbt area
  448. *
  449. * Check, if the block is bad. Either by reading the bad block table or
  450. * calling of the scan function.
  451. */
  452. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  453. {
  454. struct nand_chip *this = mtd->priv;
  455. if (!this->bbt)
  456. return this->block_bad(mtd, ofs, getchip);
  457. /* Return info from the table */
  458. return nand_isbad_bbt(mtd, ofs, allowbbt);
  459. }
  460. DEFINE_LED_TRIGGER(nand_led_trigger);
  461. /*
  462. * Wait for the ready pin, after a command
  463. * The timeout is catched later.
  464. */
  465. static void nand_wait_ready(struct mtd_info *mtd)
  466. {
  467. struct nand_chip *this = mtd->priv;
  468. unsigned long timeo = jiffies + 2;
  469. led_trigger_event(nand_led_trigger, LED_FULL);
  470. /* wait until command is processed or timeout occures */
  471. do {
  472. if (this->dev_ready(mtd))
  473. break;
  474. touch_softlockup_watchdog();
  475. } while (time_before(jiffies, timeo));
  476. led_trigger_event(nand_led_trigger, LED_OFF);
  477. }
  478. /**
  479. * nand_command - [DEFAULT] Send command to NAND device
  480. * @mtd: MTD device structure
  481. * @command: the command to be sent
  482. * @column: the column address for this command, -1 if none
  483. * @page_addr: the page address for this command, -1 if none
  484. *
  485. * Send command to NAND device. This function is used for small page
  486. * devices (256/512 Bytes per page)
  487. */
  488. static void nand_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  489. {
  490. register struct nand_chip *this = mtd->priv;
  491. /* Begin command latch cycle */
  492. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  493. /*
  494. * Write out the command to the device.
  495. */
  496. if (command == NAND_CMD_SEQIN) {
  497. int readcmd;
  498. if (column >= mtd->oobblock) {
  499. /* OOB area */
  500. column -= mtd->oobblock;
  501. readcmd = NAND_CMD_READOOB;
  502. } else if (column < 256) {
  503. /* First 256 bytes --> READ0 */
  504. readcmd = NAND_CMD_READ0;
  505. } else {
  506. column -= 256;
  507. readcmd = NAND_CMD_READ1;
  508. }
  509. this->write_byte(mtd, readcmd);
  510. }
  511. this->write_byte(mtd, command);
  512. /* Set ALE and clear CLE to start address cycle */
  513. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  514. if (column != -1 || page_addr != -1) {
  515. this->hwcontrol(mtd, NAND_CTL_SETALE);
  516. /* Serially input address */
  517. if (column != -1) {
  518. /* Adjust columns for 16 bit buswidth */
  519. if (this->options & NAND_BUSWIDTH_16)
  520. column >>= 1;
  521. this->write_byte(mtd, column);
  522. }
  523. if (page_addr != -1) {
  524. this->write_byte(mtd, (unsigned char)(page_addr & 0xff));
  525. this->write_byte(mtd, (unsigned char)((page_addr >> 8) & 0xff));
  526. /* One more address cycle for devices > 32MiB */
  527. if (this->chipsize > (32 << 20))
  528. this->write_byte(mtd, (unsigned char)((page_addr >> 16) & 0x0f));
  529. }
  530. /* Latch in address */
  531. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  532. }
  533. /*
  534. * program and erase have their own busy handlers
  535. * status and sequential in needs no delay
  536. */
  537. switch (command) {
  538. case NAND_CMD_PAGEPROG:
  539. case NAND_CMD_ERASE1:
  540. case NAND_CMD_ERASE2:
  541. case NAND_CMD_SEQIN:
  542. case NAND_CMD_STATUS:
  543. return;
  544. case NAND_CMD_RESET:
  545. if (this->dev_ready)
  546. break;
  547. udelay(this->chip_delay);
  548. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  549. this->write_byte(mtd, NAND_CMD_STATUS);
  550. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  551. while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
  552. return;
  553. /* This applies to read commands */
  554. default:
  555. /*
  556. * If we don't have access to the busy pin, we apply the given
  557. * command delay
  558. */
  559. if (!this->dev_ready) {
  560. udelay(this->chip_delay);
  561. return;
  562. }
  563. }
  564. /* Apply this short delay always to ensure that we do wait tWB in
  565. * any case on any machine. */
  566. ndelay(100);
  567. nand_wait_ready(mtd);
  568. }
  569. /**
  570. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  571. * @mtd: MTD device structure
  572. * @command: the command to be sent
  573. * @column: the column address for this command, -1 if none
  574. * @page_addr: the page address for this command, -1 if none
  575. *
  576. * Send command to NAND device. This is the version for the new large page devices
  577. * We dont have the separate regions as we have in the small page devices.
  578. * We must emulate NAND_CMD_READOOB to keep the code compatible.
  579. *
  580. */
  581. static void nand_command_lp(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  582. {
  583. register struct nand_chip *this = mtd->priv;
  584. /* Emulate NAND_CMD_READOOB */
  585. if (command == NAND_CMD_READOOB) {
  586. column += mtd->oobblock;
  587. command = NAND_CMD_READ0;
  588. }
  589. /* Begin command latch cycle */
  590. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  591. /* Write out the command to the device. */
  592. this->write_byte(mtd, (command & 0xff));
  593. /* End command latch cycle */
  594. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  595. if (column != -1 || page_addr != -1) {
  596. this->hwcontrol(mtd, NAND_CTL_SETALE);
  597. /* Serially input address */
  598. if (column != -1) {
  599. /* Adjust columns for 16 bit buswidth */
  600. if (this->options & NAND_BUSWIDTH_16)
  601. column >>= 1;
  602. this->write_byte(mtd, column & 0xff);
  603. this->write_byte(mtd, column >> 8);
  604. }
  605. if (page_addr != -1) {
  606. this->write_byte(mtd, (unsigned char)(page_addr & 0xff));
  607. this->write_byte(mtd, (unsigned char)((page_addr >> 8) & 0xff));
  608. /* One more address cycle for devices > 128MiB */
  609. if (this->chipsize > (128 << 20))
  610. this->write_byte(mtd, (unsigned char)((page_addr >> 16) & 0xff));
  611. }
  612. /* Latch in address */
  613. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  614. }
  615. /*
  616. * program and erase have their own busy handlers
  617. * status, sequential in, and deplete1 need no delay
  618. */
  619. switch (command) {
  620. case NAND_CMD_CACHEDPROG:
  621. case NAND_CMD_PAGEPROG:
  622. case NAND_CMD_ERASE1:
  623. case NAND_CMD_ERASE2:
  624. case NAND_CMD_SEQIN:
  625. case NAND_CMD_STATUS:
  626. case NAND_CMD_DEPLETE1:
  627. return;
  628. /*
  629. * read error status commands require only a short delay
  630. */
  631. case NAND_CMD_STATUS_ERROR:
  632. case NAND_CMD_STATUS_ERROR0:
  633. case NAND_CMD_STATUS_ERROR1:
  634. case NAND_CMD_STATUS_ERROR2:
  635. case NAND_CMD_STATUS_ERROR3:
  636. udelay(this->chip_delay);
  637. return;
  638. case NAND_CMD_RESET:
  639. if (this->dev_ready)
  640. break;
  641. udelay(this->chip_delay);
  642. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  643. this->write_byte(mtd, NAND_CMD_STATUS);
  644. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  645. while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
  646. return;
  647. case NAND_CMD_READ0:
  648. /* Begin command latch cycle */
  649. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  650. /* Write out the start read command */
  651. this->write_byte(mtd, NAND_CMD_READSTART);
  652. /* End command latch cycle */
  653. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  654. /* Fall through into ready check */
  655. /* This applies to read commands */
  656. default:
  657. /*
  658. * If we don't have access to the busy pin, we apply the given
  659. * command delay
  660. */
  661. if (!this->dev_ready) {
  662. udelay(this->chip_delay);
  663. return;
  664. }
  665. }
  666. /* Apply this short delay always to ensure that we do wait tWB in
  667. * any case on any machine. */
  668. ndelay(100);
  669. nand_wait_ready(mtd);
  670. }
  671. /**
  672. * nand_get_device - [GENERIC] Get chip for selected access
  673. * @this: the nand chip descriptor
  674. * @mtd: MTD device structure
  675. * @new_state: the state which is requested
  676. *
  677. * Get the device and lock it for exclusive access
  678. */
  679. static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state)
  680. {
  681. spinlock_t *lock = &this->controller->lock;
  682. wait_queue_head_t *wq = &this->controller->wq;
  683. DECLARE_WAITQUEUE(wait, current);
  684. retry:
  685. spin_lock(lock);
  686. /* Hardware controller shared among independend devices */
  687. /* Hardware controller shared among independend devices */
  688. if (!this->controller->active)
  689. this->controller->active = this;
  690. if (this->controller->active == this && this->state == FL_READY) {
  691. this->state = new_state;
  692. spin_unlock(lock);
  693. return 0;
  694. }
  695. if (new_state == FL_PM_SUSPENDED) {
  696. spin_unlock(lock);
  697. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  698. }
  699. set_current_state(TASK_UNINTERRUPTIBLE);
  700. add_wait_queue(wq, &wait);
  701. spin_unlock(lock);
  702. schedule();
  703. remove_wait_queue(wq, &wait);
  704. goto retry;
  705. }
  706. /**
  707. * nand_wait - [DEFAULT] wait until the command is done
  708. * @mtd: MTD device structure
  709. * @this: NAND chip structure
  710. * @state: state to select the max. timeout value
  711. *
  712. * Wait for command done. This applies to erase and program only
  713. * Erase can take up to 400ms and program up to 20ms according to
  714. * general NAND and SmartMedia specs
  715. *
  716. */
  717. static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
  718. {
  719. unsigned long timeo = jiffies;
  720. int status;
  721. if (state == FL_ERASING)
  722. timeo += (HZ * 400) / 1000;
  723. else
  724. timeo += (HZ * 20) / 1000;
  725. led_trigger_event(nand_led_trigger, LED_FULL);
  726. /* Apply this short delay always to ensure that we do wait tWB in
  727. * any case on any machine. */
  728. ndelay(100);
  729. if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
  730. this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
  731. else
  732. this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  733. while (time_before(jiffies, timeo)) {
  734. /* Check, if we were interrupted */
  735. if (this->state != state)
  736. return 0;
  737. if (this->dev_ready) {
  738. if (this->dev_ready(mtd))
  739. break;
  740. } else {
  741. if (this->read_byte(mtd) & NAND_STATUS_READY)
  742. break;
  743. }
  744. cond_resched();
  745. }
  746. led_trigger_event(nand_led_trigger, LED_OFF);
  747. status = (int)this->read_byte(mtd);
  748. return status;
  749. }
  750. /**
  751. * nand_write_page - [GENERIC] write one page
  752. * @mtd: MTD device structure
  753. * @this: NAND chip structure
  754. * @page: startpage inside the chip, must be called with (page & this->pagemask)
  755. * @oob_buf: out of band data buffer
  756. * @oobsel: out of band selecttion structre
  757. * @cached: 1 = enable cached programming if supported by chip
  758. *
  759. * Nand_page_program function is used for write and writev !
  760. * This function will always program a full page of data
  761. * If you call it with a non page aligned buffer, you're lost :)
  762. *
  763. * Cached programming is not supported yet.
  764. */
  765. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this, int page,
  766. u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
  767. {
  768. int i, status;
  769. u_char ecc_code[32];
  770. int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
  771. int *oob_config = oobsel->eccpos;
  772. int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
  773. int eccbytes = 0;
  774. /* FIXME: Enable cached programming */
  775. cached = 0;
  776. /* Send command to begin auto page programming */
  777. this->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  778. /* Write out complete page of data, take care of eccmode */
  779. switch (eccmode) {
  780. /* No ecc, write all */
  781. case NAND_ECC_NONE:
  782. printk(KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
  783. this->write_buf(mtd, this->data_poi, mtd->oobblock);
  784. break;
  785. /* Software ecc 3/256, write all */
  786. case NAND_ECC_SOFT:
  787. for (; eccsteps; eccsteps--) {
  788. this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
  789. for (i = 0; i < 3; i++, eccidx++)
  790. oob_buf[oob_config[eccidx]] = ecc_code[i];
  791. datidx += this->eccsize;
  792. }
  793. this->write_buf(mtd, this->data_poi, mtd->oobblock);
  794. break;
  795. default:
  796. eccbytes = this->eccbytes;
  797. for (; eccsteps; eccsteps--) {
  798. /* enable hardware ecc logic for write */
  799. this->enable_hwecc(mtd, NAND_ECC_WRITE);
  800. this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
  801. this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
  802. for (i = 0; i < eccbytes; i++, eccidx++)
  803. oob_buf[oob_config[eccidx]] = ecc_code[i];
  804. /* If the hardware ecc provides syndromes then
  805. * the ecc code must be written immidiately after
  806. * the data bytes (words) */
  807. if (this->options & NAND_HWECC_SYNDROME)
  808. this->write_buf(mtd, ecc_code, eccbytes);
  809. datidx += this->eccsize;
  810. }
  811. break;
  812. }
  813. /* Write out OOB data */
  814. if (this->options & NAND_HWECC_SYNDROME)
  815. this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
  816. else
  817. this->write_buf(mtd, oob_buf, mtd->oobsize);
  818. /* Send command to actually program the data */
  819. this->cmdfunc(mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
  820. if (!cached) {
  821. /* call wait ready function */
  822. status = this->waitfunc(mtd, this, FL_WRITING);
  823. /* See if operation failed and additional status checks are available */
  824. if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
  825. status = this->errstat(mtd, this, FL_WRITING, status, page);
  826. }
  827. /* See if device thinks it succeeded */
  828. if (status & NAND_STATUS_FAIL) {
  829. DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
  830. return -EIO;
  831. }
  832. } else {
  833. /* FIXME: Implement cached programming ! */
  834. /* wait until cache is ready */
  835. // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
  836. }
  837. return 0;
  838. }
  839. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  840. /**
  841. * nand_verify_pages - [GENERIC] verify the chip contents after a write
  842. * @mtd: MTD device structure
  843. * @this: NAND chip structure
  844. * @page: startpage inside the chip, must be called with (page & this->pagemask)
  845. * @numpages: number of pages to verify
  846. * @oob_buf: out of band data buffer
  847. * @oobsel: out of band selecttion structre
  848. * @chipnr: number of the current chip
  849. * @oobmode: 1 = full buffer verify, 0 = ecc only
  850. *
  851. * The NAND device assumes that it is always writing to a cleanly erased page.
  852. * Hence, it performs its internal write verification only on bits that
  853. * transitioned from 1 to 0. The device does NOT verify the whole page on a
  854. * byte by byte basis. It is possible that the page was not completely erased
  855. * or the page is becoming unusable due to wear. The read with ECC would catch
  856. * the error later when the ECC page check fails, but we would rather catch
  857. * it early in the page write stage. Better to write no data than invalid data.
  858. */
  859. static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
  860. u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
  861. {
  862. int i, j, datidx = 0, oobofs = 0, res = -EIO;
  863. int eccsteps = this->eccsteps;
  864. int hweccbytes;
  865. u_char oobdata[64];
  866. hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
  867. /* Send command to read back the first page */
  868. this->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  869. for (;;) {
  870. for (j = 0; j < eccsteps; j++) {
  871. /* Loop through and verify the data */
  872. if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
  873. DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
  874. goto out;
  875. }
  876. datidx += mtd->eccsize;
  877. /* Have we a hw generator layout ? */
  878. if (!hweccbytes)
  879. continue;
  880. if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
  881. DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
  882. goto out;
  883. }
  884. oobofs += hweccbytes;
  885. }
  886. /* check, if we must compare all data or if we just have to
  887. * compare the ecc bytes
  888. */
  889. if (oobmode) {
  890. if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
  891. DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
  892. goto out;
  893. }
  894. } else {
  895. /* Read always, else autoincrement fails */
  896. this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
  897. if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
  898. int ecccnt = oobsel->eccbytes;
  899. for (i = 0; i < ecccnt; i++) {
  900. int idx = oobsel->eccpos[i];
  901. if (oobdata[idx] != oob_buf[oobofs + idx]) {
  902. DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed ECC write verify, page 0x%08x, %6i bytes were succesful\n",
  903. __FUNCTION__, page, i);
  904. goto out;
  905. }
  906. }
  907. }
  908. }
  909. oobofs += mtd->oobsize - hweccbytes * eccsteps;
  910. page++;
  911. numpages--;
  912. /* Apply delay or wait for ready/busy pin
  913. * Do this before the AUTOINCR check, so no problems
  914. * arise if a chip which does auto increment
  915. * is marked as NOAUTOINCR by the board driver.
  916. * Do this also before returning, so the chip is
  917. * ready for the next command.
  918. */
  919. if (!this->dev_ready)
  920. udelay(this->chip_delay);
  921. else
  922. nand_wait_ready(mtd);
  923. /* All done, return happy */
  924. if (!numpages)
  925. return 0;
  926. /* Check, if the chip supports auto page increment */
  927. if (!NAND_CANAUTOINCR(this))
  928. this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  929. }
  930. /*
  931. * Terminate the read command. We come here in case of an error
  932. * So we must issue a reset command.
  933. */
  934. out:
  935. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  936. return res;
  937. }
  938. #endif
  939. /**
  940. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  941. * @mtd: MTD device structure
  942. * @from: offset to read from
  943. * @len: number of bytes to read
  944. * @retlen: pointer to variable to store the number of read bytes
  945. * @buf: the databuffer to put data
  946. *
  947. * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL
  948. * and flags = 0xff
  949. */
  950. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  951. {
  952. return nand_do_read_ecc(mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff);
  953. }
  954. /**
  955. * nand_read_ecc - [MTD Interface] MTD compability function for nand_do_read_ecc
  956. * @mtd: MTD device structure
  957. * @from: offset to read from
  958. * @len: number of bytes to read
  959. * @retlen: pointer to variable to store the number of read bytes
  960. * @buf: the databuffer to put data
  961. * @oob_buf: filesystem supplied oob data buffer
  962. * @oobsel: oob selection structure
  963. *
  964. * This function simply calls nand_do_read_ecc with flags = 0xff
  965. */
  966. static int nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  967. size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *oobsel)
  968. {
  969. /* use userspace supplied oobinfo, if zero */
  970. if (oobsel == NULL)
  971. oobsel = &mtd->oobinfo;
  972. return nand_do_read_ecc(mtd, from, len, retlen, buf, oob_buf, oobsel, 0xff);
  973. }
  974. /**
  975. * nand_do_read_ecc - [MTD Interface] Read data with ECC
  976. * @mtd: MTD device structure
  977. * @from: offset to read from
  978. * @len: number of bytes to read
  979. * @retlen: pointer to variable to store the number of read bytes
  980. * @buf: the databuffer to put data
  981. * @oob_buf: filesystem supplied oob data buffer (can be NULL)
  982. * @oobsel: oob selection structure
  983. * @flags: flag to indicate if nand_get_device/nand_release_device should be preformed
  984. * and how many corrected error bits are acceptable:
  985. * bits 0..7 - number of tolerable errors
  986. * bit 8 - 0 == do not get/release chip, 1 == get/release chip
  987. *
  988. * NAND read with ECC
  989. */
  990. int nand_do_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  991. size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *oobsel, int flags)
  992. {
  993. int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
  994. int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
  995. struct nand_chip *this = mtd->priv;
  996. u_char *data_poi, *oob_data = oob_buf;
  997. u_char ecc_calc[32];
  998. u_char ecc_code[32];
  999. int eccmode, eccsteps;
  1000. int *oob_config, datidx;
  1001. int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
  1002. int eccbytes;
  1003. int compareecc = 1;
  1004. int oobreadlen;
  1005. DEBUG(MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
  1006. /* Do not allow reads past end of device */
  1007. if ((from + len) > mtd->size) {
  1008. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
  1009. *retlen = 0;
  1010. return -EINVAL;
  1011. }
  1012. /* Grab the lock and see if the device is available */
  1013. if (flags & NAND_GET_DEVICE)
  1014. nand_get_device(this, mtd, FL_READING);
  1015. /* Autoplace of oob data ? Use the default placement scheme */
  1016. if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
  1017. oobsel = this->autooob;
  1018. eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
  1019. oob_config = oobsel->eccpos;
  1020. /* Select the NAND device */
  1021. chipnr = (int)(from >> this->chip_shift);
  1022. this->select_chip(mtd, chipnr);
  1023. /* First we calculate the starting page */
  1024. realpage = (int)(from >> this->page_shift);
  1025. page = realpage & this->pagemask;
  1026. /* Get raw starting column */
  1027. col = from & (mtd->oobblock - 1);
  1028. end = mtd->oobblock;
  1029. ecc = this->eccsize;
  1030. eccbytes = this->eccbytes;
  1031. if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
  1032. compareecc = 0;
  1033. oobreadlen = mtd->oobsize;
  1034. if (this->options & NAND_HWECC_SYNDROME)
  1035. oobreadlen -= oobsel->eccbytes;
  1036. /* Loop until all data read */
  1037. while (read < len) {
  1038. int aligned = (!col && (len - read) >= end);
  1039. /*
  1040. * If the read is not page aligned, we have to read into data buffer
  1041. * due to ecc, else we read into return buffer direct
  1042. */
  1043. if (aligned)
  1044. data_poi = &buf[read];
  1045. else
  1046. data_poi = this->data_buf;
  1047. /* Check, if we have this page in the buffer
  1048. *
  1049. * FIXME: Make it work when we must provide oob data too,
  1050. * check the usage of data_buf oob field
  1051. */
  1052. if (realpage == this->pagebuf && !oob_buf) {
  1053. /* aligned read ? */
  1054. if (aligned)
  1055. memcpy(data_poi, this->data_buf, end);
  1056. goto readdata;
  1057. }
  1058. /* Check, if we must send the read command */
  1059. if (sndcmd) {
  1060. this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1061. sndcmd = 0;
  1062. }
  1063. /* get oob area, if we have no oob buffer from fs-driver */
  1064. if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
  1065. oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
  1066. oob_data = &this->data_buf[end];
  1067. eccsteps = this->eccsteps;
  1068. switch (eccmode) {
  1069. case NAND_ECC_NONE:{
  1070. /* No ECC, Read in a page */
  1071. static unsigned long lastwhinge = 0;
  1072. if ((lastwhinge / HZ) != (jiffies / HZ)) {
  1073. printk(KERN_WARNING
  1074. "Reading data from NAND FLASH without ECC is not recommended\n");
  1075. lastwhinge = jiffies;
  1076. }
  1077. this->read_buf(mtd, data_poi, end);
  1078. break;
  1079. }
  1080. case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
  1081. this->read_buf(mtd, data_poi, end);
  1082. for (i = 0, datidx = 0; eccsteps; eccsteps--, i += 3, datidx += ecc)
  1083. this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
  1084. break;
  1085. default:
  1086. for (i = 0, datidx = 0; eccsteps; eccsteps--, i += eccbytes, datidx += ecc) {
  1087. this->enable_hwecc(mtd, NAND_ECC_READ);
  1088. this->read_buf(mtd, &data_poi[datidx], ecc);
  1089. /* HW ecc with syndrome calculation must read the
  1090. * syndrome from flash immidiately after the data */
  1091. if (!compareecc) {
  1092. /* Some hw ecc generators need to know when the
  1093. * syndrome is read from flash */
  1094. this->enable_hwecc(mtd, NAND_ECC_READSYN);
  1095. this->read_buf(mtd, &oob_data[i], eccbytes);
  1096. /* We calc error correction directly, it checks the hw
  1097. * generator for an error, reads back the syndrome and
  1098. * does the error correction on the fly */
  1099. ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);
  1100. if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
  1101. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: "
  1102. "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
  1103. ecc_failed++;
  1104. }
  1105. } else {
  1106. this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
  1107. }
  1108. }
  1109. break;
  1110. }
  1111. /* read oobdata */
  1112. this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
  1113. /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
  1114. if (!compareecc)
  1115. goto readoob;
  1116. /* Pick the ECC bytes out of the oob data */
  1117. for (j = 0; j < oobsel->eccbytes; j++)
  1118. ecc_code[j] = oob_data[oob_config[j]];
  1119. /* correct data, if necessary */
  1120. for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
  1121. ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
  1122. /* Get next chunk of ecc bytes */
  1123. j += eccbytes;
  1124. /* Check, if we have a fs supplied oob-buffer,
  1125. * This is the legacy mode. Used by YAFFS1
  1126. * Should go away some day
  1127. */
  1128. if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
  1129. int *p = (int *)(&oob_data[mtd->oobsize]);
  1130. p[i] = ecc_status;
  1131. }
  1132. if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
  1133. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
  1134. ecc_failed++;
  1135. }
  1136. }
  1137. readoob:
  1138. /* check, if we have a fs supplied oob-buffer */
  1139. if (oob_buf) {
  1140. /* without autoplace. Legacy mode used by YAFFS1 */
  1141. switch (oobsel->useecc) {
  1142. case MTD_NANDECC_AUTOPLACE:
  1143. case MTD_NANDECC_AUTOPL_USR:
  1144. /* Walk through the autoplace chunks */
  1145. for (i = 0; oobsel->oobfree[i][1]; i++) {
  1146. int from = oobsel->oobfree[i][0];
  1147. int num = oobsel->oobfree[i][1];
  1148. memcpy(&oob_buf[oob], &oob_data[from], num);
  1149. oob += num;
  1150. }
  1151. break;
  1152. case MTD_NANDECC_PLACE:
  1153. /* YAFFS1 legacy mode */
  1154. oob_data += this->eccsteps * sizeof(int);
  1155. default:
  1156. oob_data += mtd->oobsize;
  1157. }
  1158. }
  1159. readdata:
  1160. /* Partial page read, transfer data into fs buffer */
  1161. if (!aligned) {
  1162. for (j = col; j < end && read < len; j++)
  1163. buf[read++] = data_poi[j];
  1164. this->pagebuf = realpage;
  1165. } else
  1166. read += mtd->oobblock;
  1167. /* Apply delay or wait for ready/busy pin
  1168. * Do this before the AUTOINCR check, so no problems
  1169. * arise if a chip which does auto increment
  1170. * is marked as NOAUTOINCR by the board driver.
  1171. */
  1172. if (!this->dev_ready)
  1173. udelay(this->chip_delay);
  1174. else
  1175. nand_wait_ready(mtd);
  1176. if (read == len)
  1177. break;
  1178. /* For subsequent reads align to page boundary. */
  1179. col = 0;
  1180. /* Increment page address */
  1181. realpage++;
  1182. page = realpage & this->pagemask;
  1183. /* Check, if we cross a chip boundary */
  1184. if (!page) {
  1185. chipnr++;
  1186. this->select_chip(mtd, -1);
  1187. this->select_chip(mtd, chipnr);
  1188. }
  1189. /* Check, if the chip supports auto page increment
  1190. * or if we have hit a block boundary.
  1191. */
  1192. if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
  1193. sndcmd = 1;
  1194. }
  1195. /* Deselect and wake up anyone waiting on the device */
  1196. if (flags & NAND_GET_DEVICE)
  1197. nand_release_device(mtd);
  1198. /*
  1199. * Return success, if no ECC failures, else -EBADMSG
  1200. * fs driver will take care of that, because
  1201. * retlen == desired len and result == -EBADMSG
  1202. */
  1203. *retlen = read;
  1204. return ecc_failed ? -EBADMSG : 0;
  1205. }
  1206. /**
  1207. * nand_read_oob - [MTD Interface] NAND read out-of-band
  1208. * @mtd: MTD device structure
  1209. * @from: offset to read from
  1210. * @len: number of bytes to read
  1211. * @retlen: pointer to variable to store the number of read bytes
  1212. * @buf: the databuffer to put data
  1213. *
  1214. * NAND read out-of-band data from the spare area
  1215. */
  1216. static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1217. {
  1218. int i, col, page, chipnr;
  1219. struct nand_chip *this = mtd->priv;
  1220. int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
  1221. DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
  1222. /* Shift to get page */
  1223. page = (int)(from >> this->page_shift);
  1224. chipnr = (int)(from >> this->chip_shift);
  1225. /* Mask to get column */
  1226. col = from & (mtd->oobsize - 1);
  1227. /* Initialize return length value */
  1228. *retlen = 0;
  1229. /* Do not allow reads past end of device */
  1230. if ((from + len) > mtd->size) {
  1231. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
  1232. *retlen = 0;
  1233. return -EINVAL;
  1234. }
  1235. /* Grab the lock and see if the device is available */
  1236. nand_get_device(this, mtd, FL_READING);
  1237. /* Select the NAND device */
  1238. this->select_chip(mtd, chipnr);
  1239. /* Send the read command */
  1240. this->cmdfunc(mtd, NAND_CMD_READOOB, col, page & this->pagemask);
  1241. /*
  1242. * Read the data, if we read more than one page
  1243. * oob data, let the device transfer the data !
  1244. */
  1245. i = 0;
  1246. while (i < len) {
  1247. int thislen = mtd->oobsize - col;
  1248. thislen = min_t(int, thislen, len);
  1249. this->read_buf(mtd, &buf[i], thislen);
  1250. i += thislen;
  1251. /* Read more ? */
  1252. if (i < len) {
  1253. page++;
  1254. col = 0;
  1255. /* Check, if we cross a chip boundary */
  1256. if (!(page & this->pagemask)) {
  1257. chipnr++;
  1258. this->select_chip(mtd, -1);
  1259. this->select_chip(mtd, chipnr);
  1260. }
  1261. /* Apply delay or wait for ready/busy pin
  1262. * Do this before the AUTOINCR check, so no problems
  1263. * arise if a chip which does auto increment
  1264. * is marked as NOAUTOINCR by the board driver.
  1265. */
  1266. if (!this->dev_ready)
  1267. udelay(this->chip_delay);
  1268. else
  1269. nand_wait_ready(mtd);
  1270. /* Check, if the chip supports auto page increment
  1271. * or if we have hit a block boundary.
  1272. */
  1273. if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
  1274. /* For subsequent page reads set offset to 0 */
  1275. this->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
  1276. }
  1277. }
  1278. }
  1279. /* Deselect and wake up anyone waiting on the device */
  1280. nand_release_device(mtd);
  1281. /* Return happy */
  1282. *retlen = len;
  1283. return 0;
  1284. }
  1285. /**
  1286. * nand_read_raw - [GENERIC] Read raw data including oob into buffer
  1287. * @mtd: MTD device structure
  1288. * @buf: temporary buffer
  1289. * @from: offset to read from
  1290. * @len: number of bytes to read
  1291. * @ooblen: number of oob data bytes to read
  1292. *
  1293. * Read raw data including oob into buffer
  1294. */
  1295. int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
  1296. {
  1297. struct nand_chip *this = mtd->priv;
  1298. int page = (int)(from >> this->page_shift);
  1299. int chip = (int)(from >> this->chip_shift);
  1300. int sndcmd = 1;
  1301. int cnt = 0;
  1302. int pagesize = mtd->oobblock + mtd->oobsize;
  1303. int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
  1304. /* Do not allow reads past end of device */
  1305. if ((from + len) > mtd->size) {
  1306. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
  1307. return -EINVAL;
  1308. }
  1309. /* Grab the lock and see if the device is available */
  1310. nand_get_device(this, mtd, FL_READING);
  1311. this->select_chip(mtd, chip);
  1312. /* Add requested oob length */
  1313. len += ooblen;
  1314. while (len) {
  1315. if (sndcmd)
  1316. this->cmdfunc(mtd, NAND_CMD_READ0, 0, page & this->pagemask);
  1317. sndcmd = 0;
  1318. this->read_buf(mtd, &buf[cnt], pagesize);
  1319. len -= pagesize;
  1320. cnt += pagesize;
  1321. page++;
  1322. if (!this->dev_ready)
  1323. udelay(this->chip_delay);
  1324. else
  1325. nand_wait_ready(mtd);
  1326. /* Check, if the chip supports auto page increment */
  1327. if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
  1328. sndcmd = 1;
  1329. }
  1330. /* Deselect and wake up anyone waiting on the device */
  1331. nand_release_device(mtd);
  1332. return 0;
  1333. }
  1334. /**
  1335. * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
  1336. * @mtd: MTD device structure
  1337. * @fsbuf: buffer given by fs driver
  1338. * @oobsel: out of band selection structre
  1339. * @autoplace: 1 = place given buffer into the oob bytes
  1340. * @numpages: number of pages to prepare
  1341. *
  1342. * Return:
  1343. * 1. Filesystem buffer available and autoplacement is off,
  1344. * return filesystem buffer
  1345. * 2. No filesystem buffer or autoplace is off, return internal
  1346. * buffer
  1347. * 3. Filesystem buffer is given and autoplace selected
  1348. * put data from fs buffer into internal buffer and
  1349. * retrun internal buffer
  1350. *
  1351. * Note: The internal buffer is filled with 0xff. This must
  1352. * be done only once, when no autoplacement happens
  1353. * Autoplacement sets the buffer dirty flag, which
  1354. * forces the 0xff fill before using the buffer again.
  1355. *
  1356. */
  1357. static u_char *nand_prepare_oobbuf(struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
  1358. int autoplace, int numpages)
  1359. {
  1360. struct nand_chip *this = mtd->priv;
  1361. int i, len, ofs;
  1362. /* Zero copy fs supplied buffer */
  1363. if (fsbuf && !autoplace)
  1364. return fsbuf;
  1365. /* Check, if the buffer must be filled with ff again */
  1366. if (this->oobdirty) {
  1367. memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
  1368. this->oobdirty = 0;
  1369. }
  1370. /* If we have no autoplacement or no fs buffer use the internal one */
  1371. if (!autoplace || !fsbuf)
  1372. return this->oob_buf;
  1373. /* Walk through the pages and place the data */
  1374. this->oobdirty = 1;
  1375. ofs = 0;
  1376. while (numpages--) {
  1377. for (i = 0, len = 0; len < mtd->oobavail; i++) {
  1378. int to = ofs + oobsel->oobfree[i][0];
  1379. int num = oobsel->oobfree[i][1];
  1380. memcpy(&this->oob_buf[to], fsbuf, num);
  1381. len += num;
  1382. fsbuf += num;
  1383. }
  1384. ofs += mtd->oobavail;
  1385. }
  1386. return this->oob_buf;
  1387. }
  1388. #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
  1389. /**
  1390. * nand_write - [MTD Interface] compability function for nand_write_ecc
  1391. * @mtd: MTD device structure
  1392. * @to: offset to write to
  1393. * @len: number of bytes to write
  1394. * @retlen: pointer to variable to store the number of written bytes
  1395. * @buf: the data to write
  1396. *
  1397. * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
  1398. *
  1399. */
  1400. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  1401. {
  1402. return (nand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL));
  1403. }
  1404. /**
  1405. * nand_write_ecc - [MTD Interface] NAND write with ECC
  1406. * @mtd: MTD device structure
  1407. * @to: offset to write to
  1408. * @len: number of bytes to write
  1409. * @retlen: pointer to variable to store the number of written bytes
  1410. * @buf: the data to write
  1411. * @eccbuf: filesystem supplied oob data buffer
  1412. * @oobsel: oob selection structure
  1413. *
  1414. * NAND write with ECC
  1415. */
  1416. static int nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  1417. size_t *retlen, const u_char *buf, u_char *eccbuf,
  1418. struct nand_oobinfo *oobsel)
  1419. {
  1420. int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
  1421. int autoplace = 0, numpages, totalpages;
  1422. struct nand_chip *this = mtd->priv;
  1423. u_char *oobbuf, *bufstart;
  1424. int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
  1425. DEBUG(MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
  1426. /* Initialize retlen, in case of early exit */
  1427. *retlen = 0;
  1428. /* Do not allow write past end of device */
  1429. if ((to + len) > mtd->size) {
  1430. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
  1431. return -EINVAL;
  1432. }
  1433. /* reject writes, which are not page aligned */
  1434. if (NOTALIGNED(to) || NOTALIGNED(len)) {
  1435. printk(KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
  1436. return -EINVAL;
  1437. }
  1438. /* Grab the lock and see if the device is available */
  1439. nand_get_device(this, mtd, FL_WRITING);
  1440. /* Calculate chipnr */
  1441. chipnr = (int)(to >> this->chip_shift);
  1442. /* Select the NAND device */
  1443. this->select_chip(mtd, chipnr);
  1444. /* Check, if it is write protected */
  1445. if (nand_check_wp(mtd))
  1446. goto out;
  1447. /* if oobsel is NULL, use chip defaults */
  1448. if (oobsel == NULL)
  1449. oobsel = &mtd->oobinfo;
  1450. /* Autoplace of oob data ? Use the default placement scheme */
  1451. if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
  1452. oobsel = this->autooob;
  1453. autoplace = 1;
  1454. }
  1455. if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
  1456. autoplace = 1;
  1457. /* Setup variables and oob buffer */
  1458. totalpages = len >> this->page_shift;
  1459. page = (int)(to >> this->page_shift);
  1460. /* Invalidate the page cache, if we write to the cached page */
  1461. if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
  1462. this->pagebuf = -1;
  1463. /* Set it relative to chip */
  1464. page &= this->pagemask;
  1465. startpage = page;
  1466. /* Calc number of pages we can write in one go */
  1467. numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages);
  1468. oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
  1469. bufstart = (u_char *) buf;
  1470. /* Loop until all data is written */
  1471. while (written < len) {
  1472. this->data_poi = (u_char *) &buf[written];
  1473. /* Write one page. If this is the last page to write
  1474. * or the last page in this block, then use the
  1475. * real pageprogram command, else select cached programming
  1476. * if supported by the chip.
  1477. */
  1478. ret = nand_write_page(mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
  1479. if (ret) {
  1480. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
  1481. goto out;
  1482. }
  1483. /* Next oob page */
  1484. oob += mtd->oobsize;
  1485. /* Update written bytes count */
  1486. written += mtd->oobblock;
  1487. if (written == len)
  1488. goto cmp;
  1489. /* Increment page address */
  1490. page++;
  1491. /* Have we hit a block boundary ? Then we have to verify and
  1492. * if verify is ok, we have to setup the oob buffer for
  1493. * the next pages.
  1494. */
  1495. if (!(page & (ppblock - 1))) {
  1496. int ofs;
  1497. this->data_poi = bufstart;
  1498. ret = nand_verify_pages(mtd, this, startpage, page - startpage,
  1499. oobbuf, oobsel, chipnr, (eccbuf != NULL));
  1500. if (ret) {
  1501. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
  1502. goto out;
  1503. }
  1504. *retlen = written;
  1505. ofs = autoplace ? mtd->oobavail : mtd->oobsize;
  1506. if (eccbuf)
  1507. eccbuf += (page - startpage) * ofs;
  1508. totalpages -= page - startpage;
  1509. numpages = min(totalpages, ppblock);
  1510. page &= this->pagemask;
  1511. startpage = page;
  1512. oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
  1513. oob = 0;
  1514. /* Check, if we cross a chip boundary */
  1515. if (!page) {
  1516. chipnr++;
  1517. this->select_chip(mtd, -1);
  1518. this->select_chip(mtd, chipnr);
  1519. }
  1520. }
  1521. }
  1522. /* Verify the remaining pages */
  1523. cmp:
  1524. this->data_poi = bufstart;
  1525. ret = nand_verify_pages(mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL));
  1526. if (!ret)
  1527. *retlen = written;
  1528. else
  1529. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
  1530. out:
  1531. /* Deselect and wake up anyone waiting on the device */
  1532. nand_release_device(mtd);
  1533. return ret;
  1534. }
  1535. /**
  1536. * nand_write_oob - [MTD Interface] NAND write out-of-band
  1537. * @mtd: MTD device structure
  1538. * @to: offset to write to
  1539. * @len: number of bytes to write
  1540. * @retlen: pointer to variable to store the number of written bytes
  1541. * @buf: the data to write
  1542. *
  1543. * NAND write out-of-band
  1544. */
  1545. static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  1546. {
  1547. int column, page, status, ret = -EIO, chipnr;
  1548. struct nand_chip *this = mtd->priv;
  1549. DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
  1550. /* Shift to get page */
  1551. page = (int)(to >> this->page_shift);
  1552. chipnr = (int)(to >> this->chip_shift);
  1553. /* Mask to get column */
  1554. column = to & (mtd->oobsize - 1);
  1555. /* Initialize return length value */
  1556. *retlen = 0;
  1557. /* Do not allow write past end of page */
  1558. if ((column + len) > mtd->oobsize) {
  1559. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
  1560. return -EINVAL;
  1561. }
  1562. /* Grab the lock and see if the device is available */
  1563. nand_get_device(this, mtd, FL_WRITING);
  1564. /* Select the NAND device */
  1565. this->select_chip(mtd, chipnr);
  1566. /* Reset the chip. Some chips (like the Toshiba TC5832DC found
  1567. in one of my DiskOnChip 2000 test units) will clear the whole
  1568. data page too if we don't do this. I have no clue why, but
  1569. I seem to have 'fixed' it in the doc2000 driver in
  1570. August 1999. dwmw2. */
  1571. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1572. /* Check, if it is write protected */
  1573. if (nand_check_wp(mtd))
  1574. goto out;
  1575. /* Invalidate the page cache, if we write to the cached page */
  1576. if (page == this->pagebuf)
  1577. this->pagebuf = -1;
  1578. if (NAND_MUST_PAD(this)) {
  1579. /* Write out desired data */
  1580. this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
  1581. /* prepad 0xff for partial programming */
  1582. this->write_buf(mtd, ffchars, column);
  1583. /* write data */
  1584. this->write_buf(mtd, buf, len);
  1585. /* postpad 0xff for partial programming */
  1586. this->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
  1587. } else {
  1588. /* Write out desired data */
  1589. this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
  1590. /* write data */
  1591. this->write_buf(mtd, buf, len);
  1592. }
  1593. /* Send command to program the OOB data */
  1594. this->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1595. status = this->waitfunc(mtd, this, FL_WRITING);
  1596. /* See if device thinks it succeeded */
  1597. if (status & NAND_STATUS_FAIL) {
  1598. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
  1599. ret = -EIO;
  1600. goto out;
  1601. }
  1602. /* Return happy */
  1603. *retlen = len;
  1604. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1605. /* Send command to read back the data */
  1606. this->cmdfunc(mtd, NAND_CMD_READOOB, column, page & this->pagemask);
  1607. if (this->verify_buf(mtd, buf, len)) {
  1608. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
  1609. ret = -EIO;
  1610. goto out;
  1611. }
  1612. #endif
  1613. ret = 0;
  1614. out:
  1615. /* Deselect and wake up anyone waiting on the device */
  1616. nand_release_device(mtd);
  1617. return ret;
  1618. }
  1619. /**
  1620. * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
  1621. * @mtd: MTD device structure
  1622. * @vecs: the iovectors to write
  1623. * @count: number of vectors
  1624. * @to: offset to write to
  1625. * @retlen: pointer to variable to store the number of written bytes
  1626. *
  1627. * NAND write with kvec. This just calls the ecc function
  1628. */
  1629. static int nand_writev(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
  1630. loff_t to, size_t *retlen)
  1631. {
  1632. return (nand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL));
  1633. }
  1634. /**
  1635. * nand_writev_ecc - [MTD Interface] write with iovec with ecc
  1636. * @mtd: MTD device structure
  1637. * @vecs: the iovectors to write
  1638. * @count: number of vectors
  1639. * @to: offset to write to
  1640. * @retlen: pointer to variable to store the number of written bytes
  1641. * @eccbuf: filesystem supplied oob data buffer
  1642. * @oobsel: oob selection structure
  1643. *
  1644. * NAND write with iovec with ecc
  1645. */
  1646. static int nand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
  1647. loff_t to, size_t *retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
  1648. {
  1649. int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
  1650. int oob, numpages, autoplace = 0, startpage;
  1651. struct nand_chip *this = mtd->priv;
  1652. int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
  1653. u_char *oobbuf, *bufstart;
  1654. /* Preset written len for early exit */
  1655. *retlen = 0;
  1656. /* Calculate total length of data */
  1657. total_len = 0;
  1658. for (i = 0; i < count; i++)
  1659. total_len += (int)vecs[i].iov_len;
  1660. DEBUG(MTD_DEBUG_LEVEL3, "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int)to, (unsigned int)total_len, count);
  1661. /* Do not allow write past end of page */
  1662. if ((to + total_len) > mtd->size) {
  1663. DEBUG(MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
  1664. return -EINVAL;
  1665. }
  1666. /* reject writes, which are not page aligned */
  1667. if (NOTALIGNED(to) || NOTALIGNED(total_len)) {
  1668. printk(KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
  1669. return -EINVAL;
  1670. }
  1671. /* Grab the lock and see if the device is available */
  1672. nand_get_device(this, mtd, FL_WRITING);
  1673. /* Get the current chip-nr */
  1674. chipnr = (int)(to >> this->chip_shift);
  1675. /* Select the NAND device */
  1676. this->select_chip(mtd, chipnr);
  1677. /* Check, if it is write protected */
  1678. if (nand_check_wp(mtd))
  1679. goto out;
  1680. /* if oobsel is NULL, use chip defaults */
  1681. if (oobsel == NULL)
  1682. oobsel = &mtd->oobinfo;
  1683. /* Autoplace of oob data ? Use the default placement scheme */
  1684. if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
  1685. oobsel = this->autooob;
  1686. autoplace = 1;
  1687. }
  1688. if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
  1689. autoplace = 1;
  1690. /* Setup start page */
  1691. page = (int)(to >> this->page_shift);
  1692. /* Invalidate the page cache, if we write to the cached page */
  1693. if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
  1694. this->pagebuf = -1;
  1695. startpage = page & this->pagemask;
  1696. /* Loop until all kvec' data has been written */
  1697. len = 0;
  1698. while (count) {
  1699. /* If the given tuple is >= pagesize then
  1700. * write it out from the iov
  1701. */
  1702. if ((vecs->iov_len - len) >= mtd->oobblock) {
  1703. /* Calc number of pages we can write
  1704. * out of this iov in one go */
  1705. numpages = (vecs->iov_len - len) >> this->page_shift;
  1706. /* Do not cross block boundaries */
  1707. numpages = min(ppblock - (startpage & (ppblock - 1)), numpages);
  1708. oobbuf = nand_prepare_oobbuf(mtd, NULL, oobsel, autoplace, numpages);
  1709. bufstart = (u_char *) vecs->iov_base;
  1710. bufstart += len;
  1711. this->data_poi = bufstart;
  1712. oob = 0;
  1713. for (i = 1; i <= numpages; i++) {
  1714. /* Write one page. If this is the last page to write
  1715. * then use the real pageprogram command, else select
  1716. * cached programming if supported by the chip.
  1717. */
  1718. ret = nand_write_page(mtd, this, page & this->pagemask,
  1719. &oobbuf[oob], oobsel, i != numpages);
  1720. if (ret)
  1721. goto out;
  1722. this->data_poi += mtd->oobblock;
  1723. len += mtd->oobblock;
  1724. oob += mtd->oobsize;
  1725. page++;
  1726. }
  1727. /* Check, if we have to switch to the next tuple */
  1728. if (len >= (int)vecs->iov_len) {
  1729. vecs++;
  1730. len = 0;
  1731. count--;
  1732. }
  1733. } else {
  1734. /* We must use the internal buffer, read data out of each
  1735. * tuple until we have a full page to write
  1736. */
  1737. int cnt = 0;
  1738. while (cnt < mtd->oobblock) {
  1739. if (vecs->iov_base != NULL && vecs->iov_len)
  1740. this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
  1741. /* Check, if we have to switch to the next tuple */
  1742. if (len >= (int)vecs->iov_len) {
  1743. vecs++;
  1744. len = 0;
  1745. count--;
  1746. }
  1747. }
  1748. this->pagebuf = page;
  1749. this->data_poi = this->data_buf;
  1750. bufstart = this->data_poi;
  1751. numpages = 1;
  1752. oobbuf = nand_prepare_oobbuf(mtd, NULL, oobsel, autoplace, numpages);
  1753. ret = nand_write_page(mtd, this, page & this->pagemask, oobbuf, oobsel, 0);
  1754. if (ret)
  1755. goto out;
  1756. page++;
  1757. }
  1758. this->data_poi = bufstart;
  1759. ret = nand_verify_pages(mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
  1760. if (ret)
  1761. goto out;
  1762. written += mtd->oobblock * numpages;
  1763. /* All done ? */
  1764. if (!count)
  1765. break;
  1766. startpage = page & this->pagemask;
  1767. /* Check, if we cross a chip boundary */
  1768. if (!startpage) {
  1769. chipnr++;
  1770. this->select_chip(mtd, -1);
  1771. this->select_chip(mtd, chipnr);
  1772. }
  1773. }
  1774. ret = 0;
  1775. out:
  1776. /* Deselect and wake up anyone waiting on the device */
  1777. nand_release_device(mtd);
  1778. *retlen = written;
  1779. return ret;
  1780. }
  1781. /**
  1782. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  1783. * @mtd: MTD device structure
  1784. * @page: the page address of the block which will be erased
  1785. *
  1786. * Standard erase command for NAND chips
  1787. */
  1788. static void single_erase_cmd(struct mtd_info *mtd, int page)
  1789. {
  1790. struct nand_chip *this = mtd->priv;
  1791. /* Send commands to erase a block */
  1792. this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1793. this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1794. }
  1795. /**
  1796. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  1797. * @mtd: MTD device structure
  1798. * @page: the page address of the block which will be erased
  1799. *
  1800. * AND multi block erase command function
  1801. * Erase 4 consecutive blocks
  1802. */
  1803. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  1804. {
  1805. struct nand_chip *this = mtd->priv;
  1806. /* Send commands to erase a block */
  1807. this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1808. this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1809. this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1810. this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1811. this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1812. }
  1813. /**
  1814. * nand_erase - [MTD Interface] erase block(s)
  1815. * @mtd: MTD device structure
  1816. * @instr: erase instruction
  1817. *
  1818. * Erase one ore more blocks
  1819. */
  1820. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1821. {
  1822. return nand_erase_nand(mtd, instr, 0);
  1823. }
  1824. #define BBT_PAGE_MASK 0xffffff3f
  1825. /**
  1826. * nand_erase_intern - [NAND Interface] erase block(s)
  1827. * @mtd: MTD device structure
  1828. * @instr: erase instruction
  1829. * @allowbbt: allow erasing the bbt area
  1830. *
  1831. * Erase one ore more blocks
  1832. */
  1833. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
  1834. {
  1835. int page, len, status, pages_per_block, ret, chipnr;
  1836. struct nand_chip *this = mtd->priv;
  1837. int rewrite_bbt[NAND_MAX_CHIPS]={0}; /* flags to indicate the page, if bbt needs to be rewritten. */
  1838. unsigned int bbt_masked_page; /* bbt mask to compare to page being erased. */
  1839. /* It is used to see if the current page is in the same */
  1840. /* 256 block group and the same bank as the bbt. */
  1841. DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n", (unsigned int)instr->addr, (unsigned int)instr->len);
  1842. /* Start address must align on block boundary */
  1843. if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
  1844. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
  1845. return -EINVAL;
  1846. }
  1847. /* Length must align on block boundary */
  1848. if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
  1849. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
  1850. return -EINVAL;
  1851. }
  1852. /* Do not allow erase past end of device */
  1853. if ((instr->len + instr->addr) > mtd->size) {
  1854. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
  1855. return -EINVAL;
  1856. }
  1857. instr->fail_addr = 0xffffffff;
  1858. /* Grab the lock and see if the device is available */
  1859. nand_get_device(this, mtd, FL_ERASING);
  1860. /* Shift to get first page */
  1861. page = (int)(instr->addr >> this->page_shift);
  1862. chipnr = (int)(instr->addr >> this->chip_shift);
  1863. /* Calculate pages in each block */
  1864. pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
  1865. /* Select the NAND device */
  1866. this->select_chip(mtd, chipnr);
  1867. /* Check the WP bit */
  1868. /* Check, if it is write protected */
  1869. if (nand_check_wp(mtd)) {
  1870. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
  1871. instr->state = MTD_ERASE_FAILED;
  1872. goto erase_exit;
  1873. }
  1874. /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */
  1875. if (this->options & BBT_AUTO_REFRESH) {
  1876. bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  1877. } else {
  1878. bbt_masked_page = 0xffffffff; /* should not match anything */
  1879. }
  1880. /* Loop through the pages */
  1881. len = instr->len;
  1882. instr->state = MTD_ERASING;
  1883. while (len) {
  1884. /* Check if we have a bad block, we do not erase bad blocks ! */
  1885. if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
  1886. printk(KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
  1887. instr->state = MTD_ERASE_FAILED;
  1888. goto erase_exit;
  1889. }
  1890. /* Invalidate the page cache, if we erase the block which contains
  1891. the current cached page */
  1892. if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
  1893. this->pagebuf = -1;
  1894. this->erase_cmd(mtd, page & this->pagemask);
  1895. status = this->waitfunc(mtd, this, FL_ERASING);
  1896. /* See if operation failed and additional status checks are available */
  1897. if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
  1898. status = this->errstat(mtd, this, FL_ERASING, status, page);
  1899. }
  1900. /* See if block erase succeeded */
  1901. if (status & NAND_STATUS_FAIL) {
  1902. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
  1903. instr->state = MTD_ERASE_FAILED;
  1904. instr->fail_addr = (page << this->page_shift);
  1905. goto erase_exit;
  1906. }
  1907. /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */
  1908. if (this->options & BBT_AUTO_REFRESH) {
  1909. if (((page & BBT_PAGE_MASK) == bbt_masked_page) &&
  1910. (page != this->bbt_td->pages[chipnr])) {
  1911. rewrite_bbt[chipnr] = (page << this->page_shift);
  1912. }
  1913. }
  1914. /* Increment page address and decrement length */
  1915. len -= (1 << this->phys_erase_shift);
  1916. page += pages_per_block;
  1917. /* Check, if we cross a chip boundary */
  1918. if (len && !(page & this->pagemask)) {
  1919. chipnr++;
  1920. this->select_chip(mtd, -1);
  1921. this->select_chip(mtd, chipnr);
  1922. /* if BBT requires refresh and BBT-PERCHIP,
  1923. * set the BBT page mask to see if this BBT should be rewritten */
  1924. if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) {
  1925. bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  1926. }
  1927. }
  1928. }
  1929. instr->state = MTD_ERASE_DONE;
  1930. erase_exit:
  1931. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1932. /* Do call back function */
  1933. if (!ret)
  1934. mtd_erase_callback(instr);
  1935. /* Deselect and wake up anyone waiting on the device */
  1936. nand_release_device(mtd);
  1937. /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */
  1938. if ((this->options & BBT_AUTO_REFRESH) && (!ret)) {
  1939. for (chipnr = 0; chipnr < this->numchips; chipnr++) {
  1940. if (rewrite_bbt[chipnr]) {
  1941. /* update the BBT for chip */
  1942. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n",
  1943. chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]);
  1944. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  1945. }
  1946. }
  1947. }
  1948. /* Return more or less happy */
  1949. return ret;
  1950. }
  1951. /**
  1952. * nand_sync - [MTD Interface] sync
  1953. * @mtd: MTD device structure
  1954. *
  1955. * Sync is actually a wait for chip ready function
  1956. */
  1957. static void nand_sync(struct mtd_info *mtd)
  1958. {
  1959. struct nand_chip *this = mtd->priv;
  1960. DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
  1961. /* Grab the lock and see if the device is available */
  1962. nand_get_device(this, mtd, FL_SYNCING);
  1963. /* Release it and go back */
  1964. nand_release_device(mtd);
  1965. }
  1966. /**
  1967. * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1968. * @mtd: MTD device structure
  1969. * @ofs: offset relative to mtd start
  1970. */
  1971. static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1972. {
  1973. /* Check for invalid offset */
  1974. if (ofs > mtd->size)
  1975. return -EINVAL;
  1976. return nand_block_checkbad(mtd, ofs, 1, 0);
  1977. }
  1978. /**
  1979. * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1980. * @mtd: MTD device structure
  1981. * @ofs: offset relative to mtd start
  1982. */
  1983. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1984. {
  1985. struct nand_chip *this = mtd->priv;
  1986. int ret;
  1987. if ((ret = nand_block_isbad(mtd, ofs))) {
  1988. /* If it was bad already, return success and do nothing. */
  1989. if (ret > 0)
  1990. return 0;
  1991. return ret;
  1992. }
  1993. return this->block_markbad(mtd, ofs);
  1994. }
  1995. /**
  1996. * nand_suspend - [MTD Interface] Suspend the NAND flash
  1997. * @mtd: MTD device structure
  1998. */
  1999. static int nand_suspend(struct mtd_info *mtd)
  2000. {
  2001. struct nand_chip *this = mtd->priv;
  2002. return nand_get_device(this, mtd, FL_PM_SUSPENDED);
  2003. }
  2004. /**
  2005. * nand_resume - [MTD Interface] Resume the NAND flash
  2006. * @mtd: MTD device structure
  2007. */
  2008. static void nand_resume(struct mtd_info *mtd)
  2009. {
  2010. struct nand_chip *this = mtd->priv;
  2011. if (this->state == FL_PM_SUSPENDED)
  2012. nand_release_device(mtd);
  2013. else
  2014. printk(KERN_ERR "resume() called for the chip which is not in suspended state\n");
  2015. }
  2016. /*
  2017. * Free allocated data structures
  2018. */
  2019. static void nand_free_kmem(struct nand_chip *this)
  2020. {
  2021. /* Buffer allocated by nand_scan ? */
  2022. if (this->options & NAND_OOBBUF_ALLOC)
  2023. kfree(this->oob_buf);
  2024. /* Buffer allocated by nand_scan ? */
  2025. if (this->options & NAND_DATABUF_ALLOC)
  2026. kfree(this->data_buf);
  2027. /* Controller allocated by nand_scan ? */
  2028. if (this->options & NAND_CONTROLLER_ALLOC)
  2029. kfree(this->controller);
  2030. }
  2031. /* module_text_address() isn't exported, and it's mostly a pointless
  2032. test if this is a module _anyway_ -- they'd have to try _really_ hard
  2033. to call us from in-kernel code if the core NAND support is modular. */
  2034. #ifdef MODULE
  2035. #define caller_is_module() (1)
  2036. #else
  2037. #define caller_is_module() module_text_address((unsigned long)__builtin_return_address(0))
  2038. #endif
  2039. /**
  2040. * nand_scan - [NAND Interface] Scan for the NAND device
  2041. * @mtd: MTD device structure
  2042. * @maxchips: Number of chips to scan for
  2043. *
  2044. * This fills out all the uninitialized function pointers
  2045. * with the defaults.
  2046. * The flash ID is read and the mtd/chip structures are
  2047. * filled with the appropriate values. Buffers are allocated if
  2048. * they are not provided by the board driver
  2049. * The mtd->owner field must be set to the module of the caller
  2050. *
  2051. */
  2052. int nand_scan(struct mtd_info *mtd, int maxchips)
  2053. {
  2054. int i, nand_maf_id, nand_dev_id, busw, maf_id;
  2055. struct nand_chip *this = mtd->priv;
  2056. /* Many callers got this wrong, so check for it for a while... */
  2057. if (!mtd->owner && caller_is_module()) {
  2058. printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
  2059. BUG();
  2060. }
  2061. /* Get buswidth to select the correct functions */
  2062. busw = this->options & NAND_BUSWIDTH_16;
  2063. /* check for proper chip_delay setup, set 20us if not */
  2064. if (!this->chip_delay)
  2065. this->chip_delay = 20;
  2066. /* check, if a user supplied command function given */
  2067. if (this->cmdfunc == NULL)
  2068. this->cmdfunc = nand_command;
  2069. /* check, if a user supplied wait function given */
  2070. if (this->waitfunc == NULL)
  2071. this->waitfunc = nand_wait;
  2072. if (!this->select_chip)
  2073. this->select_chip = nand_select_chip;
  2074. if (!this->write_byte)
  2075. this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
  2076. if (!this->read_byte)
  2077. this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2078. if (!this->write_word)
  2079. this->write_word = nand_write_word;
  2080. if (!this->read_word)
  2081. this->read_word = nand_read_word;
  2082. if (!this->block_bad)
  2083. this->block_bad = nand_block_bad;
  2084. if (!this->block_markbad)
  2085. this->block_markbad = nand_default_block_markbad;
  2086. if (!this->write_buf)
  2087. this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2088. if (!this->read_buf)
  2089. this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2090. if (!this->verify_buf)
  2091. this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2092. if (!this->scan_bbt)
  2093. this->scan_bbt = nand_default_bbt;
  2094. /* Select the device */
  2095. this->select_chip(mtd, 0);
  2096. /* Send the command for reading device ID */
  2097. this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2098. /* Read manufacturer and device IDs */
  2099. nand_maf_id = this->read_byte(mtd);
  2100. nand_dev_id = this->read_byte(mtd);
  2101. /* Print and store flash device information */
  2102. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  2103. if (nand_dev_id != nand_flash_ids[i].id)
  2104. continue;
  2105. if (!mtd->name)
  2106. mtd->name = nand_flash_ids[i].name;
  2107. this->chipsize = nand_flash_ids[i].chipsize << 20;
  2108. /* New devices have all the information in additional id bytes */
  2109. if (!nand_flash_ids[i].pagesize) {
  2110. int extid;
  2111. /* The 3rd id byte contains non relevant data ATM */
  2112. extid = this->read_byte(mtd);
  2113. /* The 4th id byte is the important one */
  2114. extid = this->read_byte(mtd);
  2115. /* Calc pagesize */
  2116. mtd->oobblock = 1024 << (extid & 0x3);
  2117. extid >>= 2;
  2118. /* Calc oobsize */
  2119. mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock >> 9);
  2120. extid >>= 2;
  2121. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2122. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2123. extid >>= 2;
  2124. /* Get buswidth information */
  2125. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2126. } else {
  2127. /* Old devices have this data hardcoded in the
  2128. * device id table */
  2129. mtd->erasesize = nand_flash_ids[i].erasesize;
  2130. mtd->oobblock = nand_flash_ids[i].pagesize;
  2131. mtd->oobsize = mtd->oobblock / 32;
  2132. busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
  2133. }
  2134. /* Try to identify manufacturer */
  2135. for (maf_id = 0; nand_manuf_ids[maf_id].id != 0x0; maf_id++) {
  2136. if (nand_manuf_ids[maf_id].id == nand_maf_id)
  2137. break;
  2138. }
  2139. /* Check, if buswidth is correct. Hardware drivers should set
  2140. * this correct ! */
  2141. if (busw != (this->options & NAND_BUSWIDTH_16)) {
  2142. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2143. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
  2144. nand_manuf_ids[maf_id].name, mtd->name);
  2145. printk(KERN_WARNING
  2146. "NAND bus width %d instead %d bit\n",
  2147. (this->options & NAND_BUSWIDTH_16) ? 16 : 8, busw ? 16 : 8);
  2148. this->select_chip(mtd, -1);
  2149. return 1;
  2150. }
  2151. /* Calculate the address shift from the page size */
  2152. this->page_shift = ffs(mtd->oobblock) - 1;
  2153. this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
  2154. this->chip_shift = ffs(this->chipsize) - 1;
  2155. /* Set the bad block position */
  2156. this->badblockpos = mtd->oobblock > 512 ? NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
  2157. /* Get chip options, preserve non chip based options */
  2158. this->options &= ~NAND_CHIPOPTIONS_MSK;
  2159. this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
  2160. /* Set this as a default. Board drivers can override it, if necessary */
  2161. this->options |= NAND_NO_AUTOINCR;
  2162. /* Check if this is a not a samsung device. Do not clear the options
  2163. * for chips which are not having an extended id.
  2164. */
  2165. if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
  2166. this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2167. /* Check for AND chips with 4 page planes */
  2168. if (this->options & NAND_4PAGE_ARRAY)
  2169. this->erase_cmd = multi_erase_cmd;
  2170. else
  2171. this->erase_cmd = single_erase_cmd;
  2172. /* Do not replace user supplied command function ! */
  2173. if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
  2174. this->cmdfunc = nand_command_lp;
  2175. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2176. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
  2177. nand_manuf_ids[maf_id].name, nand_flash_ids[i].name);
  2178. break;
  2179. }
  2180. if (!nand_flash_ids[i].name) {
  2181. printk(KERN_WARNING "No NAND device found!!!\n");
  2182. this->select_chip(mtd, -1);
  2183. return 1;
  2184. }
  2185. for (i = 1; i < maxchips; i++) {
  2186. this->select_chip(mtd, i);
  2187. /* Send the command for reading device ID */
  2188. this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2189. /* Read manufacturer and device IDs */
  2190. if (nand_maf_id != this->read_byte(mtd) ||
  2191. nand_dev_id != this->read_byte(mtd))
  2192. break;
  2193. }
  2194. if (i > 1)
  2195. printk(KERN_INFO "%d NAND chips detected\n", i);
  2196. /* Allocate buffers, if necessary */
  2197. if (!this->oob_buf) {
  2198. size_t len;
  2199. len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
  2200. this->oob_buf = kmalloc(len, GFP_KERNEL);
  2201. if (!this->oob_buf) {
  2202. printk(KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
  2203. return -ENOMEM;
  2204. }
  2205. this->options |= NAND_OOBBUF_ALLOC;
  2206. }
  2207. if (!this->data_buf) {
  2208. size_t len;
  2209. len = mtd->oobblock + mtd->oobsize;
  2210. this->data_buf = kmalloc(len, GFP_KERNEL);
  2211. if (!this->data_buf) {
  2212. printk(KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
  2213. nand_free_kmem(this);
  2214. return -ENOMEM;
  2215. }
  2216. this->options |= NAND_DATABUF_ALLOC;
  2217. }
  2218. /* Store the number of chips and calc total size for mtd */
  2219. this->numchips = i;
  2220. mtd->size = i * this->chipsize;
  2221. /* Convert chipsize to number of pages per chip -1. */
  2222. this->pagemask = (this->chipsize >> this->page_shift) - 1;
  2223. /* Preset the internal oob buffer */
  2224. memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
  2225. /* If no default placement scheme is given, select an
  2226. * appropriate one */
  2227. if (!this->autooob) {
  2228. /* Select the appropriate default oob placement scheme for
  2229. * placement agnostic filesystems */
  2230. switch (mtd->oobsize) {
  2231. case 8:
  2232. this->autooob = &nand_oob_8;
  2233. break;
  2234. case 16:
  2235. this->autooob = &nand_oob_16;
  2236. break;
  2237. case 64:
  2238. this->autooob = &nand_oob_64;
  2239. break;
  2240. default:
  2241. printk(KERN_WARNING "No oob scheme defined for oobsize %d\n", mtd->oobsize);
  2242. BUG();
  2243. }
  2244. }
  2245. /* The number of bytes available for the filesystem to place fs dependend
  2246. * oob data */
  2247. mtd->oobavail = 0;
  2248. for (i = 0; this->autooob->oobfree[i][1]; i++)
  2249. mtd->oobavail += this->autooob->oobfree[i][1];
  2250. /*
  2251. * check ECC mode, default to software
  2252. * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
  2253. * fallback to software ECC
  2254. */
  2255. this->eccsize = 256; /* set default eccsize */
  2256. this->eccbytes = 3;
  2257. switch (this->eccmode) {
  2258. case NAND_ECC_HW12_2048:
  2259. if (mtd->oobblock < 2048) {
  2260. printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
  2261. mtd->oobblock);
  2262. this->eccmode = NAND_ECC_SOFT;
  2263. this->calculate_ecc = nand_calculate_ecc;
  2264. this->correct_data = nand_correct_data;
  2265. } else
  2266. this->eccsize = 2048;
  2267. break;
  2268. case NAND_ECC_HW3_512:
  2269. case NAND_ECC_HW6_512:
  2270. case NAND_ECC_HW8_512:
  2271. if (mtd->oobblock == 256) {
  2272. printk(KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
  2273. this->eccmode = NAND_ECC_SOFT;
  2274. this->calculate_ecc = nand_calculate_ecc;
  2275. this->correct_data = nand_correct_data;
  2276. } else
  2277. this->eccsize = 512; /* set eccsize to 512 */
  2278. break;
  2279. case NAND_ECC_HW3_256:
  2280. break;
  2281. case NAND_ECC_NONE:
  2282. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
  2283. this->eccmode = NAND_ECC_NONE;
  2284. break;
  2285. case NAND_ECC_SOFT:
  2286. this->calculate_ecc = nand_calculate_ecc;
  2287. this->correct_data = nand_correct_data;
  2288. break;
  2289. default:
  2290. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
  2291. BUG();
  2292. }
  2293. /* Check hardware ecc function availability and adjust number of ecc bytes per
  2294. * calculation step
  2295. */
  2296. switch (this->eccmode) {
  2297. case NAND_ECC_HW12_2048:
  2298. this->eccbytes += 4;
  2299. case NAND_ECC_HW8_512:
  2300. this->eccbytes += 2;
  2301. case NAND_ECC_HW6_512:
  2302. this->eccbytes += 3;
  2303. case NAND_ECC_HW3_512:
  2304. case NAND_ECC_HW3_256:
  2305. if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
  2306. break;
  2307. printk(KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
  2308. BUG();
  2309. }
  2310. mtd->eccsize = this->eccsize;
  2311. /* Set the number of read / write steps for one page to ensure ECC generation */
  2312. switch (this->eccmode) {
  2313. case NAND_ECC_HW12_2048:
  2314. this->eccsteps = mtd->oobblock / 2048;
  2315. break;
  2316. case NAND_ECC_HW3_512:
  2317. case NAND_ECC_HW6_512:
  2318. case NAND_ECC_HW8_512:
  2319. this->eccsteps = mtd->oobblock / 512;
  2320. break;
  2321. case NAND_ECC_HW3_256:
  2322. case NAND_ECC_SOFT:
  2323. this->eccsteps = mtd->oobblock / 256;
  2324. break;
  2325. case NAND_ECC_NONE:
  2326. this->eccsteps = 1;
  2327. break;
  2328. }
  2329. /* Initialize state, waitqueue and spinlock */
  2330. this->state = FL_READY;
  2331. if (!this->controller) {
  2332. this->controller = kzalloc(sizeof(struct nand_hw_control),
  2333. GFP_KERNEL);
  2334. if (!this->controller) {
  2335. nand_free_kmem(this);
  2336. return -ENOMEM;
  2337. }
  2338. this->options |= NAND_CONTROLLER_ALLOC;
  2339. }
  2340. init_waitqueue_head(&this->controller->wq);
  2341. spin_lock_init(&this->controller->lock);
  2342. /* De-select the device */
  2343. this->select_chip(mtd, -1);
  2344. /* Invalidate the pagebuffer reference */
  2345. this->pagebuf = -1;
  2346. /* Fill in remaining MTD driver data */
  2347. mtd->type = MTD_NANDFLASH;
  2348. mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
  2349. mtd->ecctype = MTD_ECC_SW;
  2350. mtd->erase = nand_erase;
  2351. mtd->point = NULL;
  2352. mtd->unpoint = NULL;
  2353. mtd->read = nand_read;
  2354. mtd->write = nand_write;
  2355. mtd->read_ecc = nand_read_ecc;
  2356. mtd->write_ecc = nand_write_ecc;
  2357. mtd->read_oob = nand_read_oob;
  2358. mtd->write_oob = nand_write_oob;
  2359. mtd->readv = NULL;
  2360. mtd->writev = nand_writev;
  2361. mtd->writev_ecc = nand_writev_ecc;
  2362. mtd->sync = nand_sync;
  2363. mtd->lock = NULL;
  2364. mtd->unlock = NULL;
  2365. mtd->suspend = nand_suspend;
  2366. mtd->resume = nand_resume;
  2367. mtd->block_isbad = nand_block_isbad;
  2368. mtd->block_markbad = nand_block_markbad;
  2369. /* and make the autooob the default one */
  2370. memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
  2371. /* Check, if we should skip the bad block table scan */
  2372. if (this->options & NAND_SKIP_BBTSCAN)
  2373. return 0;
  2374. /* Build bad block table */
  2375. return this->scan_bbt(mtd);
  2376. }
  2377. /**
  2378. * nand_release - [NAND Interface] Free resources held by the NAND device
  2379. * @mtd: MTD device structure
  2380. */
  2381. void nand_release(struct mtd_info *mtd)
  2382. {
  2383. struct nand_chip *this = mtd->priv;
  2384. #ifdef CONFIG_MTD_PARTITIONS
  2385. /* Deregister partitions */
  2386. del_mtd_partitions(mtd);
  2387. #endif
  2388. /* Deregister the device */
  2389. del_mtd_device(mtd);
  2390. /* Free bad block table memory */
  2391. kfree(this->bbt);
  2392. /* Free buffers */
  2393. nand_free_kmem(this);
  2394. }
  2395. EXPORT_SYMBOL_GPL(nand_scan);
  2396. EXPORT_SYMBOL_GPL(nand_release);
  2397. static int __init nand_base_init(void)
  2398. {
  2399. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  2400. return 0;
  2401. }
  2402. static void __exit nand_base_exit(void)
  2403. {
  2404. led_trigger_unregister_simple(nand_led_trigger);
  2405. }
  2406. module_init(nand_base_init);
  2407. module_exit(nand_base_exit);
  2408. MODULE_LICENSE("GPL");
  2409. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
  2410. MODULE_DESCRIPTION("Generic NAND flash driver code");