nand_base.c 70 KB

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