nand_base.c 75 KB

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