nand_base.c 75 KB

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