onenand_base.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright © 2005-2009 Samsung Electronics
  5. * Copyright © 2007 Nokia Corporation
  6. *
  7. * Kyungmin Park <kyungmin.park@samsung.com>
  8. *
  9. * Credits:
  10. * Adrian Hunter <ext-adrian.hunter@nokia.com>:
  11. * auto-placement support, read-while load support, various fixes
  12. *
  13. * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
  14. * Flex-OneNAND support
  15. * Amul Kumar Saha <amul.saha at samsung.com>
  16. * OTP support
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/onenand.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <asm/io.h>
  35. /*
  36. * Multiblock erase if number of blocks to erase is 2 or more.
  37. * Maximum number of blocks for simultaneous erase is 64.
  38. */
  39. #define MB_ERASE_MIN_BLK_COUNT 2
  40. #define MB_ERASE_MAX_BLK_COUNT 64
  41. /* Default Flex-OneNAND boundary and lock respectively */
  42. static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
  43. module_param_array(flex_bdry, int, NULL, 0400);
  44. MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
  45. "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
  46. "DIE_BDRY: SLC boundary of the die"
  47. "LOCK: Locking information for SLC boundary"
  48. " : 0->Set boundary in unlocked status"
  49. " : 1->Set boundary in locked status");
  50. /* Default OneNAND/Flex-OneNAND OTP options*/
  51. static int otp;
  52. module_param(otp, int, 0400);
  53. MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
  54. "Syntax : otp=LOCK_TYPE"
  55. "LOCK_TYPE : Keys issued, for specific OTP Lock type"
  56. " : 0 -> Default (No Blocks Locked)"
  57. " : 1 -> OTP Block lock"
  58. " : 2 -> 1st Block lock"
  59. " : 3 -> BOTH OTP Block and 1st Block lock");
  60. /*
  61. * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
  62. * For now, we expose only 64 out of 80 ecc bytes
  63. */
  64. static struct nand_ecclayout flexonenand_oob_128 = {
  65. .eccbytes = 64,
  66. .eccpos = {
  67. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  68. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  69. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  70. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  71. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  72. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  73. 102, 103, 104, 105
  74. },
  75. .oobfree = {
  76. {2, 4}, {18, 4}, {34, 4}, {50, 4},
  77. {66, 4}, {82, 4}, {98, 4}, {114, 4}
  78. }
  79. };
  80. /*
  81. * onenand_oob_128 - oob info for OneNAND with 4KB page
  82. *
  83. * Based on specification:
  84. * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
  85. *
  86. * For eccpos we expose only 64 bytes out of 72 (see struct nand_ecclayout)
  87. *
  88. * oobfree uses the spare area fields marked as
  89. * "Managed by internal ECC logic for Logical Sector Number area"
  90. */
  91. static struct nand_ecclayout onenand_oob_128 = {
  92. .eccbytes = 64,
  93. .eccpos = {
  94. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  95. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  96. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  97. 55, 56, 57, 58, 59, 60, 61, 62, 63,
  98. 71, 72, 73, 74, 75, 76, 77, 78, 79,
  99. 87, 88, 89, 90, 91, 92, 93, 94, 95,
  100. 103, 104, 105, 106, 107, 108, 109, 110, 111,
  101. 119
  102. },
  103. .oobfree = {
  104. {2, 3}, {18, 3}, {34, 3}, {50, 3},
  105. {66, 3}, {82, 3}, {98, 3}, {114, 3}
  106. }
  107. };
  108. /**
  109. * onenand_oob_64 - oob info for large (2KB) page
  110. */
  111. static struct nand_ecclayout onenand_oob_64 = {
  112. .eccbytes = 20,
  113. .eccpos = {
  114. 8, 9, 10, 11, 12,
  115. 24, 25, 26, 27, 28,
  116. 40, 41, 42, 43, 44,
  117. 56, 57, 58, 59, 60,
  118. },
  119. .oobfree = {
  120. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  121. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  122. }
  123. };
  124. /**
  125. * onenand_oob_32 - oob info for middle (1KB) page
  126. */
  127. static struct nand_ecclayout onenand_oob_32 = {
  128. .eccbytes = 10,
  129. .eccpos = {
  130. 8, 9, 10, 11, 12,
  131. 24, 25, 26, 27, 28,
  132. },
  133. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  134. };
  135. static const unsigned char ffchars[] = {
  136. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  137. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  138. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  139. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  140. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  141. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  142. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  143. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  144. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  145. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
  146. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  147. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
  148. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  149. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
  150. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  151. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
  152. };
  153. /**
  154. * onenand_readw - [OneNAND Interface] Read OneNAND register
  155. * @param addr address to read
  156. *
  157. * Read OneNAND register
  158. */
  159. static unsigned short onenand_readw(void __iomem *addr)
  160. {
  161. return readw(addr);
  162. }
  163. /**
  164. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  165. * @param value value to write
  166. * @param addr address to write
  167. *
  168. * Write OneNAND register with value
  169. */
  170. static void onenand_writew(unsigned short value, void __iomem *addr)
  171. {
  172. writew(value, addr);
  173. }
  174. /**
  175. * onenand_block_address - [DEFAULT] Get block address
  176. * @param this onenand chip data structure
  177. * @param block the block
  178. * @return translated block address if DDP, otherwise same
  179. *
  180. * Setup Start Address 1 Register (F100h)
  181. */
  182. static int onenand_block_address(struct onenand_chip *this, int block)
  183. {
  184. /* Device Flash Core select, NAND Flash Block Address */
  185. if (block & this->density_mask)
  186. return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
  187. return block;
  188. }
  189. /**
  190. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  191. * @param this onenand chip data structure
  192. * @param block the block
  193. * @return set DBS value if DDP, otherwise 0
  194. *
  195. * Setup Start Address 2 Register (F101h) for DDP
  196. */
  197. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  198. {
  199. /* Device BufferRAM Select */
  200. if (block & this->density_mask)
  201. return ONENAND_DDP_CHIP1;
  202. return ONENAND_DDP_CHIP0;
  203. }
  204. /**
  205. * onenand_page_address - [DEFAULT] Get page address
  206. * @param page the page address
  207. * @param sector the sector address
  208. * @return combined page and sector address
  209. *
  210. * Setup Start Address 8 Register (F107h)
  211. */
  212. static int onenand_page_address(int page, int sector)
  213. {
  214. /* Flash Page Address, Flash Sector Address */
  215. int fpa, fsa;
  216. fpa = page & ONENAND_FPA_MASK;
  217. fsa = sector & ONENAND_FSA_MASK;
  218. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  219. }
  220. /**
  221. * onenand_buffer_address - [DEFAULT] Get buffer address
  222. * @param dataram1 DataRAM index
  223. * @param sectors the sector address
  224. * @param count the number of sectors
  225. * @return the start buffer value
  226. *
  227. * Setup Start Buffer Register (F200h)
  228. */
  229. static int onenand_buffer_address(int dataram1, int sectors, int count)
  230. {
  231. int bsa, bsc;
  232. /* BufferRAM Sector Address */
  233. bsa = sectors & ONENAND_BSA_MASK;
  234. if (dataram1)
  235. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  236. else
  237. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  238. /* BufferRAM Sector Count */
  239. bsc = count & ONENAND_BSC_MASK;
  240. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  241. }
  242. /**
  243. * flexonenand_block- For given address return block number
  244. * @param this - OneNAND device structure
  245. * @param addr - Address for which block number is needed
  246. */
  247. static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
  248. {
  249. unsigned boundary, blk, die = 0;
  250. if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
  251. die = 1;
  252. addr -= this->diesize[0];
  253. }
  254. boundary = this->boundary[die];
  255. blk = addr >> (this->erase_shift - 1);
  256. if (blk > boundary)
  257. blk = (blk + boundary + 1) >> 1;
  258. blk += die ? this->density_mask : 0;
  259. return blk;
  260. }
  261. inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
  262. {
  263. if (!FLEXONENAND(this))
  264. return addr >> this->erase_shift;
  265. return flexonenand_block(this, addr);
  266. }
  267. /**
  268. * flexonenand_addr - Return address of the block
  269. * @this: OneNAND device structure
  270. * @block: Block number on Flex-OneNAND
  271. *
  272. * Return address of the block
  273. */
  274. static loff_t flexonenand_addr(struct onenand_chip *this, int block)
  275. {
  276. loff_t ofs = 0;
  277. int die = 0, boundary;
  278. if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
  279. block -= this->density_mask;
  280. die = 1;
  281. ofs = this->diesize[0];
  282. }
  283. boundary = this->boundary[die];
  284. ofs += (loff_t)block << (this->erase_shift - 1);
  285. if (block > (boundary + 1))
  286. ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
  287. return ofs;
  288. }
  289. loff_t onenand_addr(struct onenand_chip *this, int block)
  290. {
  291. if (!FLEXONENAND(this))
  292. return (loff_t)block << this->erase_shift;
  293. return flexonenand_addr(this, block);
  294. }
  295. EXPORT_SYMBOL(onenand_addr);
  296. /**
  297. * onenand_get_density - [DEFAULT] Get OneNAND density
  298. * @param dev_id OneNAND device ID
  299. *
  300. * Get OneNAND density from device ID
  301. */
  302. static inline int onenand_get_density(int dev_id)
  303. {
  304. int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  305. return (density & ONENAND_DEVICE_DENSITY_MASK);
  306. }
  307. /**
  308. * flexonenand_region - [Flex-OneNAND] Return erase region of addr
  309. * @param mtd MTD device structure
  310. * @param addr address whose erase region needs to be identified
  311. */
  312. int flexonenand_region(struct mtd_info *mtd, loff_t addr)
  313. {
  314. int i;
  315. for (i = 0; i < mtd->numeraseregions; i++)
  316. if (addr < mtd->eraseregions[i].offset)
  317. break;
  318. return i - 1;
  319. }
  320. EXPORT_SYMBOL(flexonenand_region);
  321. /**
  322. * onenand_command - [DEFAULT] Send command to OneNAND device
  323. * @param mtd MTD device structure
  324. * @param cmd the command to be sent
  325. * @param addr offset to read from or write to
  326. * @param len number of bytes to read or write
  327. *
  328. * Send command to OneNAND device. This function is used for middle/large page
  329. * devices (1KB/2KB Bytes per page)
  330. */
  331. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  332. {
  333. struct onenand_chip *this = mtd->priv;
  334. int value, block, page;
  335. /* Address translation */
  336. switch (cmd) {
  337. case ONENAND_CMD_UNLOCK:
  338. case ONENAND_CMD_LOCK:
  339. case ONENAND_CMD_LOCK_TIGHT:
  340. case ONENAND_CMD_UNLOCK_ALL:
  341. block = -1;
  342. page = -1;
  343. break;
  344. case FLEXONENAND_CMD_PI_ACCESS:
  345. /* addr contains die index */
  346. block = addr * this->density_mask;
  347. page = -1;
  348. break;
  349. case ONENAND_CMD_ERASE:
  350. case ONENAND_CMD_MULTIBLOCK_ERASE:
  351. case ONENAND_CMD_ERASE_VERIFY:
  352. case ONENAND_CMD_BUFFERRAM:
  353. case ONENAND_CMD_OTP_ACCESS:
  354. block = onenand_block(this, addr);
  355. page = -1;
  356. break;
  357. case FLEXONENAND_CMD_READ_PI:
  358. cmd = ONENAND_CMD_READ;
  359. block = addr * this->density_mask;
  360. page = 0;
  361. break;
  362. default:
  363. block = onenand_block(this, addr);
  364. if (FLEXONENAND(this))
  365. page = (int) (addr - onenand_addr(this, block))>>\
  366. this->page_shift;
  367. else
  368. page = (int) (addr >> this->page_shift);
  369. if (ONENAND_IS_2PLANE(this)) {
  370. /* Make the even block number */
  371. block &= ~1;
  372. /* Is it the odd plane? */
  373. if (addr & this->writesize)
  374. block++;
  375. page >>= 1;
  376. }
  377. page &= this->page_mask;
  378. break;
  379. }
  380. /* NOTE: The setting order of the registers is very important! */
  381. if (cmd == ONENAND_CMD_BUFFERRAM) {
  382. /* Select DataRAM for DDP */
  383. value = onenand_bufferram_address(this, block);
  384. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  385. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
  386. /* It is always BufferRAM0 */
  387. ONENAND_SET_BUFFERRAM0(this);
  388. else
  389. /* Switch to the next data buffer */
  390. ONENAND_SET_NEXT_BUFFERRAM(this);
  391. return 0;
  392. }
  393. if (block != -1) {
  394. /* Write 'DFS, FBA' of Flash */
  395. value = onenand_block_address(this, block);
  396. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  397. /* Select DataRAM for DDP */
  398. value = onenand_bufferram_address(this, block);
  399. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  400. }
  401. if (page != -1) {
  402. /* Now we use page size operation */
  403. int sectors = 0, count = 0;
  404. int dataram;
  405. switch (cmd) {
  406. case FLEXONENAND_CMD_RECOVER_LSB:
  407. case ONENAND_CMD_READ:
  408. case ONENAND_CMD_READOOB:
  409. if (ONENAND_IS_4KB_PAGE(this))
  410. /* It is always BufferRAM0 */
  411. dataram = ONENAND_SET_BUFFERRAM0(this);
  412. else
  413. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  414. break;
  415. default:
  416. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  417. cmd = ONENAND_CMD_2X_PROG;
  418. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  419. break;
  420. }
  421. /* Write 'FPA, FSA' of Flash */
  422. value = onenand_page_address(page, sectors);
  423. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  424. /* Write 'BSA, BSC' of DataRAM */
  425. value = onenand_buffer_address(dataram, sectors, count);
  426. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  427. }
  428. /* Interrupt clear */
  429. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  430. /* Write command */
  431. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  432. return 0;
  433. }
  434. /**
  435. * onenand_read_ecc - return ecc status
  436. * @param this onenand chip structure
  437. */
  438. static inline int onenand_read_ecc(struct onenand_chip *this)
  439. {
  440. int ecc, i, result = 0;
  441. if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
  442. return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  443. for (i = 0; i < 4; i++) {
  444. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
  445. if (likely(!ecc))
  446. continue;
  447. if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
  448. return ONENAND_ECC_2BIT_ALL;
  449. else
  450. result = ONENAND_ECC_1BIT_ALL;
  451. }
  452. return result;
  453. }
  454. /**
  455. * onenand_wait - [DEFAULT] wait until the command is done
  456. * @param mtd MTD device structure
  457. * @param state state to select the max. timeout value
  458. *
  459. * Wait for command done. This applies to all OneNAND command
  460. * Read can take up to 30us, erase up to 2ms and program up to 350us
  461. * according to general OneNAND specs
  462. */
  463. static int onenand_wait(struct mtd_info *mtd, int state)
  464. {
  465. struct onenand_chip * this = mtd->priv;
  466. unsigned long timeout;
  467. unsigned int flags = ONENAND_INT_MASTER;
  468. unsigned int interrupt = 0;
  469. unsigned int ctrl;
  470. /* The 20 msec is enough */
  471. timeout = jiffies + msecs_to_jiffies(20);
  472. while (time_before(jiffies, timeout)) {
  473. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  474. if (interrupt & flags)
  475. break;
  476. if (state != FL_READING && state != FL_PREPARING_ERASE)
  477. cond_resched();
  478. }
  479. /* To get correct interrupt status in timeout case */
  480. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  481. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  482. /*
  483. * In the Spec. it checks the controller status first
  484. * However if you get the correct information in case of
  485. * power off recovery (POR) test, it should read ECC status first
  486. */
  487. if (interrupt & ONENAND_INT_READ) {
  488. int ecc = onenand_read_ecc(this);
  489. if (ecc) {
  490. if (ecc & ONENAND_ECC_2BIT_ALL) {
  491. printk(KERN_ERR "%s: ECC error = 0x%04x\n",
  492. __func__, ecc);
  493. mtd->ecc_stats.failed++;
  494. return -EBADMSG;
  495. } else if (ecc & ONENAND_ECC_1BIT_ALL) {
  496. printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
  497. __func__, ecc);
  498. mtd->ecc_stats.corrected++;
  499. }
  500. }
  501. } else if (state == FL_READING) {
  502. printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
  503. __func__, ctrl, interrupt);
  504. return -EIO;
  505. }
  506. if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
  507. printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
  508. __func__, ctrl, interrupt);
  509. return -EIO;
  510. }
  511. if (!(interrupt & ONENAND_INT_MASTER)) {
  512. printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
  513. __func__, ctrl, interrupt);
  514. return -EIO;
  515. }
  516. /* If there's controller error, it's a real error */
  517. if (ctrl & ONENAND_CTRL_ERROR) {
  518. printk(KERN_ERR "%s: controller error = 0x%04x\n",
  519. __func__, ctrl);
  520. if (ctrl & ONENAND_CTRL_LOCK)
  521. printk(KERN_ERR "%s: it's locked error.\n", __func__);
  522. return -EIO;
  523. }
  524. return 0;
  525. }
  526. /*
  527. * onenand_interrupt - [DEFAULT] onenand interrupt handler
  528. * @param irq onenand interrupt number
  529. * @param dev_id interrupt data
  530. *
  531. * complete the work
  532. */
  533. static irqreturn_t onenand_interrupt(int irq, void *data)
  534. {
  535. struct onenand_chip *this = data;
  536. /* To handle shared interrupt */
  537. if (!this->complete.done)
  538. complete(&this->complete);
  539. return IRQ_HANDLED;
  540. }
  541. /*
  542. * onenand_interrupt_wait - [DEFAULT] wait until the command is done
  543. * @param mtd MTD device structure
  544. * @param state state to select the max. timeout value
  545. *
  546. * Wait for command done.
  547. */
  548. static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
  549. {
  550. struct onenand_chip *this = mtd->priv;
  551. wait_for_completion(&this->complete);
  552. return onenand_wait(mtd, state);
  553. }
  554. /*
  555. * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
  556. * @param mtd MTD device structure
  557. * @param state state to select the max. timeout value
  558. *
  559. * Try interrupt based wait (It is used one-time)
  560. */
  561. static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
  562. {
  563. struct onenand_chip *this = mtd->priv;
  564. unsigned long remain, timeout;
  565. /* We use interrupt wait first */
  566. this->wait = onenand_interrupt_wait;
  567. timeout = msecs_to_jiffies(100);
  568. remain = wait_for_completion_timeout(&this->complete, timeout);
  569. if (!remain) {
  570. printk(KERN_INFO "OneNAND: There's no interrupt. "
  571. "We use the normal wait\n");
  572. /* Release the irq */
  573. free_irq(this->irq, this);
  574. this->wait = onenand_wait;
  575. }
  576. return onenand_wait(mtd, state);
  577. }
  578. /*
  579. * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
  580. * @param mtd MTD device structure
  581. *
  582. * There's two method to wait onenand work
  583. * 1. polling - read interrupt status register
  584. * 2. interrupt - use the kernel interrupt method
  585. */
  586. static void onenand_setup_wait(struct mtd_info *mtd)
  587. {
  588. struct onenand_chip *this = mtd->priv;
  589. int syscfg;
  590. init_completion(&this->complete);
  591. if (this->irq <= 0) {
  592. this->wait = onenand_wait;
  593. return;
  594. }
  595. if (request_irq(this->irq, &onenand_interrupt,
  596. IRQF_SHARED, "onenand", this)) {
  597. /* If we can't get irq, use the normal wait */
  598. this->wait = onenand_wait;
  599. return;
  600. }
  601. /* Enable interrupt */
  602. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  603. syscfg |= ONENAND_SYS_CFG1_IOBE;
  604. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  605. this->wait = onenand_try_interrupt_wait;
  606. }
  607. /**
  608. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  609. * @param mtd MTD data structure
  610. * @param area BufferRAM area
  611. * @return offset given area
  612. *
  613. * Return BufferRAM offset given area
  614. */
  615. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  616. {
  617. struct onenand_chip *this = mtd->priv;
  618. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  619. /* Note: the 'this->writesize' is a real page size */
  620. if (area == ONENAND_DATARAM)
  621. return this->writesize;
  622. if (area == ONENAND_SPARERAM)
  623. return mtd->oobsize;
  624. }
  625. return 0;
  626. }
  627. /**
  628. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  629. * @param mtd MTD data structure
  630. * @param area BufferRAM area
  631. * @param buffer the databuffer to put/get data
  632. * @param offset offset to read from or write to
  633. * @param count number of bytes to read/write
  634. *
  635. * Read the BufferRAM area
  636. */
  637. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  638. unsigned char *buffer, int offset, size_t count)
  639. {
  640. struct onenand_chip *this = mtd->priv;
  641. void __iomem *bufferram;
  642. bufferram = this->base + area;
  643. bufferram += onenand_bufferram_offset(mtd, area);
  644. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  645. unsigned short word;
  646. /* Align with word(16-bit) size */
  647. count--;
  648. /* Read word and save byte */
  649. word = this->read_word(bufferram + offset + count);
  650. buffer[count] = (word & 0xff);
  651. }
  652. memcpy(buffer, bufferram + offset, count);
  653. return 0;
  654. }
  655. /**
  656. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  657. * @param mtd MTD data structure
  658. * @param area BufferRAM area
  659. * @param buffer the databuffer to put/get data
  660. * @param offset offset to read from or write to
  661. * @param count number of bytes to read/write
  662. *
  663. * Read the BufferRAM area with Sync. Burst Mode
  664. */
  665. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  666. unsigned char *buffer, int offset, size_t count)
  667. {
  668. struct onenand_chip *this = mtd->priv;
  669. void __iomem *bufferram;
  670. bufferram = this->base + area;
  671. bufferram += onenand_bufferram_offset(mtd, area);
  672. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  673. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  674. unsigned short word;
  675. /* Align with word(16-bit) size */
  676. count--;
  677. /* Read word and save byte */
  678. word = this->read_word(bufferram + offset + count);
  679. buffer[count] = (word & 0xff);
  680. }
  681. memcpy(buffer, bufferram + offset, count);
  682. this->mmcontrol(mtd, 0);
  683. return 0;
  684. }
  685. /**
  686. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  687. * @param mtd MTD data structure
  688. * @param area BufferRAM area
  689. * @param buffer the databuffer to put/get data
  690. * @param offset offset to read from or write to
  691. * @param count number of bytes to read/write
  692. *
  693. * Write the BufferRAM area
  694. */
  695. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  696. const unsigned char *buffer, int offset, size_t count)
  697. {
  698. struct onenand_chip *this = mtd->priv;
  699. void __iomem *bufferram;
  700. bufferram = this->base + area;
  701. bufferram += onenand_bufferram_offset(mtd, area);
  702. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  703. unsigned short word;
  704. int byte_offset;
  705. /* Align with word(16-bit) size */
  706. count--;
  707. /* Calculate byte access offset */
  708. byte_offset = offset + count;
  709. /* Read word and save byte */
  710. word = this->read_word(bufferram + byte_offset);
  711. word = (word & ~0xff) | buffer[count];
  712. this->write_word(word, bufferram + byte_offset);
  713. }
  714. memcpy(bufferram + offset, buffer, count);
  715. return 0;
  716. }
  717. /**
  718. * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
  719. * @param mtd MTD data structure
  720. * @param addr address to check
  721. * @return blockpage address
  722. *
  723. * Get blockpage address at 2x program mode
  724. */
  725. static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
  726. {
  727. struct onenand_chip *this = mtd->priv;
  728. int blockpage, block, page;
  729. /* Calculate the even block number */
  730. block = (int) (addr >> this->erase_shift) & ~1;
  731. /* Is it the odd plane? */
  732. if (addr & this->writesize)
  733. block++;
  734. page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
  735. blockpage = (block << 7) | page;
  736. return blockpage;
  737. }
  738. /**
  739. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  740. * @param mtd MTD data structure
  741. * @param addr address to check
  742. * @return 1 if there are valid data, otherwise 0
  743. *
  744. * Check bufferram if there is data we required
  745. */
  746. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  747. {
  748. struct onenand_chip *this = mtd->priv;
  749. int blockpage, found = 0;
  750. unsigned int i;
  751. if (ONENAND_IS_2PLANE(this))
  752. blockpage = onenand_get_2x_blockpage(mtd, addr);
  753. else
  754. blockpage = (int) (addr >> this->page_shift);
  755. /* Is there valid data? */
  756. i = ONENAND_CURRENT_BUFFERRAM(this);
  757. if (this->bufferram[i].blockpage == blockpage)
  758. found = 1;
  759. else {
  760. /* Check another BufferRAM */
  761. i = ONENAND_NEXT_BUFFERRAM(this);
  762. if (this->bufferram[i].blockpage == blockpage) {
  763. ONENAND_SET_NEXT_BUFFERRAM(this);
  764. found = 1;
  765. }
  766. }
  767. if (found && ONENAND_IS_DDP(this)) {
  768. /* Select DataRAM for DDP */
  769. int block = onenand_block(this, addr);
  770. int value = onenand_bufferram_address(this, block);
  771. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  772. }
  773. return found;
  774. }
  775. /**
  776. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  777. * @param mtd MTD data structure
  778. * @param addr address to update
  779. * @param valid valid flag
  780. *
  781. * Update BufferRAM information
  782. */
  783. static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  784. int valid)
  785. {
  786. struct onenand_chip *this = mtd->priv;
  787. int blockpage;
  788. unsigned int i;
  789. if (ONENAND_IS_2PLANE(this))
  790. blockpage = onenand_get_2x_blockpage(mtd, addr);
  791. else
  792. blockpage = (int) (addr >> this->page_shift);
  793. /* Invalidate another BufferRAM */
  794. i = ONENAND_NEXT_BUFFERRAM(this);
  795. if (this->bufferram[i].blockpage == blockpage)
  796. this->bufferram[i].blockpage = -1;
  797. /* Update BufferRAM */
  798. i = ONENAND_CURRENT_BUFFERRAM(this);
  799. if (valid)
  800. this->bufferram[i].blockpage = blockpage;
  801. else
  802. this->bufferram[i].blockpage = -1;
  803. }
  804. /**
  805. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  806. * @param mtd MTD data structure
  807. * @param addr start address to invalidate
  808. * @param len length to invalidate
  809. *
  810. * Invalidate BufferRAM information
  811. */
  812. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  813. unsigned int len)
  814. {
  815. struct onenand_chip *this = mtd->priv;
  816. int i;
  817. loff_t end_addr = addr + len;
  818. /* Invalidate BufferRAM */
  819. for (i = 0; i < MAX_BUFFERRAM; i++) {
  820. loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
  821. if (buf_addr >= addr && buf_addr < end_addr)
  822. this->bufferram[i].blockpage = -1;
  823. }
  824. }
  825. /**
  826. * onenand_get_device - [GENERIC] Get chip for selected access
  827. * @param mtd MTD device structure
  828. * @param new_state the state which is requested
  829. *
  830. * Get the device and lock it for exclusive access
  831. */
  832. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  833. {
  834. struct onenand_chip *this = mtd->priv;
  835. DECLARE_WAITQUEUE(wait, current);
  836. /*
  837. * Grab the lock and see if the device is available
  838. */
  839. while (1) {
  840. spin_lock(&this->chip_lock);
  841. if (this->state == FL_READY) {
  842. this->state = new_state;
  843. spin_unlock(&this->chip_lock);
  844. if (new_state != FL_PM_SUSPENDED && this->enable)
  845. this->enable(mtd);
  846. break;
  847. }
  848. if (new_state == FL_PM_SUSPENDED) {
  849. spin_unlock(&this->chip_lock);
  850. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  851. }
  852. set_current_state(TASK_UNINTERRUPTIBLE);
  853. add_wait_queue(&this->wq, &wait);
  854. spin_unlock(&this->chip_lock);
  855. schedule();
  856. remove_wait_queue(&this->wq, &wait);
  857. }
  858. return 0;
  859. }
  860. /**
  861. * onenand_release_device - [GENERIC] release chip
  862. * @param mtd MTD device structure
  863. *
  864. * Deselect, release chip lock and wake up anyone waiting on the device
  865. */
  866. static void onenand_release_device(struct mtd_info *mtd)
  867. {
  868. struct onenand_chip *this = mtd->priv;
  869. if (this->state != FL_PM_SUSPENDED && this->disable)
  870. this->disable(mtd);
  871. /* Release the chip */
  872. spin_lock(&this->chip_lock);
  873. this->state = FL_READY;
  874. wake_up(&this->wq);
  875. spin_unlock(&this->chip_lock);
  876. }
  877. /**
  878. * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
  879. * @param mtd MTD device structure
  880. * @param buf destination address
  881. * @param column oob offset to read from
  882. * @param thislen oob length to read
  883. */
  884. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
  885. int thislen)
  886. {
  887. struct onenand_chip *this = mtd->priv;
  888. struct nand_oobfree *free;
  889. int readcol = column;
  890. int readend = column + thislen;
  891. int lastgap = 0;
  892. unsigned int i;
  893. uint8_t *oob_buf = this->oob_buf;
  894. free = this->ecclayout->oobfree;
  895. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  896. if (readcol >= lastgap)
  897. readcol += free->offset - lastgap;
  898. if (readend >= lastgap)
  899. readend += free->offset - lastgap;
  900. lastgap = free->offset + free->length;
  901. }
  902. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  903. free = this->ecclayout->oobfree;
  904. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  905. int free_end = free->offset + free->length;
  906. if (free->offset < readend && free_end > readcol) {
  907. int st = max_t(int,free->offset,readcol);
  908. int ed = min_t(int,free_end,readend);
  909. int n = ed - st;
  910. memcpy(buf, oob_buf + st, n);
  911. buf += n;
  912. } else if (column == 0)
  913. break;
  914. }
  915. return 0;
  916. }
  917. /**
  918. * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
  919. * @param mtd MTD device structure
  920. * @param addr address to recover
  921. * @param status return value from onenand_wait / onenand_bbt_wait
  922. *
  923. * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
  924. * lower page address and MSB page has higher page address in paired pages.
  925. * If power off occurs during MSB page program, the paired LSB page data can
  926. * become corrupt. LSB page recovery read is a way to read LSB page though page
  927. * data are corrupted. When uncorrectable error occurs as a result of LSB page
  928. * read after power up, issue LSB page recovery read.
  929. */
  930. static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
  931. {
  932. struct onenand_chip *this = mtd->priv;
  933. int i;
  934. /* Recovery is only for Flex-OneNAND */
  935. if (!FLEXONENAND(this))
  936. return status;
  937. /* check if we failed due to uncorrectable error */
  938. if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
  939. return status;
  940. /* check if address lies in MLC region */
  941. i = flexonenand_region(mtd, addr);
  942. if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
  943. return status;
  944. /* We are attempting to reread, so decrement stats.failed
  945. * which was incremented by onenand_wait due to read failure
  946. */
  947. printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
  948. __func__);
  949. mtd->ecc_stats.failed--;
  950. /* Issue the LSB page recovery command */
  951. this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
  952. return this->wait(mtd, FL_READING);
  953. }
  954. /**
  955. * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
  956. * @param mtd MTD device structure
  957. * @param from offset to read from
  958. * @param ops: oob operation description structure
  959. *
  960. * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
  961. * So, read-while-load is not present.
  962. */
  963. static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  964. struct mtd_oob_ops *ops)
  965. {
  966. struct onenand_chip *this = mtd->priv;
  967. struct mtd_ecc_stats stats;
  968. size_t len = ops->len;
  969. size_t ooblen = ops->ooblen;
  970. u_char *buf = ops->datbuf;
  971. u_char *oobbuf = ops->oobbuf;
  972. int read = 0, column, thislen;
  973. int oobread = 0, oobcolumn, thisooblen, oobsize;
  974. int ret = 0;
  975. int writesize = this->writesize;
  976. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  977. (int)len);
  978. if (ops->mode == MTD_OPS_AUTO_OOB)
  979. oobsize = this->ecclayout->oobavail;
  980. else
  981. oobsize = mtd->oobsize;
  982. oobcolumn = from & (mtd->oobsize - 1);
  983. /* Do not allow reads past end of device */
  984. if (from + len > mtd->size) {
  985. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  986. __func__);
  987. ops->retlen = 0;
  988. ops->oobretlen = 0;
  989. return -EINVAL;
  990. }
  991. stats = mtd->ecc_stats;
  992. while (read < len) {
  993. cond_resched();
  994. thislen = min_t(int, writesize, len - read);
  995. column = from & (writesize - 1);
  996. if (column + thislen > writesize)
  997. thislen = writesize - column;
  998. if (!onenand_check_bufferram(mtd, from)) {
  999. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1000. ret = this->wait(mtd, FL_READING);
  1001. if (unlikely(ret))
  1002. ret = onenand_recover_lsb(mtd, from, ret);
  1003. onenand_update_bufferram(mtd, from, !ret);
  1004. if (mtd_is_eccerr(ret))
  1005. ret = 0;
  1006. if (ret)
  1007. break;
  1008. }
  1009. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1010. if (oobbuf) {
  1011. thisooblen = oobsize - oobcolumn;
  1012. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1013. if (ops->mode == MTD_OPS_AUTO_OOB)
  1014. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1015. else
  1016. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1017. oobread += thisooblen;
  1018. oobbuf += thisooblen;
  1019. oobcolumn = 0;
  1020. }
  1021. read += thislen;
  1022. if (read == len)
  1023. break;
  1024. from += thislen;
  1025. buf += thislen;
  1026. }
  1027. /*
  1028. * Return success, if no ECC failures, else -EBADMSG
  1029. * fs driver will take care of that, because
  1030. * retlen == desired len and result == -EBADMSG
  1031. */
  1032. ops->retlen = read;
  1033. ops->oobretlen = oobread;
  1034. if (ret)
  1035. return ret;
  1036. if (mtd->ecc_stats.failed - stats.failed)
  1037. return -EBADMSG;
  1038. /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
  1039. return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
  1040. }
  1041. /**
  1042. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  1043. * @param mtd MTD device structure
  1044. * @param from offset to read from
  1045. * @param ops: oob operation description structure
  1046. *
  1047. * OneNAND read main and/or out-of-band data
  1048. */
  1049. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  1050. struct mtd_oob_ops *ops)
  1051. {
  1052. struct onenand_chip *this = mtd->priv;
  1053. struct mtd_ecc_stats stats;
  1054. size_t len = ops->len;
  1055. size_t ooblen = ops->ooblen;
  1056. u_char *buf = ops->datbuf;
  1057. u_char *oobbuf = ops->oobbuf;
  1058. int read = 0, column, thislen;
  1059. int oobread = 0, oobcolumn, thisooblen, oobsize;
  1060. int ret = 0, boundary = 0;
  1061. int writesize = this->writesize;
  1062. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  1063. (int)len);
  1064. if (ops->mode == MTD_OPS_AUTO_OOB)
  1065. oobsize = this->ecclayout->oobavail;
  1066. else
  1067. oobsize = mtd->oobsize;
  1068. oobcolumn = from & (mtd->oobsize - 1);
  1069. /* Do not allow reads past end of device */
  1070. if ((from + len) > mtd->size) {
  1071. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1072. __func__);
  1073. ops->retlen = 0;
  1074. ops->oobretlen = 0;
  1075. return -EINVAL;
  1076. }
  1077. stats = mtd->ecc_stats;
  1078. /* Read-while-load method */
  1079. /* Do first load to bufferRAM */
  1080. if (read < len) {
  1081. if (!onenand_check_bufferram(mtd, from)) {
  1082. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1083. ret = this->wait(mtd, FL_READING);
  1084. onenand_update_bufferram(mtd, from, !ret);
  1085. if (mtd_is_eccerr(ret))
  1086. ret = 0;
  1087. }
  1088. }
  1089. thislen = min_t(int, writesize, len - read);
  1090. column = from & (writesize - 1);
  1091. if (column + thislen > writesize)
  1092. thislen = writesize - column;
  1093. while (!ret) {
  1094. /* If there is more to load then start next load */
  1095. from += thislen;
  1096. if (read + thislen < len) {
  1097. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1098. /*
  1099. * Chip boundary handling in DDP
  1100. * Now we issued chip 1 read and pointed chip 1
  1101. * bufferram so we have to point chip 0 bufferram.
  1102. */
  1103. if (ONENAND_IS_DDP(this) &&
  1104. unlikely(from == (this->chipsize >> 1))) {
  1105. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  1106. boundary = 1;
  1107. } else
  1108. boundary = 0;
  1109. ONENAND_SET_PREV_BUFFERRAM(this);
  1110. }
  1111. /* While load is going, read from last bufferRAM */
  1112. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1113. /* Read oob area if needed */
  1114. if (oobbuf) {
  1115. thisooblen = oobsize - oobcolumn;
  1116. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1117. if (ops->mode == MTD_OPS_AUTO_OOB)
  1118. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1119. else
  1120. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1121. oobread += thisooblen;
  1122. oobbuf += thisooblen;
  1123. oobcolumn = 0;
  1124. }
  1125. /* See if we are done */
  1126. read += thislen;
  1127. if (read == len)
  1128. break;
  1129. /* Set up for next read from bufferRAM */
  1130. if (unlikely(boundary))
  1131. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  1132. ONENAND_SET_NEXT_BUFFERRAM(this);
  1133. buf += thislen;
  1134. thislen = min_t(int, writesize, len - read);
  1135. column = 0;
  1136. cond_resched();
  1137. /* Now wait for load */
  1138. ret = this->wait(mtd, FL_READING);
  1139. onenand_update_bufferram(mtd, from, !ret);
  1140. if (mtd_is_eccerr(ret))
  1141. ret = 0;
  1142. }
  1143. /*
  1144. * Return success, if no ECC failures, else -EBADMSG
  1145. * fs driver will take care of that, because
  1146. * retlen == desired len and result == -EBADMSG
  1147. */
  1148. ops->retlen = read;
  1149. ops->oobretlen = oobread;
  1150. if (ret)
  1151. return ret;
  1152. if (mtd->ecc_stats.failed - stats.failed)
  1153. return -EBADMSG;
  1154. /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
  1155. return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
  1156. }
  1157. /**
  1158. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  1159. * @param mtd MTD device structure
  1160. * @param from offset to read from
  1161. * @param ops: oob operation description structure
  1162. *
  1163. * OneNAND read out-of-band data from the spare area
  1164. */
  1165. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  1166. struct mtd_oob_ops *ops)
  1167. {
  1168. struct onenand_chip *this = mtd->priv;
  1169. struct mtd_ecc_stats stats;
  1170. int read = 0, thislen, column, oobsize;
  1171. size_t len = ops->ooblen;
  1172. unsigned int mode = ops->mode;
  1173. u_char *buf = ops->oobbuf;
  1174. int ret = 0, readcmd;
  1175. from += ops->ooboffs;
  1176. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  1177. (int)len);
  1178. /* Initialize return length value */
  1179. ops->oobretlen = 0;
  1180. if (mode == MTD_OPS_AUTO_OOB)
  1181. oobsize = this->ecclayout->oobavail;
  1182. else
  1183. oobsize = mtd->oobsize;
  1184. column = from & (mtd->oobsize - 1);
  1185. if (unlikely(column >= oobsize)) {
  1186. printk(KERN_ERR "%s: Attempted to start read outside oob\n",
  1187. __func__);
  1188. return -EINVAL;
  1189. }
  1190. /* Do not allow reads past end of device */
  1191. if (unlikely(from >= mtd->size ||
  1192. column + len > ((mtd->size >> this->page_shift) -
  1193. (from >> this->page_shift)) * oobsize)) {
  1194. printk(KERN_ERR "%s: Attempted to read beyond end of device\n",
  1195. __func__);
  1196. return -EINVAL;
  1197. }
  1198. stats = mtd->ecc_stats;
  1199. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1200. while (read < len) {
  1201. cond_resched();
  1202. thislen = oobsize - column;
  1203. thislen = min_t(int, thislen, len);
  1204. this->command(mtd, readcmd, from, mtd->oobsize);
  1205. onenand_update_bufferram(mtd, from, 0);
  1206. ret = this->wait(mtd, FL_READING);
  1207. if (unlikely(ret))
  1208. ret = onenand_recover_lsb(mtd, from, ret);
  1209. if (ret && !mtd_is_eccerr(ret)) {
  1210. printk(KERN_ERR "%s: read failed = 0x%x\n",
  1211. __func__, ret);
  1212. break;
  1213. }
  1214. if (mode == MTD_OPS_AUTO_OOB)
  1215. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  1216. else
  1217. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1218. read += thislen;
  1219. if (read == len)
  1220. break;
  1221. buf += thislen;
  1222. /* Read more? */
  1223. if (read < len) {
  1224. /* Page size */
  1225. from += mtd->writesize;
  1226. column = 0;
  1227. }
  1228. }
  1229. ops->oobretlen = read;
  1230. if (ret)
  1231. return ret;
  1232. if (mtd->ecc_stats.failed - stats.failed)
  1233. return -EBADMSG;
  1234. return 0;
  1235. }
  1236. /**
  1237. * onenand_read - [MTD Interface] Read data from flash
  1238. * @param mtd MTD device structure
  1239. * @param from offset to read from
  1240. * @param len number of bytes to read
  1241. * @param retlen pointer to variable to store the number of read bytes
  1242. * @param buf the databuffer to put data
  1243. *
  1244. * Read with ecc
  1245. */
  1246. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1247. size_t *retlen, u_char *buf)
  1248. {
  1249. struct onenand_chip *this = mtd->priv;
  1250. struct mtd_oob_ops ops = {
  1251. .len = len,
  1252. .ooblen = 0,
  1253. .datbuf = buf,
  1254. .oobbuf = NULL,
  1255. };
  1256. int ret;
  1257. onenand_get_device(mtd, FL_READING);
  1258. ret = ONENAND_IS_4KB_PAGE(this) ?
  1259. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  1260. onenand_read_ops_nolock(mtd, from, &ops);
  1261. onenand_release_device(mtd);
  1262. *retlen = ops.retlen;
  1263. return ret;
  1264. }
  1265. /**
  1266. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  1267. * @param mtd: MTD device structure
  1268. * @param from: offset to read from
  1269. * @param ops: oob operation description structure
  1270. * Read main and/or out-of-band
  1271. */
  1272. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  1273. struct mtd_oob_ops *ops)
  1274. {
  1275. struct onenand_chip *this = mtd->priv;
  1276. int ret;
  1277. switch (ops->mode) {
  1278. case MTD_OPS_PLACE_OOB:
  1279. case MTD_OPS_AUTO_OOB:
  1280. break;
  1281. case MTD_OPS_RAW:
  1282. /* Not implemented yet */
  1283. default:
  1284. return -EINVAL;
  1285. }
  1286. onenand_get_device(mtd, FL_READING);
  1287. if (ops->datbuf)
  1288. ret = ONENAND_IS_4KB_PAGE(this) ?
  1289. onenand_mlc_read_ops_nolock(mtd, from, ops) :
  1290. onenand_read_ops_nolock(mtd, from, ops);
  1291. else
  1292. ret = onenand_read_oob_nolock(mtd, from, ops);
  1293. onenand_release_device(mtd);
  1294. return ret;
  1295. }
  1296. /**
  1297. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  1298. * @param mtd MTD device structure
  1299. * @param state state to select the max. timeout value
  1300. *
  1301. * Wait for command done.
  1302. */
  1303. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  1304. {
  1305. struct onenand_chip *this = mtd->priv;
  1306. unsigned long timeout;
  1307. unsigned int interrupt, ctrl, ecc, addr1, addr8;
  1308. /* The 20 msec is enough */
  1309. timeout = jiffies + msecs_to_jiffies(20);
  1310. while (time_before(jiffies, timeout)) {
  1311. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1312. if (interrupt & ONENAND_INT_MASTER)
  1313. break;
  1314. }
  1315. /* To get correct interrupt status in timeout case */
  1316. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1317. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  1318. addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
  1319. addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
  1320. if (interrupt & ONENAND_INT_READ) {
  1321. ecc = onenand_read_ecc(this);
  1322. if (ecc & ONENAND_ECC_2BIT_ALL) {
  1323. printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
  1324. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1325. __func__, ecc, ctrl, interrupt, addr1, addr8);
  1326. return ONENAND_BBT_READ_ECC_ERROR;
  1327. }
  1328. } else {
  1329. printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
  1330. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1331. __func__, ctrl, interrupt, addr1, addr8);
  1332. return ONENAND_BBT_READ_FATAL_ERROR;
  1333. }
  1334. /* Initial bad block case: 0x2400 or 0x0400 */
  1335. if (ctrl & ONENAND_CTRL_ERROR) {
  1336. printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
  1337. "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
  1338. return ONENAND_BBT_READ_ERROR;
  1339. }
  1340. return 0;
  1341. }
  1342. /**
  1343. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1344. * @param mtd MTD device structure
  1345. * @param from offset to read from
  1346. * @param ops oob operation description structure
  1347. *
  1348. * OneNAND read out-of-band data from the spare area for bbt scan
  1349. */
  1350. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1351. struct mtd_oob_ops *ops)
  1352. {
  1353. struct onenand_chip *this = mtd->priv;
  1354. int read = 0, thislen, column;
  1355. int ret = 0, readcmd;
  1356. size_t len = ops->ooblen;
  1357. u_char *buf = ops->oobbuf;
  1358. pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
  1359. len);
  1360. /* Initialize return value */
  1361. ops->oobretlen = 0;
  1362. /* Do not allow reads past end of device */
  1363. if (unlikely((from + len) > mtd->size)) {
  1364. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1365. __func__);
  1366. return ONENAND_BBT_READ_FATAL_ERROR;
  1367. }
  1368. /* Grab the lock and see if the device is available */
  1369. onenand_get_device(mtd, FL_READING);
  1370. column = from & (mtd->oobsize - 1);
  1371. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1372. while (read < len) {
  1373. cond_resched();
  1374. thislen = mtd->oobsize - column;
  1375. thislen = min_t(int, thislen, len);
  1376. this->command(mtd, readcmd, from, mtd->oobsize);
  1377. onenand_update_bufferram(mtd, from, 0);
  1378. ret = this->bbt_wait(mtd, FL_READING);
  1379. if (unlikely(ret))
  1380. ret = onenand_recover_lsb(mtd, from, ret);
  1381. if (ret)
  1382. break;
  1383. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1384. read += thislen;
  1385. if (read == len)
  1386. break;
  1387. buf += thislen;
  1388. /* Read more? */
  1389. if (read < len) {
  1390. /* Update Page size */
  1391. from += this->writesize;
  1392. column = 0;
  1393. }
  1394. }
  1395. /* Deselect and wake up anyone waiting on the device */
  1396. onenand_release_device(mtd);
  1397. ops->oobretlen = read;
  1398. return ret;
  1399. }
  1400. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1401. /**
  1402. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1403. * @param mtd MTD device structure
  1404. * @param buf the databuffer to verify
  1405. * @param to offset to read from
  1406. */
  1407. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1408. {
  1409. struct onenand_chip *this = mtd->priv;
  1410. u_char *oob_buf = this->oob_buf;
  1411. int status, i, readcmd;
  1412. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1413. this->command(mtd, readcmd, to, mtd->oobsize);
  1414. onenand_update_bufferram(mtd, to, 0);
  1415. status = this->wait(mtd, FL_READING);
  1416. if (status)
  1417. return status;
  1418. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  1419. for (i = 0; i < mtd->oobsize; i++)
  1420. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  1421. return -EBADMSG;
  1422. return 0;
  1423. }
  1424. /**
  1425. * onenand_verify - [GENERIC] verify the chip contents after a write
  1426. * @param mtd MTD device structure
  1427. * @param buf the databuffer to verify
  1428. * @param addr offset to read from
  1429. * @param len number of bytes to read and compare
  1430. */
  1431. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1432. {
  1433. struct onenand_chip *this = mtd->priv;
  1434. int ret = 0;
  1435. int thislen, column;
  1436. column = addr & (this->writesize - 1);
  1437. while (len != 0) {
  1438. thislen = min_t(int, this->writesize - column, len);
  1439. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1440. onenand_update_bufferram(mtd, addr, 0);
  1441. ret = this->wait(mtd, FL_READING);
  1442. if (ret)
  1443. return ret;
  1444. onenand_update_bufferram(mtd, addr, 1);
  1445. this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
  1446. if (memcmp(buf, this->verify_buf + column, thislen))
  1447. return -EBADMSG;
  1448. len -= thislen;
  1449. buf += thislen;
  1450. addr += thislen;
  1451. column = 0;
  1452. }
  1453. return 0;
  1454. }
  1455. #else
  1456. #define onenand_verify(...) (0)
  1457. #define onenand_verify_oob(...) (0)
  1458. #endif
  1459. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1460. static void onenand_panic_wait(struct mtd_info *mtd)
  1461. {
  1462. struct onenand_chip *this = mtd->priv;
  1463. unsigned int interrupt;
  1464. int i;
  1465. for (i = 0; i < 2000; i++) {
  1466. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1467. if (interrupt & ONENAND_INT_MASTER)
  1468. break;
  1469. udelay(10);
  1470. }
  1471. }
  1472. /**
  1473. * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
  1474. * @param mtd MTD device structure
  1475. * @param to offset to write to
  1476. * @param len number of bytes to write
  1477. * @param retlen pointer to variable to store the number of written bytes
  1478. * @param buf the data to write
  1479. *
  1480. * Write with ECC
  1481. */
  1482. static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1483. size_t *retlen, const u_char *buf)
  1484. {
  1485. struct onenand_chip *this = mtd->priv;
  1486. int column, subpage;
  1487. int written = 0;
  1488. int ret = 0;
  1489. if (this->state == FL_PM_SUSPENDED)
  1490. return -EBUSY;
  1491. /* Wait for any existing operation to clear */
  1492. onenand_panic_wait(mtd);
  1493. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1494. (int)len);
  1495. /* Reject writes, which are not page aligned */
  1496. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1497. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1498. __func__);
  1499. return -EINVAL;
  1500. }
  1501. column = to & (mtd->writesize - 1);
  1502. /* Loop until all data write */
  1503. while (written < len) {
  1504. int thislen = min_t(int, mtd->writesize - column, len - written);
  1505. u_char *wbuf = (u_char *) buf;
  1506. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1507. /* Partial page write */
  1508. subpage = thislen < mtd->writesize;
  1509. if (subpage) {
  1510. memset(this->page_buf, 0xff, mtd->writesize);
  1511. memcpy(this->page_buf + column, buf, thislen);
  1512. wbuf = this->page_buf;
  1513. }
  1514. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1515. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  1516. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1517. onenand_panic_wait(mtd);
  1518. /* In partial page write we don't update bufferram */
  1519. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1520. if (ONENAND_IS_2PLANE(this)) {
  1521. ONENAND_SET_BUFFERRAM1(this);
  1522. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1523. }
  1524. if (ret) {
  1525. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1526. break;
  1527. }
  1528. written += thislen;
  1529. if (written == len)
  1530. break;
  1531. column = 0;
  1532. to += thislen;
  1533. buf += thislen;
  1534. }
  1535. *retlen = written;
  1536. return ret;
  1537. }
  1538. /**
  1539. * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
  1540. * @param mtd MTD device structure
  1541. * @param oob_buf oob buffer
  1542. * @param buf source address
  1543. * @param column oob offset to write to
  1544. * @param thislen oob length to write
  1545. */
  1546. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1547. const u_char *buf, int column, int thislen)
  1548. {
  1549. struct onenand_chip *this = mtd->priv;
  1550. struct nand_oobfree *free;
  1551. int writecol = column;
  1552. int writeend = column + thislen;
  1553. int lastgap = 0;
  1554. unsigned int i;
  1555. free = this->ecclayout->oobfree;
  1556. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1557. if (writecol >= lastgap)
  1558. writecol += free->offset - lastgap;
  1559. if (writeend >= lastgap)
  1560. writeend += free->offset - lastgap;
  1561. lastgap = free->offset + free->length;
  1562. }
  1563. free = this->ecclayout->oobfree;
  1564. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1565. int free_end = free->offset + free->length;
  1566. if (free->offset < writeend && free_end > writecol) {
  1567. int st = max_t(int,free->offset,writecol);
  1568. int ed = min_t(int,free_end,writeend);
  1569. int n = ed - st;
  1570. memcpy(oob_buf + st, buf, n);
  1571. buf += n;
  1572. } else if (column == 0)
  1573. break;
  1574. }
  1575. return 0;
  1576. }
  1577. /**
  1578. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1579. * @param mtd MTD device structure
  1580. * @param to offset to write to
  1581. * @param ops oob operation description structure
  1582. *
  1583. * Write main and/or oob with ECC
  1584. */
  1585. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1586. struct mtd_oob_ops *ops)
  1587. {
  1588. struct onenand_chip *this = mtd->priv;
  1589. int written = 0, column, thislen = 0, subpage = 0;
  1590. int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
  1591. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1592. size_t len = ops->len;
  1593. size_t ooblen = ops->ooblen;
  1594. const u_char *buf = ops->datbuf;
  1595. const u_char *oob = ops->oobbuf;
  1596. u_char *oobbuf;
  1597. int ret = 0, cmd;
  1598. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1599. (int)len);
  1600. /* Initialize retlen, in case of early exit */
  1601. ops->retlen = 0;
  1602. ops->oobretlen = 0;
  1603. /* Reject writes, which are not page aligned */
  1604. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1605. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1606. __func__);
  1607. return -EINVAL;
  1608. }
  1609. /* Check zero length */
  1610. if (!len)
  1611. return 0;
  1612. if (ops->mode == MTD_OPS_AUTO_OOB)
  1613. oobsize = this->ecclayout->oobavail;
  1614. else
  1615. oobsize = mtd->oobsize;
  1616. oobcolumn = to & (mtd->oobsize - 1);
  1617. column = to & (mtd->writesize - 1);
  1618. /* Loop until all data write */
  1619. while (1) {
  1620. if (written < len) {
  1621. u_char *wbuf = (u_char *) buf;
  1622. thislen = min_t(int, mtd->writesize - column, len - written);
  1623. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1624. cond_resched();
  1625. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1626. /* Partial page write */
  1627. subpage = thislen < mtd->writesize;
  1628. if (subpage) {
  1629. memset(this->page_buf, 0xff, mtd->writesize);
  1630. memcpy(this->page_buf + column, buf, thislen);
  1631. wbuf = this->page_buf;
  1632. }
  1633. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1634. if (oob) {
  1635. oobbuf = this->oob_buf;
  1636. /* We send data to spare ram with oobsize
  1637. * to prevent byte access */
  1638. memset(oobbuf, 0xff, mtd->oobsize);
  1639. if (ops->mode == MTD_OPS_AUTO_OOB)
  1640. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1641. else
  1642. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1643. oobwritten += thisooblen;
  1644. oob += thisooblen;
  1645. oobcolumn = 0;
  1646. } else
  1647. oobbuf = (u_char *) ffchars;
  1648. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1649. } else
  1650. ONENAND_SET_NEXT_BUFFERRAM(this);
  1651. /*
  1652. * 2 PLANE, MLC, and Flex-OneNAND do not support
  1653. * write-while-program feature.
  1654. */
  1655. if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
  1656. ONENAND_SET_PREV_BUFFERRAM(this);
  1657. ret = this->wait(mtd, FL_WRITING);
  1658. /* In partial page write we don't update bufferram */
  1659. onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
  1660. if (ret) {
  1661. written -= prevlen;
  1662. printk(KERN_ERR "%s: write failed %d\n",
  1663. __func__, ret);
  1664. break;
  1665. }
  1666. if (written == len) {
  1667. /* Only check verify write turn on */
  1668. ret = onenand_verify(mtd, buf - len, to - len, len);
  1669. if (ret)
  1670. printk(KERN_ERR "%s: verify failed %d\n",
  1671. __func__, ret);
  1672. break;
  1673. }
  1674. ONENAND_SET_NEXT_BUFFERRAM(this);
  1675. }
  1676. this->ongoing = 0;
  1677. cmd = ONENAND_CMD_PROG;
  1678. /* Exclude 1st OTP and OTP blocks for cache program feature */
  1679. if (ONENAND_IS_CACHE_PROGRAM(this) &&
  1680. likely(onenand_block(this, to) != 0) &&
  1681. ONENAND_IS_4KB_PAGE(this) &&
  1682. ((written + thislen) < len)) {
  1683. cmd = ONENAND_CMD_2X_CACHE_PROG;
  1684. this->ongoing = 1;
  1685. }
  1686. this->command(mtd, cmd, to, mtd->writesize);
  1687. /*
  1688. * 2 PLANE, MLC, and Flex-OneNAND wait here
  1689. */
  1690. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
  1691. ret = this->wait(mtd, FL_WRITING);
  1692. /* In partial page write we don't update bufferram */
  1693. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1694. if (ret) {
  1695. printk(KERN_ERR "%s: write failed %d\n",
  1696. __func__, ret);
  1697. break;
  1698. }
  1699. /* Only check verify write turn on */
  1700. ret = onenand_verify(mtd, buf, to, thislen);
  1701. if (ret) {
  1702. printk(KERN_ERR "%s: verify failed %d\n",
  1703. __func__, ret);
  1704. break;
  1705. }
  1706. written += thislen;
  1707. if (written == len)
  1708. break;
  1709. } else
  1710. written += thislen;
  1711. column = 0;
  1712. prev_subpage = subpage;
  1713. prev = to;
  1714. prevlen = thislen;
  1715. to += thislen;
  1716. buf += thislen;
  1717. first = 0;
  1718. }
  1719. /* In error case, clear all bufferrams */
  1720. if (written != len)
  1721. onenand_invalidate_bufferram(mtd, 0, -1);
  1722. ops->retlen = written;
  1723. ops->oobretlen = oobwritten;
  1724. return ret;
  1725. }
  1726. /**
  1727. * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
  1728. * @param mtd MTD device structure
  1729. * @param to offset to write to
  1730. * @param len number of bytes to write
  1731. * @param retlen pointer to variable to store the number of written bytes
  1732. * @param buf the data to write
  1733. * @param mode operation mode
  1734. *
  1735. * OneNAND write out-of-band
  1736. */
  1737. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1738. struct mtd_oob_ops *ops)
  1739. {
  1740. struct onenand_chip *this = mtd->priv;
  1741. int column, ret = 0, oobsize;
  1742. int written = 0, oobcmd;
  1743. u_char *oobbuf;
  1744. size_t len = ops->ooblen;
  1745. const u_char *buf = ops->oobbuf;
  1746. unsigned int mode = ops->mode;
  1747. to += ops->ooboffs;
  1748. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1749. (int)len);
  1750. /* Initialize retlen, in case of early exit */
  1751. ops->oobretlen = 0;
  1752. if (mode == MTD_OPS_AUTO_OOB)
  1753. oobsize = this->ecclayout->oobavail;
  1754. else
  1755. oobsize = mtd->oobsize;
  1756. column = to & (mtd->oobsize - 1);
  1757. if (unlikely(column >= oobsize)) {
  1758. printk(KERN_ERR "%s: Attempted to start write outside oob\n",
  1759. __func__);
  1760. return -EINVAL;
  1761. }
  1762. /* For compatibility with NAND: Do not allow write past end of page */
  1763. if (unlikely(column + len > oobsize)) {
  1764. printk(KERN_ERR "%s: Attempt to write past end of page\n",
  1765. __func__);
  1766. return -EINVAL;
  1767. }
  1768. /* Do not allow reads past end of device */
  1769. if (unlikely(to >= mtd->size ||
  1770. column + len > ((mtd->size >> this->page_shift) -
  1771. (to >> this->page_shift)) * oobsize)) {
  1772. printk(KERN_ERR "%s: Attempted to write past end of device\n",
  1773. __func__);
  1774. return -EINVAL;
  1775. }
  1776. oobbuf = this->oob_buf;
  1777. oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1778. /* Loop until all data write */
  1779. while (written < len) {
  1780. int thislen = min_t(int, oobsize, len - written);
  1781. cond_resched();
  1782. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1783. /* We send data to spare ram with oobsize
  1784. * to prevent byte access */
  1785. memset(oobbuf, 0xff, mtd->oobsize);
  1786. if (mode == MTD_OPS_AUTO_OOB)
  1787. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1788. else
  1789. memcpy(oobbuf + column, buf, thislen);
  1790. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1791. if (ONENAND_IS_4KB_PAGE(this)) {
  1792. /* Set main area of DataRAM to 0xff*/
  1793. memset(this->page_buf, 0xff, mtd->writesize);
  1794. this->write_bufferram(mtd, ONENAND_DATARAM,
  1795. this->page_buf, 0, mtd->writesize);
  1796. }
  1797. this->command(mtd, oobcmd, to, mtd->oobsize);
  1798. onenand_update_bufferram(mtd, to, 0);
  1799. if (ONENAND_IS_2PLANE(this)) {
  1800. ONENAND_SET_BUFFERRAM1(this);
  1801. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1802. }
  1803. ret = this->wait(mtd, FL_WRITING);
  1804. if (ret) {
  1805. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1806. break;
  1807. }
  1808. ret = onenand_verify_oob(mtd, oobbuf, to);
  1809. if (ret) {
  1810. printk(KERN_ERR "%s: verify failed %d\n",
  1811. __func__, ret);
  1812. break;
  1813. }
  1814. written += thislen;
  1815. if (written == len)
  1816. break;
  1817. to += mtd->writesize;
  1818. buf += thislen;
  1819. column = 0;
  1820. }
  1821. ops->oobretlen = written;
  1822. return ret;
  1823. }
  1824. /**
  1825. * onenand_write - [MTD Interface] write buffer to FLASH
  1826. * @param mtd MTD device structure
  1827. * @param to offset to write to
  1828. * @param len number of bytes to write
  1829. * @param retlen pointer to variable to store the number of written bytes
  1830. * @param buf the data to write
  1831. *
  1832. * Write with ECC
  1833. */
  1834. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1835. size_t *retlen, const u_char *buf)
  1836. {
  1837. struct mtd_oob_ops ops = {
  1838. .len = len,
  1839. .ooblen = 0,
  1840. .datbuf = (u_char *) buf,
  1841. .oobbuf = NULL,
  1842. };
  1843. int ret;
  1844. onenand_get_device(mtd, FL_WRITING);
  1845. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1846. onenand_release_device(mtd);
  1847. *retlen = ops.retlen;
  1848. return ret;
  1849. }
  1850. /**
  1851. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1852. * @param mtd: MTD device structure
  1853. * @param to: offset to write
  1854. * @param ops: oob operation description structure
  1855. */
  1856. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1857. struct mtd_oob_ops *ops)
  1858. {
  1859. int ret;
  1860. switch (ops->mode) {
  1861. case MTD_OPS_PLACE_OOB:
  1862. case MTD_OPS_AUTO_OOB:
  1863. break;
  1864. case MTD_OPS_RAW:
  1865. /* Not implemented yet */
  1866. default:
  1867. return -EINVAL;
  1868. }
  1869. onenand_get_device(mtd, FL_WRITING);
  1870. if (ops->datbuf)
  1871. ret = onenand_write_ops_nolock(mtd, to, ops);
  1872. else
  1873. ret = onenand_write_oob_nolock(mtd, to, ops);
  1874. onenand_release_device(mtd);
  1875. return ret;
  1876. }
  1877. /**
  1878. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1879. * @param mtd MTD device structure
  1880. * @param ofs offset from device start
  1881. * @param allowbbt 1, if its allowed to access the bbt area
  1882. *
  1883. * Check, if the block is bad. Either by reading the bad block table or
  1884. * calling of the scan function.
  1885. */
  1886. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1887. {
  1888. struct onenand_chip *this = mtd->priv;
  1889. struct bbm_info *bbm = this->bbm;
  1890. /* Return info from the table */
  1891. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1892. }
  1893. static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
  1894. struct erase_info *instr)
  1895. {
  1896. struct onenand_chip *this = mtd->priv;
  1897. loff_t addr = instr->addr;
  1898. int len = instr->len;
  1899. unsigned int block_size = (1 << this->erase_shift);
  1900. int ret = 0;
  1901. while (len) {
  1902. this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
  1903. ret = this->wait(mtd, FL_VERIFYING_ERASE);
  1904. if (ret) {
  1905. printk(KERN_ERR "%s: Failed verify, block %d\n",
  1906. __func__, onenand_block(this, addr));
  1907. instr->state = MTD_ERASE_FAILED;
  1908. instr->fail_addr = addr;
  1909. return -1;
  1910. }
  1911. len -= block_size;
  1912. addr += block_size;
  1913. }
  1914. return 0;
  1915. }
  1916. /**
  1917. * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
  1918. * @param mtd MTD device structure
  1919. * @param instr erase instruction
  1920. * @param region erase region
  1921. *
  1922. * Erase one or more blocks up to 64 block at a time
  1923. */
  1924. static int onenand_multiblock_erase(struct mtd_info *mtd,
  1925. struct erase_info *instr,
  1926. unsigned int block_size)
  1927. {
  1928. struct onenand_chip *this = mtd->priv;
  1929. loff_t addr = instr->addr;
  1930. int len = instr->len;
  1931. int eb_count = 0;
  1932. int ret = 0;
  1933. int bdry_block = 0;
  1934. instr->state = MTD_ERASING;
  1935. if (ONENAND_IS_DDP(this)) {
  1936. loff_t bdry_addr = this->chipsize >> 1;
  1937. if (addr < bdry_addr && (addr + len) > bdry_addr)
  1938. bdry_block = bdry_addr >> this->erase_shift;
  1939. }
  1940. /* Pre-check bbs */
  1941. while (len) {
  1942. /* Check if we have a bad block, we do not erase bad blocks */
  1943. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  1944. printk(KERN_WARNING "%s: attempt to erase a bad block "
  1945. "at addr 0x%012llx\n",
  1946. __func__, (unsigned long long) addr);
  1947. instr->state = MTD_ERASE_FAILED;
  1948. return -EIO;
  1949. }
  1950. len -= block_size;
  1951. addr += block_size;
  1952. }
  1953. len = instr->len;
  1954. addr = instr->addr;
  1955. /* loop over 64 eb batches */
  1956. while (len) {
  1957. struct erase_info verify_instr = *instr;
  1958. int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
  1959. verify_instr.addr = addr;
  1960. verify_instr.len = 0;
  1961. /* do not cross chip boundary */
  1962. if (bdry_block) {
  1963. int this_block = (addr >> this->erase_shift);
  1964. if (this_block < bdry_block) {
  1965. max_eb_count = min(max_eb_count,
  1966. (bdry_block - this_block));
  1967. }
  1968. }
  1969. eb_count = 0;
  1970. while (len > block_size && eb_count < (max_eb_count - 1)) {
  1971. this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
  1972. addr, block_size);
  1973. onenand_invalidate_bufferram(mtd, addr, block_size);
  1974. ret = this->wait(mtd, FL_PREPARING_ERASE);
  1975. if (ret) {
  1976. printk(KERN_ERR "%s: Failed multiblock erase, "
  1977. "block %d\n", __func__,
  1978. onenand_block(this, addr));
  1979. instr->state = MTD_ERASE_FAILED;
  1980. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1981. return -EIO;
  1982. }
  1983. len -= block_size;
  1984. addr += block_size;
  1985. eb_count++;
  1986. }
  1987. /* last block of 64-eb series */
  1988. cond_resched();
  1989. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1990. onenand_invalidate_bufferram(mtd, addr, block_size);
  1991. ret = this->wait(mtd, FL_ERASING);
  1992. /* Check if it is write protected */
  1993. if (ret) {
  1994. printk(KERN_ERR "%s: Failed erase, block %d\n",
  1995. __func__, onenand_block(this, addr));
  1996. instr->state = MTD_ERASE_FAILED;
  1997. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1998. return -EIO;
  1999. }
  2000. len -= block_size;
  2001. addr += block_size;
  2002. eb_count++;
  2003. /* verify */
  2004. verify_instr.len = eb_count * block_size;
  2005. if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
  2006. instr->state = verify_instr.state;
  2007. instr->fail_addr = verify_instr.fail_addr;
  2008. return -EIO;
  2009. }
  2010. }
  2011. return 0;
  2012. }
  2013. /**
  2014. * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
  2015. * @param mtd MTD device structure
  2016. * @param instr erase instruction
  2017. * @param region erase region
  2018. * @param block_size erase block size
  2019. *
  2020. * Erase one or more blocks one block at a time
  2021. */
  2022. static int onenand_block_by_block_erase(struct mtd_info *mtd,
  2023. struct erase_info *instr,
  2024. struct mtd_erase_region_info *region,
  2025. unsigned int block_size)
  2026. {
  2027. struct onenand_chip *this = mtd->priv;
  2028. loff_t addr = instr->addr;
  2029. int len = instr->len;
  2030. loff_t region_end = 0;
  2031. int ret = 0;
  2032. if (region) {
  2033. /* region is set for Flex-OneNAND */
  2034. region_end = region->offset + region->erasesize * region->numblocks;
  2035. }
  2036. instr->state = MTD_ERASING;
  2037. /* Loop through the blocks */
  2038. while (len) {
  2039. cond_resched();
  2040. /* Check if we have a bad block, we do not erase bad blocks */
  2041. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  2042. printk(KERN_WARNING "%s: attempt to erase a bad block "
  2043. "at addr 0x%012llx\n",
  2044. __func__, (unsigned long long) addr);
  2045. instr->state = MTD_ERASE_FAILED;
  2046. return -EIO;
  2047. }
  2048. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  2049. onenand_invalidate_bufferram(mtd, addr, block_size);
  2050. ret = this->wait(mtd, FL_ERASING);
  2051. /* Check, if it is write protected */
  2052. if (ret) {
  2053. printk(KERN_ERR "%s: Failed erase, block %d\n",
  2054. __func__, onenand_block(this, addr));
  2055. instr->state = MTD_ERASE_FAILED;
  2056. instr->fail_addr = addr;
  2057. return -EIO;
  2058. }
  2059. len -= block_size;
  2060. addr += block_size;
  2061. if (region && addr == region_end) {
  2062. if (!len)
  2063. break;
  2064. region++;
  2065. block_size = region->erasesize;
  2066. region_end = region->offset + region->erasesize * region->numblocks;
  2067. if (len & (block_size - 1)) {
  2068. /* FIXME: This should be handled at MTD partitioning level. */
  2069. printk(KERN_ERR "%s: Unaligned address\n",
  2070. __func__);
  2071. return -EIO;
  2072. }
  2073. }
  2074. }
  2075. return 0;
  2076. }
  2077. /**
  2078. * onenand_erase - [MTD Interface] erase block(s)
  2079. * @param mtd MTD device structure
  2080. * @param instr erase instruction
  2081. *
  2082. * Erase one or more blocks
  2083. */
  2084. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2085. {
  2086. struct onenand_chip *this = mtd->priv;
  2087. unsigned int block_size;
  2088. loff_t addr = instr->addr;
  2089. loff_t len = instr->len;
  2090. int ret = 0;
  2091. struct mtd_erase_region_info *region = NULL;
  2092. loff_t region_offset = 0;
  2093. pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
  2094. (unsigned long long)instr->addr,
  2095. (unsigned long long)instr->len);
  2096. if (FLEXONENAND(this)) {
  2097. /* Find the eraseregion of this address */
  2098. int i = flexonenand_region(mtd, addr);
  2099. region = &mtd->eraseregions[i];
  2100. block_size = region->erasesize;
  2101. /* Start address within region must align on block boundary.
  2102. * Erase region's start offset is always block start address.
  2103. */
  2104. region_offset = region->offset;
  2105. } else
  2106. block_size = 1 << this->erase_shift;
  2107. /* Start address must align on block boundary */
  2108. if (unlikely((addr - region_offset) & (block_size - 1))) {
  2109. printk(KERN_ERR "%s: Unaligned address\n", __func__);
  2110. return -EINVAL;
  2111. }
  2112. /* Length must align on block boundary */
  2113. if (unlikely(len & (block_size - 1))) {
  2114. printk(KERN_ERR "%s: Length not block aligned\n", __func__);
  2115. return -EINVAL;
  2116. }
  2117. /* Grab the lock and see if the device is available */
  2118. onenand_get_device(mtd, FL_ERASING);
  2119. if (ONENAND_IS_4KB_PAGE(this) || region ||
  2120. instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
  2121. /* region is set for Flex-OneNAND (no mb erase) */
  2122. ret = onenand_block_by_block_erase(mtd, instr,
  2123. region, block_size);
  2124. } else {
  2125. ret = onenand_multiblock_erase(mtd, instr, block_size);
  2126. }
  2127. /* Deselect and wake up anyone waiting on the device */
  2128. onenand_release_device(mtd);
  2129. /* Do call back function */
  2130. if (!ret) {
  2131. instr->state = MTD_ERASE_DONE;
  2132. mtd_erase_callback(instr);
  2133. }
  2134. return ret;
  2135. }
  2136. /**
  2137. * onenand_sync - [MTD Interface] sync
  2138. * @param mtd MTD device structure
  2139. *
  2140. * Sync is actually a wait for chip ready function
  2141. */
  2142. static void onenand_sync(struct mtd_info *mtd)
  2143. {
  2144. pr_debug("%s: called\n", __func__);
  2145. /* Grab the lock and see if the device is available */
  2146. onenand_get_device(mtd, FL_SYNCING);
  2147. /* Release it and go back */
  2148. onenand_release_device(mtd);
  2149. }
  2150. /**
  2151. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  2152. * @param mtd MTD device structure
  2153. * @param ofs offset relative to mtd start
  2154. *
  2155. * Check whether the block is bad
  2156. */
  2157. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  2158. {
  2159. int ret;
  2160. onenand_get_device(mtd, FL_READING);
  2161. ret = onenand_block_isbad_nolock(mtd, ofs, 0);
  2162. onenand_release_device(mtd);
  2163. return ret;
  2164. }
  2165. /**
  2166. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  2167. * @param mtd MTD device structure
  2168. * @param ofs offset from device start
  2169. *
  2170. * This is the default implementation, which can be overridden by
  2171. * a hardware specific driver.
  2172. */
  2173. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2174. {
  2175. struct onenand_chip *this = mtd->priv;
  2176. struct bbm_info *bbm = this->bbm;
  2177. u_char buf[2] = {0, 0};
  2178. struct mtd_oob_ops ops = {
  2179. .mode = MTD_OPS_PLACE_OOB,
  2180. .ooblen = 2,
  2181. .oobbuf = buf,
  2182. .ooboffs = 0,
  2183. };
  2184. int block;
  2185. /* Get block number */
  2186. block = onenand_block(this, ofs);
  2187. if (bbm->bbt)
  2188. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  2189. /* We write two bytes, so we don't have to mess with 16-bit access */
  2190. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  2191. /* FIXME : What to do when marking SLC block in partition
  2192. * with MLC erasesize? For now, it is not advisable to
  2193. * create partitions containing both SLC and MLC regions.
  2194. */
  2195. return onenand_write_oob_nolock(mtd, ofs, &ops);
  2196. }
  2197. /**
  2198. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  2199. * @param mtd MTD device structure
  2200. * @param ofs offset relative to mtd start
  2201. *
  2202. * Mark the block as bad
  2203. */
  2204. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2205. {
  2206. int ret;
  2207. ret = onenand_block_isbad(mtd, ofs);
  2208. if (ret) {
  2209. /* If it was bad already, return success and do nothing */
  2210. if (ret > 0)
  2211. return 0;
  2212. return ret;
  2213. }
  2214. onenand_get_device(mtd, FL_WRITING);
  2215. ret = mtd_block_markbad(mtd, ofs);
  2216. onenand_release_device(mtd);
  2217. return ret;
  2218. }
  2219. /**
  2220. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  2221. * @param mtd MTD device structure
  2222. * @param ofs offset relative to mtd start
  2223. * @param len number of bytes to lock or unlock
  2224. * @param cmd lock or unlock command
  2225. *
  2226. * Lock or unlock one or more blocks
  2227. */
  2228. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  2229. {
  2230. struct onenand_chip *this = mtd->priv;
  2231. int start, end, block, value, status;
  2232. int wp_status_mask;
  2233. start = onenand_block(this, ofs);
  2234. end = onenand_block(this, ofs + len) - 1;
  2235. if (cmd == ONENAND_CMD_LOCK)
  2236. wp_status_mask = ONENAND_WP_LS;
  2237. else
  2238. wp_status_mask = ONENAND_WP_US;
  2239. /* Continuous lock scheme */
  2240. if (this->options & ONENAND_HAS_CONT_LOCK) {
  2241. /* Set start block address */
  2242. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2243. /* Set end block address */
  2244. this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  2245. /* Write lock command */
  2246. this->command(mtd, cmd, 0, 0);
  2247. /* There's no return value */
  2248. this->wait(mtd, FL_LOCKING);
  2249. /* Sanity check */
  2250. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2251. & ONENAND_CTRL_ONGO)
  2252. continue;
  2253. /* Check lock status */
  2254. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2255. if (!(status & wp_status_mask))
  2256. printk(KERN_ERR "%s: wp status = 0x%x\n",
  2257. __func__, status);
  2258. return 0;
  2259. }
  2260. /* Block lock scheme */
  2261. for (block = start; block < end + 1; block++) {
  2262. /* Set block address */
  2263. value = onenand_block_address(this, block);
  2264. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2265. /* Select DataRAM for DDP */
  2266. value = onenand_bufferram_address(this, block);
  2267. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2268. /* Set start block address */
  2269. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2270. /* Write lock command */
  2271. this->command(mtd, cmd, 0, 0);
  2272. /* There's no return value */
  2273. this->wait(mtd, FL_LOCKING);
  2274. /* Sanity check */
  2275. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2276. & ONENAND_CTRL_ONGO)
  2277. continue;
  2278. /* Check lock status */
  2279. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2280. if (!(status & wp_status_mask))
  2281. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2282. __func__, block, status);
  2283. }
  2284. return 0;
  2285. }
  2286. /**
  2287. * onenand_lock - [MTD Interface] Lock block(s)
  2288. * @param mtd MTD device structure
  2289. * @param ofs offset relative to mtd start
  2290. * @param len number of bytes to unlock
  2291. *
  2292. * Lock one or more blocks
  2293. */
  2294. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2295. {
  2296. int ret;
  2297. onenand_get_device(mtd, FL_LOCKING);
  2298. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  2299. onenand_release_device(mtd);
  2300. return ret;
  2301. }
  2302. /**
  2303. * onenand_unlock - [MTD Interface] Unlock block(s)
  2304. * @param mtd MTD device structure
  2305. * @param ofs offset relative to mtd start
  2306. * @param len number of bytes to unlock
  2307. *
  2308. * Unlock one or more blocks
  2309. */
  2310. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2311. {
  2312. int ret;
  2313. onenand_get_device(mtd, FL_LOCKING);
  2314. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2315. onenand_release_device(mtd);
  2316. return ret;
  2317. }
  2318. /**
  2319. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  2320. * @param this onenand chip data structure
  2321. *
  2322. * Check lock status
  2323. */
  2324. static int onenand_check_lock_status(struct onenand_chip *this)
  2325. {
  2326. unsigned int value, block, status;
  2327. unsigned int end;
  2328. end = this->chipsize >> this->erase_shift;
  2329. for (block = 0; block < end; block++) {
  2330. /* Set block address */
  2331. value = onenand_block_address(this, block);
  2332. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2333. /* Select DataRAM for DDP */
  2334. value = onenand_bufferram_address(this, block);
  2335. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2336. /* Set start block address */
  2337. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2338. /* Check lock status */
  2339. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2340. if (!(status & ONENAND_WP_US)) {
  2341. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2342. __func__, block, status);
  2343. return 0;
  2344. }
  2345. }
  2346. return 1;
  2347. }
  2348. /**
  2349. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  2350. * @param mtd MTD device structure
  2351. *
  2352. * Unlock all blocks
  2353. */
  2354. static void onenand_unlock_all(struct mtd_info *mtd)
  2355. {
  2356. struct onenand_chip *this = mtd->priv;
  2357. loff_t ofs = 0;
  2358. loff_t len = mtd->size;
  2359. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  2360. /* Set start block address */
  2361. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2362. /* Write unlock command */
  2363. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  2364. /* There's no return value */
  2365. this->wait(mtd, FL_LOCKING);
  2366. /* Sanity check */
  2367. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2368. & ONENAND_CTRL_ONGO)
  2369. continue;
  2370. /* Don't check lock status */
  2371. if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
  2372. return;
  2373. /* Check lock status */
  2374. if (onenand_check_lock_status(this))
  2375. return;
  2376. /* Workaround for all block unlock in DDP */
  2377. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  2378. /* All blocks on another chip */
  2379. ofs = this->chipsize >> 1;
  2380. len = this->chipsize >> 1;
  2381. }
  2382. }
  2383. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2384. }
  2385. #ifdef CONFIG_MTD_ONENAND_OTP
  2386. /**
  2387. * onenand_otp_command - Send OTP specific command to OneNAND device
  2388. * @param mtd MTD device structure
  2389. * @param cmd the command to be sent
  2390. * @param addr offset to read from or write to
  2391. * @param len number of bytes to read or write
  2392. */
  2393. static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
  2394. size_t len)
  2395. {
  2396. struct onenand_chip *this = mtd->priv;
  2397. int value, block, page;
  2398. /* Address translation */
  2399. switch (cmd) {
  2400. case ONENAND_CMD_OTP_ACCESS:
  2401. block = (int) (addr >> this->erase_shift);
  2402. page = -1;
  2403. break;
  2404. default:
  2405. block = (int) (addr >> this->erase_shift);
  2406. page = (int) (addr >> this->page_shift);
  2407. if (ONENAND_IS_2PLANE(this)) {
  2408. /* Make the even block number */
  2409. block &= ~1;
  2410. /* Is it the odd plane? */
  2411. if (addr & this->writesize)
  2412. block++;
  2413. page >>= 1;
  2414. }
  2415. page &= this->page_mask;
  2416. break;
  2417. }
  2418. if (block != -1) {
  2419. /* Write 'DFS, FBA' of Flash */
  2420. value = onenand_block_address(this, block);
  2421. this->write_word(value, this->base +
  2422. ONENAND_REG_START_ADDRESS1);
  2423. }
  2424. if (page != -1) {
  2425. /* Now we use page size operation */
  2426. int sectors = 4, count = 4;
  2427. int dataram;
  2428. switch (cmd) {
  2429. default:
  2430. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  2431. cmd = ONENAND_CMD_2X_PROG;
  2432. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  2433. break;
  2434. }
  2435. /* Write 'FPA, FSA' of Flash */
  2436. value = onenand_page_address(page, sectors);
  2437. this->write_word(value, this->base +
  2438. ONENAND_REG_START_ADDRESS8);
  2439. /* Write 'BSA, BSC' of DataRAM */
  2440. value = onenand_buffer_address(dataram, sectors, count);
  2441. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  2442. }
  2443. /* Interrupt clear */
  2444. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  2445. /* Write command */
  2446. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  2447. return 0;
  2448. }
  2449. /**
  2450. * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
  2451. * @param mtd MTD device structure
  2452. * @param to offset to write to
  2453. * @param len number of bytes to write
  2454. * @param retlen pointer to variable to store the number of written bytes
  2455. * @param buf the data to write
  2456. *
  2457. * OneNAND write out-of-band only for OTP
  2458. */
  2459. static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  2460. struct mtd_oob_ops *ops)
  2461. {
  2462. struct onenand_chip *this = mtd->priv;
  2463. int column, ret = 0, oobsize;
  2464. int written = 0;
  2465. u_char *oobbuf;
  2466. size_t len = ops->ooblen;
  2467. const u_char *buf = ops->oobbuf;
  2468. int block, value, status;
  2469. to += ops->ooboffs;
  2470. /* Initialize retlen, in case of early exit */
  2471. ops->oobretlen = 0;
  2472. oobsize = mtd->oobsize;
  2473. column = to & (mtd->oobsize - 1);
  2474. oobbuf = this->oob_buf;
  2475. /* Loop until all data write */
  2476. while (written < len) {
  2477. int thislen = min_t(int, oobsize, len - written);
  2478. cond_resched();
  2479. block = (int) (to >> this->erase_shift);
  2480. /*
  2481. * Write 'DFS, FBA' of Flash
  2482. * Add: F100h DQ=DFS, FBA
  2483. */
  2484. value = onenand_block_address(this, block);
  2485. this->write_word(value, this->base +
  2486. ONENAND_REG_START_ADDRESS1);
  2487. /*
  2488. * Select DataRAM for DDP
  2489. * Add: F101h DQ=DBS
  2490. */
  2491. value = onenand_bufferram_address(this, block);
  2492. this->write_word(value, this->base +
  2493. ONENAND_REG_START_ADDRESS2);
  2494. ONENAND_SET_NEXT_BUFFERRAM(this);
  2495. /*
  2496. * Enter OTP access mode
  2497. */
  2498. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2499. this->wait(mtd, FL_OTPING);
  2500. /* We send data to spare ram with oobsize
  2501. * to prevent byte access */
  2502. memcpy(oobbuf + column, buf, thislen);
  2503. /*
  2504. * Write Data into DataRAM
  2505. * Add: 8th Word
  2506. * in sector0/spare/page0
  2507. * DQ=XXFCh
  2508. */
  2509. this->write_bufferram(mtd, ONENAND_SPARERAM,
  2510. oobbuf, 0, mtd->oobsize);
  2511. onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  2512. onenand_update_bufferram(mtd, to, 0);
  2513. if (ONENAND_IS_2PLANE(this)) {
  2514. ONENAND_SET_BUFFERRAM1(this);
  2515. onenand_update_bufferram(mtd, to + this->writesize, 0);
  2516. }
  2517. ret = this->wait(mtd, FL_WRITING);
  2518. if (ret) {
  2519. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  2520. break;
  2521. }
  2522. /* Exit OTP access mode */
  2523. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2524. this->wait(mtd, FL_RESETING);
  2525. status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  2526. status &= 0x60;
  2527. if (status == 0x60) {
  2528. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2529. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2530. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2531. } else if (status == 0x20) {
  2532. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2533. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2534. printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
  2535. } else if (status == 0x40) {
  2536. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2537. printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
  2538. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2539. } else {
  2540. printk(KERN_DEBUG "Reboot to check\n");
  2541. }
  2542. written += thislen;
  2543. if (written == len)
  2544. break;
  2545. to += mtd->writesize;
  2546. buf += thislen;
  2547. column = 0;
  2548. }
  2549. ops->oobretlen = written;
  2550. return ret;
  2551. }
  2552. /* Internal OTP operation */
  2553. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  2554. size_t *retlen, u_char *buf);
  2555. /**
  2556. * do_otp_read - [DEFAULT] Read OTP block area
  2557. * @param mtd MTD device structure
  2558. * @param from The offset to read
  2559. * @param len number of bytes to read
  2560. * @param retlen pointer to variable to store the number of readbytes
  2561. * @param buf the databuffer to put/get data
  2562. *
  2563. * Read OTP block area.
  2564. */
  2565. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  2566. size_t *retlen, u_char *buf)
  2567. {
  2568. struct onenand_chip *this = mtd->priv;
  2569. struct mtd_oob_ops ops = {
  2570. .len = len,
  2571. .ooblen = 0,
  2572. .datbuf = buf,
  2573. .oobbuf = NULL,
  2574. };
  2575. int ret;
  2576. /* Enter OTP access mode */
  2577. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2578. this->wait(mtd, FL_OTPING);
  2579. ret = ONENAND_IS_4KB_PAGE(this) ?
  2580. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  2581. onenand_read_ops_nolock(mtd, from, &ops);
  2582. /* Exit OTP access mode */
  2583. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2584. this->wait(mtd, FL_RESETING);
  2585. return ret;
  2586. }
  2587. /**
  2588. * do_otp_write - [DEFAULT] Write OTP block area
  2589. * @param mtd MTD device structure
  2590. * @param to The offset to write
  2591. * @param len number of bytes to write
  2592. * @param retlen pointer to variable to store the number of write bytes
  2593. * @param buf the databuffer to put/get data
  2594. *
  2595. * Write OTP block area.
  2596. */
  2597. static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  2598. size_t *retlen, u_char *buf)
  2599. {
  2600. struct onenand_chip *this = mtd->priv;
  2601. unsigned char *pbuf = buf;
  2602. int ret;
  2603. struct mtd_oob_ops ops;
  2604. /* Force buffer page aligned */
  2605. if (len < mtd->writesize) {
  2606. memcpy(this->page_buf, buf, len);
  2607. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  2608. pbuf = this->page_buf;
  2609. len = mtd->writesize;
  2610. }
  2611. /* Enter OTP access mode */
  2612. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2613. this->wait(mtd, FL_OTPING);
  2614. ops.len = len;
  2615. ops.ooblen = 0;
  2616. ops.datbuf = pbuf;
  2617. ops.oobbuf = NULL;
  2618. ret = onenand_write_ops_nolock(mtd, to, &ops);
  2619. *retlen = ops.retlen;
  2620. /* Exit OTP access mode */
  2621. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2622. this->wait(mtd, FL_RESETING);
  2623. return ret;
  2624. }
  2625. /**
  2626. * do_otp_lock - [DEFAULT] Lock OTP block area
  2627. * @param mtd MTD device structure
  2628. * @param from The offset to lock
  2629. * @param len number of bytes to lock
  2630. * @param retlen pointer to variable to store the number of lock bytes
  2631. * @param buf the databuffer to put/get data
  2632. *
  2633. * Lock OTP block area.
  2634. */
  2635. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  2636. size_t *retlen, u_char *buf)
  2637. {
  2638. struct onenand_chip *this = mtd->priv;
  2639. struct mtd_oob_ops ops;
  2640. int ret;
  2641. if (FLEXONENAND(this)) {
  2642. /* Enter OTP access mode */
  2643. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2644. this->wait(mtd, FL_OTPING);
  2645. /*
  2646. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2647. * main area of page 49.
  2648. */
  2649. ops.len = mtd->writesize;
  2650. ops.ooblen = 0;
  2651. ops.datbuf = buf;
  2652. ops.oobbuf = NULL;
  2653. ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
  2654. *retlen = ops.retlen;
  2655. /* Exit OTP access mode */
  2656. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2657. this->wait(mtd, FL_RESETING);
  2658. } else {
  2659. ops.mode = MTD_OPS_PLACE_OOB;
  2660. ops.ooblen = len;
  2661. ops.oobbuf = buf;
  2662. ops.ooboffs = 0;
  2663. ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
  2664. *retlen = ops.oobretlen;
  2665. }
  2666. return ret;
  2667. }
  2668. /**
  2669. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  2670. * @param mtd MTD device structure
  2671. * @param from The offset to read/write
  2672. * @param len number of bytes to read/write
  2673. * @param retlen pointer to variable to store the number of read bytes
  2674. * @param buf the databuffer to put/get data
  2675. * @param action do given action
  2676. * @param mode specify user and factory
  2677. *
  2678. * Handle OTP operation.
  2679. */
  2680. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  2681. size_t *retlen, u_char *buf,
  2682. otp_op_t action, int mode)
  2683. {
  2684. struct onenand_chip *this = mtd->priv;
  2685. int otp_pages;
  2686. int density;
  2687. int ret = 0;
  2688. *retlen = 0;
  2689. density = onenand_get_density(this->device_id);
  2690. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  2691. otp_pages = 20;
  2692. else
  2693. otp_pages = 50;
  2694. if (mode == MTD_OTP_FACTORY) {
  2695. from += mtd->writesize * otp_pages;
  2696. otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
  2697. }
  2698. /* Check User/Factory boundary */
  2699. if (mode == MTD_OTP_USER) {
  2700. if (mtd->writesize * otp_pages < from + len)
  2701. return 0;
  2702. } else {
  2703. if (mtd->writesize * otp_pages < len)
  2704. return 0;
  2705. }
  2706. onenand_get_device(mtd, FL_OTPING);
  2707. while (len > 0 && otp_pages > 0) {
  2708. if (!action) { /* OTP Info functions */
  2709. struct otp_info *otpinfo;
  2710. len -= sizeof(struct otp_info);
  2711. if (len <= 0) {
  2712. ret = -ENOSPC;
  2713. break;
  2714. }
  2715. otpinfo = (struct otp_info *) buf;
  2716. otpinfo->start = from;
  2717. otpinfo->length = mtd->writesize;
  2718. otpinfo->locked = 0;
  2719. from += mtd->writesize;
  2720. buf += sizeof(struct otp_info);
  2721. *retlen += sizeof(struct otp_info);
  2722. } else {
  2723. size_t tmp_retlen;
  2724. ret = action(mtd, from, len, &tmp_retlen, buf);
  2725. buf += tmp_retlen;
  2726. len -= tmp_retlen;
  2727. *retlen += tmp_retlen;
  2728. if (ret)
  2729. break;
  2730. }
  2731. otp_pages--;
  2732. }
  2733. onenand_release_device(mtd);
  2734. return ret;
  2735. }
  2736. /**
  2737. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  2738. * @param mtd MTD device structure
  2739. * @param buf the databuffer to put/get data
  2740. * @param len number of bytes to read
  2741. *
  2742. * Read factory OTP info.
  2743. */
  2744. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  2745. struct otp_info *buf, size_t len)
  2746. {
  2747. size_t retlen;
  2748. int ret;
  2749. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  2750. return ret ? : retlen;
  2751. }
  2752. /**
  2753. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  2754. * @param mtd MTD device structure
  2755. * @param from The offset to read
  2756. * @param len number of bytes to read
  2757. * @param retlen pointer to variable to store the number of read bytes
  2758. * @param buf the databuffer to put/get data
  2759. *
  2760. * Read factory OTP area.
  2761. */
  2762. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  2763. size_t len, size_t *retlen, u_char *buf)
  2764. {
  2765. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  2766. }
  2767. /**
  2768. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  2769. * @param mtd MTD device structure
  2770. * @param buf the databuffer to put/get data
  2771. * @param len number of bytes to read
  2772. *
  2773. * Read user OTP info.
  2774. */
  2775. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  2776. struct otp_info *buf, size_t len)
  2777. {
  2778. size_t retlen;
  2779. int ret;
  2780. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  2781. return ret ? : retlen;
  2782. }
  2783. /**
  2784. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  2785. * @param mtd MTD device structure
  2786. * @param from The offset to read
  2787. * @param len number of bytes to read
  2788. * @param retlen pointer to variable to store the number of read bytes
  2789. * @param buf the databuffer to put/get data
  2790. *
  2791. * Read user OTP area.
  2792. */
  2793. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2794. size_t len, size_t *retlen, u_char *buf)
  2795. {
  2796. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  2797. }
  2798. /**
  2799. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  2800. * @param mtd MTD device structure
  2801. * @param from The offset to write
  2802. * @param len number of bytes to write
  2803. * @param retlen pointer to variable to store the number of write bytes
  2804. * @param buf the databuffer to put/get data
  2805. *
  2806. * Write user OTP area.
  2807. */
  2808. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2809. size_t len, size_t *retlen, u_char *buf)
  2810. {
  2811. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  2812. }
  2813. /**
  2814. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  2815. * @param mtd MTD device structure
  2816. * @param from The offset to lock
  2817. * @param len number of bytes to unlock
  2818. *
  2819. * Write lock mark on spare area in page 0 in OTP block
  2820. */
  2821. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2822. size_t len)
  2823. {
  2824. struct onenand_chip *this = mtd->priv;
  2825. u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
  2826. size_t retlen;
  2827. int ret;
  2828. unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
  2829. memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
  2830. : mtd->oobsize);
  2831. /*
  2832. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  2833. * We write 16 bytes spare area instead of 2 bytes.
  2834. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2835. * main area of page 49.
  2836. */
  2837. from = 0;
  2838. len = FLEXONENAND(this) ? mtd->writesize : 16;
  2839. /*
  2840. * Note: OTP lock operation
  2841. * OTP block : 0xXXFC XX 1111 1100
  2842. * 1st block : 0xXXF3 (If chip support) XX 1111 0011
  2843. * Both : 0xXXF0 (If chip support) XX 1111 0000
  2844. */
  2845. if (FLEXONENAND(this))
  2846. otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
  2847. /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
  2848. if (otp == 1)
  2849. buf[otp_lock_offset] = 0xFC;
  2850. else if (otp == 2)
  2851. buf[otp_lock_offset] = 0xF3;
  2852. else if (otp == 3)
  2853. buf[otp_lock_offset] = 0xF0;
  2854. else if (otp != 0)
  2855. printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
  2856. ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
  2857. return ret ? : retlen;
  2858. }
  2859. #endif /* CONFIG_MTD_ONENAND_OTP */
  2860. /**
  2861. * onenand_check_features - Check and set OneNAND features
  2862. * @param mtd MTD data structure
  2863. *
  2864. * Check and set OneNAND features
  2865. * - lock scheme
  2866. * - two plane
  2867. */
  2868. static void onenand_check_features(struct mtd_info *mtd)
  2869. {
  2870. struct onenand_chip *this = mtd->priv;
  2871. unsigned int density, process, numbufs;
  2872. /* Lock scheme depends on density and process */
  2873. density = onenand_get_density(this->device_id);
  2874. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  2875. numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
  2876. /* Lock scheme */
  2877. switch (density) {
  2878. case ONENAND_DEVICE_DENSITY_4Gb:
  2879. if (ONENAND_IS_DDP(this))
  2880. this->options |= ONENAND_HAS_2PLANE;
  2881. else if (numbufs == 1) {
  2882. this->options |= ONENAND_HAS_4KB_PAGE;
  2883. this->options |= ONENAND_HAS_CACHE_PROGRAM;
  2884. /*
  2885. * There are two different 4KiB pagesize chips
  2886. * and no way to detect it by H/W config values.
  2887. *
  2888. * To detect the correct NOP for each chips,
  2889. * It should check the version ID as workaround.
  2890. *
  2891. * Now it has as following
  2892. * KFM4G16Q4M has NOP 4 with version ID 0x0131
  2893. * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
  2894. */
  2895. if ((this->version_id & 0xf) == 0xe)
  2896. this->options |= ONENAND_HAS_NOP_1;
  2897. }
  2898. case ONENAND_DEVICE_DENSITY_2Gb:
  2899. /* 2Gb DDP does not have 2 plane */
  2900. if (!ONENAND_IS_DDP(this))
  2901. this->options |= ONENAND_HAS_2PLANE;
  2902. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2903. case ONENAND_DEVICE_DENSITY_1Gb:
  2904. /* A-Die has all block unlock */
  2905. if (process)
  2906. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2907. break;
  2908. default:
  2909. /* Some OneNAND has continuous lock scheme */
  2910. if (!process)
  2911. this->options |= ONENAND_HAS_CONT_LOCK;
  2912. break;
  2913. }
  2914. /* The MLC has 4KiB pagesize. */
  2915. if (ONENAND_IS_MLC(this))
  2916. this->options |= ONENAND_HAS_4KB_PAGE;
  2917. if (ONENAND_IS_4KB_PAGE(this))
  2918. this->options &= ~ONENAND_HAS_2PLANE;
  2919. if (FLEXONENAND(this)) {
  2920. this->options &= ~ONENAND_HAS_CONT_LOCK;
  2921. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2922. }
  2923. if (this->options & ONENAND_HAS_CONT_LOCK)
  2924. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  2925. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  2926. printk(KERN_DEBUG "Chip support all block unlock\n");
  2927. if (this->options & ONENAND_HAS_2PLANE)
  2928. printk(KERN_DEBUG "Chip has 2 plane\n");
  2929. if (this->options & ONENAND_HAS_4KB_PAGE)
  2930. printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
  2931. if (this->options & ONENAND_HAS_CACHE_PROGRAM)
  2932. printk(KERN_DEBUG "Chip has cache program feature\n");
  2933. }
  2934. /**
  2935. * onenand_print_device_info - Print device & version ID
  2936. * @param device device ID
  2937. * @param version version ID
  2938. *
  2939. * Print device & version ID
  2940. */
  2941. static void onenand_print_device_info(int device, int version)
  2942. {
  2943. int vcc, demuxed, ddp, density, flexonenand;
  2944. vcc = device & ONENAND_DEVICE_VCC_MASK;
  2945. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  2946. ddp = device & ONENAND_DEVICE_IS_DDP;
  2947. density = onenand_get_density(device);
  2948. flexonenand = device & DEVICE_IS_FLEXONENAND;
  2949. printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  2950. demuxed ? "" : "Muxed ",
  2951. flexonenand ? "Flex-" : "",
  2952. ddp ? "(DDP)" : "",
  2953. (16 << density),
  2954. vcc ? "2.65/3.3" : "1.8",
  2955. device);
  2956. printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
  2957. }
  2958. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  2959. {ONENAND_MFR_SAMSUNG, "Samsung"},
  2960. {ONENAND_MFR_NUMONYX, "Numonyx"},
  2961. };
  2962. /**
  2963. * onenand_check_maf - Check manufacturer ID
  2964. * @param manuf manufacturer ID
  2965. *
  2966. * Check manufacturer ID
  2967. */
  2968. static int onenand_check_maf(int manuf)
  2969. {
  2970. int size = ARRAY_SIZE(onenand_manuf_ids);
  2971. char *name;
  2972. int i;
  2973. for (i = 0; i < size; i++)
  2974. if (manuf == onenand_manuf_ids[i].id)
  2975. break;
  2976. if (i < size)
  2977. name = onenand_manuf_ids[i].name;
  2978. else
  2979. name = "Unknown";
  2980. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2981. return (i == size);
  2982. }
  2983. /**
  2984. * flexonenand_get_boundary - Reads the SLC boundary
  2985. * @param onenand_info - onenand info structure
  2986. **/
  2987. static int flexonenand_get_boundary(struct mtd_info *mtd)
  2988. {
  2989. struct onenand_chip *this = mtd->priv;
  2990. unsigned die, bdry;
  2991. int syscfg, locked;
  2992. /* Disable ECC */
  2993. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2994. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  2995. for (die = 0; die < this->dies; die++) {
  2996. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  2997. this->wait(mtd, FL_SYNCING);
  2998. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  2999. this->wait(mtd, FL_READING);
  3000. bdry = this->read_word(this->base + ONENAND_DATARAM);
  3001. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  3002. locked = 0;
  3003. else
  3004. locked = 1;
  3005. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  3006. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  3007. this->wait(mtd, FL_RESETING);
  3008. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  3009. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  3010. }
  3011. /* Enable ECC */
  3012. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3013. return 0;
  3014. }
  3015. /**
  3016. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  3017. * boundary[], diesize[], mtd->size, mtd->erasesize
  3018. * @param mtd - MTD device structure
  3019. */
  3020. static void flexonenand_get_size(struct mtd_info *mtd)
  3021. {
  3022. struct onenand_chip *this = mtd->priv;
  3023. int die, i, eraseshift, density;
  3024. int blksperdie, maxbdry;
  3025. loff_t ofs;
  3026. density = onenand_get_density(this->device_id);
  3027. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  3028. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3029. maxbdry = blksperdie - 1;
  3030. eraseshift = this->erase_shift - 1;
  3031. mtd->numeraseregions = this->dies << 1;
  3032. /* This fills up the device boundary */
  3033. flexonenand_get_boundary(mtd);
  3034. die = ofs = 0;
  3035. i = -1;
  3036. for (; die < this->dies; die++) {
  3037. if (!die || this->boundary[die-1] != maxbdry) {
  3038. i++;
  3039. mtd->eraseregions[i].offset = ofs;
  3040. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3041. mtd->eraseregions[i].numblocks =
  3042. this->boundary[die] + 1;
  3043. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3044. eraseshift++;
  3045. } else {
  3046. mtd->numeraseregions -= 1;
  3047. mtd->eraseregions[i].numblocks +=
  3048. this->boundary[die] + 1;
  3049. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  3050. }
  3051. if (this->boundary[die] != maxbdry) {
  3052. i++;
  3053. mtd->eraseregions[i].offset = ofs;
  3054. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3055. mtd->eraseregions[i].numblocks = maxbdry ^
  3056. this->boundary[die];
  3057. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3058. eraseshift--;
  3059. } else
  3060. mtd->numeraseregions -= 1;
  3061. }
  3062. /* Expose MLC erase size except when all blocks are SLC */
  3063. mtd->erasesize = 1 << this->erase_shift;
  3064. if (mtd->numeraseregions == 1)
  3065. mtd->erasesize >>= 1;
  3066. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  3067. for (i = 0; i < mtd->numeraseregions; i++)
  3068. printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
  3069. " numblocks: %04u]\n",
  3070. (unsigned int) mtd->eraseregions[i].offset,
  3071. mtd->eraseregions[i].erasesize,
  3072. mtd->eraseregions[i].numblocks);
  3073. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  3074. this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
  3075. this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
  3076. << (this->erase_shift - 1);
  3077. mtd->size += this->diesize[die];
  3078. }
  3079. }
  3080. /**
  3081. * flexonenand_check_blocks_erased - Check if blocks are erased
  3082. * @param mtd_info - mtd info structure
  3083. * @param start - first erase block to check
  3084. * @param end - last erase block to check
  3085. *
  3086. * Converting an unerased block from MLC to SLC
  3087. * causes byte values to change. Since both data and its ECC
  3088. * have changed, reads on the block give uncorrectable error.
  3089. * This might lead to the block being detected as bad.
  3090. *
  3091. * Avoid this by ensuring that the block to be converted is
  3092. * erased.
  3093. */
  3094. static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
  3095. {
  3096. struct onenand_chip *this = mtd->priv;
  3097. int i, ret;
  3098. int block;
  3099. struct mtd_oob_ops ops = {
  3100. .mode = MTD_OPS_PLACE_OOB,
  3101. .ooboffs = 0,
  3102. .ooblen = mtd->oobsize,
  3103. .datbuf = NULL,
  3104. .oobbuf = this->oob_buf,
  3105. };
  3106. loff_t addr;
  3107. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  3108. for (block = start; block <= end; block++) {
  3109. addr = flexonenand_addr(this, block);
  3110. if (onenand_block_isbad_nolock(mtd, addr, 0))
  3111. continue;
  3112. /*
  3113. * Since main area write results in ECC write to spare,
  3114. * it is sufficient to check only ECC bytes for change.
  3115. */
  3116. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  3117. if (ret)
  3118. return ret;
  3119. for (i = 0; i < mtd->oobsize; i++)
  3120. if (this->oob_buf[i] != 0xff)
  3121. break;
  3122. if (i != mtd->oobsize) {
  3123. printk(KERN_WARNING "%s: Block %d not erased.\n",
  3124. __func__, block);
  3125. return 1;
  3126. }
  3127. }
  3128. return 0;
  3129. }
  3130. /**
  3131. * flexonenand_set_boundary - Writes the SLC boundary
  3132. * @param mtd - mtd info structure
  3133. */
  3134. static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  3135. int boundary, int lock)
  3136. {
  3137. struct onenand_chip *this = mtd->priv;
  3138. int ret, density, blksperdie, old, new, thisboundary;
  3139. loff_t addr;
  3140. /* Change only once for SDP Flex-OneNAND */
  3141. if (die && (!ONENAND_IS_DDP(this)))
  3142. return 0;
  3143. /* boundary value of -1 indicates no required change */
  3144. if (boundary < 0 || boundary == this->boundary[die])
  3145. return 0;
  3146. density = onenand_get_density(this->device_id);
  3147. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  3148. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3149. if (boundary >= blksperdie) {
  3150. printk(KERN_ERR "%s: Invalid boundary value. "
  3151. "Boundary not changed.\n", __func__);
  3152. return -EINVAL;
  3153. }
  3154. /* Check if converting blocks are erased */
  3155. old = this->boundary[die] + (die * this->density_mask);
  3156. new = boundary + (die * this->density_mask);
  3157. ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
  3158. if (ret) {
  3159. printk(KERN_ERR "%s: Please erase blocks "
  3160. "before boundary change\n", __func__);
  3161. return ret;
  3162. }
  3163. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  3164. this->wait(mtd, FL_SYNCING);
  3165. /* Check is boundary is locked */
  3166. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3167. this->wait(mtd, FL_READING);
  3168. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  3169. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  3170. printk(KERN_ERR "%s: boundary locked\n", __func__);
  3171. ret = 1;
  3172. goto out;
  3173. }
  3174. printk(KERN_INFO "Changing die %d boundary: %d%s\n",
  3175. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  3176. addr = die ? this->diesize[0] : 0;
  3177. boundary &= FLEXONENAND_PI_MASK;
  3178. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  3179. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  3180. ret = this->wait(mtd, FL_ERASING);
  3181. if (ret) {
  3182. printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
  3183. __func__, die);
  3184. goto out;
  3185. }
  3186. this->write_word(boundary, this->base + ONENAND_DATARAM);
  3187. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  3188. ret = this->wait(mtd, FL_WRITING);
  3189. if (ret) {
  3190. printk(KERN_ERR "%s: Failed PI write for Die %d\n",
  3191. __func__, die);
  3192. goto out;
  3193. }
  3194. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  3195. ret = this->wait(mtd, FL_WRITING);
  3196. out:
  3197. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  3198. this->wait(mtd, FL_RESETING);
  3199. if (!ret)
  3200. /* Recalculate device size on boundary change*/
  3201. flexonenand_get_size(mtd);
  3202. return ret;
  3203. }
  3204. /**
  3205. * onenand_chip_probe - [OneNAND Interface] The generic chip probe
  3206. * @param mtd MTD device structure
  3207. *
  3208. * OneNAND detection method:
  3209. * Compare the values from command with ones from register
  3210. */
  3211. static int onenand_chip_probe(struct mtd_info *mtd)
  3212. {
  3213. struct onenand_chip *this = mtd->priv;
  3214. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  3215. int syscfg;
  3216. /* Save system configuration 1 */
  3217. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  3218. /* Clear Sync. Burst Read mode to read BootRAM */
  3219. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
  3220. /* Send the command for reading device ID from BootRAM */
  3221. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  3222. /* Read manufacturer and device IDs from BootRAM */
  3223. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  3224. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  3225. /* Reset OneNAND to read default register values */
  3226. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  3227. /* Wait reset */
  3228. this->wait(mtd, FL_RESETING);
  3229. /* Restore system configuration 1 */
  3230. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3231. /* Check manufacturer ID */
  3232. if (onenand_check_maf(bram_maf_id))
  3233. return -ENXIO;
  3234. /* Read manufacturer and device IDs from Register */
  3235. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3236. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3237. /* Check OneNAND device */
  3238. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  3239. return -ENXIO;
  3240. return 0;
  3241. }
  3242. /**
  3243. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  3244. * @param mtd MTD device structure
  3245. */
  3246. static int onenand_probe(struct mtd_info *mtd)
  3247. {
  3248. struct onenand_chip *this = mtd->priv;
  3249. int dev_id, ver_id;
  3250. int density;
  3251. int ret;
  3252. ret = this->chip_probe(mtd);
  3253. if (ret)
  3254. return ret;
  3255. /* Device and version IDs from Register */
  3256. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3257. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  3258. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  3259. /* Flash device information */
  3260. onenand_print_device_info(dev_id, ver_id);
  3261. this->device_id = dev_id;
  3262. this->version_id = ver_id;
  3263. /* Check OneNAND features */
  3264. onenand_check_features(mtd);
  3265. density = onenand_get_density(dev_id);
  3266. if (FLEXONENAND(this)) {
  3267. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  3268. /* Maximum possible erase regions */
  3269. mtd->numeraseregions = this->dies << 1;
  3270. mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
  3271. * (this->dies << 1), GFP_KERNEL);
  3272. if (!mtd->eraseregions)
  3273. return -ENOMEM;
  3274. }
  3275. /*
  3276. * For Flex-OneNAND, chipsize represents maximum possible device size.
  3277. * mtd->size represents the actual device size.
  3278. */
  3279. this->chipsize = (16 << density) << 20;
  3280. /* OneNAND page size & block size */
  3281. /* The data buffer size is equal to page size */
  3282. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  3283. /* We use the full BufferRAM */
  3284. if (ONENAND_IS_4KB_PAGE(this))
  3285. mtd->writesize <<= 1;
  3286. mtd->oobsize = mtd->writesize >> 5;
  3287. /* Pages per a block are always 64 in OneNAND */
  3288. mtd->erasesize = mtd->writesize << 6;
  3289. /*
  3290. * Flex-OneNAND SLC area has 64 pages per block.
  3291. * Flex-OneNAND MLC area has 128 pages per block.
  3292. * Expose MLC erase size to find erase_shift and page_mask.
  3293. */
  3294. if (FLEXONENAND(this))
  3295. mtd->erasesize <<= 1;
  3296. this->erase_shift = ffs(mtd->erasesize) - 1;
  3297. this->page_shift = ffs(mtd->writesize) - 1;
  3298. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  3299. /* Set density mask. it is used for DDP */
  3300. if (ONENAND_IS_DDP(this))
  3301. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  3302. /* It's real page size */
  3303. this->writesize = mtd->writesize;
  3304. /* REVISIT: Multichip handling */
  3305. if (FLEXONENAND(this))
  3306. flexonenand_get_size(mtd);
  3307. else
  3308. mtd->size = this->chipsize;
  3309. /*
  3310. * We emulate the 4KiB page and 256KiB erase block size
  3311. * But oobsize is still 64 bytes.
  3312. * It is only valid if you turn on 2X program support,
  3313. * Otherwise it will be ignored by compiler.
  3314. */
  3315. if (ONENAND_IS_2PLANE(this)) {
  3316. mtd->writesize <<= 1;
  3317. mtd->erasesize <<= 1;
  3318. }
  3319. return 0;
  3320. }
  3321. /**
  3322. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  3323. * @param mtd MTD device structure
  3324. */
  3325. static int onenand_suspend(struct mtd_info *mtd)
  3326. {
  3327. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  3328. }
  3329. /**
  3330. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  3331. * @param mtd MTD device structure
  3332. */
  3333. static void onenand_resume(struct mtd_info *mtd)
  3334. {
  3335. struct onenand_chip *this = mtd->priv;
  3336. if (this->state == FL_PM_SUSPENDED)
  3337. onenand_release_device(mtd);
  3338. else
  3339. printk(KERN_ERR "%s: resume() called for the chip which is not "
  3340. "in suspended state\n", __func__);
  3341. }
  3342. /**
  3343. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  3344. * @param mtd MTD device structure
  3345. * @param maxchips Number of chips to scan for
  3346. *
  3347. * This fills out all the not initialized function pointers
  3348. * with the defaults.
  3349. * The flash ID is read and the mtd/chip structures are
  3350. * filled with the appropriate values.
  3351. */
  3352. int onenand_scan(struct mtd_info *mtd, int maxchips)
  3353. {
  3354. int i, ret;
  3355. struct onenand_chip *this = mtd->priv;
  3356. if (!this->read_word)
  3357. this->read_word = onenand_readw;
  3358. if (!this->write_word)
  3359. this->write_word = onenand_writew;
  3360. if (!this->command)
  3361. this->command = onenand_command;
  3362. if (!this->wait)
  3363. onenand_setup_wait(mtd);
  3364. if (!this->bbt_wait)
  3365. this->bbt_wait = onenand_bbt_wait;
  3366. if (!this->unlock_all)
  3367. this->unlock_all = onenand_unlock_all;
  3368. if (!this->chip_probe)
  3369. this->chip_probe = onenand_chip_probe;
  3370. if (!this->read_bufferram)
  3371. this->read_bufferram = onenand_read_bufferram;
  3372. if (!this->write_bufferram)
  3373. this->write_bufferram = onenand_write_bufferram;
  3374. if (!this->block_markbad)
  3375. this->block_markbad = onenand_default_block_markbad;
  3376. if (!this->scan_bbt)
  3377. this->scan_bbt = onenand_default_bbt;
  3378. if (onenand_probe(mtd))
  3379. return -ENXIO;
  3380. /* Set Sync. Burst Read after probing */
  3381. if (this->mmcontrol) {
  3382. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  3383. this->read_bufferram = onenand_sync_read_bufferram;
  3384. }
  3385. /* Allocate buffers, if necessary */
  3386. if (!this->page_buf) {
  3387. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3388. if (!this->page_buf) {
  3389. printk(KERN_ERR "%s: Can't allocate page_buf\n",
  3390. __func__);
  3391. return -ENOMEM;
  3392. }
  3393. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3394. this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3395. if (!this->verify_buf) {
  3396. kfree(this->page_buf);
  3397. return -ENOMEM;
  3398. }
  3399. #endif
  3400. this->options |= ONENAND_PAGEBUF_ALLOC;
  3401. }
  3402. if (!this->oob_buf) {
  3403. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  3404. if (!this->oob_buf) {
  3405. printk(KERN_ERR "%s: Can't allocate oob_buf\n",
  3406. __func__);
  3407. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3408. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  3409. kfree(this->page_buf);
  3410. }
  3411. return -ENOMEM;
  3412. }
  3413. this->options |= ONENAND_OOBBUF_ALLOC;
  3414. }
  3415. this->state = FL_READY;
  3416. init_waitqueue_head(&this->wq);
  3417. spin_lock_init(&this->chip_lock);
  3418. /*
  3419. * Allow subpage writes up to oobsize.
  3420. */
  3421. switch (mtd->oobsize) {
  3422. case 128:
  3423. if (FLEXONENAND(this)) {
  3424. this->ecclayout = &flexonenand_oob_128;
  3425. mtd->subpage_sft = 0;
  3426. } else {
  3427. this->ecclayout = &onenand_oob_128;
  3428. mtd->subpage_sft = 2;
  3429. }
  3430. if (ONENAND_IS_NOP_1(this))
  3431. mtd->subpage_sft = 0;
  3432. break;
  3433. case 64:
  3434. this->ecclayout = &onenand_oob_64;
  3435. mtd->subpage_sft = 2;
  3436. break;
  3437. case 32:
  3438. this->ecclayout = &onenand_oob_32;
  3439. mtd->subpage_sft = 1;
  3440. break;
  3441. default:
  3442. printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
  3443. __func__, mtd->oobsize);
  3444. mtd->subpage_sft = 0;
  3445. /* To prevent kernel oops */
  3446. this->ecclayout = &onenand_oob_32;
  3447. break;
  3448. }
  3449. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3450. /*
  3451. * The number of bytes available for a client to place data into
  3452. * the out of band area
  3453. */
  3454. this->ecclayout->oobavail = 0;
  3455. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  3456. this->ecclayout->oobfree[i].length; i++)
  3457. this->ecclayout->oobavail +=
  3458. this->ecclayout->oobfree[i].length;
  3459. mtd->oobavail = this->ecclayout->oobavail;
  3460. mtd->ecclayout = this->ecclayout;
  3461. mtd->ecc_strength = 1;
  3462. /* Fill in remaining MTD driver data */
  3463. mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
  3464. mtd->flags = MTD_CAP_NANDFLASH;
  3465. mtd->_erase = onenand_erase;
  3466. mtd->_point = NULL;
  3467. mtd->_unpoint = NULL;
  3468. mtd->_read = onenand_read;
  3469. mtd->_write = onenand_write;
  3470. mtd->_read_oob = onenand_read_oob;
  3471. mtd->_write_oob = onenand_write_oob;
  3472. mtd->_panic_write = onenand_panic_write;
  3473. #ifdef CONFIG_MTD_ONENAND_OTP
  3474. mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
  3475. mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
  3476. mtd->_get_user_prot_info = onenand_get_user_prot_info;
  3477. mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
  3478. mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
  3479. mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
  3480. #endif
  3481. mtd->_sync = onenand_sync;
  3482. mtd->_lock = onenand_lock;
  3483. mtd->_unlock = onenand_unlock;
  3484. mtd->_suspend = onenand_suspend;
  3485. mtd->_resume = onenand_resume;
  3486. mtd->_block_isbad = onenand_block_isbad;
  3487. mtd->_block_markbad = onenand_block_markbad;
  3488. mtd->owner = THIS_MODULE;
  3489. mtd->writebufsize = mtd->writesize;
  3490. /* Unlock whole block */
  3491. if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
  3492. this->unlock_all(mtd);
  3493. ret = this->scan_bbt(mtd);
  3494. if ((!FLEXONENAND(this)) || ret)
  3495. return ret;
  3496. /* Change Flex-OneNAND boundaries if required */
  3497. for (i = 0; i < MAX_DIES; i++)
  3498. flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
  3499. flex_bdry[(2 * i) + 1]);
  3500. return 0;
  3501. }
  3502. /**
  3503. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  3504. * @param mtd MTD device structure
  3505. */
  3506. void onenand_release(struct mtd_info *mtd)
  3507. {
  3508. struct onenand_chip *this = mtd->priv;
  3509. /* Deregister partitions */
  3510. mtd_device_unregister(mtd);
  3511. /* Free bad block table memory, if allocated */
  3512. if (this->bbm) {
  3513. struct bbm_info *bbm = this->bbm;
  3514. kfree(bbm->bbt);
  3515. kfree(this->bbm);
  3516. }
  3517. /* Buffers allocated by onenand_scan */
  3518. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3519. kfree(this->page_buf);
  3520. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3521. kfree(this->verify_buf);
  3522. #endif
  3523. }
  3524. if (this->options & ONENAND_OOBBUF_ALLOC)
  3525. kfree(this->oob_buf);
  3526. kfree(mtd->eraseregions);
  3527. }
  3528. EXPORT_SYMBOL_GPL(onenand_scan);
  3529. EXPORT_SYMBOL_GPL(onenand_release);
  3530. MODULE_LICENSE("GPL");
  3531. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  3532. MODULE_DESCRIPTION("Generic OneNAND flash driver code");