nand_base.c 117 KB

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