nand_base.c 69 KB

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