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