nand_base.c 74 KB

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