nand_base.c 78 KB

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