nand_base.c 70 KB

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