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