nand_base.c 73 KB

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