nand_base.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177
  1. /*
  2. * Overview:
  3. * This is the generic MTD driver for NAND flash devices. It should be
  4. * capable of working with almost all NAND chips currently available.
  5. *
  6. * Additional technical information is available on
  7. * http://www.linux-mtd.infradead.org/doc/nand.html
  8. *
  9. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  10. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  11. *
  12. * Credits:
  13. * David Woodhouse for adding multichip support
  14. *
  15. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  16. * rework for 2K page size chips
  17. *
  18. * TODO:
  19. * Enable cached programming for 2k page size chips
  20. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  21. * if we have HW ECC support.
  22. * BBT table is not serialized, has to be fixed
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License version 2 as
  26. * published by the Free Software Foundation.
  27. *
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <common.h>
  31. #include <malloc.h>
  32. #include <watchdog.h>
  33. #include <linux/err.h>
  34. #include <linux/compat.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/nand.h>
  37. #include <linux/mtd/nand_ecc.h>
  38. #include <linux/mtd/nand_bch.h>
  39. #ifdef CONFIG_MTD_PARTITIONS
  40. #include <linux/mtd/partitions.h>
  41. #endif
  42. #include <asm/io.h>
  43. #include <asm/errno.h>
  44. /* Define default oob placement schemes for large and small page devices */
  45. static struct nand_ecclayout nand_oob_8 = {
  46. .eccbytes = 3,
  47. .eccpos = {0, 1, 2},
  48. .oobfree = {
  49. {.offset = 3,
  50. .length = 2},
  51. {.offset = 6,
  52. .length = 2} }
  53. };
  54. static struct nand_ecclayout nand_oob_16 = {
  55. .eccbytes = 6,
  56. .eccpos = {0, 1, 2, 3, 6, 7},
  57. .oobfree = {
  58. {.offset = 8,
  59. . length = 8} }
  60. };
  61. static struct nand_ecclayout nand_oob_64 = {
  62. .eccbytes = 24,
  63. .eccpos = {
  64. 40, 41, 42, 43, 44, 45, 46, 47,
  65. 48, 49, 50, 51, 52, 53, 54, 55,
  66. 56, 57, 58, 59, 60, 61, 62, 63},
  67. .oobfree = {
  68. {.offset = 2,
  69. .length = 38} }
  70. };
  71. static struct nand_ecclayout nand_oob_128 = {
  72. .eccbytes = 48,
  73. .eccpos = {
  74. 80, 81, 82, 83, 84, 85, 86, 87,
  75. 88, 89, 90, 91, 92, 93, 94, 95,
  76. 96, 97, 98, 99, 100, 101, 102, 103,
  77. 104, 105, 106, 107, 108, 109, 110, 111,
  78. 112, 113, 114, 115, 116, 117, 118, 119,
  79. 120, 121, 122, 123, 124, 125, 126, 127},
  80. .oobfree = {
  81. {.offset = 2,
  82. .length = 78} }
  83. };
  84. static int nand_get_device(struct mtd_info *mtd, int new_state);
  85. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  86. struct mtd_oob_ops *ops);
  87. /*
  88. * For devices which display every fart in the system on a separate LED. Is
  89. * compiled away when LED support is disabled.
  90. */
  91. DEFINE_LED_TRIGGER(nand_led_trigger);
  92. static int check_offs_len(struct mtd_info *mtd,
  93. loff_t ofs, uint64_t len)
  94. {
  95. struct nand_chip *chip = mtd_to_nand(mtd);
  96. int ret = 0;
  97. /* Start address must align on block boundary */
  98. if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
  99. pr_debug("%s: unaligned address\n", __func__);
  100. ret = -EINVAL;
  101. }
  102. /* Length must align on block boundary */
  103. if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
  104. pr_debug("%s: length not block aligned\n", __func__);
  105. ret = -EINVAL;
  106. }
  107. return ret;
  108. }
  109. /**
  110. * nand_release_device - [GENERIC] release chip
  111. * @mtd: MTD device structure
  112. *
  113. * Release chip lock and wake up anyone waiting on the device.
  114. */
  115. static void nand_release_device(struct mtd_info *mtd)
  116. {
  117. struct nand_chip *chip = mtd_to_nand(mtd);
  118. /* De-select the NAND device */
  119. chip->select_chip(mtd, -1);
  120. }
  121. /**
  122. * nand_read_byte - [DEFAULT] read one byte from the chip
  123. * @mtd: MTD device structure
  124. *
  125. * Default read function for 8bit buswidth
  126. */
  127. uint8_t nand_read_byte(struct mtd_info *mtd)
  128. {
  129. struct nand_chip *chip = mtd_to_nand(mtd);
  130. return readb(chip->IO_ADDR_R);
  131. }
  132. /**
  133. * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  134. * @mtd: MTD device structure
  135. *
  136. * Default read function for 16bit buswidth with endianness conversion.
  137. *
  138. */
  139. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  140. {
  141. struct nand_chip *chip = mtd_to_nand(mtd);
  142. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  143. }
  144. /**
  145. * nand_read_word - [DEFAULT] read one word from the chip
  146. * @mtd: MTD device structure
  147. *
  148. * Default read function for 16bit buswidth without endianness conversion.
  149. */
  150. static u16 nand_read_word(struct mtd_info *mtd)
  151. {
  152. struct nand_chip *chip = mtd_to_nand(mtd);
  153. return readw(chip->IO_ADDR_R);
  154. }
  155. /**
  156. * nand_select_chip - [DEFAULT] control CE line
  157. * @mtd: MTD device structure
  158. * @chipnr: chipnumber to select, -1 for deselect
  159. *
  160. * Default select function for 1 chip devices.
  161. */
  162. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  163. {
  164. struct nand_chip *chip = mtd_to_nand(mtd);
  165. switch (chipnr) {
  166. case -1:
  167. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  168. break;
  169. case 0:
  170. break;
  171. default:
  172. BUG();
  173. }
  174. }
  175. /**
  176. * nand_write_byte - [DEFAULT] write single byte to chip
  177. * @mtd: MTD device structure
  178. * @byte: value to write
  179. *
  180. * Default function to write a byte to I/O[7:0]
  181. */
  182. static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
  183. {
  184. struct nand_chip *chip = mtd_to_nand(mtd);
  185. chip->write_buf(mtd, &byte, 1);
  186. }
  187. /**
  188. * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
  189. * @mtd: MTD device structure
  190. * @byte: value to write
  191. *
  192. * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
  193. */
  194. static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
  195. {
  196. struct nand_chip *chip = mtd_to_nand(mtd);
  197. uint16_t word = byte;
  198. /*
  199. * It's not entirely clear what should happen to I/O[15:8] when writing
  200. * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
  201. *
  202. * When the host supports a 16-bit bus width, only data is
  203. * transferred at the 16-bit width. All address and command line
  204. * transfers shall use only the lower 8-bits of the data bus. During
  205. * command transfers, the host may place any value on the upper
  206. * 8-bits of the data bus. During address transfers, the host shall
  207. * set the upper 8-bits of the data bus to 00h.
  208. *
  209. * One user of the write_byte callback is nand_onfi_set_features. The
  210. * four parameters are specified to be written to I/O[7:0], but this is
  211. * neither an address nor a command transfer. Let's assume a 0 on the
  212. * upper I/O lines is OK.
  213. */
  214. chip->write_buf(mtd, (uint8_t *)&word, 2);
  215. }
  216. #if !defined(CONFIG_BLACKFIN)
  217. static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
  218. {
  219. int i;
  220. for (i = 0; i < len; i++)
  221. writeb(buf[i], addr);
  222. }
  223. static void ioread8_rep(void *addr, uint8_t *buf, int len)
  224. {
  225. int i;
  226. for (i = 0; i < len; i++)
  227. buf[i] = readb(addr);
  228. }
  229. static void ioread16_rep(void *addr, void *buf, int len)
  230. {
  231. int i;
  232. u16 *p = (u16 *) buf;
  233. for (i = 0; i < len; i++)
  234. p[i] = readw(addr);
  235. }
  236. static void iowrite16_rep(void *addr, void *buf, int len)
  237. {
  238. int i;
  239. u16 *p = (u16 *) buf;
  240. for (i = 0; i < len; i++)
  241. writew(p[i], addr);
  242. }
  243. #endif
  244. /**
  245. * nand_write_buf - [DEFAULT] write buffer to chip
  246. * @mtd: MTD device structure
  247. * @buf: data buffer
  248. * @len: number of bytes to write
  249. *
  250. * Default write function for 8bit buswidth.
  251. */
  252. void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  253. {
  254. struct nand_chip *chip = mtd_to_nand(mtd);
  255. iowrite8_rep(chip->IO_ADDR_W, buf, len);
  256. }
  257. /**
  258. * nand_read_buf - [DEFAULT] read chip data into buffer
  259. * @mtd: MTD device structure
  260. * @buf: buffer to store date
  261. * @len: number of bytes to read
  262. *
  263. * Default read function for 8bit buswidth.
  264. */
  265. void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  266. {
  267. struct nand_chip *chip = mtd_to_nand(mtd);
  268. ioread8_rep(chip->IO_ADDR_R, buf, len);
  269. }
  270. /**
  271. * nand_write_buf16 - [DEFAULT] write buffer to chip
  272. * @mtd: MTD device structure
  273. * @buf: data buffer
  274. * @len: number of bytes to write
  275. *
  276. * Default write function for 16bit buswidth.
  277. */
  278. void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  279. {
  280. struct nand_chip *chip = mtd_to_nand(mtd);
  281. u16 *p = (u16 *) buf;
  282. iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
  283. }
  284. /**
  285. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  286. * @mtd: MTD device structure
  287. * @buf: buffer to store date
  288. * @len: number of bytes to read
  289. *
  290. * Default read function for 16bit buswidth.
  291. */
  292. void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  293. {
  294. struct nand_chip *chip = mtd_to_nand(mtd);
  295. u16 *p = (u16 *) buf;
  296. ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
  297. }
  298. /**
  299. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  300. * @mtd: MTD device structure
  301. * @ofs: offset from device start
  302. *
  303. * Check, if the block is bad.
  304. */
  305. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
  306. {
  307. int page, res = 0, i = 0;
  308. struct nand_chip *chip = mtd_to_nand(mtd);
  309. u16 bad;
  310. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  311. ofs += mtd->erasesize - mtd->writesize;
  312. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  313. do {
  314. if (chip->options & NAND_BUSWIDTH_16) {
  315. chip->cmdfunc(mtd, NAND_CMD_READOOB,
  316. chip->badblockpos & 0xFE, page);
  317. bad = cpu_to_le16(chip->read_word(mtd));
  318. if (chip->badblockpos & 0x1)
  319. bad >>= 8;
  320. else
  321. bad &= 0xFF;
  322. } else {
  323. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
  324. page);
  325. bad = chip->read_byte(mtd);
  326. }
  327. if (likely(chip->badblockbits == 8))
  328. res = bad != 0xFF;
  329. else
  330. res = hweight8(bad) < chip->badblockbits;
  331. ofs += mtd->writesize;
  332. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  333. i++;
  334. } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
  335. return res;
  336. }
  337. /**
  338. * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
  339. * @mtd: MTD device structure
  340. * @ofs: offset from device start
  341. *
  342. * This is the default implementation, which can be overridden by a hardware
  343. * specific driver. It provides the details for writing a bad block marker to a
  344. * block.
  345. */
  346. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  347. {
  348. struct nand_chip *chip = mtd_to_nand(mtd);
  349. struct mtd_oob_ops ops;
  350. uint8_t buf[2] = { 0, 0 };
  351. int ret = 0, res, i = 0;
  352. memset(&ops, 0, sizeof(ops));
  353. ops.oobbuf = buf;
  354. ops.ooboffs = chip->badblockpos;
  355. if (chip->options & NAND_BUSWIDTH_16) {
  356. ops.ooboffs &= ~0x01;
  357. ops.len = ops.ooblen = 2;
  358. } else {
  359. ops.len = ops.ooblen = 1;
  360. }
  361. ops.mode = MTD_OPS_PLACE_OOB;
  362. /* Write to first/last page(s) if necessary */
  363. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  364. ofs += mtd->erasesize - mtd->writesize;
  365. do {
  366. res = nand_do_write_oob(mtd, ofs, &ops);
  367. if (!ret)
  368. ret = res;
  369. i++;
  370. ofs += mtd->writesize;
  371. } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
  372. return ret;
  373. }
  374. /**
  375. * nand_block_markbad_lowlevel - mark a block bad
  376. * @mtd: MTD device structure
  377. * @ofs: offset from device start
  378. *
  379. * This function performs the generic NAND bad block marking steps (i.e., bad
  380. * block table(s) and/or marker(s)). We only allow the hardware driver to
  381. * specify how to write bad block markers to OOB (chip->block_markbad).
  382. *
  383. * We try operations in the following order:
  384. * (1) erase the affected block, to allow OOB marker to be written cleanly
  385. * (2) write bad block marker to OOB area of affected block (unless flag
  386. * NAND_BBT_NO_OOB_BBM is present)
  387. * (3) update the BBT
  388. * Note that we retain the first error encountered in (2) or (3), finish the
  389. * procedures, and dump the error in the end.
  390. */
  391. static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
  392. {
  393. struct nand_chip *chip = mtd_to_nand(mtd);
  394. int res, ret = 0;
  395. if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
  396. struct erase_info einfo;
  397. /* Attempt erase before marking OOB */
  398. memset(&einfo, 0, sizeof(einfo));
  399. einfo.mtd = mtd;
  400. einfo.addr = ofs;
  401. einfo.len = 1ULL << chip->phys_erase_shift;
  402. nand_erase_nand(mtd, &einfo, 0);
  403. /* Write bad block marker to OOB */
  404. nand_get_device(mtd, FL_WRITING);
  405. ret = chip->block_markbad(mtd, ofs);
  406. nand_release_device(mtd);
  407. }
  408. /* Mark block bad in BBT */
  409. if (chip->bbt) {
  410. res = nand_markbad_bbt(mtd, ofs);
  411. if (!ret)
  412. ret = res;
  413. }
  414. if (!ret)
  415. mtd->ecc_stats.badblocks++;
  416. return ret;
  417. }
  418. /**
  419. * nand_check_wp - [GENERIC] check if the chip is write protected
  420. * @mtd: MTD device structure
  421. *
  422. * Check, if the device is write protected. The function expects, that the
  423. * device is already selected.
  424. */
  425. static int nand_check_wp(struct mtd_info *mtd)
  426. {
  427. struct nand_chip *chip = mtd_to_nand(mtd);
  428. /* Broken xD cards report WP despite being writable */
  429. if (chip->options & NAND_BROKEN_XD)
  430. return 0;
  431. /* Check the WP bit */
  432. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  433. return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  434. }
  435. /**
  436. * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
  437. * @mtd: MTD device structure
  438. * @ofs: offset from device start
  439. *
  440. * Check if the block is marked as reserved.
  441. */
  442. static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  443. {
  444. struct nand_chip *chip = mtd_to_nand(mtd);
  445. if (!chip->bbt)
  446. return 0;
  447. /* Return info from the table */
  448. return nand_isreserved_bbt(mtd, ofs);
  449. }
  450. /**
  451. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  452. * @mtd: MTD device structure
  453. * @ofs: offset from device start
  454. * @allowbbt: 1, if its allowed to access the bbt area
  455. *
  456. * Check, if the block is bad. Either by reading the bad block table or
  457. * calling of the scan function.
  458. */
  459. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  460. {
  461. struct nand_chip *chip = mtd_to_nand(mtd);
  462. if (!(chip->options & NAND_SKIP_BBTSCAN) &&
  463. !(chip->options & NAND_BBT_SCANNED)) {
  464. chip->options |= NAND_BBT_SCANNED;
  465. chip->scan_bbt(mtd);
  466. }
  467. if (!chip->bbt)
  468. return chip->block_bad(mtd, ofs);
  469. /* Return info from the table */
  470. return nand_isbad_bbt(mtd, ofs, allowbbt);
  471. }
  472. /**
  473. * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
  474. * @mtd: MTD device structure
  475. *
  476. * Wait for the ready pin after a command, and warn if a timeout occurs.
  477. */
  478. void nand_wait_ready(struct mtd_info *mtd)
  479. {
  480. struct nand_chip *chip = mtd_to_nand(mtd);
  481. u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
  482. u32 time_start;
  483. time_start = get_timer(0);
  484. /* Wait until command is processed or timeout occurs */
  485. while (get_timer(time_start) < timeo) {
  486. if (chip->dev_ready)
  487. if (chip->dev_ready(mtd))
  488. break;
  489. }
  490. if (!chip->dev_ready(mtd))
  491. pr_warn("timeout while waiting for chip to become ready\n");
  492. }
  493. EXPORT_SYMBOL_GPL(nand_wait_ready);
  494. /**
  495. * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
  496. * @mtd: MTD device structure
  497. * @timeo: Timeout in ms
  498. *
  499. * Wait for status ready (i.e. command done) or timeout.
  500. */
  501. static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
  502. {
  503. register struct nand_chip *chip = mtd_to_nand(mtd);
  504. u32 time_start;
  505. timeo = (CONFIG_SYS_HZ * timeo) / 1000;
  506. time_start = get_timer(0);
  507. while (get_timer(time_start) < timeo) {
  508. if ((chip->read_byte(mtd) & NAND_STATUS_READY))
  509. break;
  510. WATCHDOG_RESET();
  511. }
  512. };
  513. /**
  514. * nand_command - [DEFAULT] Send command to NAND device
  515. * @mtd: MTD device structure
  516. * @command: the command to be sent
  517. * @column: the column address for this command, -1 if none
  518. * @page_addr: the page address for this command, -1 if none
  519. *
  520. * Send command to NAND device. This function is used for small page devices
  521. * (512 Bytes per page).
  522. */
  523. static void nand_command(struct mtd_info *mtd, unsigned int command,
  524. int column, int page_addr)
  525. {
  526. register struct nand_chip *chip = mtd_to_nand(mtd);
  527. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  528. /* Write out the command to the device */
  529. if (command == NAND_CMD_SEQIN) {
  530. int readcmd;
  531. if (column >= mtd->writesize) {
  532. /* OOB area */
  533. column -= mtd->writesize;
  534. readcmd = NAND_CMD_READOOB;
  535. } else if (column < 256) {
  536. /* First 256 bytes --> READ0 */
  537. readcmd = NAND_CMD_READ0;
  538. } else {
  539. column -= 256;
  540. readcmd = NAND_CMD_READ1;
  541. }
  542. chip->cmd_ctrl(mtd, readcmd, ctrl);
  543. ctrl &= ~NAND_CTRL_CHANGE;
  544. }
  545. chip->cmd_ctrl(mtd, command, ctrl);
  546. /* Address cycle, when necessary */
  547. ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
  548. /* Serially input address */
  549. if (column != -1) {
  550. /* Adjust columns for 16 bit buswidth */
  551. if (chip->options & NAND_BUSWIDTH_16 &&
  552. !nand_opcode_8bits(command))
  553. column >>= 1;
  554. chip->cmd_ctrl(mtd, column, ctrl);
  555. ctrl &= ~NAND_CTRL_CHANGE;
  556. }
  557. if (page_addr != -1) {
  558. chip->cmd_ctrl(mtd, page_addr, ctrl);
  559. ctrl &= ~NAND_CTRL_CHANGE;
  560. chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
  561. /* One more address cycle for devices > 32MiB */
  562. if (chip->chipsize > (32 << 20))
  563. chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
  564. }
  565. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  566. /*
  567. * Program and erase have their own busy handlers status and sequential
  568. * in needs no delay
  569. */
  570. switch (command) {
  571. case NAND_CMD_PAGEPROG:
  572. case NAND_CMD_ERASE1:
  573. case NAND_CMD_ERASE2:
  574. case NAND_CMD_SEQIN:
  575. case NAND_CMD_STATUS:
  576. return;
  577. case NAND_CMD_RESET:
  578. if (chip->dev_ready)
  579. break;
  580. udelay(chip->chip_delay);
  581. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  582. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  583. chip->cmd_ctrl(mtd,
  584. NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  585. /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
  586. nand_wait_status_ready(mtd, 250);
  587. return;
  588. /* This applies to read commands */
  589. default:
  590. /*
  591. * If we don't have access to the busy pin, we apply the given
  592. * command delay
  593. */
  594. if (!chip->dev_ready) {
  595. udelay(chip->chip_delay);
  596. return;
  597. }
  598. }
  599. /*
  600. * Apply this short delay always to ensure that we do wait tWB in
  601. * any case on any machine.
  602. */
  603. ndelay(100);
  604. nand_wait_ready(mtd);
  605. }
  606. /**
  607. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  608. * @mtd: MTD device structure
  609. * @command: the command to be sent
  610. * @column: the column address for this command, -1 if none
  611. * @page_addr: the page address for this command, -1 if none
  612. *
  613. * Send command to NAND device. This is the version for the new large page
  614. * devices. We don't have the separate regions as we have in the small page
  615. * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
  616. */
  617. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  618. int column, int page_addr)
  619. {
  620. register struct nand_chip *chip = mtd_to_nand(mtd);
  621. /* Emulate NAND_CMD_READOOB */
  622. if (command == NAND_CMD_READOOB) {
  623. column += mtd->writesize;
  624. command = NAND_CMD_READ0;
  625. }
  626. /* Command latch cycle */
  627. chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  628. if (column != -1 || page_addr != -1) {
  629. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  630. /* Serially input address */
  631. if (column != -1) {
  632. /* Adjust columns for 16 bit buswidth */
  633. if (chip->options & NAND_BUSWIDTH_16 &&
  634. !nand_opcode_8bits(command))
  635. column >>= 1;
  636. chip->cmd_ctrl(mtd, column, ctrl);
  637. ctrl &= ~NAND_CTRL_CHANGE;
  638. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  639. }
  640. if (page_addr != -1) {
  641. chip->cmd_ctrl(mtd, page_addr, ctrl);
  642. chip->cmd_ctrl(mtd, page_addr >> 8,
  643. NAND_NCE | NAND_ALE);
  644. /* One more address cycle for devices > 128MiB */
  645. if (chip->chipsize > (128 << 20))
  646. chip->cmd_ctrl(mtd, page_addr >> 16,
  647. NAND_NCE | NAND_ALE);
  648. }
  649. }
  650. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  651. /*
  652. * Program and erase have their own busy handlers status, sequential
  653. * in and status need no delay.
  654. */
  655. switch (command) {
  656. case NAND_CMD_CACHEDPROG:
  657. case NAND_CMD_PAGEPROG:
  658. case NAND_CMD_ERASE1:
  659. case NAND_CMD_ERASE2:
  660. case NAND_CMD_SEQIN:
  661. case NAND_CMD_RNDIN:
  662. case NAND_CMD_STATUS:
  663. return;
  664. case NAND_CMD_RESET:
  665. if (chip->dev_ready)
  666. break;
  667. udelay(chip->chip_delay);
  668. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  669. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  670. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  671. NAND_NCE | NAND_CTRL_CHANGE);
  672. /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
  673. nand_wait_status_ready(mtd, 250);
  674. return;
  675. case NAND_CMD_RNDOUT:
  676. /* No ready / busy check necessary */
  677. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  678. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  679. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  680. NAND_NCE | NAND_CTRL_CHANGE);
  681. return;
  682. case NAND_CMD_READ0:
  683. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  684. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  685. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  686. NAND_NCE | NAND_CTRL_CHANGE);
  687. /* This applies to read commands */
  688. default:
  689. /*
  690. * If we don't have access to the busy pin, we apply the given
  691. * command delay.
  692. */
  693. if (!chip->dev_ready) {
  694. udelay(chip->chip_delay);
  695. return;
  696. }
  697. }
  698. /*
  699. * Apply this short delay always to ensure that we do wait tWB in
  700. * any case on any machine.
  701. */
  702. ndelay(100);
  703. nand_wait_ready(mtd);
  704. }
  705. /**
  706. * panic_nand_get_device - [GENERIC] Get chip for selected access
  707. * @chip: the nand chip descriptor
  708. * @mtd: MTD device structure
  709. * @new_state: the state which is requested
  710. *
  711. * Used when in panic, no locks are taken.
  712. */
  713. static void panic_nand_get_device(struct nand_chip *chip,
  714. struct mtd_info *mtd, int new_state)
  715. {
  716. /* Hardware controller shared among independent devices */
  717. chip->controller->active = chip;
  718. chip->state = new_state;
  719. }
  720. /**
  721. * nand_get_device - [GENERIC] Get chip for selected access
  722. * @mtd: MTD device structure
  723. * @new_state: the state which is requested
  724. *
  725. * Get the device and lock it for exclusive access
  726. */
  727. static int
  728. nand_get_device(struct mtd_info *mtd, int new_state)
  729. {
  730. struct nand_chip *chip = mtd_to_nand(mtd);
  731. chip->state = new_state;
  732. return 0;
  733. }
  734. /**
  735. * panic_nand_wait - [GENERIC] wait until the command is done
  736. * @mtd: MTD device structure
  737. * @chip: NAND chip structure
  738. * @timeo: timeout
  739. *
  740. * Wait for command done. This is a helper function for nand_wait used when
  741. * we are in interrupt context. May happen when in panic and trying to write
  742. * an oops through mtdoops.
  743. */
  744. static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
  745. unsigned long timeo)
  746. {
  747. int i;
  748. for (i = 0; i < timeo; i++) {
  749. if (chip->dev_ready) {
  750. if (chip->dev_ready(mtd))
  751. break;
  752. } else {
  753. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  754. break;
  755. }
  756. mdelay(1);
  757. }
  758. }
  759. /**
  760. * nand_wait - [DEFAULT] wait until the command is done
  761. * @mtd: MTD device structure
  762. * @chip: NAND chip structure
  763. *
  764. * Wait for command done. This applies to erase and program only.
  765. */
  766. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  767. {
  768. int status;
  769. unsigned long timeo = 400;
  770. led_trigger_event(nand_led_trigger, LED_FULL);
  771. /*
  772. * Apply this short delay always to ensure that we do wait tWB in any
  773. * case on any machine.
  774. */
  775. ndelay(100);
  776. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  777. u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
  778. u32 time_start;
  779. time_start = get_timer(0);
  780. while (get_timer(time_start) < timer) {
  781. if (chip->dev_ready) {
  782. if (chip->dev_ready(mtd))
  783. break;
  784. } else {
  785. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  786. break;
  787. }
  788. }
  789. led_trigger_event(nand_led_trigger, LED_OFF);
  790. status = (int)chip->read_byte(mtd);
  791. /* This can happen if in case of timeout or buggy dev_ready */
  792. WARN_ON(!(status & NAND_STATUS_READY));
  793. return status;
  794. }
  795. #define BITS_PER_BYTE 8
  796. /**
  797. * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
  798. * @buf: buffer to test
  799. * @len: buffer length
  800. * @bitflips_threshold: maximum number of bitflips
  801. *
  802. * Check if a buffer contains only 0xff, which means the underlying region
  803. * has been erased and is ready to be programmed.
  804. * The bitflips_threshold specify the maximum number of bitflips before
  805. * considering the region is not erased.
  806. * Note: The logic of this function has been extracted from the memweight
  807. * implementation, except that nand_check_erased_buf function exit before
  808. * testing the whole buffer if the number of bitflips exceed the
  809. * bitflips_threshold value.
  810. *
  811. * Returns a positive number of bitflips less than or equal to
  812. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  813. * threshold.
  814. */
  815. static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
  816. {
  817. const unsigned char *bitmap = buf;
  818. int bitflips = 0;
  819. int weight;
  820. for (; len && ((uintptr_t)bitmap) % sizeof(long);
  821. len--, bitmap++) {
  822. weight = hweight8(*bitmap);
  823. bitflips += BITS_PER_BYTE - weight;
  824. if (unlikely(bitflips > bitflips_threshold))
  825. return -EBADMSG;
  826. }
  827. for (; len >= 4; len -= 4, bitmap += 4) {
  828. weight = hweight32(*((u32 *)bitmap));
  829. bitflips += 32 - weight;
  830. if (unlikely(bitflips > bitflips_threshold))
  831. return -EBADMSG;
  832. }
  833. for (; len > 0; len--, bitmap++) {
  834. weight = hweight8(*bitmap);
  835. bitflips += BITS_PER_BYTE - weight;
  836. if (unlikely(bitflips > bitflips_threshold))
  837. return -EBADMSG;
  838. }
  839. return bitflips;
  840. }
  841. /**
  842. * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
  843. * 0xff data
  844. * @data: data buffer to test
  845. * @datalen: data length
  846. * @ecc: ECC buffer
  847. * @ecclen: ECC length
  848. * @extraoob: extra OOB buffer
  849. * @extraooblen: extra OOB length
  850. * @bitflips_threshold: maximum number of bitflips
  851. *
  852. * Check if a data buffer and its associated ECC and OOB data contains only
  853. * 0xff pattern, which means the underlying region has been erased and is
  854. * ready to be programmed.
  855. * The bitflips_threshold specify the maximum number of bitflips before
  856. * considering the region as not erased.
  857. *
  858. * Note:
  859. * 1/ ECC algorithms are working on pre-defined block sizes which are usually
  860. * different from the NAND page size. When fixing bitflips, ECC engines will
  861. * report the number of errors per chunk, and the NAND core infrastructure
  862. * expect you to return the maximum number of bitflips for the whole page.
  863. * This is why you should always use this function on a single chunk and
  864. * not on the whole page. After checking each chunk you should update your
  865. * max_bitflips value accordingly.
  866. * 2/ When checking for bitflips in erased pages you should not only check
  867. * the payload data but also their associated ECC data, because a user might
  868. * have programmed almost all bits to 1 but a few. In this case, we
  869. * shouldn't consider the chunk as erased, and checking ECC bytes prevent
  870. * this case.
  871. * 3/ The extraoob argument is optional, and should be used if some of your OOB
  872. * data are protected by the ECC engine.
  873. * It could also be used if you support subpages and want to attach some
  874. * extra OOB data to an ECC chunk.
  875. *
  876. * Returns a positive number of bitflips less than or equal to
  877. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  878. * threshold. In case of success, the passed buffers are filled with 0xff.
  879. */
  880. int nand_check_erased_ecc_chunk(void *data, int datalen,
  881. void *ecc, int ecclen,
  882. void *extraoob, int extraooblen,
  883. int bitflips_threshold)
  884. {
  885. int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
  886. data_bitflips = nand_check_erased_buf(data, datalen,
  887. bitflips_threshold);
  888. if (data_bitflips < 0)
  889. return data_bitflips;
  890. bitflips_threshold -= data_bitflips;
  891. ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
  892. if (ecc_bitflips < 0)
  893. return ecc_bitflips;
  894. bitflips_threshold -= ecc_bitflips;
  895. extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
  896. bitflips_threshold);
  897. if (extraoob_bitflips < 0)
  898. return extraoob_bitflips;
  899. if (data_bitflips)
  900. memset(data, 0xff, datalen);
  901. if (ecc_bitflips)
  902. memset(ecc, 0xff, ecclen);
  903. if (extraoob_bitflips)
  904. memset(extraoob, 0xff, extraooblen);
  905. return data_bitflips + ecc_bitflips + extraoob_bitflips;
  906. }
  907. EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
  908. /**
  909. * nand_read_page_raw - [INTERN] read raw page data without ecc
  910. * @mtd: mtd info structure
  911. * @chip: nand chip info structure
  912. * @buf: buffer to store read data
  913. * @oob_required: caller requires OOB data read to chip->oob_poi
  914. * @page: page number to read
  915. *
  916. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  917. */
  918. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  919. uint8_t *buf, int oob_required, int page)
  920. {
  921. chip->read_buf(mtd, buf, mtd->writesize);
  922. if (oob_required)
  923. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  924. return 0;
  925. }
  926. /**
  927. * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
  928. * @mtd: mtd info structure
  929. * @chip: nand chip info structure
  930. * @buf: buffer to store read data
  931. * @oob_required: caller requires OOB data read to chip->oob_poi
  932. * @page: page number to read
  933. *
  934. * We need a special oob layout and handling even when OOB isn't used.
  935. */
  936. static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
  937. struct nand_chip *chip, uint8_t *buf,
  938. int oob_required, int page)
  939. {
  940. int eccsize = chip->ecc.size;
  941. int eccbytes = chip->ecc.bytes;
  942. uint8_t *oob = chip->oob_poi;
  943. int steps, size;
  944. for (steps = chip->ecc.steps; steps > 0; steps--) {
  945. chip->read_buf(mtd, buf, eccsize);
  946. buf += eccsize;
  947. if (chip->ecc.prepad) {
  948. chip->read_buf(mtd, oob, chip->ecc.prepad);
  949. oob += chip->ecc.prepad;
  950. }
  951. chip->read_buf(mtd, oob, eccbytes);
  952. oob += eccbytes;
  953. if (chip->ecc.postpad) {
  954. chip->read_buf(mtd, oob, chip->ecc.postpad);
  955. oob += chip->ecc.postpad;
  956. }
  957. }
  958. size = mtd->oobsize - (oob - chip->oob_poi);
  959. if (size)
  960. chip->read_buf(mtd, oob, size);
  961. return 0;
  962. }
  963. /**
  964. * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
  965. * @mtd: mtd info structure
  966. * @chip: nand chip info structure
  967. * @buf: buffer to store read data
  968. * @oob_required: caller requires OOB data read to chip->oob_poi
  969. * @page: page number to read
  970. */
  971. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  972. uint8_t *buf, int oob_required, int page)
  973. {
  974. int i, eccsize = chip->ecc.size;
  975. int eccbytes = chip->ecc.bytes;
  976. int eccsteps = chip->ecc.steps;
  977. uint8_t *p = buf;
  978. uint8_t *ecc_calc = chip->buffers->ecccalc;
  979. uint8_t *ecc_code = chip->buffers->ecccode;
  980. uint32_t *eccpos = chip->ecc.layout->eccpos;
  981. unsigned int max_bitflips = 0;
  982. chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
  983. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  984. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  985. for (i = 0; i < chip->ecc.total; i++)
  986. ecc_code[i] = chip->oob_poi[eccpos[i]];
  987. eccsteps = chip->ecc.steps;
  988. p = buf;
  989. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  990. int stat;
  991. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  992. if (stat < 0) {
  993. mtd->ecc_stats.failed++;
  994. } else {
  995. mtd->ecc_stats.corrected += stat;
  996. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  997. }
  998. }
  999. return max_bitflips;
  1000. }
  1001. /**
  1002. * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
  1003. * @mtd: mtd info structure
  1004. * @chip: nand chip info structure
  1005. * @data_offs: offset of requested data within the page
  1006. * @readlen: data length
  1007. * @bufpoi: buffer to store read data
  1008. * @page: page number to read
  1009. */
  1010. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1011. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
  1012. int page)
  1013. {
  1014. int start_step, end_step, num_steps;
  1015. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1016. uint8_t *p;
  1017. int data_col_addr, i, gaps = 0;
  1018. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1019. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1020. int index;
  1021. unsigned int max_bitflips = 0;
  1022. /* Column address within the page aligned to ECC size (256bytes) */
  1023. start_step = data_offs / chip->ecc.size;
  1024. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1025. num_steps = end_step - start_step + 1;
  1026. index = start_step * chip->ecc.bytes;
  1027. /* Data size aligned to ECC ecc.size */
  1028. datafrag_len = num_steps * chip->ecc.size;
  1029. eccfrag_len = num_steps * chip->ecc.bytes;
  1030. data_col_addr = start_step * chip->ecc.size;
  1031. /* If we read not a page aligned data */
  1032. if (data_col_addr != 0)
  1033. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1034. p = bufpoi + data_col_addr;
  1035. chip->read_buf(mtd, p, datafrag_len);
  1036. /* Calculate ECC */
  1037. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1038. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1039. /*
  1040. * The performance is faster if we position offsets according to
  1041. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1042. */
  1043. for (i = 0; i < eccfrag_len - 1; i++) {
  1044. if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
  1045. gaps = 1;
  1046. break;
  1047. }
  1048. }
  1049. if (gaps) {
  1050. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1051. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1052. } else {
  1053. /*
  1054. * Send the command to read the particular ECC bytes take care
  1055. * about buswidth alignment in read_buf.
  1056. */
  1057. aligned_pos = eccpos[index] & ~(busw - 1);
  1058. aligned_len = eccfrag_len;
  1059. if (eccpos[index] & (busw - 1))
  1060. aligned_len++;
  1061. if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1062. aligned_len++;
  1063. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1064. mtd->writesize + aligned_pos, -1);
  1065. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1066. }
  1067. for (i = 0; i < eccfrag_len; i++)
  1068. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1069. p = bufpoi + data_col_addr;
  1070. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1071. int stat;
  1072. stat = chip->ecc.correct(mtd, p,
  1073. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1074. if (stat == -EBADMSG &&
  1075. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1076. /* check for empty pages with bitflips */
  1077. stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1078. &chip->buffers->ecccode[i],
  1079. chip->ecc.bytes,
  1080. NULL, 0,
  1081. chip->ecc.strength);
  1082. }
  1083. if (stat < 0) {
  1084. mtd->ecc_stats.failed++;
  1085. } else {
  1086. mtd->ecc_stats.corrected += stat;
  1087. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1088. }
  1089. }
  1090. return max_bitflips;
  1091. }
  1092. /**
  1093. * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
  1094. * @mtd: mtd info structure
  1095. * @chip: nand chip info structure
  1096. * @buf: buffer to store read data
  1097. * @oob_required: caller requires OOB data read to chip->oob_poi
  1098. * @page: page number to read
  1099. *
  1100. * Not for syndrome calculating ECC controllers which need a special oob layout.
  1101. */
  1102. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1103. uint8_t *buf, int oob_required, int page)
  1104. {
  1105. int i, eccsize = chip->ecc.size;
  1106. int eccbytes = chip->ecc.bytes;
  1107. int eccsteps = chip->ecc.steps;
  1108. uint8_t *p = buf;
  1109. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1110. uint8_t *ecc_code = chip->buffers->ecccode;
  1111. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1112. unsigned int max_bitflips = 0;
  1113. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1114. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1115. chip->read_buf(mtd, p, eccsize);
  1116. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1117. }
  1118. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1119. for (i = 0; i < chip->ecc.total; i++)
  1120. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1121. eccsteps = chip->ecc.steps;
  1122. p = buf;
  1123. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1124. int stat;
  1125. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1126. if (stat == -EBADMSG &&
  1127. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1128. /* check for empty pages with bitflips */
  1129. stat = nand_check_erased_ecc_chunk(p, eccsize,
  1130. &ecc_code[i], eccbytes,
  1131. NULL, 0,
  1132. chip->ecc.strength);
  1133. }
  1134. if (stat < 0) {
  1135. mtd->ecc_stats.failed++;
  1136. } else {
  1137. mtd->ecc_stats.corrected += stat;
  1138. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1139. }
  1140. }
  1141. return max_bitflips;
  1142. }
  1143. /**
  1144. * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
  1145. * @mtd: mtd info structure
  1146. * @chip: nand chip info structure
  1147. * @buf: buffer to store read data
  1148. * @oob_required: caller requires OOB data read to chip->oob_poi
  1149. * @page: page number to read
  1150. *
  1151. * Hardware ECC for large page chips, require OOB to be read first. For this
  1152. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1153. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1154. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1155. * the data area, by overwriting the NAND manufacturer bad block markings.
  1156. */
  1157. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1158. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  1159. {
  1160. int i, eccsize = chip->ecc.size;
  1161. int eccbytes = chip->ecc.bytes;
  1162. int eccsteps = chip->ecc.steps;
  1163. uint8_t *p = buf;
  1164. uint8_t *ecc_code = chip->buffers->ecccode;
  1165. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1166. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1167. unsigned int max_bitflips = 0;
  1168. /* Read the OOB area first */
  1169. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1170. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1171. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1172. for (i = 0; i < chip->ecc.total; i++)
  1173. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1174. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1175. int stat;
  1176. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1177. chip->read_buf(mtd, p, eccsize);
  1178. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1179. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1180. if (stat == -EBADMSG &&
  1181. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1182. /* check for empty pages with bitflips */
  1183. stat = nand_check_erased_ecc_chunk(p, eccsize,
  1184. &ecc_code[i], eccbytes,
  1185. NULL, 0,
  1186. chip->ecc.strength);
  1187. }
  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. }
  1195. return max_bitflips;
  1196. }
  1197. /**
  1198. * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
  1199. * @mtd: mtd info structure
  1200. * @chip: nand chip info structure
  1201. * @buf: buffer to store read data
  1202. * @oob_required: caller requires OOB data read to chip->oob_poi
  1203. * @page: page number to read
  1204. *
  1205. * The hw generator calculates the error syndrome automatically. Therefore we
  1206. * need a special oob layout and handling.
  1207. */
  1208. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1209. uint8_t *buf, int oob_required, int page)
  1210. {
  1211. int i, eccsize = chip->ecc.size;
  1212. int eccbytes = chip->ecc.bytes;
  1213. int eccsteps = chip->ecc.steps;
  1214. int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  1215. uint8_t *p = buf;
  1216. uint8_t *oob = chip->oob_poi;
  1217. unsigned int max_bitflips = 0;
  1218. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1219. int stat;
  1220. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1221. chip->read_buf(mtd, p, eccsize);
  1222. if (chip->ecc.prepad) {
  1223. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1224. oob += chip->ecc.prepad;
  1225. }
  1226. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1227. chip->read_buf(mtd, oob, eccbytes);
  1228. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1229. oob += eccbytes;
  1230. if (chip->ecc.postpad) {
  1231. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1232. oob += chip->ecc.postpad;
  1233. }
  1234. if (stat == -EBADMSG &&
  1235. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1236. /* check for empty pages with bitflips */
  1237. stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1238. oob - eccpadbytes,
  1239. eccpadbytes,
  1240. NULL, 0,
  1241. chip->ecc.strength);
  1242. }
  1243. if (stat < 0) {
  1244. mtd->ecc_stats.failed++;
  1245. } else {
  1246. mtd->ecc_stats.corrected += stat;
  1247. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1248. }
  1249. }
  1250. /* Calculate remaining oob bytes */
  1251. i = mtd->oobsize - (oob - chip->oob_poi);
  1252. if (i)
  1253. chip->read_buf(mtd, oob, i);
  1254. return max_bitflips;
  1255. }
  1256. /**
  1257. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  1258. * @chip: nand chip structure
  1259. * @oob: oob destination address
  1260. * @ops: oob ops structure
  1261. * @len: size of oob to transfer
  1262. */
  1263. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1264. struct mtd_oob_ops *ops, size_t len)
  1265. {
  1266. switch (ops->mode) {
  1267. case MTD_OPS_PLACE_OOB:
  1268. case MTD_OPS_RAW:
  1269. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1270. return oob + len;
  1271. case MTD_OPS_AUTO_OOB: {
  1272. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1273. uint32_t boffs = 0, roffs = ops->ooboffs;
  1274. size_t bytes = 0;
  1275. for (; free->length && len; free++, len -= bytes) {
  1276. /* Read request not from offset 0? */
  1277. if (unlikely(roffs)) {
  1278. if (roffs >= free->length) {
  1279. roffs -= free->length;
  1280. continue;
  1281. }
  1282. boffs = free->offset + roffs;
  1283. bytes = min_t(size_t, len,
  1284. (free->length - roffs));
  1285. roffs = 0;
  1286. } else {
  1287. bytes = min_t(size_t, len, free->length);
  1288. boffs = free->offset;
  1289. }
  1290. memcpy(oob, chip->oob_poi + boffs, bytes);
  1291. oob += bytes;
  1292. }
  1293. return oob;
  1294. }
  1295. default:
  1296. BUG();
  1297. }
  1298. return NULL;
  1299. }
  1300. /**
  1301. * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
  1302. * @mtd: MTD device structure
  1303. * @retry_mode: the retry mode to use
  1304. *
  1305. * Some vendors supply a special command to shift the Vt threshold, to be used
  1306. * when there are too many bitflips in a page (i.e., ECC error). After setting
  1307. * a new threshold, the host should retry reading the page.
  1308. */
  1309. static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
  1310. {
  1311. struct nand_chip *chip = mtd_to_nand(mtd);
  1312. pr_debug("setting READ RETRY mode %d\n", retry_mode);
  1313. if (retry_mode >= chip->read_retries)
  1314. return -EINVAL;
  1315. if (!chip->setup_read_retry)
  1316. return -EOPNOTSUPP;
  1317. return chip->setup_read_retry(mtd, retry_mode);
  1318. }
  1319. /**
  1320. * nand_do_read_ops - [INTERN] Read data with ECC
  1321. * @mtd: MTD device structure
  1322. * @from: offset to read from
  1323. * @ops: oob ops structure
  1324. *
  1325. * Internal function. Called with chip held.
  1326. */
  1327. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1328. struct mtd_oob_ops *ops)
  1329. {
  1330. int chipnr, page, realpage, col, bytes, aligned, oob_required;
  1331. struct nand_chip *chip = mtd_to_nand(mtd);
  1332. int ret = 0;
  1333. uint32_t readlen = ops->len;
  1334. uint32_t oobreadlen = ops->ooblen;
  1335. uint32_t max_oobsize = mtd_oobavail(mtd, ops);
  1336. uint8_t *bufpoi, *oob, *buf;
  1337. int use_bufpoi;
  1338. unsigned int max_bitflips = 0;
  1339. int retry_mode = 0;
  1340. bool ecc_fail = false;
  1341. chipnr = (int)(from >> chip->chip_shift);
  1342. chip->select_chip(mtd, chipnr);
  1343. realpage = (int)(from >> chip->page_shift);
  1344. page = realpage & chip->pagemask;
  1345. col = (int)(from & (mtd->writesize - 1));
  1346. buf = ops->datbuf;
  1347. oob = ops->oobbuf;
  1348. oob_required = oob ? 1 : 0;
  1349. while (1) {
  1350. unsigned int ecc_failures = mtd->ecc_stats.failed;
  1351. WATCHDOG_RESET();
  1352. bytes = min(mtd->writesize - col, readlen);
  1353. aligned = (bytes == mtd->writesize);
  1354. if (!aligned)
  1355. use_bufpoi = 1;
  1356. else
  1357. use_bufpoi = 0;
  1358. /* Is the current page in the buffer? */
  1359. if (realpage != chip->pagebuf || oob) {
  1360. bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
  1361. if (use_bufpoi && aligned)
  1362. pr_debug("%s: using read bounce buffer for buf@%p\n",
  1363. __func__, buf);
  1364. read_retry:
  1365. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1366. /*
  1367. * Now read the page into the buffer. Absent an error,
  1368. * the read methods return max bitflips per ecc step.
  1369. */
  1370. if (unlikely(ops->mode == MTD_OPS_RAW))
  1371. ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
  1372. oob_required,
  1373. page);
  1374. else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
  1375. !oob)
  1376. ret = chip->ecc.read_subpage(mtd, chip,
  1377. col, bytes, bufpoi,
  1378. page);
  1379. else
  1380. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1381. oob_required, page);
  1382. if (ret < 0) {
  1383. if (use_bufpoi)
  1384. /* Invalidate page cache */
  1385. chip->pagebuf = -1;
  1386. break;
  1387. }
  1388. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  1389. /* Transfer not aligned data */
  1390. if (use_bufpoi) {
  1391. if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
  1392. !(mtd->ecc_stats.failed - ecc_failures) &&
  1393. (ops->mode != MTD_OPS_RAW)) {
  1394. chip->pagebuf = realpage;
  1395. chip->pagebuf_bitflips = ret;
  1396. } else {
  1397. /* Invalidate page cache */
  1398. chip->pagebuf = -1;
  1399. }
  1400. memcpy(buf, chip->buffers->databuf + col, bytes);
  1401. }
  1402. if (unlikely(oob)) {
  1403. int toread = min(oobreadlen, max_oobsize);
  1404. if (toread) {
  1405. oob = nand_transfer_oob(chip,
  1406. oob, ops, toread);
  1407. oobreadlen -= toread;
  1408. }
  1409. }
  1410. if (chip->options & NAND_NEED_READRDY) {
  1411. /* Apply delay or wait for ready/busy pin */
  1412. if (!chip->dev_ready)
  1413. udelay(chip->chip_delay);
  1414. else
  1415. nand_wait_ready(mtd);
  1416. }
  1417. if (mtd->ecc_stats.failed - ecc_failures) {
  1418. if (retry_mode + 1 < chip->read_retries) {
  1419. retry_mode++;
  1420. ret = nand_setup_read_retry(mtd,
  1421. retry_mode);
  1422. if (ret < 0)
  1423. break;
  1424. /* Reset failures; retry */
  1425. mtd->ecc_stats.failed = ecc_failures;
  1426. goto read_retry;
  1427. } else {
  1428. /* No more retry modes; real failure */
  1429. ecc_fail = true;
  1430. }
  1431. }
  1432. buf += bytes;
  1433. } else {
  1434. memcpy(buf, chip->buffers->databuf + col, bytes);
  1435. buf += bytes;
  1436. max_bitflips = max_t(unsigned int, max_bitflips,
  1437. chip->pagebuf_bitflips);
  1438. }
  1439. readlen -= bytes;
  1440. /* Reset to retry mode 0 */
  1441. if (retry_mode) {
  1442. ret = nand_setup_read_retry(mtd, 0);
  1443. if (ret < 0)
  1444. break;
  1445. retry_mode = 0;
  1446. }
  1447. if (!readlen)
  1448. break;
  1449. /* For subsequent reads align to page boundary */
  1450. col = 0;
  1451. /* Increment page address */
  1452. realpage++;
  1453. page = realpage & chip->pagemask;
  1454. /* Check, if we cross a chip boundary */
  1455. if (!page) {
  1456. chipnr++;
  1457. chip->select_chip(mtd, -1);
  1458. chip->select_chip(mtd, chipnr);
  1459. }
  1460. }
  1461. chip->select_chip(mtd, -1);
  1462. ops->retlen = ops->len - (size_t) readlen;
  1463. if (oob)
  1464. ops->oobretlen = ops->ooblen - oobreadlen;
  1465. if (ret < 0)
  1466. return ret;
  1467. if (ecc_fail)
  1468. return -EBADMSG;
  1469. return max_bitflips;
  1470. }
  1471. /**
  1472. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1473. * @mtd: MTD device structure
  1474. * @from: offset to read from
  1475. * @len: number of bytes to read
  1476. * @retlen: pointer to variable to store the number of read bytes
  1477. * @buf: the databuffer to put data
  1478. *
  1479. * Get hold of the chip and call nand_do_read.
  1480. */
  1481. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1482. size_t *retlen, uint8_t *buf)
  1483. {
  1484. struct mtd_oob_ops ops;
  1485. int ret;
  1486. nand_get_device(mtd, FL_READING);
  1487. memset(&ops, 0, sizeof(ops));
  1488. ops.len = len;
  1489. ops.datbuf = buf;
  1490. ops.mode = MTD_OPS_PLACE_OOB;
  1491. ret = nand_do_read_ops(mtd, from, &ops);
  1492. *retlen = ops.retlen;
  1493. nand_release_device(mtd);
  1494. return ret;
  1495. }
  1496. /**
  1497. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1498. * @mtd: mtd info structure
  1499. * @chip: nand chip info structure
  1500. * @page: page number to read
  1501. */
  1502. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1503. int page)
  1504. {
  1505. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1506. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1507. return 0;
  1508. }
  1509. /**
  1510. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1511. * with syndromes
  1512. * @mtd: mtd info structure
  1513. * @chip: nand chip info structure
  1514. * @page: page number to read
  1515. */
  1516. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1517. int page)
  1518. {
  1519. int length = mtd->oobsize;
  1520. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1521. int eccsize = chip->ecc.size;
  1522. uint8_t *bufpoi = chip->oob_poi;
  1523. int i, toread, sndrnd = 0, pos;
  1524. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1525. for (i = 0; i < chip->ecc.steps; i++) {
  1526. if (sndrnd) {
  1527. pos = eccsize + i * (eccsize + chunk);
  1528. if (mtd->writesize > 512)
  1529. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1530. else
  1531. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1532. } else
  1533. sndrnd = 1;
  1534. toread = min_t(int, length, chunk);
  1535. chip->read_buf(mtd, bufpoi, toread);
  1536. bufpoi += toread;
  1537. length -= toread;
  1538. }
  1539. if (length > 0)
  1540. chip->read_buf(mtd, bufpoi, length);
  1541. return 0;
  1542. }
  1543. /**
  1544. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1545. * @mtd: mtd info structure
  1546. * @chip: nand chip info structure
  1547. * @page: page number to write
  1548. */
  1549. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1550. int page)
  1551. {
  1552. int status = 0;
  1553. const uint8_t *buf = chip->oob_poi;
  1554. int length = mtd->oobsize;
  1555. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1556. chip->write_buf(mtd, buf, length);
  1557. /* Send command to program the OOB data */
  1558. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1559. status = chip->waitfunc(mtd, chip);
  1560. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1561. }
  1562. /**
  1563. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1564. * with syndrome - only for large page flash
  1565. * @mtd: mtd info structure
  1566. * @chip: nand chip info structure
  1567. * @page: page number to write
  1568. */
  1569. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1570. struct nand_chip *chip, int page)
  1571. {
  1572. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1573. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1574. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1575. const uint8_t *bufpoi = chip->oob_poi;
  1576. /*
  1577. * data-ecc-data-ecc ... ecc-oob
  1578. * or
  1579. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1580. */
  1581. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1582. pos = steps * (eccsize + chunk);
  1583. steps = 0;
  1584. } else
  1585. pos = eccsize;
  1586. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1587. for (i = 0; i < steps; i++) {
  1588. if (sndcmd) {
  1589. if (mtd->writesize <= 512) {
  1590. uint32_t fill = 0xFFFFFFFF;
  1591. len = eccsize;
  1592. while (len > 0) {
  1593. int num = min_t(int, len, 4);
  1594. chip->write_buf(mtd, (uint8_t *)&fill,
  1595. num);
  1596. len -= num;
  1597. }
  1598. } else {
  1599. pos = eccsize + i * (eccsize + chunk);
  1600. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1601. }
  1602. } else
  1603. sndcmd = 1;
  1604. len = min_t(int, length, chunk);
  1605. chip->write_buf(mtd, bufpoi, len);
  1606. bufpoi += len;
  1607. length -= len;
  1608. }
  1609. if (length > 0)
  1610. chip->write_buf(mtd, bufpoi, length);
  1611. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1612. status = chip->waitfunc(mtd, chip);
  1613. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1614. }
  1615. /**
  1616. * nand_do_read_oob - [INTERN] NAND read out-of-band
  1617. * @mtd: MTD device structure
  1618. * @from: offset to read from
  1619. * @ops: oob operations description structure
  1620. *
  1621. * NAND read out-of-band data from the spare area.
  1622. */
  1623. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1624. struct mtd_oob_ops *ops)
  1625. {
  1626. int page, realpage, chipnr;
  1627. struct nand_chip *chip = mtd_to_nand(mtd);
  1628. struct mtd_ecc_stats stats;
  1629. int readlen = ops->ooblen;
  1630. int len;
  1631. uint8_t *buf = ops->oobbuf;
  1632. int ret = 0;
  1633. pr_debug("%s: from = 0x%08Lx, len = %i\n",
  1634. __func__, (unsigned long long)from, readlen);
  1635. stats = mtd->ecc_stats;
  1636. len = mtd_oobavail(mtd, ops);
  1637. if (unlikely(ops->ooboffs >= len)) {
  1638. pr_debug("%s: attempt to start read outside oob\n",
  1639. __func__);
  1640. return -EINVAL;
  1641. }
  1642. /* Do not allow reads past end of device */
  1643. if (unlikely(from >= mtd->size ||
  1644. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1645. (from >> chip->page_shift)) * len)) {
  1646. pr_debug("%s: attempt to read beyond end of device\n",
  1647. __func__);
  1648. return -EINVAL;
  1649. }
  1650. chipnr = (int)(from >> chip->chip_shift);
  1651. chip->select_chip(mtd, chipnr);
  1652. /* Shift to get page */
  1653. realpage = (int)(from >> chip->page_shift);
  1654. page = realpage & chip->pagemask;
  1655. while (1) {
  1656. WATCHDOG_RESET();
  1657. if (ops->mode == MTD_OPS_RAW)
  1658. ret = chip->ecc.read_oob_raw(mtd, chip, page);
  1659. else
  1660. ret = chip->ecc.read_oob(mtd, chip, page);
  1661. if (ret < 0)
  1662. break;
  1663. len = min(len, readlen);
  1664. buf = nand_transfer_oob(chip, buf, ops, len);
  1665. if (chip->options & NAND_NEED_READRDY) {
  1666. /* Apply delay or wait for ready/busy pin */
  1667. if (!chip->dev_ready)
  1668. udelay(chip->chip_delay);
  1669. else
  1670. nand_wait_ready(mtd);
  1671. }
  1672. readlen -= len;
  1673. if (!readlen)
  1674. break;
  1675. /* Increment page address */
  1676. realpage++;
  1677. page = realpage & chip->pagemask;
  1678. /* Check, if we cross a chip boundary */
  1679. if (!page) {
  1680. chipnr++;
  1681. chip->select_chip(mtd, -1);
  1682. chip->select_chip(mtd, chipnr);
  1683. }
  1684. }
  1685. chip->select_chip(mtd, -1);
  1686. ops->oobretlen = ops->ooblen - readlen;
  1687. if (ret < 0)
  1688. return ret;
  1689. if (mtd->ecc_stats.failed - stats.failed)
  1690. return -EBADMSG;
  1691. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1692. }
  1693. /**
  1694. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1695. * @mtd: MTD device structure
  1696. * @from: offset to read from
  1697. * @ops: oob operation description structure
  1698. *
  1699. * NAND read data and/or out-of-band data.
  1700. */
  1701. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1702. struct mtd_oob_ops *ops)
  1703. {
  1704. int ret = -ENOTSUPP;
  1705. ops->retlen = 0;
  1706. /* Do not allow reads past end of device */
  1707. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1708. pr_debug("%s: attempt to read beyond end of device\n",
  1709. __func__);
  1710. return -EINVAL;
  1711. }
  1712. nand_get_device(mtd, FL_READING);
  1713. switch (ops->mode) {
  1714. case MTD_OPS_PLACE_OOB:
  1715. case MTD_OPS_AUTO_OOB:
  1716. case MTD_OPS_RAW:
  1717. break;
  1718. default:
  1719. goto out;
  1720. }
  1721. if (!ops->datbuf)
  1722. ret = nand_do_read_oob(mtd, from, ops);
  1723. else
  1724. ret = nand_do_read_ops(mtd, from, ops);
  1725. out:
  1726. nand_release_device(mtd);
  1727. return ret;
  1728. }
  1729. /**
  1730. * nand_write_page_raw - [INTERN] raw page write function
  1731. * @mtd: mtd info structure
  1732. * @chip: nand chip info structure
  1733. * @buf: data buffer
  1734. * @oob_required: must write chip->oob_poi to OOB
  1735. * @page: page number to write
  1736. *
  1737. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1738. */
  1739. static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1740. const uint8_t *buf, int oob_required, int page)
  1741. {
  1742. chip->write_buf(mtd, buf, mtd->writesize);
  1743. if (oob_required)
  1744. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1745. return 0;
  1746. }
  1747. /**
  1748. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  1749. * @mtd: mtd info structure
  1750. * @chip: nand chip info structure
  1751. * @buf: data buffer
  1752. * @oob_required: must write chip->oob_poi to OOB
  1753. * @page: page number to write
  1754. *
  1755. * We need a special oob layout and handling even when ECC isn't checked.
  1756. */
  1757. static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1758. struct nand_chip *chip,
  1759. const uint8_t *buf, int oob_required,
  1760. int page)
  1761. {
  1762. int eccsize = chip->ecc.size;
  1763. int eccbytes = chip->ecc.bytes;
  1764. uint8_t *oob = chip->oob_poi;
  1765. int steps, size;
  1766. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1767. chip->write_buf(mtd, buf, eccsize);
  1768. buf += eccsize;
  1769. if (chip->ecc.prepad) {
  1770. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1771. oob += chip->ecc.prepad;
  1772. }
  1773. chip->write_buf(mtd, oob, eccbytes);
  1774. oob += eccbytes;
  1775. if (chip->ecc.postpad) {
  1776. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1777. oob += chip->ecc.postpad;
  1778. }
  1779. }
  1780. size = mtd->oobsize - (oob - chip->oob_poi);
  1781. if (size)
  1782. chip->write_buf(mtd, oob, size);
  1783. return 0;
  1784. }
  1785. /**
  1786. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  1787. * @mtd: mtd info structure
  1788. * @chip: nand chip info structure
  1789. * @buf: data buffer
  1790. * @oob_required: must write chip->oob_poi to OOB
  1791. * @page: page number to write
  1792. */
  1793. static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1794. const uint8_t *buf, int oob_required,
  1795. int page)
  1796. {
  1797. int i, eccsize = chip->ecc.size;
  1798. int eccbytes = chip->ecc.bytes;
  1799. int eccsteps = chip->ecc.steps;
  1800. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1801. const uint8_t *p = buf;
  1802. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1803. /* Software ECC calculation */
  1804. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1805. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1806. for (i = 0; i < chip->ecc.total; i++)
  1807. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1808. return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
  1809. }
  1810. /**
  1811. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  1812. * @mtd: mtd info structure
  1813. * @chip: nand chip info structure
  1814. * @buf: data buffer
  1815. * @oob_required: must write chip->oob_poi to OOB
  1816. * @page: page number to write
  1817. */
  1818. static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1819. const uint8_t *buf, int oob_required,
  1820. int page)
  1821. {
  1822. int i, eccsize = chip->ecc.size;
  1823. int eccbytes = chip->ecc.bytes;
  1824. int eccsteps = chip->ecc.steps;
  1825. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1826. const uint8_t *p = buf;
  1827. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1828. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1829. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1830. chip->write_buf(mtd, p, eccsize);
  1831. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1832. }
  1833. for (i = 0; i < chip->ecc.total; i++)
  1834. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1835. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1836. return 0;
  1837. }
  1838. /**
  1839. * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
  1840. * @mtd: mtd info structure
  1841. * @chip: nand chip info structure
  1842. * @offset: column address of subpage within the page
  1843. * @data_len: data length
  1844. * @buf: data buffer
  1845. * @oob_required: must write chip->oob_poi to OOB
  1846. * @page: page number to write
  1847. */
  1848. static int nand_write_subpage_hwecc(struct mtd_info *mtd,
  1849. struct nand_chip *chip, uint32_t offset,
  1850. uint32_t data_len, const uint8_t *buf,
  1851. int oob_required, int page)
  1852. {
  1853. uint8_t *oob_buf = chip->oob_poi;
  1854. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1855. int ecc_size = chip->ecc.size;
  1856. int ecc_bytes = chip->ecc.bytes;
  1857. int ecc_steps = chip->ecc.steps;
  1858. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1859. uint32_t start_step = offset / ecc_size;
  1860. uint32_t end_step = (offset + data_len - 1) / ecc_size;
  1861. int oob_bytes = mtd->oobsize / ecc_steps;
  1862. int step, i;
  1863. for (step = 0; step < ecc_steps; step++) {
  1864. /* configure controller for WRITE access */
  1865. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1866. /* write data (untouched subpages already masked by 0xFF) */
  1867. chip->write_buf(mtd, buf, ecc_size);
  1868. /* mask ECC of un-touched subpages by padding 0xFF */
  1869. if ((step < start_step) || (step > end_step))
  1870. memset(ecc_calc, 0xff, ecc_bytes);
  1871. else
  1872. chip->ecc.calculate(mtd, buf, ecc_calc);
  1873. /* mask OOB of un-touched subpages by padding 0xFF */
  1874. /* if oob_required, preserve OOB metadata of written subpage */
  1875. if (!oob_required || (step < start_step) || (step > end_step))
  1876. memset(oob_buf, 0xff, oob_bytes);
  1877. buf += ecc_size;
  1878. ecc_calc += ecc_bytes;
  1879. oob_buf += oob_bytes;
  1880. }
  1881. /* copy calculated ECC for whole page to chip->buffer->oob */
  1882. /* this include masked-value(0xFF) for unwritten subpages */
  1883. ecc_calc = chip->buffers->ecccalc;
  1884. for (i = 0; i < chip->ecc.total; i++)
  1885. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1886. /* write OOB buffer to NAND device */
  1887. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1888. return 0;
  1889. }
  1890. /**
  1891. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  1892. * @mtd: mtd info structure
  1893. * @chip: nand chip info structure
  1894. * @buf: data buffer
  1895. * @oob_required: must write chip->oob_poi to OOB
  1896. * @page: page number to write
  1897. *
  1898. * The hw generator calculates the error syndrome automatically. Therefore we
  1899. * need a special oob layout and handling.
  1900. */
  1901. static int nand_write_page_syndrome(struct mtd_info *mtd,
  1902. struct nand_chip *chip,
  1903. const uint8_t *buf, int oob_required,
  1904. int page)
  1905. {
  1906. int i, eccsize = chip->ecc.size;
  1907. int eccbytes = chip->ecc.bytes;
  1908. int eccsteps = chip->ecc.steps;
  1909. const uint8_t *p = buf;
  1910. uint8_t *oob = chip->oob_poi;
  1911. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1912. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1913. chip->write_buf(mtd, p, eccsize);
  1914. if (chip->ecc.prepad) {
  1915. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1916. oob += chip->ecc.prepad;
  1917. }
  1918. chip->ecc.calculate(mtd, p, oob);
  1919. chip->write_buf(mtd, oob, eccbytes);
  1920. oob += eccbytes;
  1921. if (chip->ecc.postpad) {
  1922. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1923. oob += chip->ecc.postpad;
  1924. }
  1925. }
  1926. /* Calculate remaining oob bytes */
  1927. i = mtd->oobsize - (oob - chip->oob_poi);
  1928. if (i)
  1929. chip->write_buf(mtd, oob, i);
  1930. return 0;
  1931. }
  1932. /**
  1933. * nand_write_page - [REPLACEABLE] write one page
  1934. * @mtd: MTD device structure
  1935. * @chip: NAND chip descriptor
  1936. * @offset: address offset within the page
  1937. * @data_len: length of actual data to be written
  1938. * @buf: the data to write
  1939. * @oob_required: must write chip->oob_poi to OOB
  1940. * @page: page number to write
  1941. * @cached: cached programming
  1942. * @raw: use _raw version of write_page
  1943. */
  1944. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1945. uint32_t offset, int data_len, const uint8_t *buf,
  1946. int oob_required, int page, int cached, int raw)
  1947. {
  1948. int status, subpage;
  1949. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  1950. chip->ecc.write_subpage)
  1951. subpage = offset || (data_len < mtd->writesize);
  1952. else
  1953. subpage = 0;
  1954. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1955. if (unlikely(raw))
  1956. status = chip->ecc.write_page_raw(mtd, chip, buf,
  1957. oob_required, page);
  1958. else if (subpage)
  1959. status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
  1960. buf, oob_required, page);
  1961. else
  1962. status = chip->ecc.write_page(mtd, chip, buf, oob_required,
  1963. page);
  1964. if (status < 0)
  1965. return status;
  1966. /*
  1967. * Cached progamming disabled for now. Not sure if it's worth the
  1968. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  1969. */
  1970. cached = 0;
  1971. if (!cached || !NAND_HAS_CACHEPROG(chip)) {
  1972. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1973. status = chip->waitfunc(mtd, chip);
  1974. /*
  1975. * See if operation failed and additional status checks are
  1976. * available.
  1977. */
  1978. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1979. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1980. page);
  1981. if (status & NAND_STATUS_FAIL)
  1982. return -EIO;
  1983. } else {
  1984. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1985. status = chip->waitfunc(mtd, chip);
  1986. }
  1987. return 0;
  1988. }
  1989. /**
  1990. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  1991. * @mtd: MTD device structure
  1992. * @oob: oob data buffer
  1993. * @len: oob data write length
  1994. * @ops: oob ops structure
  1995. */
  1996. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  1997. struct mtd_oob_ops *ops)
  1998. {
  1999. struct nand_chip *chip = mtd_to_nand(mtd);
  2000. /*
  2001. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  2002. * data from a previous OOB read.
  2003. */
  2004. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2005. switch (ops->mode) {
  2006. case MTD_OPS_PLACE_OOB:
  2007. case MTD_OPS_RAW:
  2008. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  2009. return oob + len;
  2010. case MTD_OPS_AUTO_OOB: {
  2011. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  2012. uint32_t boffs = 0, woffs = ops->ooboffs;
  2013. size_t bytes = 0;
  2014. for (; free->length && len; free++, len -= bytes) {
  2015. /* Write request not from offset 0? */
  2016. if (unlikely(woffs)) {
  2017. if (woffs >= free->length) {
  2018. woffs -= free->length;
  2019. continue;
  2020. }
  2021. boffs = free->offset + woffs;
  2022. bytes = min_t(size_t, len,
  2023. (free->length - woffs));
  2024. woffs = 0;
  2025. } else {
  2026. bytes = min_t(size_t, len, free->length);
  2027. boffs = free->offset;
  2028. }
  2029. memcpy(chip->oob_poi + boffs, oob, bytes);
  2030. oob += bytes;
  2031. }
  2032. return oob;
  2033. }
  2034. default:
  2035. BUG();
  2036. }
  2037. return NULL;
  2038. }
  2039. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  2040. /**
  2041. * nand_do_write_ops - [INTERN] NAND write with ECC
  2042. * @mtd: MTD device structure
  2043. * @to: offset to write to
  2044. * @ops: oob operations description structure
  2045. *
  2046. * NAND write with ECC.
  2047. */
  2048. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  2049. struct mtd_oob_ops *ops)
  2050. {
  2051. int chipnr, realpage, page, blockmask, column;
  2052. struct nand_chip *chip = mtd_to_nand(mtd);
  2053. uint32_t writelen = ops->len;
  2054. uint32_t oobwritelen = ops->ooblen;
  2055. uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
  2056. uint8_t *oob = ops->oobbuf;
  2057. uint8_t *buf = ops->datbuf;
  2058. int ret;
  2059. int oob_required = oob ? 1 : 0;
  2060. ops->retlen = 0;
  2061. if (!writelen)
  2062. return 0;
  2063. /* Reject writes, which are not page aligned */
  2064. if (NOTALIGNED(to)) {
  2065. pr_notice("%s: attempt to write non page aligned data\n",
  2066. __func__);
  2067. return -EINVAL;
  2068. }
  2069. column = to & (mtd->writesize - 1);
  2070. chipnr = (int)(to >> chip->chip_shift);
  2071. chip->select_chip(mtd, chipnr);
  2072. /* Check, if it is write protected */
  2073. if (nand_check_wp(mtd)) {
  2074. ret = -EIO;
  2075. goto err_out;
  2076. }
  2077. realpage = (int)(to >> chip->page_shift);
  2078. page = realpage & chip->pagemask;
  2079. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  2080. /* Invalidate the page cache, when we write to the cached page */
  2081. if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
  2082. ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
  2083. chip->pagebuf = -1;
  2084. /* Don't allow multipage oob writes with offset */
  2085. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
  2086. ret = -EINVAL;
  2087. goto err_out;
  2088. }
  2089. while (1) {
  2090. int bytes = mtd->writesize;
  2091. int cached = writelen > bytes && page != blockmask;
  2092. uint8_t *wbuf = buf;
  2093. int use_bufpoi;
  2094. int part_pagewr = (column || writelen < (mtd->writesize - 1));
  2095. if (part_pagewr)
  2096. use_bufpoi = 1;
  2097. else
  2098. use_bufpoi = 0;
  2099. WATCHDOG_RESET();
  2100. /* Partial page write?, or need to use bounce buffer */
  2101. if (use_bufpoi) {
  2102. pr_debug("%s: using write bounce buffer for buf@%p\n",
  2103. __func__, buf);
  2104. cached = 0;
  2105. if (part_pagewr)
  2106. bytes = min_t(int, bytes - column, writelen);
  2107. chip->pagebuf = -1;
  2108. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  2109. memcpy(&chip->buffers->databuf[column], buf, bytes);
  2110. wbuf = chip->buffers->databuf;
  2111. }
  2112. if (unlikely(oob)) {
  2113. size_t len = min(oobwritelen, oobmaxlen);
  2114. oob = nand_fill_oob(mtd, oob, len, ops);
  2115. oobwritelen -= len;
  2116. } else {
  2117. /* We still need to erase leftover OOB data */
  2118. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2119. }
  2120. ret = chip->write_page(mtd, chip, column, bytes, wbuf,
  2121. oob_required, page, cached,
  2122. (ops->mode == MTD_OPS_RAW));
  2123. if (ret)
  2124. break;
  2125. writelen -= bytes;
  2126. if (!writelen)
  2127. break;
  2128. column = 0;
  2129. buf += bytes;
  2130. realpage++;
  2131. page = realpage & chip->pagemask;
  2132. /* Check, if we cross a chip boundary */
  2133. if (!page) {
  2134. chipnr++;
  2135. chip->select_chip(mtd, -1);
  2136. chip->select_chip(mtd, chipnr);
  2137. }
  2138. }
  2139. ops->retlen = ops->len - writelen;
  2140. if (unlikely(oob))
  2141. ops->oobretlen = ops->ooblen;
  2142. err_out:
  2143. chip->select_chip(mtd, -1);
  2144. return ret;
  2145. }
  2146. /**
  2147. * panic_nand_write - [MTD Interface] NAND write with ECC
  2148. * @mtd: MTD device structure
  2149. * @to: offset to write to
  2150. * @len: number of bytes to write
  2151. * @retlen: pointer to variable to store the number of written bytes
  2152. * @buf: the data to write
  2153. *
  2154. * NAND write with ECC. Used when performing writes in interrupt context, this
  2155. * may for example be called by mtdoops when writing an oops while in panic.
  2156. */
  2157. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2158. size_t *retlen, const uint8_t *buf)
  2159. {
  2160. struct nand_chip *chip = mtd_to_nand(mtd);
  2161. struct mtd_oob_ops ops;
  2162. int ret;
  2163. /* Wait for the device to get ready */
  2164. panic_nand_wait(mtd, chip, 400);
  2165. /* Grab the device */
  2166. panic_nand_get_device(chip, mtd, FL_WRITING);
  2167. memset(&ops, 0, sizeof(ops));
  2168. ops.len = len;
  2169. ops.datbuf = (uint8_t *)buf;
  2170. ops.mode = MTD_OPS_PLACE_OOB;
  2171. ret = nand_do_write_ops(mtd, to, &ops);
  2172. *retlen = ops.retlen;
  2173. return ret;
  2174. }
  2175. /**
  2176. * nand_write - [MTD Interface] NAND write with ECC
  2177. * @mtd: MTD device structure
  2178. * @to: offset to write to
  2179. * @len: number of bytes to write
  2180. * @retlen: pointer to variable to store the number of written bytes
  2181. * @buf: the data to write
  2182. *
  2183. * NAND write with ECC.
  2184. */
  2185. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2186. size_t *retlen, const uint8_t *buf)
  2187. {
  2188. struct mtd_oob_ops ops;
  2189. int ret;
  2190. nand_get_device(mtd, FL_WRITING);
  2191. memset(&ops, 0, sizeof(ops));
  2192. ops.len = len;
  2193. ops.datbuf = (uint8_t *)buf;
  2194. ops.mode = MTD_OPS_PLACE_OOB;
  2195. ret = nand_do_write_ops(mtd, to, &ops);
  2196. *retlen = ops.retlen;
  2197. nand_release_device(mtd);
  2198. return ret;
  2199. }
  2200. /**
  2201. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2202. * @mtd: MTD device structure
  2203. * @to: offset to write to
  2204. * @ops: oob operation description structure
  2205. *
  2206. * NAND write out-of-band.
  2207. */
  2208. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2209. struct mtd_oob_ops *ops)
  2210. {
  2211. int chipnr, page, status, len;
  2212. struct nand_chip *chip = mtd_to_nand(mtd);
  2213. pr_debug("%s: to = 0x%08x, len = %i\n",
  2214. __func__, (unsigned int)to, (int)ops->ooblen);
  2215. len = mtd_oobavail(mtd, ops);
  2216. /* Do not allow write past end of page */
  2217. if ((ops->ooboffs + ops->ooblen) > len) {
  2218. pr_debug("%s: attempt to write past end of page\n",
  2219. __func__);
  2220. return -EINVAL;
  2221. }
  2222. if (unlikely(ops->ooboffs >= len)) {
  2223. pr_debug("%s: attempt to start write outside oob\n",
  2224. __func__);
  2225. return -EINVAL;
  2226. }
  2227. /* Do not allow write past end of device */
  2228. if (unlikely(to >= mtd->size ||
  2229. ops->ooboffs + ops->ooblen >
  2230. ((mtd->size >> chip->page_shift) -
  2231. (to >> chip->page_shift)) * len)) {
  2232. pr_debug("%s: attempt to write beyond end of device\n",
  2233. __func__);
  2234. return -EINVAL;
  2235. }
  2236. chipnr = (int)(to >> chip->chip_shift);
  2237. chip->select_chip(mtd, chipnr);
  2238. /* Shift to get page */
  2239. page = (int)(to >> chip->page_shift);
  2240. /*
  2241. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2242. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2243. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2244. * it in the doc2000 driver in August 1999. dwmw2.
  2245. */
  2246. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2247. /* Check, if it is write protected */
  2248. if (nand_check_wp(mtd)) {
  2249. chip->select_chip(mtd, -1);
  2250. return -EROFS;
  2251. }
  2252. /* Invalidate the page cache, if we write to the cached page */
  2253. if (page == chip->pagebuf)
  2254. chip->pagebuf = -1;
  2255. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2256. if (ops->mode == MTD_OPS_RAW)
  2257. status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
  2258. else
  2259. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2260. chip->select_chip(mtd, -1);
  2261. if (status)
  2262. return status;
  2263. ops->oobretlen = ops->ooblen;
  2264. return 0;
  2265. }
  2266. /**
  2267. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2268. * @mtd: MTD device structure
  2269. * @to: offset to write to
  2270. * @ops: oob operation description structure
  2271. */
  2272. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2273. struct mtd_oob_ops *ops)
  2274. {
  2275. int ret = -ENOTSUPP;
  2276. ops->retlen = 0;
  2277. /* Do not allow writes past end of device */
  2278. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2279. pr_debug("%s: attempt to write beyond end of device\n",
  2280. __func__);
  2281. return -EINVAL;
  2282. }
  2283. nand_get_device(mtd, FL_WRITING);
  2284. switch (ops->mode) {
  2285. case MTD_OPS_PLACE_OOB:
  2286. case MTD_OPS_AUTO_OOB:
  2287. case MTD_OPS_RAW:
  2288. break;
  2289. default:
  2290. goto out;
  2291. }
  2292. if (!ops->datbuf)
  2293. ret = nand_do_write_oob(mtd, to, ops);
  2294. else
  2295. ret = nand_do_write_ops(mtd, to, ops);
  2296. out:
  2297. nand_release_device(mtd);
  2298. return ret;
  2299. }
  2300. /**
  2301. * single_erase - [GENERIC] NAND standard block erase command function
  2302. * @mtd: MTD device structure
  2303. * @page: the page address of the block which will be erased
  2304. *
  2305. * Standard erase command for NAND chips. Returns NAND status.
  2306. */
  2307. static int single_erase(struct mtd_info *mtd, int page)
  2308. {
  2309. struct nand_chip *chip = mtd_to_nand(mtd);
  2310. /* Send commands to erase a block */
  2311. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2312. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2313. return chip->waitfunc(mtd, chip);
  2314. }
  2315. /**
  2316. * nand_erase - [MTD Interface] erase block(s)
  2317. * @mtd: MTD device structure
  2318. * @instr: erase instruction
  2319. *
  2320. * Erase one ore more blocks.
  2321. */
  2322. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2323. {
  2324. return nand_erase_nand(mtd, instr, 0);
  2325. }
  2326. /**
  2327. * nand_erase_nand - [INTERN] erase block(s)
  2328. * @mtd: MTD device structure
  2329. * @instr: erase instruction
  2330. * @allowbbt: allow erasing the bbt area
  2331. *
  2332. * Erase one ore more blocks.
  2333. */
  2334. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2335. int allowbbt)
  2336. {
  2337. int page, status, pages_per_block, ret, chipnr;
  2338. struct nand_chip *chip = mtd_to_nand(mtd);
  2339. loff_t len;
  2340. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2341. __func__, (unsigned long long)instr->addr,
  2342. (unsigned long long)instr->len);
  2343. if (check_offs_len(mtd, instr->addr, instr->len))
  2344. return -EINVAL;
  2345. /* Grab the lock and see if the device is available */
  2346. nand_get_device(mtd, FL_ERASING);
  2347. /* Shift to get first page */
  2348. page = (int)(instr->addr >> chip->page_shift);
  2349. chipnr = (int)(instr->addr >> chip->chip_shift);
  2350. /* Calculate pages in each block */
  2351. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2352. /* Select the NAND device */
  2353. chip->select_chip(mtd, chipnr);
  2354. /* Check, if it is write protected */
  2355. if (nand_check_wp(mtd)) {
  2356. pr_debug("%s: device is write protected!\n",
  2357. __func__);
  2358. instr->state = MTD_ERASE_FAILED;
  2359. goto erase_exit;
  2360. }
  2361. /* Loop through the pages */
  2362. len = instr->len;
  2363. instr->state = MTD_ERASING;
  2364. while (len) {
  2365. WATCHDOG_RESET();
  2366. /* Check if we have a bad block, we do not erase bad blocks! */
  2367. if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
  2368. chip->page_shift, allowbbt)) {
  2369. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2370. __func__, page);
  2371. instr->state = MTD_ERASE_FAILED;
  2372. goto erase_exit;
  2373. }
  2374. /*
  2375. * Invalidate the page cache, if we erase the block which
  2376. * contains the current cached page.
  2377. */
  2378. if (page <= chip->pagebuf && chip->pagebuf <
  2379. (page + pages_per_block))
  2380. chip->pagebuf = -1;
  2381. status = chip->erase(mtd, page & chip->pagemask);
  2382. /*
  2383. * See if operation failed and additional status checks are
  2384. * available
  2385. */
  2386. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2387. status = chip->errstat(mtd, chip, FL_ERASING,
  2388. status, page);
  2389. /* See if block erase succeeded */
  2390. if (status & NAND_STATUS_FAIL) {
  2391. pr_debug("%s: failed erase, page 0x%08x\n",
  2392. __func__, page);
  2393. instr->state = MTD_ERASE_FAILED;
  2394. instr->fail_addr =
  2395. ((loff_t)page << chip->page_shift);
  2396. goto erase_exit;
  2397. }
  2398. /* Increment page address and decrement length */
  2399. len -= (1ULL << chip->phys_erase_shift);
  2400. page += pages_per_block;
  2401. /* Check, if we cross a chip boundary */
  2402. if (len && !(page & chip->pagemask)) {
  2403. chipnr++;
  2404. chip->select_chip(mtd, -1);
  2405. chip->select_chip(mtd, chipnr);
  2406. }
  2407. }
  2408. instr->state = MTD_ERASE_DONE;
  2409. erase_exit:
  2410. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2411. /* Deselect and wake up anyone waiting on the device */
  2412. chip->select_chip(mtd, -1);
  2413. nand_release_device(mtd);
  2414. /* Do call back function */
  2415. if (!ret)
  2416. mtd_erase_callback(instr);
  2417. /* Return more or less happy */
  2418. return ret;
  2419. }
  2420. /**
  2421. * nand_sync - [MTD Interface] sync
  2422. * @mtd: MTD device structure
  2423. *
  2424. * Sync is actually a wait for chip ready function.
  2425. */
  2426. static void nand_sync(struct mtd_info *mtd)
  2427. {
  2428. pr_debug("%s: called\n", __func__);
  2429. /* Grab the lock and see if the device is available */
  2430. nand_get_device(mtd, FL_SYNCING);
  2431. /* Release it and go back */
  2432. nand_release_device(mtd);
  2433. }
  2434. /**
  2435. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2436. * @mtd: MTD device structure
  2437. * @offs: offset relative to mtd start
  2438. */
  2439. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2440. {
  2441. struct nand_chip *chip = mtd_to_nand(mtd);
  2442. int chipnr = (int)(offs >> chip->chip_shift);
  2443. int ret;
  2444. /* Select the NAND device */
  2445. nand_get_device(mtd, FL_READING);
  2446. chip->select_chip(mtd, chipnr);
  2447. ret = nand_block_checkbad(mtd, offs, 0);
  2448. chip->select_chip(mtd, -1);
  2449. nand_release_device(mtd);
  2450. return ret;
  2451. }
  2452. /**
  2453. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2454. * @mtd: MTD device structure
  2455. * @ofs: offset relative to mtd start
  2456. */
  2457. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2458. {
  2459. int ret;
  2460. ret = nand_block_isbad(mtd, ofs);
  2461. if (ret) {
  2462. /* If it was bad already, return success and do nothing */
  2463. if (ret > 0)
  2464. return 0;
  2465. return ret;
  2466. }
  2467. return nand_block_markbad_lowlevel(mtd, ofs);
  2468. }
  2469. /**
  2470. * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
  2471. * @mtd: MTD device structure
  2472. * @chip: nand chip info structure
  2473. * @addr: feature address.
  2474. * @subfeature_param: the subfeature parameters, a four bytes array.
  2475. */
  2476. static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
  2477. int addr, uint8_t *subfeature_param)
  2478. {
  2479. int status;
  2480. int i;
  2481. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2482. if (!chip->onfi_version ||
  2483. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2484. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2485. return -EINVAL;
  2486. #endif
  2487. chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
  2488. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2489. chip->write_byte(mtd, subfeature_param[i]);
  2490. status = chip->waitfunc(mtd, chip);
  2491. if (status & NAND_STATUS_FAIL)
  2492. return -EIO;
  2493. return 0;
  2494. }
  2495. /**
  2496. * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
  2497. * @mtd: MTD device structure
  2498. * @chip: nand chip info structure
  2499. * @addr: feature address.
  2500. * @subfeature_param: the subfeature parameters, a four bytes array.
  2501. */
  2502. static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
  2503. int addr, uint8_t *subfeature_param)
  2504. {
  2505. int i;
  2506. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2507. if (!chip->onfi_version ||
  2508. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2509. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2510. return -EINVAL;
  2511. #endif
  2512. chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
  2513. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2514. *subfeature_param++ = chip->read_byte(mtd);
  2515. return 0;
  2516. }
  2517. /* Set default functions */
  2518. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2519. {
  2520. /* check for proper chip_delay setup, set 20us if not */
  2521. if (!chip->chip_delay)
  2522. chip->chip_delay = 20;
  2523. /* check, if a user supplied command function given */
  2524. if (chip->cmdfunc == NULL)
  2525. chip->cmdfunc = nand_command;
  2526. /* check, if a user supplied wait function given */
  2527. if (chip->waitfunc == NULL)
  2528. chip->waitfunc = nand_wait;
  2529. if (!chip->select_chip)
  2530. chip->select_chip = nand_select_chip;
  2531. /* set for ONFI nand */
  2532. if (!chip->onfi_set_features)
  2533. chip->onfi_set_features = nand_onfi_set_features;
  2534. if (!chip->onfi_get_features)
  2535. chip->onfi_get_features = nand_onfi_get_features;
  2536. /* If called twice, pointers that depend on busw may need to be reset */
  2537. if (!chip->read_byte || chip->read_byte == nand_read_byte)
  2538. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2539. if (!chip->read_word)
  2540. chip->read_word = nand_read_word;
  2541. if (!chip->block_bad)
  2542. chip->block_bad = nand_block_bad;
  2543. if (!chip->block_markbad)
  2544. chip->block_markbad = nand_default_block_markbad;
  2545. if (!chip->write_buf || chip->write_buf == nand_write_buf)
  2546. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2547. if (!chip->write_byte || chip->write_byte == nand_write_byte)
  2548. chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
  2549. if (!chip->read_buf || chip->read_buf == nand_read_buf)
  2550. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2551. if (!chip->scan_bbt)
  2552. chip->scan_bbt = nand_default_bbt;
  2553. if (!chip->controller) {
  2554. chip->controller = &chip->hwcontrol;
  2555. spin_lock_init(&chip->controller->lock);
  2556. init_waitqueue_head(&chip->controller->wq);
  2557. }
  2558. }
  2559. /* Sanitize ONFI strings so we can safely print them */
  2560. static void sanitize_string(char *s, size_t len)
  2561. {
  2562. ssize_t i;
  2563. /* Null terminate */
  2564. s[len - 1] = 0;
  2565. /* Remove non printable chars */
  2566. for (i = 0; i < len - 1; i++) {
  2567. if (s[i] < ' ' || s[i] > 127)
  2568. s[i] = '?';
  2569. }
  2570. /* Remove trailing spaces */
  2571. strim(s);
  2572. }
  2573. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2574. {
  2575. int i;
  2576. while (len--) {
  2577. crc ^= *p++ << 8;
  2578. for (i = 0; i < 8; i++)
  2579. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2580. }
  2581. return crc;
  2582. }
  2583. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2584. /* Parse the Extended Parameter Page. */
  2585. static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
  2586. struct nand_chip *chip, struct nand_onfi_params *p)
  2587. {
  2588. struct onfi_ext_param_page *ep;
  2589. struct onfi_ext_section *s;
  2590. struct onfi_ext_ecc_info *ecc;
  2591. uint8_t *cursor;
  2592. int ret = -EINVAL;
  2593. int len;
  2594. int i;
  2595. len = le16_to_cpu(p->ext_param_page_length) * 16;
  2596. ep = kmalloc(len, GFP_KERNEL);
  2597. if (!ep)
  2598. return -ENOMEM;
  2599. /* Send our own NAND_CMD_PARAM. */
  2600. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2601. /* Use the Change Read Column command to skip the ONFI param pages. */
  2602. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  2603. sizeof(*p) * p->num_of_param_pages , -1);
  2604. /* Read out the Extended Parameter Page. */
  2605. chip->read_buf(mtd, (uint8_t *)ep, len);
  2606. if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
  2607. != le16_to_cpu(ep->crc))) {
  2608. pr_debug("fail in the CRC.\n");
  2609. goto ext_out;
  2610. }
  2611. /*
  2612. * Check the signature.
  2613. * Do not strictly follow the ONFI spec, maybe changed in future.
  2614. */
  2615. if (strncmp((char *)ep->sig, "EPPS", 4)) {
  2616. pr_debug("The signature is invalid.\n");
  2617. goto ext_out;
  2618. }
  2619. /* find the ECC section. */
  2620. cursor = (uint8_t *)(ep + 1);
  2621. for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
  2622. s = ep->sections + i;
  2623. if (s->type == ONFI_SECTION_TYPE_2)
  2624. break;
  2625. cursor += s->length * 16;
  2626. }
  2627. if (i == ONFI_EXT_SECTION_MAX) {
  2628. pr_debug("We can not find the ECC section.\n");
  2629. goto ext_out;
  2630. }
  2631. /* get the info we want. */
  2632. ecc = (struct onfi_ext_ecc_info *)cursor;
  2633. if (!ecc->codeword_size) {
  2634. pr_debug("Invalid codeword size\n");
  2635. goto ext_out;
  2636. }
  2637. chip->ecc_strength_ds = ecc->ecc_bits;
  2638. chip->ecc_step_ds = 1 << ecc->codeword_size;
  2639. ret = 0;
  2640. ext_out:
  2641. kfree(ep);
  2642. return ret;
  2643. }
  2644. static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
  2645. {
  2646. struct nand_chip *chip = mtd_to_nand(mtd);
  2647. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  2648. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  2649. feature);
  2650. }
  2651. /*
  2652. * Configure chip properties from Micron vendor-specific ONFI table
  2653. */
  2654. static void nand_onfi_detect_micron(struct nand_chip *chip,
  2655. struct nand_onfi_params *p)
  2656. {
  2657. struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
  2658. if (le16_to_cpu(p->vendor_revision) < 1)
  2659. return;
  2660. chip->read_retries = micron->read_retry_options;
  2661. chip->setup_read_retry = nand_setup_read_retry_micron;
  2662. }
  2663. /*
  2664. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2665. */
  2666. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2667. int *busw)
  2668. {
  2669. struct nand_onfi_params *p = &chip->onfi_params;
  2670. int i, j;
  2671. int val;
  2672. /* Try ONFI for unknown chip or LP */
  2673. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2674. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2675. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2676. return 0;
  2677. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2678. for (i = 0; i < 3; i++) {
  2679. for (j = 0; j < sizeof(*p); j++)
  2680. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  2681. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2682. le16_to_cpu(p->crc)) {
  2683. break;
  2684. }
  2685. }
  2686. if (i == 3) {
  2687. pr_err("Could not find valid ONFI parameter page; aborting\n");
  2688. return 0;
  2689. }
  2690. /* Check version */
  2691. val = le16_to_cpu(p->revision);
  2692. if (val & (1 << 5))
  2693. chip->onfi_version = 23;
  2694. else if (val & (1 << 4))
  2695. chip->onfi_version = 22;
  2696. else if (val & (1 << 3))
  2697. chip->onfi_version = 21;
  2698. else if (val & (1 << 2))
  2699. chip->onfi_version = 20;
  2700. else if (val & (1 << 1))
  2701. chip->onfi_version = 10;
  2702. if (!chip->onfi_version) {
  2703. pr_info("unsupported ONFI version: %d\n", val);
  2704. return 0;
  2705. }
  2706. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2707. sanitize_string(p->model, sizeof(p->model));
  2708. if (!mtd->name)
  2709. mtd->name = p->model;
  2710. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2711. /*
  2712. * pages_per_block and blocks_per_lun may not be a power-of-2 size
  2713. * (don't ask me who thought of this...). MTD assumes that these
  2714. * dimensions will be power-of-2, so just truncate the remaining area.
  2715. */
  2716. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2717. mtd->erasesize *= mtd->writesize;
  2718. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2719. /* See erasesize comment */
  2720. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2721. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2722. chip->bits_per_cell = p->bits_per_cell;
  2723. if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
  2724. *busw = NAND_BUSWIDTH_16;
  2725. else
  2726. *busw = 0;
  2727. if (p->ecc_bits != 0xff) {
  2728. chip->ecc_strength_ds = p->ecc_bits;
  2729. chip->ecc_step_ds = 512;
  2730. } else if (chip->onfi_version >= 21 &&
  2731. (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
  2732. /*
  2733. * The nand_flash_detect_ext_param_page() uses the
  2734. * Change Read Column command which maybe not supported
  2735. * by the chip->cmdfunc. So try to update the chip->cmdfunc
  2736. * now. We do not replace user supplied command function.
  2737. */
  2738. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2739. chip->cmdfunc = nand_command_lp;
  2740. /* The Extended Parameter Page is supported since ONFI 2.1. */
  2741. if (nand_flash_detect_ext_param_page(mtd, chip, p))
  2742. pr_warn("Failed to detect ONFI extended param page\n");
  2743. } else {
  2744. pr_warn("Could not retrieve ONFI ECC requirements\n");
  2745. }
  2746. if (p->jedec_id == NAND_MFR_MICRON)
  2747. nand_onfi_detect_micron(chip, p);
  2748. return 1;
  2749. }
  2750. #else
  2751. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2752. int *busw)
  2753. {
  2754. return 0;
  2755. }
  2756. #endif
  2757. /*
  2758. * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
  2759. */
  2760. static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
  2761. int *busw)
  2762. {
  2763. struct nand_jedec_params *p = &chip->jedec_params;
  2764. struct jedec_ecc_info *ecc;
  2765. int val;
  2766. int i, j;
  2767. /* Try JEDEC for unknown chip or LP */
  2768. chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
  2769. if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
  2770. chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
  2771. chip->read_byte(mtd) != 'C')
  2772. return 0;
  2773. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
  2774. for (i = 0; i < 3; i++) {
  2775. for (j = 0; j < sizeof(*p); j++)
  2776. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  2777. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
  2778. le16_to_cpu(p->crc))
  2779. break;
  2780. }
  2781. if (i == 3) {
  2782. pr_err("Could not find valid JEDEC parameter page; aborting\n");
  2783. return 0;
  2784. }
  2785. /* Check version */
  2786. val = le16_to_cpu(p->revision);
  2787. if (val & (1 << 2))
  2788. chip->jedec_version = 10;
  2789. else if (val & (1 << 1))
  2790. chip->jedec_version = 1; /* vendor specific version */
  2791. if (!chip->jedec_version) {
  2792. pr_info("unsupported JEDEC version: %d\n", val);
  2793. return 0;
  2794. }
  2795. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2796. sanitize_string(p->model, sizeof(p->model));
  2797. if (!mtd->name)
  2798. mtd->name = p->model;
  2799. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2800. /* Please reference to the comment for nand_flash_detect_onfi. */
  2801. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2802. mtd->erasesize *= mtd->writesize;
  2803. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2804. /* Please reference to the comment for nand_flash_detect_onfi. */
  2805. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2806. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2807. chip->bits_per_cell = p->bits_per_cell;
  2808. if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
  2809. *busw = NAND_BUSWIDTH_16;
  2810. else
  2811. *busw = 0;
  2812. /* ECC info */
  2813. ecc = &p->ecc_info[0];
  2814. if (ecc->codeword_size >= 9) {
  2815. chip->ecc_strength_ds = ecc->ecc_bits;
  2816. chip->ecc_step_ds = 1 << ecc->codeword_size;
  2817. } else {
  2818. pr_warn("Invalid codeword size\n");
  2819. }
  2820. return 1;
  2821. }
  2822. /*
  2823. * nand_id_has_period - Check if an ID string has a given wraparound period
  2824. * @id_data: the ID string
  2825. * @arrlen: the length of the @id_data array
  2826. * @period: the period of repitition
  2827. *
  2828. * Check if an ID string is repeated within a given sequence of bytes at
  2829. * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
  2830. * period of 3). This is a helper function for nand_id_len(). Returns non-zero
  2831. * if the repetition has a period of @period; otherwise, returns zero.
  2832. */
  2833. static int nand_id_has_period(u8 *id_data, int arrlen, int period)
  2834. {
  2835. int i, j;
  2836. for (i = 0; i < period; i++)
  2837. for (j = i + period; j < arrlen; j += period)
  2838. if (id_data[i] != id_data[j])
  2839. return 0;
  2840. return 1;
  2841. }
  2842. /*
  2843. * nand_id_len - Get the length of an ID string returned by CMD_READID
  2844. * @id_data: the ID string
  2845. * @arrlen: the length of the @id_data array
  2846. * Returns the length of the ID string, according to known wraparound/trailing
  2847. * zero patterns. If no pattern exists, returns the length of the array.
  2848. */
  2849. static int nand_id_len(u8 *id_data, int arrlen)
  2850. {
  2851. int last_nonzero, period;
  2852. /* Find last non-zero byte */
  2853. for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
  2854. if (id_data[last_nonzero])
  2855. break;
  2856. /* All zeros */
  2857. if (last_nonzero < 0)
  2858. return 0;
  2859. /* Calculate wraparound period */
  2860. for (period = 1; period < arrlen; period++)
  2861. if (nand_id_has_period(id_data, arrlen, period))
  2862. break;
  2863. /* There's a repeated pattern */
  2864. if (period < arrlen)
  2865. return period;
  2866. /* There are trailing zeros */
  2867. if (last_nonzero < arrlen - 1)
  2868. return last_nonzero + 1;
  2869. /* No pattern detected */
  2870. return arrlen;
  2871. }
  2872. /* Extract the bits of per cell from the 3rd byte of the extended ID */
  2873. static int nand_get_bits_per_cell(u8 cellinfo)
  2874. {
  2875. int bits;
  2876. bits = cellinfo & NAND_CI_CELLTYPE_MSK;
  2877. bits >>= NAND_CI_CELLTYPE_SHIFT;
  2878. return bits + 1;
  2879. }
  2880. /*
  2881. * Many new NAND share similar device ID codes, which represent the size of the
  2882. * chip. The rest of the parameters must be decoded according to generic or
  2883. * manufacturer-specific "extended ID" decoding patterns.
  2884. */
  2885. static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
  2886. u8 id_data[8], int *busw)
  2887. {
  2888. int extid, id_len;
  2889. /* The 3rd id byte holds MLC / multichip data */
  2890. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  2891. /* The 4th id byte is the important one */
  2892. extid = id_data[3];
  2893. id_len = nand_id_len(id_data, 8);
  2894. /*
  2895. * Field definitions are in the following datasheets:
  2896. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2897. * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
  2898. * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
  2899. *
  2900. * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
  2901. * ID to decide what to do.
  2902. */
  2903. if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
  2904. !nand_is_slc(chip) && id_data[5] != 0x00) {
  2905. /* Calc pagesize */
  2906. mtd->writesize = 2048 << (extid & 0x03);
  2907. extid >>= 2;
  2908. /* Calc oobsize */
  2909. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  2910. case 1:
  2911. mtd->oobsize = 128;
  2912. break;
  2913. case 2:
  2914. mtd->oobsize = 218;
  2915. break;
  2916. case 3:
  2917. mtd->oobsize = 400;
  2918. break;
  2919. case 4:
  2920. mtd->oobsize = 436;
  2921. break;
  2922. case 5:
  2923. mtd->oobsize = 512;
  2924. break;
  2925. case 6:
  2926. mtd->oobsize = 640;
  2927. break;
  2928. case 7:
  2929. default: /* Other cases are "reserved" (unknown) */
  2930. mtd->oobsize = 1024;
  2931. break;
  2932. }
  2933. extid >>= 2;
  2934. /* Calc blocksize */
  2935. mtd->erasesize = (128 * 1024) <<
  2936. (((extid >> 1) & 0x04) | (extid & 0x03));
  2937. *busw = 0;
  2938. } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
  2939. !nand_is_slc(chip)) {
  2940. unsigned int tmp;
  2941. /* Calc pagesize */
  2942. mtd->writesize = 2048 << (extid & 0x03);
  2943. extid >>= 2;
  2944. /* Calc oobsize */
  2945. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  2946. case 0:
  2947. mtd->oobsize = 128;
  2948. break;
  2949. case 1:
  2950. mtd->oobsize = 224;
  2951. break;
  2952. case 2:
  2953. mtd->oobsize = 448;
  2954. break;
  2955. case 3:
  2956. mtd->oobsize = 64;
  2957. break;
  2958. case 4:
  2959. mtd->oobsize = 32;
  2960. break;
  2961. case 5:
  2962. mtd->oobsize = 16;
  2963. break;
  2964. default:
  2965. mtd->oobsize = 640;
  2966. break;
  2967. }
  2968. extid >>= 2;
  2969. /* Calc blocksize */
  2970. tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
  2971. if (tmp < 0x03)
  2972. mtd->erasesize = (128 * 1024) << tmp;
  2973. else if (tmp == 0x03)
  2974. mtd->erasesize = 768 * 1024;
  2975. else
  2976. mtd->erasesize = (64 * 1024) << tmp;
  2977. *busw = 0;
  2978. } else {
  2979. /* Calc pagesize */
  2980. mtd->writesize = 1024 << (extid & 0x03);
  2981. extid >>= 2;
  2982. /* Calc oobsize */
  2983. mtd->oobsize = (8 << (extid & 0x01)) *
  2984. (mtd->writesize >> 9);
  2985. extid >>= 2;
  2986. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2987. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2988. extid >>= 2;
  2989. /* Get buswidth information */
  2990. *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2991. /*
  2992. * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
  2993. * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
  2994. * follows:
  2995. * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
  2996. * 110b -> 24nm
  2997. * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
  2998. */
  2999. if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
  3000. nand_is_slc(chip) &&
  3001. (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
  3002. !(id_data[4] & 0x80) /* !BENAND */) {
  3003. mtd->oobsize = 32 * mtd->writesize >> 9;
  3004. }
  3005. }
  3006. }
  3007. /*
  3008. * Old devices have chip data hardcoded in the device ID table. nand_decode_id
  3009. * decodes a matching ID table entry and assigns the MTD size parameters for
  3010. * the chip.
  3011. */
  3012. static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
  3013. struct nand_flash_dev *type, u8 id_data[8],
  3014. int *busw)
  3015. {
  3016. int maf_id = id_data[0];
  3017. mtd->erasesize = type->erasesize;
  3018. mtd->writesize = type->pagesize;
  3019. mtd->oobsize = mtd->writesize / 32;
  3020. *busw = type->options & NAND_BUSWIDTH_16;
  3021. /* All legacy ID NAND are small-page, SLC */
  3022. chip->bits_per_cell = 1;
  3023. /*
  3024. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  3025. * some Spansion chips have erasesize that conflicts with size
  3026. * listed in nand_ids table.
  3027. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  3028. */
  3029. if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
  3030. && id_data[6] == 0x00 && id_data[7] == 0x00
  3031. && mtd->writesize == 512) {
  3032. mtd->erasesize = 128 * 1024;
  3033. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  3034. }
  3035. }
  3036. /*
  3037. * Set the bad block marker/indicator (BBM/BBI) patterns according to some
  3038. * heuristic patterns using various detected parameters (e.g., manufacturer,
  3039. * page size, cell-type information).
  3040. */
  3041. static void nand_decode_bbm_options(struct mtd_info *mtd,
  3042. struct nand_chip *chip, u8 id_data[8])
  3043. {
  3044. int maf_id = id_data[0];
  3045. /* Set the bad block position */
  3046. if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
  3047. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  3048. else
  3049. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  3050. /*
  3051. * Bad block marker is stored in the last page of each block on Samsung
  3052. * and Hynix MLC devices; stored in first two pages of each block on
  3053. * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
  3054. * AMD/Spansion, and Macronix. All others scan only the first page.
  3055. */
  3056. if (!nand_is_slc(chip) &&
  3057. (maf_id == NAND_MFR_SAMSUNG ||
  3058. maf_id == NAND_MFR_HYNIX))
  3059. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  3060. else if ((nand_is_slc(chip) &&
  3061. (maf_id == NAND_MFR_SAMSUNG ||
  3062. maf_id == NAND_MFR_HYNIX ||
  3063. maf_id == NAND_MFR_TOSHIBA ||
  3064. maf_id == NAND_MFR_AMD ||
  3065. maf_id == NAND_MFR_MACRONIX)) ||
  3066. (mtd->writesize == 2048 &&
  3067. maf_id == NAND_MFR_MICRON))
  3068. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  3069. }
  3070. static inline bool is_full_id_nand(struct nand_flash_dev *type)
  3071. {
  3072. return type->id_len;
  3073. }
  3074. static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
  3075. struct nand_flash_dev *type, u8 *id_data, int *busw)
  3076. {
  3077. if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
  3078. mtd->writesize = type->pagesize;
  3079. mtd->erasesize = type->erasesize;
  3080. mtd->oobsize = type->oobsize;
  3081. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  3082. chip->chipsize = (uint64_t)type->chipsize << 20;
  3083. chip->options |= type->options;
  3084. chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
  3085. chip->ecc_step_ds = NAND_ECC_STEP(type);
  3086. chip->onfi_timing_mode_default =
  3087. type->onfi_timing_mode_default;
  3088. *busw = type->options & NAND_BUSWIDTH_16;
  3089. if (!mtd->name)
  3090. mtd->name = type->name;
  3091. return true;
  3092. }
  3093. return false;
  3094. }
  3095. /*
  3096. * Get the flash and manufacturer id and lookup if the type is supported.
  3097. */
  3098. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  3099. struct nand_chip *chip,
  3100. int *maf_id, int *dev_id,
  3101. struct nand_flash_dev *type)
  3102. {
  3103. int busw;
  3104. int i, maf_idx;
  3105. u8 id_data[8];
  3106. /* Select the device */
  3107. chip->select_chip(mtd, 0);
  3108. /*
  3109. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  3110. * after power-up.
  3111. */
  3112. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3113. /* Send the command for reading device ID */
  3114. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3115. /* Read manufacturer and device IDs */
  3116. *maf_id = chip->read_byte(mtd);
  3117. *dev_id = chip->read_byte(mtd);
  3118. /*
  3119. * Try again to make sure, as some systems the bus-hold or other
  3120. * interface concerns can cause random data which looks like a
  3121. * possibly credible NAND flash to appear. If the two results do
  3122. * not match, ignore the device completely.
  3123. */
  3124. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3125. /* Read entire ID string */
  3126. for (i = 0; i < 8; i++)
  3127. id_data[i] = chip->read_byte(mtd);
  3128. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  3129. pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
  3130. *maf_id, *dev_id, id_data[0], id_data[1]);
  3131. return ERR_PTR(-ENODEV);
  3132. }
  3133. if (!type)
  3134. type = nand_flash_ids;
  3135. for (; type->name != NULL; type++) {
  3136. if (is_full_id_nand(type)) {
  3137. if (find_full_id_nand(mtd, chip, type, id_data, &busw))
  3138. goto ident_done;
  3139. } else if (*dev_id == type->dev_id) {
  3140. break;
  3141. }
  3142. }
  3143. chip->onfi_version = 0;
  3144. if (!type->name || !type->pagesize) {
  3145. /* Check if the chip is ONFI compliant */
  3146. if (nand_flash_detect_onfi(mtd, chip, &busw))
  3147. goto ident_done;
  3148. /* Check if the chip is JEDEC compliant */
  3149. if (nand_flash_detect_jedec(mtd, chip, &busw))
  3150. goto ident_done;
  3151. }
  3152. if (!type->name)
  3153. return ERR_PTR(-ENODEV);
  3154. if (!mtd->name)
  3155. mtd->name = type->name;
  3156. chip->chipsize = (uint64_t)type->chipsize << 20;
  3157. if (!type->pagesize) {
  3158. /* Decode parameters from extended ID */
  3159. nand_decode_ext_id(mtd, chip, id_data, &busw);
  3160. } else {
  3161. nand_decode_id(mtd, chip, type, id_data, &busw);
  3162. }
  3163. /* Get chip options */
  3164. chip->options |= type->options;
  3165. /*
  3166. * Check if chip is not a Samsung device. Do not clear the
  3167. * options for chips which do not have an extended id.
  3168. */
  3169. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  3170. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  3171. ident_done:
  3172. /* Try to identify manufacturer */
  3173. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  3174. if (nand_manuf_ids[maf_idx].id == *maf_id)
  3175. break;
  3176. }
  3177. if (chip->options & NAND_BUSWIDTH_AUTO) {
  3178. WARN_ON(chip->options & NAND_BUSWIDTH_16);
  3179. chip->options |= busw;
  3180. nand_set_defaults(chip, busw);
  3181. } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  3182. /*
  3183. * Check, if buswidth is correct. Hardware drivers should set
  3184. * chip correct!
  3185. */
  3186. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3187. *maf_id, *dev_id);
  3188. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
  3189. pr_warn("bus width %d instead %d bit\n",
  3190. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  3191. busw ? 16 : 8);
  3192. return ERR_PTR(-EINVAL);
  3193. }
  3194. nand_decode_bbm_options(mtd, chip, id_data);
  3195. /* Calculate the address shift from the page size */
  3196. chip->page_shift = ffs(mtd->writesize) - 1;
  3197. /* Convert chipsize to number of pages per chip -1 */
  3198. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  3199. chip->bbt_erase_shift = chip->phys_erase_shift =
  3200. ffs(mtd->erasesize) - 1;
  3201. if (chip->chipsize & 0xffffffff)
  3202. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  3203. else {
  3204. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  3205. chip->chip_shift += 32 - 1;
  3206. }
  3207. chip->badblockbits = 8;
  3208. chip->erase = single_erase;
  3209. /* Do not replace user supplied command function! */
  3210. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  3211. chip->cmdfunc = nand_command_lp;
  3212. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3213. *maf_id, *dev_id);
  3214. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  3215. if (chip->onfi_version)
  3216. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3217. chip->onfi_params.model);
  3218. else if (chip->jedec_version)
  3219. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3220. chip->jedec_params.model);
  3221. else
  3222. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3223. type->name);
  3224. #else
  3225. if (chip->jedec_version)
  3226. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3227. chip->jedec_params.model);
  3228. else
  3229. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3230. type->name);
  3231. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3232. type->name);
  3233. #endif
  3234. pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
  3235. (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
  3236. mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
  3237. return type;
  3238. }
  3239. /**
  3240. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  3241. * @mtd: MTD device structure
  3242. * @maxchips: number of chips to scan for
  3243. * @table: alternative NAND ID table
  3244. *
  3245. * This is the first phase of the normal nand_scan() function. It reads the
  3246. * flash ID and sets up MTD fields accordingly.
  3247. *
  3248. */
  3249. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  3250. struct nand_flash_dev *table)
  3251. {
  3252. int i, nand_maf_id, nand_dev_id;
  3253. struct nand_chip *chip = mtd_to_nand(mtd);
  3254. struct nand_flash_dev *type;
  3255. /* Set the default functions */
  3256. nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
  3257. /* Read the flash type */
  3258. type = nand_get_flash_type(mtd, chip, &nand_maf_id,
  3259. &nand_dev_id, table);
  3260. if (IS_ERR(type)) {
  3261. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  3262. pr_warn("No NAND device found\n");
  3263. chip->select_chip(mtd, -1);
  3264. return PTR_ERR(type);
  3265. }
  3266. chip->select_chip(mtd, -1);
  3267. /* Check for a chip array */
  3268. for (i = 1; i < maxchips; i++) {
  3269. chip->select_chip(mtd, i);
  3270. /* See comment in nand_get_flash_type for reset */
  3271. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3272. /* Send the command for reading device ID */
  3273. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3274. /* Read manufacturer and device IDs */
  3275. if (nand_maf_id != chip->read_byte(mtd) ||
  3276. nand_dev_id != chip->read_byte(mtd)) {
  3277. chip->select_chip(mtd, -1);
  3278. break;
  3279. }
  3280. chip->select_chip(mtd, -1);
  3281. }
  3282. #ifdef DEBUG
  3283. if (i > 1)
  3284. pr_info("%d chips detected\n", i);
  3285. #endif
  3286. /* Store the number of chips and calc total size for mtd */
  3287. chip->numchips = i;
  3288. mtd->size = i * chip->chipsize;
  3289. return 0;
  3290. }
  3291. EXPORT_SYMBOL(nand_scan_ident);
  3292. /*
  3293. * Check if the chip configuration meet the datasheet requirements.
  3294. * If our configuration corrects A bits per B bytes and the minimum
  3295. * required correction level is X bits per Y bytes, then we must ensure
  3296. * both of the following are true:
  3297. *
  3298. * (1) A / B >= X / Y
  3299. * (2) A >= X
  3300. *
  3301. * Requirement (1) ensures we can correct for the required bitflip density.
  3302. * Requirement (2) ensures we can correct even when all bitflips are clumped
  3303. * in the same sector.
  3304. */
  3305. static bool nand_ecc_strength_good(struct mtd_info *mtd)
  3306. {
  3307. struct nand_chip *chip = mtd_to_nand(mtd);
  3308. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3309. int corr, ds_corr;
  3310. if (ecc->size == 0 || chip->ecc_step_ds == 0)
  3311. /* Not enough information */
  3312. return true;
  3313. /*
  3314. * We get the number of corrected bits per page to compare
  3315. * the correction density.
  3316. */
  3317. corr = (mtd->writesize * ecc->strength) / ecc->size;
  3318. ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
  3319. return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
  3320. }
  3321. /**
  3322. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  3323. * @mtd: MTD device structure
  3324. *
  3325. * This is the second phase of the normal nand_scan() function. It fills out
  3326. * all the uninitialized function pointers with the defaults and scans for a
  3327. * bad block table if appropriate.
  3328. */
  3329. int nand_scan_tail(struct mtd_info *mtd)
  3330. {
  3331. int i;
  3332. struct nand_chip *chip = mtd_to_nand(mtd);
  3333. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3334. struct nand_buffers *nbuf;
  3335. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  3336. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  3337. !(chip->bbt_options & NAND_BBT_USE_FLASH));
  3338. if (!(chip->options & NAND_OWN_BUFFERS)) {
  3339. nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
  3340. chip->buffers = nbuf;
  3341. } else {
  3342. if (!chip->buffers)
  3343. return -ENOMEM;
  3344. }
  3345. /* Set the internal oob buffer location, just after the page data */
  3346. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  3347. /*
  3348. * If no default placement scheme is given, select an appropriate one.
  3349. */
  3350. if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  3351. switch (mtd->oobsize) {
  3352. case 8:
  3353. ecc->layout = &nand_oob_8;
  3354. break;
  3355. case 16:
  3356. ecc->layout = &nand_oob_16;
  3357. break;
  3358. case 64:
  3359. ecc->layout = &nand_oob_64;
  3360. break;
  3361. case 128:
  3362. ecc->layout = &nand_oob_128;
  3363. break;
  3364. default:
  3365. pr_warn("No oob scheme defined for oobsize %d\n",
  3366. mtd->oobsize);
  3367. BUG();
  3368. }
  3369. }
  3370. if (!chip->write_page)
  3371. chip->write_page = nand_write_page;
  3372. /*
  3373. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  3374. * selected and we have 256 byte pagesize fallback to software ECC
  3375. */
  3376. switch (ecc->mode) {
  3377. case NAND_ECC_HW_OOB_FIRST:
  3378. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  3379. if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
  3380. pr_warn("No ECC functions supplied; hardware ECC not possible\n");
  3381. BUG();
  3382. }
  3383. if (!ecc->read_page)
  3384. ecc->read_page = nand_read_page_hwecc_oob_first;
  3385. case NAND_ECC_HW:
  3386. /* Use standard hwecc read page function? */
  3387. if (!ecc->read_page)
  3388. ecc->read_page = nand_read_page_hwecc;
  3389. if (!ecc->write_page)
  3390. ecc->write_page = nand_write_page_hwecc;
  3391. if (!ecc->read_page_raw)
  3392. ecc->read_page_raw = nand_read_page_raw;
  3393. if (!ecc->write_page_raw)
  3394. ecc->write_page_raw = nand_write_page_raw;
  3395. if (!ecc->read_oob)
  3396. ecc->read_oob = nand_read_oob_std;
  3397. if (!ecc->write_oob)
  3398. ecc->write_oob = nand_write_oob_std;
  3399. if (!ecc->read_subpage)
  3400. ecc->read_subpage = nand_read_subpage;
  3401. if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
  3402. ecc->write_subpage = nand_write_subpage_hwecc;
  3403. case NAND_ECC_HW_SYNDROME:
  3404. if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
  3405. (!ecc->read_page ||
  3406. ecc->read_page == nand_read_page_hwecc ||
  3407. !ecc->write_page ||
  3408. ecc->write_page == nand_write_page_hwecc)) {
  3409. pr_warn("No ECC functions supplied; hardware ECC not possible\n");
  3410. BUG();
  3411. }
  3412. /* Use standard syndrome read/write page function? */
  3413. if (!ecc->read_page)
  3414. ecc->read_page = nand_read_page_syndrome;
  3415. if (!ecc->write_page)
  3416. ecc->write_page = nand_write_page_syndrome;
  3417. if (!ecc->read_page_raw)
  3418. ecc->read_page_raw = nand_read_page_raw_syndrome;
  3419. if (!ecc->write_page_raw)
  3420. ecc->write_page_raw = nand_write_page_raw_syndrome;
  3421. if (!ecc->read_oob)
  3422. ecc->read_oob = nand_read_oob_syndrome;
  3423. if (!ecc->write_oob)
  3424. ecc->write_oob = nand_write_oob_syndrome;
  3425. if (mtd->writesize >= ecc->size) {
  3426. if (!ecc->strength) {
  3427. pr_warn("Driver must set ecc.strength when using hardware ECC\n");
  3428. BUG();
  3429. }
  3430. break;
  3431. }
  3432. pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
  3433. ecc->size, mtd->writesize);
  3434. ecc->mode = NAND_ECC_SOFT;
  3435. case NAND_ECC_SOFT:
  3436. ecc->calculate = nand_calculate_ecc;
  3437. ecc->correct = nand_correct_data;
  3438. ecc->read_page = nand_read_page_swecc;
  3439. ecc->read_subpage = nand_read_subpage;
  3440. ecc->write_page = nand_write_page_swecc;
  3441. ecc->read_page_raw = nand_read_page_raw;
  3442. ecc->write_page_raw = nand_write_page_raw;
  3443. ecc->read_oob = nand_read_oob_std;
  3444. ecc->write_oob = nand_write_oob_std;
  3445. if (!ecc->size)
  3446. ecc->size = 256;
  3447. ecc->bytes = 3;
  3448. ecc->strength = 1;
  3449. break;
  3450. case NAND_ECC_SOFT_BCH:
  3451. if (!mtd_nand_has_bch()) {
  3452. pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  3453. BUG();
  3454. }
  3455. ecc->calculate = nand_bch_calculate_ecc;
  3456. ecc->correct = nand_bch_correct_data;
  3457. ecc->read_page = nand_read_page_swecc;
  3458. ecc->read_subpage = nand_read_subpage;
  3459. ecc->write_page = nand_write_page_swecc;
  3460. ecc->read_page_raw = nand_read_page_raw;
  3461. ecc->write_page_raw = nand_write_page_raw;
  3462. ecc->read_oob = nand_read_oob_std;
  3463. ecc->write_oob = nand_write_oob_std;
  3464. /*
  3465. * Board driver should supply ecc.size and ecc.strength values
  3466. * to select how many bits are correctable. Otherwise, default
  3467. * to 4 bits for large page devices.
  3468. */
  3469. if (!ecc->size && (mtd->oobsize >= 64)) {
  3470. ecc->size = 512;
  3471. ecc->strength = 4;
  3472. }
  3473. /* See nand_bch_init() for details. */
  3474. ecc->bytes = 0;
  3475. ecc->priv = nand_bch_init(mtd);
  3476. if (!ecc->priv) {
  3477. pr_warn("BCH ECC initialization failed!\n");
  3478. BUG();
  3479. }
  3480. break;
  3481. case NAND_ECC_NONE:
  3482. pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
  3483. ecc->read_page = nand_read_page_raw;
  3484. ecc->write_page = nand_write_page_raw;
  3485. ecc->read_oob = nand_read_oob_std;
  3486. ecc->read_page_raw = nand_read_page_raw;
  3487. ecc->write_page_raw = nand_write_page_raw;
  3488. ecc->write_oob = nand_write_oob_std;
  3489. ecc->size = mtd->writesize;
  3490. ecc->bytes = 0;
  3491. ecc->strength = 0;
  3492. break;
  3493. default:
  3494. pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
  3495. BUG();
  3496. }
  3497. /* For many systems, the standard OOB write also works for raw */
  3498. if (!ecc->read_oob_raw)
  3499. ecc->read_oob_raw = ecc->read_oob;
  3500. if (!ecc->write_oob_raw)
  3501. ecc->write_oob_raw = ecc->write_oob;
  3502. /*
  3503. * The number of bytes available for a client to place data into
  3504. * the out of band area.
  3505. */
  3506. mtd->oobavail = 0;
  3507. if (ecc->layout) {
  3508. for (i = 0; ecc->layout->oobfree[i].length; i++)
  3509. mtd->oobavail += ecc->layout->oobfree[i].length;
  3510. }
  3511. /* ECC sanity check: warn if it's too weak */
  3512. if (!nand_ecc_strength_good(mtd))
  3513. pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  3514. mtd->name);
  3515. /*
  3516. * Set the number of read / write steps for one page depending on ECC
  3517. * mode.
  3518. */
  3519. ecc->steps = mtd->writesize / ecc->size;
  3520. if (ecc->steps * ecc->size != mtd->writesize) {
  3521. pr_warn("Invalid ECC parameters\n");
  3522. BUG();
  3523. }
  3524. ecc->total = ecc->steps * ecc->bytes;
  3525. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  3526. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
  3527. switch (ecc->steps) {
  3528. case 2:
  3529. mtd->subpage_sft = 1;
  3530. break;
  3531. case 4:
  3532. case 8:
  3533. case 16:
  3534. mtd->subpage_sft = 2;
  3535. break;
  3536. }
  3537. }
  3538. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3539. /* Initialize state */
  3540. chip->state = FL_READY;
  3541. /* Invalidate the pagebuffer reference */
  3542. chip->pagebuf = -1;
  3543. /* Large page NAND with SOFT_ECC should support subpage reads */
  3544. switch (ecc->mode) {
  3545. case NAND_ECC_SOFT:
  3546. case NAND_ECC_SOFT_BCH:
  3547. if (chip->page_shift > 9)
  3548. chip->options |= NAND_SUBPAGE_READ;
  3549. break;
  3550. default:
  3551. break;
  3552. }
  3553. /* Fill in remaining MTD driver data */
  3554. mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
  3555. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  3556. MTD_CAP_NANDFLASH;
  3557. mtd->_erase = nand_erase;
  3558. mtd->_read = nand_read;
  3559. mtd->_write = nand_write;
  3560. mtd->_panic_write = panic_nand_write;
  3561. mtd->_read_oob = nand_read_oob;
  3562. mtd->_write_oob = nand_write_oob;
  3563. mtd->_sync = nand_sync;
  3564. mtd->_lock = NULL;
  3565. mtd->_unlock = NULL;
  3566. mtd->_block_isreserved = nand_block_isreserved;
  3567. mtd->_block_isbad = nand_block_isbad;
  3568. mtd->_block_markbad = nand_block_markbad;
  3569. mtd->writebufsize = mtd->writesize;
  3570. /* propagate ecc info to mtd_info */
  3571. mtd->ecclayout = ecc->layout;
  3572. mtd->ecc_strength = ecc->strength;
  3573. mtd->ecc_step_size = ecc->size;
  3574. /*
  3575. * Initialize bitflip_threshold to its default prior scan_bbt() call.
  3576. * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
  3577. * properly set.
  3578. */
  3579. if (!mtd->bitflip_threshold)
  3580. mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
  3581. return 0;
  3582. }
  3583. EXPORT_SYMBOL(nand_scan_tail);
  3584. /**
  3585. * nand_scan - [NAND Interface] Scan for the NAND device
  3586. * @mtd: MTD device structure
  3587. * @maxchips: number of chips to scan for
  3588. *
  3589. * This fills out all the uninitialized function pointers with the defaults.
  3590. * The flash ID is read and the mtd/chip structures are filled with the
  3591. * appropriate values.
  3592. */
  3593. int nand_scan(struct mtd_info *mtd, int maxchips)
  3594. {
  3595. int ret;
  3596. ret = nand_scan_ident(mtd, maxchips, NULL);
  3597. if (!ret)
  3598. ret = nand_scan_tail(mtd);
  3599. return ret;
  3600. }
  3601. EXPORT_SYMBOL(nand_scan);
  3602. MODULE_LICENSE("GPL");
  3603. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3604. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3605. MODULE_DESCRIPTION("Generic NAND flash driver code");