nand_base.c 122 KB

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