nand_base.c 71 KB

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