nand_base.c 74 KB

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