nand_base.c 131 KB

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