nand_base.c 83 KB

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