nand_base.c 131 KB

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