nand_base.c 97 KB

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