nand_base.c 76 KB

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