nand_base.c 92 KB

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