nand_base.c 75 KB

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