nand_base.c 73 KB

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