nand_base.c 79 KB

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