nand_base.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548
  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->ecc.steps;
  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(chip))
  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_page_swecc - {REPLACABLE] software ecc based page read function
  873. * @mtd: mtd info structure
  874. * @chip: nand chip info structure
  875. * @buf: buffer to store read data
  876. */
  877. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  878. uint8_t *buf)
  879. {
  880. int i, eccsize = chip->ecc.size;
  881. int eccbytes = chip->ecc.bytes;
  882. int eccsteps = chip->ecc.steps;
  883. uint8_t *p = buf;
  884. uint8_t *ecc_calc = chip->oob_buf + mtd->oobsize;
  885. uint8_t *ecc_code = ecc_calc + mtd->oobsize;
  886. int *eccpos = chip->autooob->eccpos;
  887. chip->read_buf(mtd, buf, mtd->writesize);
  888. chip->read_buf(mtd, chip->oob_buf, mtd->oobsize);
  889. if (chip->ecc.mode == NAND_ECC_NONE)
  890. return 0;
  891. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  892. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  893. for (i = 0; i < chip->ecc.total; i++)
  894. ecc_code[i] = chip->oob_buf[eccpos[i]];
  895. eccsteps = chip->ecc.steps;
  896. p = buf;
  897. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  898. int stat;
  899. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  900. if (stat == -1)
  901. mtd->ecc_stats.failed++;
  902. else
  903. mtd->ecc_stats.corrected += stat;
  904. }
  905. return 0;
  906. }
  907. /**
  908. * nand_read_page_hwecc - {REPLACABLE] hardware ecc based page read function
  909. * @mtd: mtd info structure
  910. * @chip: nand chip info structure
  911. * @buf: buffer to store read data
  912. *
  913. * Not for syndrome calculating ecc controllers which need a special oob layout
  914. */
  915. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  916. uint8_t *buf)
  917. {
  918. int i, eccsize = chip->ecc.size;
  919. int eccbytes = chip->ecc.bytes;
  920. int eccsteps = chip->ecc.steps;
  921. uint8_t *p = buf;
  922. uint8_t *ecc_calc = chip->oob_buf + mtd->oobsize;
  923. uint8_t *ecc_code = ecc_calc + mtd->oobsize;
  924. int *eccpos = chip->autooob->eccpos;
  925. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  926. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  927. chip->read_buf(mtd, p, eccsize);
  928. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  929. }
  930. chip->read_buf(mtd, chip->oob_buf, mtd->oobsize);
  931. for (i = 0; i < chip->ecc.total; i++)
  932. ecc_code[i] = chip->oob_buf[eccpos[i]];
  933. eccsteps = chip->ecc.steps;
  934. p = buf;
  935. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  936. int stat;
  937. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  938. if (stat == -1)
  939. mtd->ecc_stats.failed++;
  940. else
  941. mtd->ecc_stats.corrected += stat;
  942. }
  943. return 0;
  944. }
  945. /**
  946. * nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
  947. * @mtd: mtd info structure
  948. * @chip: nand chip info structure
  949. * @buf: buffer to store read data
  950. *
  951. * The hw generator calculates the error syndrome automatically. Therefor
  952. * we need a special oob layout and .
  953. */
  954. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  955. uint8_t *buf)
  956. {
  957. int i, eccsize = chip->ecc.size;
  958. int eccbytes = chip->ecc.bytes;
  959. int eccsteps = chip->ecc.steps;
  960. uint8_t *p = buf;
  961. uint8_t *oob = chip->oob_buf;
  962. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  963. int stat;
  964. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  965. chip->read_buf(mtd, p, eccsize);
  966. if (chip->ecc.prepad) {
  967. chip->read_buf(mtd, oob, chip->ecc.prepad);
  968. oob += chip->ecc.prepad;
  969. }
  970. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  971. chip->read_buf(mtd, oob, eccbytes);
  972. stat = chip->ecc.correct(mtd, p, oob, NULL);
  973. if (stat == -1)
  974. mtd->ecc_stats.failed++;
  975. else
  976. mtd->ecc_stats.corrected += stat;
  977. oob += eccbytes;
  978. if (chip->ecc.postpad) {
  979. chip->read_buf(mtd, oob, chip->ecc.postpad);
  980. oob += chip->ecc.postpad;
  981. }
  982. }
  983. /* Calculate remaining oob bytes */
  984. i = oob - chip->oob_buf;
  985. if (i)
  986. chip->read_buf(mtd, oob, i);
  987. return 0;
  988. }
  989. /**
  990. * nand_do_read - [Internal] Read data with ECC
  991. *
  992. * @mtd: MTD device structure
  993. * @from: offset to read from
  994. * @len: number of bytes to read
  995. * @retlen: pointer to variable to store the number of read bytes
  996. * @buf: the databuffer to put data
  997. *
  998. * Internal function. Called with chip held.
  999. */
  1000. int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
  1001. size_t *retlen, uint8_t *buf)
  1002. {
  1003. int chipnr, page, realpage, col, bytes, aligned;
  1004. struct nand_chip *chip = mtd->priv;
  1005. struct mtd_ecc_stats stats;
  1006. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1007. int sndcmd = 1;
  1008. int ret = 0;
  1009. uint32_t readlen = len;
  1010. uint8_t *bufpoi;
  1011. stats = mtd->ecc_stats;
  1012. chipnr = (int)(from >> chip->chip_shift);
  1013. chip->select_chip(mtd, chipnr);
  1014. realpage = (int)(from >> chip->page_shift);
  1015. page = realpage & chip->pagemask;
  1016. col = (int)(from & (mtd->writesize - 1));
  1017. while(1) {
  1018. bytes = min(mtd->writesize - col, readlen);
  1019. aligned = (bytes == mtd->writesize);
  1020. /* Is the current page in the buffer ? */
  1021. if (realpage != chip->pagebuf) {
  1022. bufpoi = aligned ? buf : chip->data_buf;
  1023. if (likely(sndcmd)) {
  1024. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1025. sndcmd = 0;
  1026. }
  1027. /* Now read the page into the buffer */
  1028. ret = chip->ecc.read_page(mtd, chip, bufpoi);
  1029. if (ret < 0)
  1030. break;
  1031. /* Transfer not aligned data */
  1032. if (!aligned) {
  1033. chip->pagebuf = realpage;
  1034. memcpy(buf, chip->data_buf + col, bytes);
  1035. }
  1036. if (!(chip->options & NAND_NO_READRDY)) {
  1037. /*
  1038. * Apply delay or wait for ready/busy pin. Do
  1039. * this before the AUTOINCR check, so no
  1040. * problems arise if a chip which does auto
  1041. * increment is marked as NOAUTOINCR by the
  1042. * board driver.
  1043. */
  1044. if (!chip->dev_ready)
  1045. udelay(chip->chip_delay);
  1046. else
  1047. nand_wait_ready(mtd);
  1048. }
  1049. } else
  1050. memcpy(buf, chip->data_buf + col, bytes);
  1051. buf += bytes;
  1052. readlen -= bytes;
  1053. if (!readlen)
  1054. break;
  1055. /* For subsequent reads align to page boundary. */
  1056. col = 0;
  1057. /* Increment page address */
  1058. realpage++;
  1059. page = realpage & chip->pagemask;
  1060. /* Check, if we cross a chip boundary */
  1061. if (!page) {
  1062. chipnr++;
  1063. chip->select_chip(mtd, -1);
  1064. chip->select_chip(mtd, chipnr);
  1065. }
  1066. /* Check, if the chip supports auto page increment
  1067. * or if we have hit a block boundary.
  1068. */
  1069. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1070. sndcmd = 1;
  1071. }
  1072. *retlen = len - (size_t) readlen;
  1073. if (ret)
  1074. return ret;
  1075. return mtd->ecc_stats.failed - stats.failed ? -EBADMSG : 0;
  1076. }
  1077. /**
  1078. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  1079. * @mtd: MTD device structure
  1080. * @from: offset to read from
  1081. * @len: number of bytes to read
  1082. * @retlen: pointer to variable to store the number of read bytes
  1083. * @buf: the databuffer to put data
  1084. *
  1085. * Get hold of the chip and call nand_do_read
  1086. */
  1087. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1088. size_t *retlen, uint8_t *buf)
  1089. {
  1090. int ret;
  1091. *retlen = 0;
  1092. /* Do not allow reads past end of device */
  1093. if ((from + len) > mtd->size)
  1094. return -EINVAL;
  1095. if (!len)
  1096. return 0;
  1097. nand_get_device(mtd->priv, mtd, FL_READING);
  1098. ret = nand_do_read(mtd, from, len, retlen, buf);
  1099. nand_release_device(mtd);
  1100. return ret;
  1101. }
  1102. /**
  1103. * nand_read_oob - [MTD Interface] NAND read out-of-band
  1104. * @mtd: MTD device structure
  1105. * @from: offset to read from
  1106. * @len: number of bytes to read
  1107. * @retlen: pointer to variable to store the number of read bytes
  1108. * @buf: the databuffer to put data
  1109. *
  1110. * NAND read out-of-band data from the spare area
  1111. */
  1112. static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  1113. size_t *retlen, uint8_t *buf)
  1114. {
  1115. int col, page, realpage, chipnr, sndcmd = 1;
  1116. struct nand_chip *chip = mtd->priv;
  1117. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1118. int readlen = len;
  1119. DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
  1120. (unsigned int)from, (int)len);
  1121. /* Initialize return length value */
  1122. *retlen = 0;
  1123. /* Do not allow reads past end of device */
  1124. if ((from + len) > mtd->size) {
  1125. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1126. "Attempt read beyond end of device\n");
  1127. return -EINVAL;
  1128. }
  1129. nand_get_device(chip, mtd, FL_READING);
  1130. chipnr = (int)(from >> chip->chip_shift);
  1131. chip->select_chip(mtd, chipnr);
  1132. /* Shift to get page */
  1133. realpage = (int)(from >> chip->page_shift);
  1134. page = realpage & chip->pagemask;
  1135. /* Mask to get column */
  1136. col = from & (mtd->oobsize - 1);
  1137. while(1) {
  1138. int bytes = min((int)(mtd->oobsize - col), readlen);
  1139. if (likely(sndcmd)) {
  1140. chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
  1141. sndcmd = 0;
  1142. }
  1143. chip->read_buf(mtd, buf, bytes);
  1144. readlen -= bytes;
  1145. if (!readlen)
  1146. break;
  1147. if (!(chip->options & NAND_NO_READRDY)) {
  1148. /*
  1149. * Apply delay or wait for ready/busy pin. Do this
  1150. * before the AUTOINCR check, so no problems arise if a
  1151. * chip which does auto increment is marked as
  1152. * NOAUTOINCR by the board driver.
  1153. */
  1154. if (!chip->dev_ready)
  1155. udelay(chip->chip_delay);
  1156. else
  1157. nand_wait_ready(mtd);
  1158. }
  1159. buf += bytes;
  1160. bytes = mtd->oobsize;
  1161. col = 0;
  1162. /* Increment page address */
  1163. realpage++;
  1164. page = realpage & chip->pagemask;
  1165. /* Check, if we cross a chip boundary */
  1166. if (!page) {
  1167. chipnr++;
  1168. chip->select_chip(mtd, -1);
  1169. chip->select_chip(mtd, chipnr);
  1170. }
  1171. /* Check, if the chip supports auto page increment
  1172. * or if we have hit a block boundary.
  1173. */
  1174. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1175. sndcmd = 1;
  1176. }
  1177. /* Deselect and wake up anyone waiting on the device */
  1178. nand_release_device(mtd);
  1179. *retlen = len;
  1180. return 0;
  1181. }
  1182. /**
  1183. * nand_read_raw - [GENERIC] Read raw data including oob into buffer
  1184. * @mtd: MTD device structure
  1185. * @buf: temporary buffer
  1186. * @from: offset to read from
  1187. * @len: number of bytes to read
  1188. * @ooblen: number of oob data bytes to read
  1189. *
  1190. * Read raw data including oob into buffer
  1191. */
  1192. int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len,
  1193. size_t ooblen)
  1194. {
  1195. struct nand_chip *chip = mtd->priv;
  1196. int page = (int)(from >> chip->page_shift);
  1197. int chipnr = (int)(from >> chip->chip_shift);
  1198. int sndcmd = 1;
  1199. int cnt = 0;
  1200. int pagesize = mtd->writesize + mtd->oobsize;
  1201. int blockcheck;
  1202. /* Do not allow reads past end of device */
  1203. if ((from + len) > mtd->size) {
  1204. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: "
  1205. "Attempt read beyond end of device\n");
  1206. return -EINVAL;
  1207. }
  1208. /* Grab the lock and see if the device is available */
  1209. nand_get_device(chip, mtd, FL_READING);
  1210. chip->select_chip(mtd, chipnr);
  1211. /* Add requested oob length */
  1212. len += ooblen;
  1213. blockcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1214. while (len) {
  1215. if (sndcmd)
  1216. chip->cmdfunc(mtd, NAND_CMD_READ0, 0,
  1217. page & chip->pagemask);
  1218. sndcmd = 0;
  1219. chip->read_buf(mtd, &buf[cnt], pagesize);
  1220. len -= pagesize;
  1221. cnt += pagesize;
  1222. page++;
  1223. if (!chip->dev_ready)
  1224. udelay(chip->chip_delay);
  1225. else
  1226. nand_wait_ready(mtd);
  1227. /*
  1228. * Check, if the chip supports auto page increment or if we
  1229. * cross a block boundary.
  1230. */
  1231. if (!NAND_CANAUTOINCR(chip) || !(page & blockcheck))
  1232. sndcmd = 1;
  1233. }
  1234. /* Deselect and wake up anyone waiting on the device */
  1235. nand_release_device(mtd);
  1236. return 0;
  1237. }
  1238. /**
  1239. * nand_write_raw - [GENERIC] Write raw data including oob
  1240. * @mtd: MTD device structure
  1241. * @buf: source buffer
  1242. * @to: offset to write to
  1243. * @len: number of bytes to write
  1244. * @buf: source buffer
  1245. * @oob: oob buffer
  1246. *
  1247. * Write raw data including oob
  1248. */
  1249. int nand_write_raw(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  1250. uint8_t *buf, uint8_t *oob)
  1251. {
  1252. struct nand_chip *chip = mtd->priv;
  1253. int page = (int)(to >> chip->page_shift);
  1254. int chipnr = (int)(to >> chip->chip_shift);
  1255. int ret;
  1256. *retlen = 0;
  1257. /* Do not allow writes past end of device */
  1258. if ((to + len) > mtd->size) {
  1259. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt write "
  1260. "beyond end of device\n");
  1261. return -EINVAL;
  1262. }
  1263. /* Grab the lock and see if the device is available */
  1264. nand_get_device(chip, mtd, FL_WRITING);
  1265. chip->select_chip(mtd, chipnr);
  1266. chip->data_poi = buf;
  1267. while (len != *retlen) {
  1268. ret = nand_write_page(mtd, chip, page, oob, &mtd->oobinfo, 0);
  1269. if (ret)
  1270. return ret;
  1271. page++;
  1272. *retlen += mtd->writesize;
  1273. chip->data_poi += mtd->writesize;
  1274. oob += mtd->oobsize;
  1275. }
  1276. /* Deselect and wake up anyone waiting on the device */
  1277. nand_release_device(mtd);
  1278. return 0;
  1279. }
  1280. EXPORT_SYMBOL_GPL(nand_write_raw);
  1281. /**
  1282. * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
  1283. * @mtd: MTD device structure
  1284. * @fsbuf: buffer given by fs driver
  1285. * @oobsel: out of band selection structre
  1286. * @autoplace: 1 = place given buffer into the oob bytes
  1287. * @numpages: number of pages to prepare
  1288. *
  1289. * Return:
  1290. * 1. Filesystem buffer available and autoplacement is off,
  1291. * return filesystem buffer
  1292. * 2. No filesystem buffer or autoplace is off, return internal
  1293. * buffer
  1294. * 3. Filesystem buffer is given and autoplace selected
  1295. * put data from fs buffer into internal buffer and
  1296. * retrun internal buffer
  1297. *
  1298. * Note: The internal buffer is filled with 0xff. This must
  1299. * be done only once, when no autoplacement happens
  1300. * Autoplacement sets the buffer dirty flag, which
  1301. * forces the 0xff fill before using the buffer again.
  1302. *
  1303. */
  1304. static uint8_t *nand_prepare_oobbuf(struct mtd_info *mtd, uint8_t *fsbuf, struct nand_oobinfo *oobsel,
  1305. int autoplace, int numpages)
  1306. {
  1307. struct nand_chip *chip = mtd->priv;
  1308. int i, len, ofs;
  1309. /* Zero copy fs supplied buffer */
  1310. if (fsbuf && !autoplace)
  1311. return fsbuf;
  1312. /* Check, if the buffer must be filled with ff again */
  1313. if (chip->oobdirty) {
  1314. memset(chip->oob_buf, 0xff, mtd->oobsize << (chip->phys_erase_shift - chip->page_shift));
  1315. chip->oobdirty = 0;
  1316. }
  1317. /* If we have no autoplacement or no fs buffer use the internal one */
  1318. if (!autoplace || !fsbuf)
  1319. return chip->oob_buf;
  1320. /* Walk through the pages and place the data */
  1321. chip->oobdirty = 1;
  1322. ofs = 0;
  1323. while (numpages--) {
  1324. for (i = 0, len = 0; len < mtd->oobavail; i++) {
  1325. int to = ofs + oobsel->oobfree[i][0];
  1326. int num = oobsel->oobfree[i][1];
  1327. memcpy(&chip->oob_buf[to], fsbuf, num);
  1328. len += num;
  1329. fsbuf += num;
  1330. }
  1331. ofs += mtd->oobavail;
  1332. }
  1333. return chip->oob_buf;
  1334. }
  1335. #define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
  1336. /**
  1337. * nand_write - [MTD Interface] NAND write with ECC
  1338. * @mtd: MTD device structure
  1339. * @to: offset to write to
  1340. * @len: number of bytes to write
  1341. * @retlen: pointer to variable to store the number of written bytes
  1342. * @buf: the data to write
  1343. *
  1344. * NAND write with ECC
  1345. */
  1346. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1347. size_t *retlen, const uint8_t *buf)
  1348. {
  1349. int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
  1350. int autoplace = 0, numpages, totalpages;
  1351. struct nand_chip *chip = mtd->priv;
  1352. uint8_t *oobbuf, *bufstart, *eccbuf = NULL;
  1353. int ppblock = (1 << (chip->phys_erase_shift - chip->page_shift));
  1354. struct nand_oobinfo *oobsel = &mtd->oobinfo;
  1355. DEBUG(MTD_DEBUG_LEVEL3, "nand_write: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
  1356. /* Initialize retlen, in case of early exit */
  1357. *retlen = 0;
  1358. /* Do not allow write past end of device */
  1359. if ((to + len) > mtd->size) {
  1360. DEBUG(MTD_DEBUG_LEVEL0, "nand_write: Attempt to write past end of page\n");
  1361. return -EINVAL;
  1362. }
  1363. /* reject writes, which are not page aligned */
  1364. if (NOTALIGNED(to) || NOTALIGNED(len)) {
  1365. printk(KERN_NOTICE "nand_write: Attempt to write not page aligned data\n");
  1366. return -EINVAL;
  1367. }
  1368. /* Grab the lock and see if the device is available */
  1369. nand_get_device(chip, mtd, FL_WRITING);
  1370. /* Calculate chipnr */
  1371. chipnr = (int)(to >> chip->chip_shift);
  1372. /* Select the NAND device */
  1373. chip->select_chip(mtd, chipnr);
  1374. /* Check, if it is write protected */
  1375. if (nand_check_wp(mtd))
  1376. goto out;
  1377. /* Autoplace of oob data ? Use the default placement scheme */
  1378. if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
  1379. oobsel = chip->autooob;
  1380. autoplace = 1;
  1381. }
  1382. if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
  1383. autoplace = 1;
  1384. /* Setup variables and oob buffer */
  1385. totalpages = len >> chip->page_shift;
  1386. page = (int)(to >> chip->page_shift);
  1387. /* Invalidate the page cache, if we write to the cached page */
  1388. if (page <= chip->pagebuf && chip->pagebuf < (page + totalpages))
  1389. chip->pagebuf = -1;
  1390. /* Set it relative to chip */
  1391. page &= chip->pagemask;
  1392. startpage = page;
  1393. /* Calc number of pages we can write in one go */
  1394. numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages);
  1395. oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
  1396. bufstart = (uint8_t *) buf;
  1397. /* Loop until all data is written */
  1398. while (written < len) {
  1399. chip->data_poi = (uint8_t *) &buf[written];
  1400. /* Write one page. If this is the last page to write
  1401. * or the last page in this block, then use the
  1402. * real pageprogram command, else select cached programming
  1403. * if supported by the chip.
  1404. */
  1405. ret = nand_write_page(mtd, chip, page, &oobbuf[oob], oobsel, (--numpages > 0));
  1406. if (ret) {
  1407. DEBUG(MTD_DEBUG_LEVEL0, "nand_write: write_page failed %d\n", ret);
  1408. goto out;
  1409. }
  1410. /* Next oob page */
  1411. oob += mtd->oobsize;
  1412. /* Update written bytes count */
  1413. written += mtd->writesize;
  1414. if (written == len)
  1415. goto cmp;
  1416. /* Increment page address */
  1417. page++;
  1418. /* Have we hit a block boundary ? Then we have to verify and
  1419. * if verify is ok, we have to setup the oob buffer for
  1420. * the next pages.
  1421. */
  1422. if (!(page & (ppblock - 1))) {
  1423. int ofs;
  1424. chip->data_poi = bufstart;
  1425. ret = nand_verify_pages(mtd, chip, startpage, page - startpage,
  1426. oobbuf, oobsel, chipnr, (eccbuf != NULL));
  1427. if (ret) {
  1428. DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
  1429. goto out;
  1430. }
  1431. *retlen = written;
  1432. ofs = autoplace ? mtd->oobavail : mtd->oobsize;
  1433. if (eccbuf)
  1434. eccbuf += (page - startpage) * ofs;
  1435. totalpages -= page - startpage;
  1436. numpages = min(totalpages, ppblock);
  1437. page &= chip->pagemask;
  1438. startpage = page;
  1439. oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
  1440. oob = 0;
  1441. /* Check, if we cross a chip boundary */
  1442. if (!page) {
  1443. chipnr++;
  1444. chip->select_chip(mtd, -1);
  1445. chip->select_chip(mtd, chipnr);
  1446. }
  1447. }
  1448. }
  1449. /* Verify the remaining pages */
  1450. cmp:
  1451. chip->data_poi = bufstart;
  1452. ret = nand_verify_pages(mtd, chip, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL));
  1453. if (!ret)
  1454. *retlen = written;
  1455. else
  1456. DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
  1457. out:
  1458. /* Deselect and wake up anyone waiting on the device */
  1459. nand_release_device(mtd);
  1460. return ret;
  1461. }
  1462. /**
  1463. * nand_write_oob - [MTD Interface] NAND write out-of-band
  1464. * @mtd: MTD device structure
  1465. * @to: offset to write to
  1466. * @len: number of bytes to write
  1467. * @retlen: pointer to variable to store the number of written bytes
  1468. * @buf: the data to write
  1469. *
  1470. * NAND write out-of-band
  1471. */
  1472. static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  1473. size_t *retlen, const uint8_t *buf)
  1474. {
  1475. int column, page, status, ret = -EIO, chipnr;
  1476. struct nand_chip *chip = mtd->priv;
  1477. DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
  1478. (unsigned int)to, (int)len);
  1479. /* Initialize return length value */
  1480. *retlen = 0;
  1481. /* Do not allow write past end of page */
  1482. column = to & (mtd->oobsize - 1);
  1483. if ((column + len) > mtd->oobsize) {
  1484. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1485. "Attempt to write past end of page\n");
  1486. return -EINVAL;
  1487. }
  1488. nand_get_device(chip, mtd, FL_WRITING);
  1489. chipnr = (int)(to >> chip->chip_shift);
  1490. chip->select_chip(mtd, chipnr);
  1491. /* Shift to get page */
  1492. page = (int)(to >> chip->page_shift);
  1493. /*
  1494. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  1495. * of my DiskOnChip 2000 test units) will clear the whole data page too
  1496. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  1497. * it in the doc2000 driver in August 1999. dwmw2.
  1498. */
  1499. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1500. /* Check, if it is write protected */
  1501. if (nand_check_wp(mtd))
  1502. goto out;
  1503. /* Invalidate the page cache, if we write to the cached page */
  1504. if (page == chip->pagebuf)
  1505. chip->pagebuf = -1;
  1506. if (NAND_MUST_PAD(chip)) {
  1507. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize,
  1508. page & chip->pagemask);
  1509. /* prepad 0xff for partial programming */
  1510. chip->write_buf(mtd, ffchars, column);
  1511. /* write data */
  1512. chip->write_buf(mtd, buf, len);
  1513. /* postpad 0xff for partial programming */
  1514. chip->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
  1515. } else {
  1516. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column,
  1517. page & chip->pagemask);
  1518. chip->write_buf(mtd, buf, len);
  1519. }
  1520. /* Send command to program the OOB data */
  1521. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1522. status = chip->waitfunc(mtd, chip, FL_WRITING);
  1523. /* See if device thinks it succeeded */
  1524. if (status & NAND_STATUS_FAIL) {
  1525. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1526. "Failed write, page 0x%08x\n", page);
  1527. ret = -EIO;
  1528. goto out;
  1529. }
  1530. *retlen = len;
  1531. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1532. /* Send command to read back the data */
  1533. chip->cmdfunc(mtd, NAND_CMD_READOOB, column, page & chip->pagemask);
  1534. if (chip->verify_buf(mtd, buf, len)) {
  1535. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1536. "Failed write verify, page 0x%08x\n", page);
  1537. ret = -EIO;
  1538. goto out;
  1539. }
  1540. #endif
  1541. ret = 0;
  1542. out:
  1543. /* Deselect and wake up anyone waiting on the device */
  1544. nand_release_device(mtd);
  1545. return ret;
  1546. }
  1547. /**
  1548. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  1549. * @mtd: MTD device structure
  1550. * @page: the page address of the block which will be erased
  1551. *
  1552. * Standard erase command for NAND chips
  1553. */
  1554. static void single_erase_cmd(struct mtd_info *mtd, int page)
  1555. {
  1556. struct nand_chip *chip = mtd->priv;
  1557. /* Send commands to erase a block */
  1558. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1559. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1560. }
  1561. /**
  1562. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  1563. * @mtd: MTD device structure
  1564. * @page: the page address of the block which will be erased
  1565. *
  1566. * AND multi block erase command function
  1567. * Erase 4 consecutive blocks
  1568. */
  1569. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  1570. {
  1571. struct nand_chip *chip = mtd->priv;
  1572. /* Send commands to erase a block */
  1573. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1574. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1575. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1576. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1577. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1578. }
  1579. /**
  1580. * nand_erase - [MTD Interface] erase block(s)
  1581. * @mtd: MTD device structure
  1582. * @instr: erase instruction
  1583. *
  1584. * Erase one ore more blocks
  1585. */
  1586. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1587. {
  1588. return nand_erase_nand(mtd, instr, 0);
  1589. }
  1590. #define BBT_PAGE_MASK 0xffffff3f
  1591. /**
  1592. * nand_erase_nand - [Internal] erase block(s)
  1593. * @mtd: MTD device structure
  1594. * @instr: erase instruction
  1595. * @allowbbt: allow erasing the bbt area
  1596. *
  1597. * Erase one ore more blocks
  1598. */
  1599. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  1600. int allowbbt)
  1601. {
  1602. int page, len, status, pages_per_block, ret, chipnr;
  1603. struct nand_chip *chip = mtd->priv;
  1604. int rewrite_bbt[NAND_MAX_CHIPS]={0};
  1605. unsigned int bbt_masked_page = 0xffffffff;
  1606. DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
  1607. (unsigned int)instr->addr, (unsigned int)instr->len);
  1608. /* Start address must align on block boundary */
  1609. if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
  1610. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
  1611. return -EINVAL;
  1612. }
  1613. /* Length must align on block boundary */
  1614. if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
  1615. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1616. "Length not block aligned\n");
  1617. return -EINVAL;
  1618. }
  1619. /* Do not allow erase past end of device */
  1620. if ((instr->len + instr->addr) > mtd->size) {
  1621. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1622. "Erase past end of device\n");
  1623. return -EINVAL;
  1624. }
  1625. instr->fail_addr = 0xffffffff;
  1626. /* Grab the lock and see if the device is available */
  1627. nand_get_device(chip, mtd, FL_ERASING);
  1628. /* Shift to get first page */
  1629. page = (int)(instr->addr >> chip->page_shift);
  1630. chipnr = (int)(instr->addr >> chip->chip_shift);
  1631. /* Calculate pages in each block */
  1632. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  1633. /* Select the NAND device */
  1634. chip->select_chip(mtd, chipnr);
  1635. /* Check, if it is write protected */
  1636. if (nand_check_wp(mtd)) {
  1637. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1638. "Device is write protected!!!\n");
  1639. instr->state = MTD_ERASE_FAILED;
  1640. goto erase_exit;
  1641. }
  1642. /*
  1643. * If BBT requires refresh, set the BBT page mask to see if the BBT
  1644. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  1645. * can not be matched. This is also done when the bbt is actually
  1646. * erased to avoid recusrsive updates
  1647. */
  1648. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  1649. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  1650. /* Loop through the pages */
  1651. len = instr->len;
  1652. instr->state = MTD_ERASING;
  1653. while (len) {
  1654. /*
  1655. * heck if we have a bad block, we do not erase bad blocks !
  1656. */
  1657. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  1658. chip->page_shift, 0, allowbbt)) {
  1659. printk(KERN_WARNING "nand_erase: attempt to erase a "
  1660. "bad block at page 0x%08x\n", page);
  1661. instr->state = MTD_ERASE_FAILED;
  1662. goto erase_exit;
  1663. }
  1664. /*
  1665. * Invalidate the page cache, if we erase the block which
  1666. * contains the current cached page
  1667. */
  1668. if (page <= chip->pagebuf && chip->pagebuf <
  1669. (page + pages_per_block))
  1670. chip->pagebuf = -1;
  1671. chip->erase_cmd(mtd, page & chip->pagemask);
  1672. status = chip->waitfunc(mtd, chip, FL_ERASING);
  1673. /*
  1674. * See if operation failed and additional status checks are
  1675. * available
  1676. */
  1677. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1678. status = chip->errstat(mtd, chip, FL_ERASING,
  1679. status, page);
  1680. /* See if block erase succeeded */
  1681. if (status & NAND_STATUS_FAIL) {
  1682. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1683. "Failed erase, page 0x%08x\n", page);
  1684. instr->state = MTD_ERASE_FAILED;
  1685. instr->fail_addr = (page << chip->page_shift);
  1686. goto erase_exit;
  1687. }
  1688. /*
  1689. * If BBT requires refresh, set the BBT rewrite flag to the
  1690. * page being erased
  1691. */
  1692. if (bbt_masked_page != 0xffffffff &&
  1693. (page & BBT_PAGE_MASK) == bbt_masked_page)
  1694. rewrite_bbt[chipnr] = (page << chip->page_shift);
  1695. /* Increment page address and decrement length */
  1696. len -= (1 << chip->phys_erase_shift);
  1697. page += pages_per_block;
  1698. /* Check, if we cross a chip boundary */
  1699. if (len && !(page & chip->pagemask)) {
  1700. chipnr++;
  1701. chip->select_chip(mtd, -1);
  1702. chip->select_chip(mtd, chipnr);
  1703. /*
  1704. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  1705. * page mask to see if this BBT should be rewritten
  1706. */
  1707. if (bbt_masked_page != 0xffffffff &&
  1708. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  1709. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  1710. BBT_PAGE_MASK;
  1711. }
  1712. }
  1713. instr->state = MTD_ERASE_DONE;
  1714. erase_exit:
  1715. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1716. /* Do call back function */
  1717. if (!ret)
  1718. mtd_erase_callback(instr);
  1719. /* Deselect and wake up anyone waiting on the device */
  1720. nand_release_device(mtd);
  1721. /*
  1722. * If BBT requires refresh and erase was successful, rewrite any
  1723. * selected bad block tables
  1724. */
  1725. if (bbt_masked_page == 0xffffffff || ret)
  1726. return ret;
  1727. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  1728. if (!rewrite_bbt[chipnr])
  1729. continue;
  1730. /* update the BBT for chip */
  1731. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
  1732. "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
  1733. chip->bbt_td->pages[chipnr]);
  1734. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  1735. }
  1736. /* Return more or less happy */
  1737. return ret;
  1738. }
  1739. /**
  1740. * nand_sync - [MTD Interface] sync
  1741. * @mtd: MTD device structure
  1742. *
  1743. * Sync is actually a wait for chip ready function
  1744. */
  1745. static void nand_sync(struct mtd_info *mtd)
  1746. {
  1747. struct nand_chip *chip = mtd->priv;
  1748. DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
  1749. /* Grab the lock and see if the device is available */
  1750. nand_get_device(chip, mtd, FL_SYNCING);
  1751. /* Release it and go back */
  1752. nand_release_device(mtd);
  1753. }
  1754. /**
  1755. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  1756. * @mtd: MTD device structure
  1757. * @ofs: offset relative to mtd start
  1758. */
  1759. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  1760. {
  1761. /* Check for invalid offset */
  1762. if (offs > mtd->size)
  1763. return -EINVAL;
  1764. return nand_block_checkbad(mtd, offs, 1, 0);
  1765. }
  1766. /**
  1767. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  1768. * @mtd: MTD device structure
  1769. * @ofs: offset relative to mtd start
  1770. */
  1771. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1772. {
  1773. struct nand_chip *chip = mtd->priv;
  1774. int ret;
  1775. if ((ret = nand_block_isbad(mtd, ofs))) {
  1776. /* If it was bad already, return success and do nothing. */
  1777. if (ret > 0)
  1778. return 0;
  1779. return ret;
  1780. }
  1781. return chip->block_markbad(mtd, ofs);
  1782. }
  1783. /**
  1784. * nand_suspend - [MTD Interface] Suspend the NAND flash
  1785. * @mtd: MTD device structure
  1786. */
  1787. static int nand_suspend(struct mtd_info *mtd)
  1788. {
  1789. struct nand_chip *chip = mtd->priv;
  1790. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  1791. }
  1792. /**
  1793. * nand_resume - [MTD Interface] Resume the NAND flash
  1794. * @mtd: MTD device structure
  1795. */
  1796. static void nand_resume(struct mtd_info *mtd)
  1797. {
  1798. struct nand_chip *chip = mtd->priv;
  1799. if (chip->state == FL_PM_SUSPENDED)
  1800. nand_release_device(mtd);
  1801. else
  1802. printk(KERN_ERR "nand_resume() called for a chip which is not "
  1803. "in suspended state\n");
  1804. }
  1805. /*
  1806. * Free allocated data structures
  1807. */
  1808. static void nand_free_kmem(struct nand_chip *chip)
  1809. {
  1810. /* Buffer allocated by nand_scan ? */
  1811. if (chip->options & NAND_OOBBUF_ALLOC)
  1812. kfree(chip->oob_buf);
  1813. /* Buffer allocated by nand_scan ? */
  1814. if (chip->options & NAND_DATABUF_ALLOC)
  1815. kfree(chip->data_buf);
  1816. /* Controller allocated by nand_scan ? */
  1817. if (chip->options & NAND_CONTROLLER_ALLOC)
  1818. kfree(chip->controller);
  1819. }
  1820. /*
  1821. * Allocate buffers and data structures
  1822. */
  1823. static int nand_allocate_kmem(struct mtd_info *mtd, struct nand_chip *chip)
  1824. {
  1825. size_t len;
  1826. if (!chip->oob_buf) {
  1827. len = mtd->oobsize <<
  1828. (chip->phys_erase_shift - chip->page_shift);
  1829. chip->oob_buf = kmalloc(len, GFP_KERNEL);
  1830. if (!chip->oob_buf)
  1831. goto outerr;
  1832. chip->options |= NAND_OOBBUF_ALLOC;
  1833. }
  1834. if (!chip->data_buf) {
  1835. len = mtd->writesize + mtd->oobsize;
  1836. chip->data_buf = kmalloc(len, GFP_KERNEL);
  1837. if (!chip->data_buf)
  1838. goto outerr;
  1839. chip->options |= NAND_DATABUF_ALLOC;
  1840. }
  1841. if (!chip->controller) {
  1842. chip->controller = kzalloc(sizeof(struct nand_hw_control),
  1843. GFP_KERNEL);
  1844. if (!chip->controller)
  1845. goto outerr;
  1846. spin_lock_init(&chip->controller->lock);
  1847. init_waitqueue_head(&chip->controller->wq);
  1848. chip->options |= NAND_CONTROLLER_ALLOC;
  1849. }
  1850. return 0;
  1851. outerr:
  1852. printk(KERN_ERR "nand_scan(): Cannot allocate buffers\n");
  1853. nand_free_kmem(chip);
  1854. return -ENOMEM;
  1855. }
  1856. /*
  1857. * Set default functions
  1858. */
  1859. static void nand_set_defaults(struct nand_chip *chip, int busw)
  1860. {
  1861. /* check for proper chip_delay setup, set 20us if not */
  1862. if (!chip->chip_delay)
  1863. chip->chip_delay = 20;
  1864. /* check, if a user supplied command function given */
  1865. if (chip->cmdfunc == NULL)
  1866. chip->cmdfunc = nand_command;
  1867. /* check, if a user supplied wait function given */
  1868. if (chip->waitfunc == NULL)
  1869. chip->waitfunc = nand_wait;
  1870. if (!chip->select_chip)
  1871. chip->select_chip = nand_select_chip;
  1872. if (!chip->read_byte)
  1873. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  1874. if (!chip->read_word)
  1875. chip->read_word = nand_read_word;
  1876. if (!chip->block_bad)
  1877. chip->block_bad = nand_block_bad;
  1878. if (!chip->block_markbad)
  1879. chip->block_markbad = nand_default_block_markbad;
  1880. if (!chip->write_buf)
  1881. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  1882. if (!chip->read_buf)
  1883. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  1884. if (!chip->verify_buf)
  1885. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  1886. if (!chip->scan_bbt)
  1887. chip->scan_bbt = nand_default_bbt;
  1888. }
  1889. /*
  1890. * Get the flash and manufacturer id and lookup if the type is supported
  1891. */
  1892. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  1893. struct nand_chip *chip,
  1894. int busw, int *maf_id)
  1895. {
  1896. struct nand_flash_dev *type = NULL;
  1897. int i, dev_id, maf_idx;
  1898. /* Select the device */
  1899. chip->select_chip(mtd, 0);
  1900. /* Send the command for reading device ID */
  1901. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  1902. /* Read manufacturer and device IDs */
  1903. *maf_id = chip->read_byte(mtd);
  1904. dev_id = chip->read_byte(mtd);
  1905. /* Lookup the flash id */
  1906. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  1907. if (dev_id == nand_flash_ids[i].id) {
  1908. type = &nand_flash_ids[i];
  1909. break;
  1910. }
  1911. }
  1912. if (!type)
  1913. return ERR_PTR(-ENODEV);
  1914. chip->chipsize = nand_flash_ids[i].chipsize << 20;
  1915. /* Newer devices have all the information in additional id bytes */
  1916. if (!nand_flash_ids[i].pagesize) {
  1917. int extid;
  1918. /* The 3rd id byte contains non relevant data ATM */
  1919. extid = chip->read_byte(mtd);
  1920. /* The 4th id byte is the important one */
  1921. extid = chip->read_byte(mtd);
  1922. /* Calc pagesize */
  1923. mtd->writesize = 1024 << (extid & 0x3);
  1924. extid >>= 2;
  1925. /* Calc oobsize */
  1926. mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
  1927. extid >>= 2;
  1928. /* Calc blocksize. Blocksize is multiples of 64KiB */
  1929. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  1930. extid >>= 2;
  1931. /* Get buswidth information */
  1932. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  1933. } else {
  1934. /*
  1935. * Old devices have chip data hardcoded in the device id table
  1936. */
  1937. mtd->erasesize = nand_flash_ids[i].erasesize;
  1938. mtd->writesize = nand_flash_ids[i].pagesize;
  1939. mtd->oobsize = mtd->writesize / 32;
  1940. busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
  1941. }
  1942. /* Try to identify manufacturer */
  1943. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_id++) {
  1944. if (nand_manuf_ids[maf_idx].id == *maf_id)
  1945. break;
  1946. }
  1947. /*
  1948. * Check, if buswidth is correct. Hardware drivers should set
  1949. * chip correct !
  1950. */
  1951. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  1952. printk(KERN_INFO "NAND device: Manufacturer ID:"
  1953. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  1954. dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  1955. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  1956. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  1957. busw ? 16 : 8);
  1958. return ERR_PTR(-EINVAL);
  1959. }
  1960. /* Calculate the address shift from the page size */
  1961. chip->page_shift = ffs(mtd->writesize) - 1;
  1962. /* Convert chipsize to number of pages per chip -1. */
  1963. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  1964. chip->bbt_erase_shift = chip->phys_erase_shift =
  1965. ffs(mtd->erasesize) - 1;
  1966. chip->chip_shift = ffs(chip->chipsize) - 1;
  1967. /* Set the bad block position */
  1968. chip->badblockpos = mtd->writesize > 512 ?
  1969. NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
  1970. /* Get chip options, preserve non chip based options */
  1971. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  1972. chip->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
  1973. /*
  1974. * Set chip as a default. Board drivers can override it, if necessary
  1975. */
  1976. chip->options |= NAND_NO_AUTOINCR;
  1977. /* Check if chip is a not a samsung device. Do not clear the
  1978. * options for chips which are not having an extended id.
  1979. */
  1980. if (*maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
  1981. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  1982. /* Check for AND chips with 4 page planes */
  1983. if (chip->options & NAND_4PAGE_ARRAY)
  1984. chip->erase_cmd = multi_erase_cmd;
  1985. else
  1986. chip->erase_cmd = single_erase_cmd;
  1987. /* Do not replace user supplied command function ! */
  1988. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  1989. chip->cmdfunc = nand_command_lp;
  1990. printk(KERN_INFO "NAND device: Manufacturer ID:"
  1991. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
  1992. nand_manuf_ids[maf_idx].name, type->name);
  1993. return type;
  1994. }
  1995. /* module_text_address() isn't exported, and it's mostly a pointless
  1996. test if this is a module _anyway_ -- they'd have to try _really_ hard
  1997. to call us from in-kernel code if the core NAND support is modular. */
  1998. #ifdef MODULE
  1999. #define caller_is_module() (1)
  2000. #else
  2001. #define caller_is_module() \
  2002. module_text_address((unsigned long)__builtin_return_address(0))
  2003. #endif
  2004. /**
  2005. * nand_scan - [NAND Interface] Scan for the NAND device
  2006. * @mtd: MTD device structure
  2007. * @maxchips: Number of chips to scan for
  2008. *
  2009. * This fills out all the uninitialized function pointers
  2010. * with the defaults.
  2011. * The flash ID is read and the mtd/chip structures are
  2012. * filled with the appropriate values. Buffers are allocated if
  2013. * they are not provided by the board driver
  2014. * The mtd->owner field must be set to the module of the caller
  2015. *
  2016. */
  2017. int nand_scan(struct mtd_info *mtd, int maxchips)
  2018. {
  2019. int i, busw, nand_maf_id;
  2020. struct nand_chip *chip = mtd->priv;
  2021. struct nand_flash_dev *type;
  2022. /* Many callers got this wrong, so check for it for a while... */
  2023. if (!mtd->owner && caller_is_module()) {
  2024. printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
  2025. BUG();
  2026. }
  2027. /* Get buswidth to select the correct functions */
  2028. busw = chip->options & NAND_BUSWIDTH_16;
  2029. /* Set the default functions */
  2030. nand_set_defaults(chip, busw);
  2031. /* Read the flash type */
  2032. type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
  2033. if (IS_ERR(type)) {
  2034. printk(KERN_WARNING "No NAND device found!!!\n");
  2035. chip->select_chip(mtd, -1);
  2036. return PTR_ERR(type);
  2037. }
  2038. /* Check for a chip array */
  2039. for (i = 1; i < maxchips; i++) {
  2040. chip->select_chip(mtd, i);
  2041. /* Send the command for reading device ID */
  2042. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2043. /* Read manufacturer and device IDs */
  2044. if (nand_maf_id != chip->read_byte(mtd) ||
  2045. type->id != chip->read_byte(mtd))
  2046. break;
  2047. }
  2048. if (i > 1)
  2049. printk(KERN_INFO "%d NAND chips detected\n", i);
  2050. /* Store the number of chips and calc total size for mtd */
  2051. chip->numchips = i;
  2052. mtd->size = i * chip->chipsize;
  2053. /* Allocate buffers and data structures */
  2054. if (nand_allocate_kmem(mtd, chip))
  2055. return -ENOMEM;
  2056. /* Preset the internal oob buffer */
  2057. memset(chip->oob_buf, 0xff,
  2058. mtd->oobsize << (chip->phys_erase_shift - chip->page_shift));
  2059. /*
  2060. * If no default placement scheme is given, select an appropriate one
  2061. */
  2062. if (!chip->autooob) {
  2063. switch (mtd->oobsize) {
  2064. case 8:
  2065. chip->autooob = &nand_oob_8;
  2066. break;
  2067. case 16:
  2068. chip->autooob = &nand_oob_16;
  2069. break;
  2070. case 64:
  2071. chip->autooob = &nand_oob_64;
  2072. break;
  2073. default:
  2074. printk(KERN_WARNING "No oob scheme defined for "
  2075. "oobsize %d\n", mtd->oobsize);
  2076. BUG();
  2077. }
  2078. }
  2079. /*
  2080. * The number of bytes available for the filesystem to place fs
  2081. * dependend oob data
  2082. */
  2083. mtd->oobavail = 0;
  2084. for (i = 0; chip->autooob->oobfree[i][1]; i++)
  2085. mtd->oobavail += chip->autooob->oobfree[i][1];
  2086. /*
  2087. * check ECC mode, default to software if 3byte/512byte hardware ECC is
  2088. * selected and we have 256 byte pagesize fallback to software ECC
  2089. */
  2090. switch (chip->ecc.mode) {
  2091. case NAND_ECC_HW:
  2092. /* Use standard hwecc read page function ? */
  2093. if (!chip->ecc.read_page)
  2094. chip->ecc.read_page = nand_read_page_hwecc;
  2095. case NAND_ECC_HW_SYNDROME:
  2096. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2097. !chip->ecc.hwctl) {
  2098. printk(KERN_WARNING "No ECC functions supplied, "
  2099. "Hardware ECC not possible\n");
  2100. BUG();
  2101. }
  2102. /* Use standard syndrome read page function ? */
  2103. if (!chip->ecc.read_page)
  2104. chip->ecc.read_page = nand_read_page_syndrome;
  2105. if (mtd->writesize >= chip->ecc.size)
  2106. break;
  2107. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2108. "%d byte page size, fallback to SW ECC\n",
  2109. chip->ecc.size, mtd->writesize);
  2110. chip->ecc.mode = NAND_ECC_SOFT;
  2111. case NAND_ECC_SOFT:
  2112. chip->ecc.calculate = nand_calculate_ecc;
  2113. chip->ecc.correct = nand_correct_data;
  2114. chip->ecc.read_page = nand_read_page_swecc;
  2115. chip->ecc.size = 256;
  2116. chip->ecc.bytes = 3;
  2117. break;
  2118. case NAND_ECC_NONE:
  2119. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2120. "This is not recommended !!\n");
  2121. chip->ecc.read_page = nand_read_page_swecc;
  2122. chip->ecc.size = mtd->writesize;
  2123. chip->ecc.bytes = 0;
  2124. break;
  2125. default:
  2126. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2127. chip->ecc.mode);
  2128. BUG();
  2129. }
  2130. /*
  2131. * Set the number of read / write steps for one page depending on ECC
  2132. * mode
  2133. */
  2134. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2135. if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2136. printk(KERN_WARNING "Invalid ecc parameters\n");
  2137. BUG();
  2138. }
  2139. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2140. /* Initialize state */
  2141. chip->state = FL_READY;
  2142. /* De-select the device */
  2143. chip->select_chip(mtd, -1);
  2144. /* Invalidate the pagebuffer reference */
  2145. chip->pagebuf = -1;
  2146. /* Fill in remaining MTD driver data */
  2147. mtd->type = MTD_NANDFLASH;
  2148. mtd->flags = MTD_CAP_NANDFLASH;
  2149. mtd->ecctype = MTD_ECC_SW;
  2150. mtd->erase = nand_erase;
  2151. mtd->point = NULL;
  2152. mtd->unpoint = NULL;
  2153. mtd->read = nand_read;
  2154. mtd->write = nand_write;
  2155. mtd->read_oob = nand_read_oob;
  2156. mtd->write_oob = nand_write_oob;
  2157. mtd->sync = nand_sync;
  2158. mtd->lock = NULL;
  2159. mtd->unlock = NULL;
  2160. mtd->suspend = nand_suspend;
  2161. mtd->resume = nand_resume;
  2162. mtd->block_isbad = nand_block_isbad;
  2163. mtd->block_markbad = nand_block_markbad;
  2164. /* and make the autooob the default one */
  2165. memcpy(&mtd->oobinfo, chip->autooob, sizeof(mtd->oobinfo));
  2166. /* Check, if we should skip the bad block table scan */
  2167. if (chip->options & NAND_SKIP_BBTSCAN)
  2168. return 0;
  2169. /* Build bad block table */
  2170. return chip->scan_bbt(mtd);
  2171. }
  2172. /**
  2173. * nand_release - [NAND Interface] Free resources held by the NAND device
  2174. * @mtd: MTD device structure
  2175. */
  2176. void nand_release(struct mtd_info *mtd)
  2177. {
  2178. struct nand_chip *chip = mtd->priv;
  2179. #ifdef CONFIG_MTD_PARTITIONS
  2180. /* Deregister partitions */
  2181. del_mtd_partitions(mtd);
  2182. #endif
  2183. /* Deregister the device */
  2184. del_mtd_device(mtd);
  2185. /* Free bad block table memory */
  2186. kfree(chip->bbt);
  2187. /* Free buffers */
  2188. nand_free_kmem(chip);
  2189. }
  2190. EXPORT_SYMBOL_GPL(nand_scan);
  2191. EXPORT_SYMBOL_GPL(nand_release);
  2192. static int __init nand_base_init(void)
  2193. {
  2194. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  2195. return 0;
  2196. }
  2197. static void __exit nand_base_exit(void)
  2198. {
  2199. led_trigger_unregister_simple(nand_led_trigger);
  2200. }
  2201. module_init(nand_base_init);
  2202. module_exit(nand_base_exit);
  2203. MODULE_LICENSE("GPL");
  2204. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
  2205. MODULE_DESCRIPTION("Generic NAND flash driver code");