nand_base.c 84 KB

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