nand_base.c 93 KB

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