nand_base.c 128 KB

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