nand_base.c 70 KB

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