nand_base.c 128 KB

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