nand_base.c 117 KB

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