onenand_base.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171
  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 - [Internal] 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 (status != -EBADMSG && 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. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
  977. __func__, (unsigned int) from, (int) len);
  978. if (ops->mode == MTD_OOB_AUTO)
  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 (ret == -EBADMSG)
  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_OOB_AUTO)
  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 mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1039. }
  1040. /**
  1041. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  1042. * @param mtd MTD device structure
  1043. * @param from offset to read from
  1044. * @param ops: oob operation description structure
  1045. *
  1046. * OneNAND read main and/or out-of-band data
  1047. */
  1048. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  1049. struct mtd_oob_ops *ops)
  1050. {
  1051. struct onenand_chip *this = mtd->priv;
  1052. struct mtd_ecc_stats stats;
  1053. size_t len = ops->len;
  1054. size_t ooblen = ops->ooblen;
  1055. u_char *buf = ops->datbuf;
  1056. u_char *oobbuf = ops->oobbuf;
  1057. int read = 0, column, thislen;
  1058. int oobread = 0, oobcolumn, thisooblen, oobsize;
  1059. int ret = 0, boundary = 0;
  1060. int writesize = this->writesize;
  1061. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
  1062. __func__, (unsigned int) from, (int) len);
  1063. if (ops->mode == MTD_OOB_AUTO)
  1064. oobsize = this->ecclayout->oobavail;
  1065. else
  1066. oobsize = mtd->oobsize;
  1067. oobcolumn = from & (mtd->oobsize - 1);
  1068. /* Do not allow reads past end of device */
  1069. if ((from + len) > mtd->size) {
  1070. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1071. __func__);
  1072. ops->retlen = 0;
  1073. ops->oobretlen = 0;
  1074. return -EINVAL;
  1075. }
  1076. stats = mtd->ecc_stats;
  1077. /* Read-while-load method */
  1078. /* Do first load to bufferRAM */
  1079. if (read < len) {
  1080. if (!onenand_check_bufferram(mtd, from)) {
  1081. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1082. ret = this->wait(mtd, FL_READING);
  1083. onenand_update_bufferram(mtd, from, !ret);
  1084. if (ret == -EBADMSG)
  1085. ret = 0;
  1086. }
  1087. }
  1088. thislen = min_t(int, writesize, len - read);
  1089. column = from & (writesize - 1);
  1090. if (column + thislen > writesize)
  1091. thislen = writesize - column;
  1092. while (!ret) {
  1093. /* If there is more to load then start next load */
  1094. from += thislen;
  1095. if (read + thislen < len) {
  1096. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1097. /*
  1098. * Chip boundary handling in DDP
  1099. * Now we issued chip 1 read and pointed chip 1
  1100. * bufferram so we have to point chip 0 bufferram.
  1101. */
  1102. if (ONENAND_IS_DDP(this) &&
  1103. unlikely(from == (this->chipsize >> 1))) {
  1104. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  1105. boundary = 1;
  1106. } else
  1107. boundary = 0;
  1108. ONENAND_SET_PREV_BUFFERRAM(this);
  1109. }
  1110. /* While load is going, read from last bufferRAM */
  1111. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1112. /* Read oob area if needed */
  1113. if (oobbuf) {
  1114. thisooblen = oobsize - oobcolumn;
  1115. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1116. if (ops->mode == MTD_OOB_AUTO)
  1117. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1118. else
  1119. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1120. oobread += thisooblen;
  1121. oobbuf += thisooblen;
  1122. oobcolumn = 0;
  1123. }
  1124. /* See if we are done */
  1125. read += thislen;
  1126. if (read == len)
  1127. break;
  1128. /* Set up for next read from bufferRAM */
  1129. if (unlikely(boundary))
  1130. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  1131. ONENAND_SET_NEXT_BUFFERRAM(this);
  1132. buf += thislen;
  1133. thislen = min_t(int, writesize, len - read);
  1134. column = 0;
  1135. cond_resched();
  1136. /* Now wait for load */
  1137. ret = this->wait(mtd, FL_READING);
  1138. onenand_update_bufferram(mtd, from, !ret);
  1139. if (ret == -EBADMSG)
  1140. ret = 0;
  1141. }
  1142. /*
  1143. * Return success, if no ECC failures, else -EBADMSG
  1144. * fs driver will take care of that, because
  1145. * retlen == desired len and result == -EBADMSG
  1146. */
  1147. ops->retlen = read;
  1148. ops->oobretlen = oobread;
  1149. if (ret)
  1150. return ret;
  1151. if (mtd->ecc_stats.failed - stats.failed)
  1152. return -EBADMSG;
  1153. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1154. }
  1155. /**
  1156. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  1157. * @param mtd MTD device structure
  1158. * @param from offset to read from
  1159. * @param ops: oob operation description structure
  1160. *
  1161. * OneNAND read out-of-band data from the spare area
  1162. */
  1163. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  1164. struct mtd_oob_ops *ops)
  1165. {
  1166. struct onenand_chip *this = mtd->priv;
  1167. struct mtd_ecc_stats stats;
  1168. int read = 0, thislen, column, oobsize;
  1169. size_t len = ops->ooblen;
  1170. mtd_oob_mode_t mode = ops->mode;
  1171. u_char *buf = ops->oobbuf;
  1172. int ret = 0, readcmd;
  1173. from += ops->ooboffs;
  1174. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
  1175. __func__, (unsigned int) from, (int) len);
  1176. /* Initialize return length value */
  1177. ops->oobretlen = 0;
  1178. if (mode == MTD_OOB_AUTO)
  1179. oobsize = this->ecclayout->oobavail;
  1180. else
  1181. oobsize = mtd->oobsize;
  1182. column = from & (mtd->oobsize - 1);
  1183. if (unlikely(column >= oobsize)) {
  1184. printk(KERN_ERR "%s: Attempted to start read outside oob\n",
  1185. __func__);
  1186. return -EINVAL;
  1187. }
  1188. /* Do not allow reads past end of device */
  1189. if (unlikely(from >= mtd->size ||
  1190. column + len > ((mtd->size >> this->page_shift) -
  1191. (from >> this->page_shift)) * oobsize)) {
  1192. printk(KERN_ERR "%s: Attempted to read beyond end of device\n",
  1193. __func__);
  1194. return -EINVAL;
  1195. }
  1196. stats = mtd->ecc_stats;
  1197. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1198. while (read < len) {
  1199. cond_resched();
  1200. thislen = oobsize - column;
  1201. thislen = min_t(int, thislen, len);
  1202. this->command(mtd, readcmd, from, mtd->oobsize);
  1203. onenand_update_bufferram(mtd, from, 0);
  1204. ret = this->wait(mtd, FL_READING);
  1205. if (unlikely(ret))
  1206. ret = onenand_recover_lsb(mtd, from, ret);
  1207. if (ret && ret != -EBADMSG) {
  1208. printk(KERN_ERR "%s: read failed = 0x%x\n",
  1209. __func__, ret);
  1210. break;
  1211. }
  1212. if (mode == MTD_OOB_AUTO)
  1213. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  1214. else
  1215. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1216. read += thislen;
  1217. if (read == len)
  1218. break;
  1219. buf += thislen;
  1220. /* Read more? */
  1221. if (read < len) {
  1222. /* Page size */
  1223. from += mtd->writesize;
  1224. column = 0;
  1225. }
  1226. }
  1227. ops->oobretlen = read;
  1228. if (ret)
  1229. return ret;
  1230. if (mtd->ecc_stats.failed - stats.failed)
  1231. return -EBADMSG;
  1232. return 0;
  1233. }
  1234. /**
  1235. * onenand_read - [MTD Interface] Read data from flash
  1236. * @param mtd MTD device structure
  1237. * @param from offset to read from
  1238. * @param len number of bytes to read
  1239. * @param retlen pointer to variable to store the number of read bytes
  1240. * @param buf the databuffer to put data
  1241. *
  1242. * Read with ecc
  1243. */
  1244. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1245. size_t *retlen, u_char *buf)
  1246. {
  1247. struct onenand_chip *this = mtd->priv;
  1248. struct mtd_oob_ops ops = {
  1249. .len = len,
  1250. .ooblen = 0,
  1251. .datbuf = buf,
  1252. .oobbuf = NULL,
  1253. };
  1254. int ret;
  1255. onenand_get_device(mtd, FL_READING);
  1256. ret = ONENAND_IS_4KB_PAGE(this) ?
  1257. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  1258. onenand_read_ops_nolock(mtd, from, &ops);
  1259. onenand_release_device(mtd);
  1260. *retlen = ops.retlen;
  1261. return ret;
  1262. }
  1263. /**
  1264. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  1265. * @param mtd: MTD device structure
  1266. * @param from: offset to read from
  1267. * @param ops: oob operation description structure
  1268. * Read main and/or out-of-band
  1269. */
  1270. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  1271. struct mtd_oob_ops *ops)
  1272. {
  1273. struct onenand_chip *this = mtd->priv;
  1274. int ret;
  1275. switch (ops->mode) {
  1276. case MTD_OOB_PLACE:
  1277. case MTD_OOB_AUTO:
  1278. break;
  1279. case MTD_OOB_RAW:
  1280. /* Not implemented yet */
  1281. default:
  1282. return -EINVAL;
  1283. }
  1284. onenand_get_device(mtd, FL_READING);
  1285. if (ops->datbuf)
  1286. ret = ONENAND_IS_4KB_PAGE(this) ?
  1287. onenand_mlc_read_ops_nolock(mtd, from, ops) :
  1288. onenand_read_ops_nolock(mtd, from, ops);
  1289. else
  1290. ret = onenand_read_oob_nolock(mtd, from, ops);
  1291. onenand_release_device(mtd);
  1292. return ret;
  1293. }
  1294. /**
  1295. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  1296. * @param mtd MTD device structure
  1297. * @param state state to select the max. timeout value
  1298. *
  1299. * Wait for command done.
  1300. */
  1301. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  1302. {
  1303. struct onenand_chip *this = mtd->priv;
  1304. unsigned long timeout;
  1305. unsigned int interrupt, ctrl, ecc, addr1, addr8;
  1306. /* The 20 msec is enough */
  1307. timeout = jiffies + msecs_to_jiffies(20);
  1308. while (time_before(jiffies, timeout)) {
  1309. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1310. if (interrupt & ONENAND_INT_MASTER)
  1311. break;
  1312. }
  1313. /* To get correct interrupt status in timeout case */
  1314. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1315. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  1316. addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
  1317. addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
  1318. if (interrupt & ONENAND_INT_READ) {
  1319. ecc = onenand_read_ecc(this);
  1320. if (ecc & ONENAND_ECC_2BIT_ALL) {
  1321. printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
  1322. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1323. __func__, ecc, ctrl, interrupt, addr1, addr8);
  1324. return ONENAND_BBT_READ_ECC_ERROR;
  1325. }
  1326. } else {
  1327. printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
  1328. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1329. __func__, ctrl, interrupt, addr1, addr8);
  1330. return ONENAND_BBT_READ_FATAL_ERROR;
  1331. }
  1332. /* Initial bad block case: 0x2400 or 0x0400 */
  1333. if (ctrl & ONENAND_CTRL_ERROR) {
  1334. printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
  1335. "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
  1336. return ONENAND_BBT_READ_ERROR;
  1337. }
  1338. return 0;
  1339. }
  1340. /**
  1341. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1342. * @param mtd MTD device structure
  1343. * @param from offset to read from
  1344. * @param ops oob operation description structure
  1345. *
  1346. * OneNAND read out-of-band data from the spare area for bbt scan
  1347. */
  1348. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1349. struct mtd_oob_ops *ops)
  1350. {
  1351. struct onenand_chip *this = mtd->priv;
  1352. int read = 0, thislen, column;
  1353. int ret = 0, readcmd;
  1354. size_t len = ops->ooblen;
  1355. u_char *buf = ops->oobbuf;
  1356. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %zi\n",
  1357. __func__, (unsigned int) from, len);
  1358. /* Initialize return value */
  1359. ops->oobretlen = 0;
  1360. /* Do not allow reads past end of device */
  1361. if (unlikely((from + len) > mtd->size)) {
  1362. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1363. __func__);
  1364. return ONENAND_BBT_READ_FATAL_ERROR;
  1365. }
  1366. /* Grab the lock and see if the device is available */
  1367. onenand_get_device(mtd, FL_READING);
  1368. column = from & (mtd->oobsize - 1);
  1369. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1370. while (read < len) {
  1371. cond_resched();
  1372. thislen = mtd->oobsize - column;
  1373. thislen = min_t(int, thislen, len);
  1374. this->command(mtd, readcmd, from, mtd->oobsize);
  1375. onenand_update_bufferram(mtd, from, 0);
  1376. ret = this->bbt_wait(mtd, FL_READING);
  1377. if (unlikely(ret))
  1378. ret = onenand_recover_lsb(mtd, from, ret);
  1379. if (ret)
  1380. break;
  1381. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1382. read += thislen;
  1383. if (read == len)
  1384. break;
  1385. buf += thislen;
  1386. /* Read more? */
  1387. if (read < len) {
  1388. /* Update Page size */
  1389. from += this->writesize;
  1390. column = 0;
  1391. }
  1392. }
  1393. /* Deselect and wake up anyone waiting on the device */
  1394. onenand_release_device(mtd);
  1395. ops->oobretlen = read;
  1396. return ret;
  1397. }
  1398. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1399. /**
  1400. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1401. * @param mtd MTD device structure
  1402. * @param buf the databuffer to verify
  1403. * @param to offset to read from
  1404. */
  1405. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1406. {
  1407. struct onenand_chip *this = mtd->priv;
  1408. u_char *oob_buf = this->oob_buf;
  1409. int status, i, readcmd;
  1410. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1411. this->command(mtd, readcmd, to, mtd->oobsize);
  1412. onenand_update_bufferram(mtd, to, 0);
  1413. status = this->wait(mtd, FL_READING);
  1414. if (status)
  1415. return status;
  1416. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  1417. for (i = 0; i < mtd->oobsize; i++)
  1418. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  1419. return -EBADMSG;
  1420. return 0;
  1421. }
  1422. /**
  1423. * onenand_verify - [GENERIC] verify the chip contents after a write
  1424. * @param mtd MTD device structure
  1425. * @param buf the databuffer to verify
  1426. * @param addr offset to read from
  1427. * @param len number of bytes to read and compare
  1428. */
  1429. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1430. {
  1431. struct onenand_chip *this = mtd->priv;
  1432. int ret = 0;
  1433. int thislen, column;
  1434. column = addr & (this->writesize - 1);
  1435. while (len != 0) {
  1436. thislen = min_t(int, this->writesize - column, len);
  1437. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1438. onenand_update_bufferram(mtd, addr, 0);
  1439. ret = this->wait(mtd, FL_READING);
  1440. if (ret)
  1441. return ret;
  1442. onenand_update_bufferram(mtd, addr, 1);
  1443. this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
  1444. if (memcmp(buf, this->verify_buf + column, thislen))
  1445. return -EBADMSG;
  1446. len -= thislen;
  1447. buf += thislen;
  1448. addr += thislen;
  1449. column = 0;
  1450. }
  1451. return 0;
  1452. }
  1453. #else
  1454. #define onenand_verify(...) (0)
  1455. #define onenand_verify_oob(...) (0)
  1456. #endif
  1457. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1458. static void onenand_panic_wait(struct mtd_info *mtd)
  1459. {
  1460. struct onenand_chip *this = mtd->priv;
  1461. unsigned int interrupt;
  1462. int i;
  1463. for (i = 0; i < 2000; i++) {
  1464. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1465. if (interrupt & ONENAND_INT_MASTER)
  1466. break;
  1467. udelay(10);
  1468. }
  1469. }
  1470. /**
  1471. * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
  1472. * @param mtd MTD device structure
  1473. * @param to offset to write to
  1474. * @param len number of bytes to write
  1475. * @param retlen pointer to variable to store the number of written bytes
  1476. * @param buf the data to write
  1477. *
  1478. * Write with ECC
  1479. */
  1480. static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1481. size_t *retlen, const u_char *buf)
  1482. {
  1483. struct onenand_chip *this = mtd->priv;
  1484. int column, subpage;
  1485. int written = 0;
  1486. int ret = 0;
  1487. if (this->state == FL_PM_SUSPENDED)
  1488. return -EBUSY;
  1489. /* Wait for any existing operation to clear */
  1490. onenand_panic_wait(mtd);
  1491. DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
  1492. __func__, (unsigned int) to, (int) len);
  1493. /* Initialize retlen, in case of early exit */
  1494. *retlen = 0;
  1495. /* Do not allow writes past end of device */
  1496. if (unlikely((to + len) > mtd->size)) {
  1497. printk(KERN_ERR "%s: Attempt write to past end of device\n",
  1498. __func__);
  1499. return -EINVAL;
  1500. }
  1501. /* Reject writes, which are not page aligned */
  1502. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1503. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1504. __func__);
  1505. return -EINVAL;
  1506. }
  1507. column = to & (mtd->writesize - 1);
  1508. /* Loop until all data write */
  1509. while (written < len) {
  1510. int thislen = min_t(int, mtd->writesize - column, len - written);
  1511. u_char *wbuf = (u_char *) buf;
  1512. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1513. /* Partial page write */
  1514. subpage = thislen < mtd->writesize;
  1515. if (subpage) {
  1516. memset(this->page_buf, 0xff, mtd->writesize);
  1517. memcpy(this->page_buf + column, buf, thislen);
  1518. wbuf = this->page_buf;
  1519. }
  1520. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1521. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  1522. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1523. onenand_panic_wait(mtd);
  1524. /* In partial page write we don't update bufferram */
  1525. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1526. if (ONENAND_IS_2PLANE(this)) {
  1527. ONENAND_SET_BUFFERRAM1(this);
  1528. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1529. }
  1530. if (ret) {
  1531. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1532. break;
  1533. }
  1534. written += thislen;
  1535. if (written == len)
  1536. break;
  1537. column = 0;
  1538. to += thislen;
  1539. buf += thislen;
  1540. }
  1541. *retlen = written;
  1542. return ret;
  1543. }
  1544. /**
  1545. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  1546. * @param mtd MTD device structure
  1547. * @param oob_buf oob buffer
  1548. * @param buf source address
  1549. * @param column oob offset to write to
  1550. * @param thislen oob length to write
  1551. */
  1552. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1553. const u_char *buf, int column, int thislen)
  1554. {
  1555. struct onenand_chip *this = mtd->priv;
  1556. struct nand_oobfree *free;
  1557. int writecol = column;
  1558. int writeend = column + thislen;
  1559. int lastgap = 0;
  1560. unsigned int i;
  1561. free = this->ecclayout->oobfree;
  1562. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1563. if (writecol >= lastgap)
  1564. writecol += free->offset - lastgap;
  1565. if (writeend >= lastgap)
  1566. writeend += free->offset - lastgap;
  1567. lastgap = free->offset + free->length;
  1568. }
  1569. free = this->ecclayout->oobfree;
  1570. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1571. int free_end = free->offset + free->length;
  1572. if (free->offset < writeend && free_end > writecol) {
  1573. int st = max_t(int,free->offset,writecol);
  1574. int ed = min_t(int,free_end,writeend);
  1575. int n = ed - st;
  1576. memcpy(oob_buf + st, buf, n);
  1577. buf += n;
  1578. } else if (column == 0)
  1579. break;
  1580. }
  1581. return 0;
  1582. }
  1583. /**
  1584. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1585. * @param mtd MTD device structure
  1586. * @param to offset to write to
  1587. * @param ops oob operation description structure
  1588. *
  1589. * Write main and/or oob with ECC
  1590. */
  1591. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1592. struct mtd_oob_ops *ops)
  1593. {
  1594. struct onenand_chip *this = mtd->priv;
  1595. int written = 0, column, thislen = 0, subpage = 0;
  1596. int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
  1597. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1598. size_t len = ops->len;
  1599. size_t ooblen = ops->ooblen;
  1600. const u_char *buf = ops->datbuf;
  1601. const u_char *oob = ops->oobbuf;
  1602. u_char *oobbuf;
  1603. int ret = 0, cmd;
  1604. DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
  1605. __func__, (unsigned int) to, (int) len);
  1606. /* Initialize retlen, in case of early exit */
  1607. ops->retlen = 0;
  1608. ops->oobretlen = 0;
  1609. /* Do not allow writes past end of device */
  1610. if (unlikely((to + len) > mtd->size)) {
  1611. printk(KERN_ERR "%s: Attempt write to past end of device\n",
  1612. __func__);
  1613. return -EINVAL;
  1614. }
  1615. /* Reject writes, which are not page aligned */
  1616. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1617. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1618. __func__);
  1619. return -EINVAL;
  1620. }
  1621. /* Check zero length */
  1622. if (!len)
  1623. return 0;
  1624. if (ops->mode == MTD_OOB_AUTO)
  1625. oobsize = this->ecclayout->oobavail;
  1626. else
  1627. oobsize = mtd->oobsize;
  1628. oobcolumn = to & (mtd->oobsize - 1);
  1629. column = to & (mtd->writesize - 1);
  1630. /* Loop until all data write */
  1631. while (1) {
  1632. if (written < len) {
  1633. u_char *wbuf = (u_char *) buf;
  1634. thislen = min_t(int, mtd->writesize - column, len - written);
  1635. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1636. cond_resched();
  1637. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1638. /* Partial page write */
  1639. subpage = thislen < mtd->writesize;
  1640. if (subpage) {
  1641. memset(this->page_buf, 0xff, mtd->writesize);
  1642. memcpy(this->page_buf + column, buf, thislen);
  1643. wbuf = this->page_buf;
  1644. }
  1645. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1646. if (oob) {
  1647. oobbuf = this->oob_buf;
  1648. /* We send data to spare ram with oobsize
  1649. * to prevent byte access */
  1650. memset(oobbuf, 0xff, mtd->oobsize);
  1651. if (ops->mode == MTD_OOB_AUTO)
  1652. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1653. else
  1654. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1655. oobwritten += thisooblen;
  1656. oob += thisooblen;
  1657. oobcolumn = 0;
  1658. } else
  1659. oobbuf = (u_char *) ffchars;
  1660. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1661. } else
  1662. ONENAND_SET_NEXT_BUFFERRAM(this);
  1663. /*
  1664. * 2 PLANE, MLC, and Flex-OneNAND do not support
  1665. * write-while-program feature.
  1666. */
  1667. if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
  1668. ONENAND_SET_PREV_BUFFERRAM(this);
  1669. ret = this->wait(mtd, FL_WRITING);
  1670. /* In partial page write we don't update bufferram */
  1671. onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
  1672. if (ret) {
  1673. written -= prevlen;
  1674. printk(KERN_ERR "%s: write failed %d\n",
  1675. __func__, ret);
  1676. break;
  1677. }
  1678. if (written == len) {
  1679. /* Only check verify write turn on */
  1680. ret = onenand_verify(mtd, buf - len, to - len, len);
  1681. if (ret)
  1682. printk(KERN_ERR "%s: verify failed %d\n",
  1683. __func__, ret);
  1684. break;
  1685. }
  1686. ONENAND_SET_NEXT_BUFFERRAM(this);
  1687. }
  1688. this->ongoing = 0;
  1689. cmd = ONENAND_CMD_PROG;
  1690. /* Exclude 1st OTP and OTP blocks for cache program feature */
  1691. if (ONENAND_IS_CACHE_PROGRAM(this) &&
  1692. likely(onenand_block(this, to) != 0) &&
  1693. ONENAND_IS_4KB_PAGE(this) &&
  1694. ((written + thislen) < len)) {
  1695. cmd = ONENAND_CMD_2X_CACHE_PROG;
  1696. this->ongoing = 1;
  1697. }
  1698. this->command(mtd, cmd, to, mtd->writesize);
  1699. /*
  1700. * 2 PLANE, MLC, and Flex-OneNAND wait here
  1701. */
  1702. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
  1703. ret = this->wait(mtd, FL_WRITING);
  1704. /* In partial page write we don't update bufferram */
  1705. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1706. if (ret) {
  1707. printk(KERN_ERR "%s: write failed %d\n",
  1708. __func__, ret);
  1709. break;
  1710. }
  1711. /* Only check verify write turn on */
  1712. ret = onenand_verify(mtd, buf, to, thislen);
  1713. if (ret) {
  1714. printk(KERN_ERR "%s: verify failed %d\n",
  1715. __func__, ret);
  1716. break;
  1717. }
  1718. written += thislen;
  1719. if (written == len)
  1720. break;
  1721. } else
  1722. written += thislen;
  1723. column = 0;
  1724. prev_subpage = subpage;
  1725. prev = to;
  1726. prevlen = thislen;
  1727. to += thislen;
  1728. buf += thislen;
  1729. first = 0;
  1730. }
  1731. /* In error case, clear all bufferrams */
  1732. if (written != len)
  1733. onenand_invalidate_bufferram(mtd, 0, -1);
  1734. ops->retlen = written;
  1735. ops->oobretlen = oobwritten;
  1736. return ret;
  1737. }
  1738. /**
  1739. * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
  1740. * @param mtd MTD device structure
  1741. * @param to offset to write to
  1742. * @param len number of bytes to write
  1743. * @param retlen pointer to variable to store the number of written bytes
  1744. * @param buf the data to write
  1745. * @param mode operation mode
  1746. *
  1747. * OneNAND write out-of-band
  1748. */
  1749. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1750. struct mtd_oob_ops *ops)
  1751. {
  1752. struct onenand_chip *this = mtd->priv;
  1753. int column, ret = 0, oobsize;
  1754. int written = 0, oobcmd;
  1755. u_char *oobbuf;
  1756. size_t len = ops->ooblen;
  1757. const u_char *buf = ops->oobbuf;
  1758. mtd_oob_mode_t mode = ops->mode;
  1759. to += ops->ooboffs;
  1760. DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
  1761. __func__, (unsigned int) to, (int) len);
  1762. /* Initialize retlen, in case of early exit */
  1763. ops->oobretlen = 0;
  1764. if (mode == MTD_OOB_AUTO)
  1765. oobsize = this->ecclayout->oobavail;
  1766. else
  1767. oobsize = mtd->oobsize;
  1768. column = to & (mtd->oobsize - 1);
  1769. if (unlikely(column >= oobsize)) {
  1770. printk(KERN_ERR "%s: Attempted to start write outside oob\n",
  1771. __func__);
  1772. return -EINVAL;
  1773. }
  1774. /* For compatibility with NAND: Do not allow write past end of page */
  1775. if (unlikely(column + len > oobsize)) {
  1776. printk(KERN_ERR "%s: Attempt to write past end of page\n",
  1777. __func__);
  1778. return -EINVAL;
  1779. }
  1780. /* Do not allow reads past end of device */
  1781. if (unlikely(to >= mtd->size ||
  1782. column + len > ((mtd->size >> this->page_shift) -
  1783. (to >> this->page_shift)) * oobsize)) {
  1784. printk(KERN_ERR "%s: Attempted to write past end of device\n",
  1785. __func__);
  1786. return -EINVAL;
  1787. }
  1788. oobbuf = this->oob_buf;
  1789. oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1790. /* Loop until all data write */
  1791. while (written < len) {
  1792. int thislen = min_t(int, oobsize, len - written);
  1793. cond_resched();
  1794. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1795. /* We send data to spare ram with oobsize
  1796. * to prevent byte access */
  1797. memset(oobbuf, 0xff, mtd->oobsize);
  1798. if (mode == MTD_OOB_AUTO)
  1799. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1800. else
  1801. memcpy(oobbuf + column, buf, thislen);
  1802. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1803. if (ONENAND_IS_4KB_PAGE(this)) {
  1804. /* Set main area of DataRAM to 0xff*/
  1805. memset(this->page_buf, 0xff, mtd->writesize);
  1806. this->write_bufferram(mtd, ONENAND_DATARAM,
  1807. this->page_buf, 0, mtd->writesize);
  1808. }
  1809. this->command(mtd, oobcmd, to, mtd->oobsize);
  1810. onenand_update_bufferram(mtd, to, 0);
  1811. if (ONENAND_IS_2PLANE(this)) {
  1812. ONENAND_SET_BUFFERRAM1(this);
  1813. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1814. }
  1815. ret = this->wait(mtd, FL_WRITING);
  1816. if (ret) {
  1817. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1818. break;
  1819. }
  1820. ret = onenand_verify_oob(mtd, oobbuf, to);
  1821. if (ret) {
  1822. printk(KERN_ERR "%s: verify failed %d\n",
  1823. __func__, ret);
  1824. break;
  1825. }
  1826. written += thislen;
  1827. if (written == len)
  1828. break;
  1829. to += mtd->writesize;
  1830. buf += thislen;
  1831. column = 0;
  1832. }
  1833. ops->oobretlen = written;
  1834. return ret;
  1835. }
  1836. /**
  1837. * onenand_write - [MTD Interface] write buffer to FLASH
  1838. * @param mtd MTD device structure
  1839. * @param to offset to write to
  1840. * @param len number of bytes to write
  1841. * @param retlen pointer to variable to store the number of written bytes
  1842. * @param buf the data to write
  1843. *
  1844. * Write with ECC
  1845. */
  1846. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1847. size_t *retlen, const u_char *buf)
  1848. {
  1849. struct mtd_oob_ops ops = {
  1850. .len = len,
  1851. .ooblen = 0,
  1852. .datbuf = (u_char *) buf,
  1853. .oobbuf = NULL,
  1854. };
  1855. int ret;
  1856. onenand_get_device(mtd, FL_WRITING);
  1857. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1858. onenand_release_device(mtd);
  1859. *retlen = ops.retlen;
  1860. return ret;
  1861. }
  1862. /**
  1863. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1864. * @param mtd: MTD device structure
  1865. * @param to: offset to write
  1866. * @param ops: oob operation description structure
  1867. */
  1868. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1869. struct mtd_oob_ops *ops)
  1870. {
  1871. int ret;
  1872. switch (ops->mode) {
  1873. case MTD_OOB_PLACE:
  1874. case MTD_OOB_AUTO:
  1875. break;
  1876. case MTD_OOB_RAW:
  1877. /* Not implemented yet */
  1878. default:
  1879. return -EINVAL;
  1880. }
  1881. onenand_get_device(mtd, FL_WRITING);
  1882. if (ops->datbuf)
  1883. ret = onenand_write_ops_nolock(mtd, to, ops);
  1884. else
  1885. ret = onenand_write_oob_nolock(mtd, to, ops);
  1886. onenand_release_device(mtd);
  1887. return ret;
  1888. }
  1889. /**
  1890. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1891. * @param mtd MTD device structure
  1892. * @param ofs offset from device start
  1893. * @param allowbbt 1, if its allowed to access the bbt area
  1894. *
  1895. * Check, if the block is bad. Either by reading the bad block table or
  1896. * calling of the scan function.
  1897. */
  1898. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1899. {
  1900. struct onenand_chip *this = mtd->priv;
  1901. struct bbm_info *bbm = this->bbm;
  1902. /* Return info from the table */
  1903. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1904. }
  1905. static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
  1906. struct erase_info *instr)
  1907. {
  1908. struct onenand_chip *this = mtd->priv;
  1909. loff_t addr = instr->addr;
  1910. int len = instr->len;
  1911. unsigned int block_size = (1 << this->erase_shift);
  1912. int ret = 0;
  1913. while (len) {
  1914. this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
  1915. ret = this->wait(mtd, FL_VERIFYING_ERASE);
  1916. if (ret) {
  1917. printk(KERN_ERR "%s: Failed verify, block %d\n",
  1918. __func__, onenand_block(this, addr));
  1919. instr->state = MTD_ERASE_FAILED;
  1920. instr->fail_addr = addr;
  1921. return -1;
  1922. }
  1923. len -= block_size;
  1924. addr += block_size;
  1925. }
  1926. return 0;
  1927. }
  1928. /**
  1929. * onenand_multiblock_erase - [Internal] erase block(s) using multiblock erase
  1930. * @param mtd MTD device structure
  1931. * @param instr erase instruction
  1932. * @param region erase region
  1933. *
  1934. * Erase one or more blocks up to 64 block at a time
  1935. */
  1936. static int onenand_multiblock_erase(struct mtd_info *mtd,
  1937. struct erase_info *instr,
  1938. unsigned int block_size)
  1939. {
  1940. struct onenand_chip *this = mtd->priv;
  1941. loff_t addr = instr->addr;
  1942. int len = instr->len;
  1943. int eb_count = 0;
  1944. int ret = 0;
  1945. int bdry_block = 0;
  1946. instr->state = MTD_ERASING;
  1947. if (ONENAND_IS_DDP(this)) {
  1948. loff_t bdry_addr = this->chipsize >> 1;
  1949. if (addr < bdry_addr && (addr + len) > bdry_addr)
  1950. bdry_block = bdry_addr >> this->erase_shift;
  1951. }
  1952. /* Pre-check bbs */
  1953. while (len) {
  1954. /* Check if we have a bad block, we do not erase bad blocks */
  1955. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  1956. printk(KERN_WARNING "%s: attempt to erase a bad block "
  1957. "at addr 0x%012llx\n",
  1958. __func__, (unsigned long long) addr);
  1959. instr->state = MTD_ERASE_FAILED;
  1960. return -EIO;
  1961. }
  1962. len -= block_size;
  1963. addr += block_size;
  1964. }
  1965. len = instr->len;
  1966. addr = instr->addr;
  1967. /* loop over 64 eb batches */
  1968. while (len) {
  1969. struct erase_info verify_instr = *instr;
  1970. int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
  1971. verify_instr.addr = addr;
  1972. verify_instr.len = 0;
  1973. /* do not cross chip boundary */
  1974. if (bdry_block) {
  1975. int this_block = (addr >> this->erase_shift);
  1976. if (this_block < bdry_block) {
  1977. max_eb_count = min(max_eb_count,
  1978. (bdry_block - this_block));
  1979. }
  1980. }
  1981. eb_count = 0;
  1982. while (len > block_size && eb_count < (max_eb_count - 1)) {
  1983. this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
  1984. addr, block_size);
  1985. onenand_invalidate_bufferram(mtd, addr, block_size);
  1986. ret = this->wait(mtd, FL_PREPARING_ERASE);
  1987. if (ret) {
  1988. printk(KERN_ERR "%s: Failed multiblock erase, "
  1989. "block %d\n", __func__,
  1990. onenand_block(this, addr));
  1991. instr->state = MTD_ERASE_FAILED;
  1992. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1993. return -EIO;
  1994. }
  1995. len -= block_size;
  1996. addr += block_size;
  1997. eb_count++;
  1998. }
  1999. /* last block of 64-eb series */
  2000. cond_resched();
  2001. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  2002. onenand_invalidate_bufferram(mtd, addr, block_size);
  2003. ret = this->wait(mtd, FL_ERASING);
  2004. /* Check if it is write protected */
  2005. if (ret) {
  2006. printk(KERN_ERR "%s: Failed erase, block %d\n",
  2007. __func__, onenand_block(this, addr));
  2008. instr->state = MTD_ERASE_FAILED;
  2009. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  2010. return -EIO;
  2011. }
  2012. len -= block_size;
  2013. addr += block_size;
  2014. eb_count++;
  2015. /* verify */
  2016. verify_instr.len = eb_count * block_size;
  2017. if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
  2018. instr->state = verify_instr.state;
  2019. instr->fail_addr = verify_instr.fail_addr;
  2020. return -EIO;
  2021. }
  2022. }
  2023. return 0;
  2024. }
  2025. /**
  2026. * onenand_block_by_block_erase - [Internal] erase block(s) using regular erase
  2027. * @param mtd MTD device structure
  2028. * @param instr erase instruction
  2029. * @param region erase region
  2030. * @param block_size erase block size
  2031. *
  2032. * Erase one or more blocks one block at a time
  2033. */
  2034. static int onenand_block_by_block_erase(struct mtd_info *mtd,
  2035. struct erase_info *instr,
  2036. struct mtd_erase_region_info *region,
  2037. unsigned int block_size)
  2038. {
  2039. struct onenand_chip *this = mtd->priv;
  2040. loff_t addr = instr->addr;
  2041. int len = instr->len;
  2042. loff_t region_end = 0;
  2043. int ret = 0;
  2044. if (region) {
  2045. /* region is set for Flex-OneNAND */
  2046. region_end = region->offset + region->erasesize * region->numblocks;
  2047. }
  2048. instr->state = MTD_ERASING;
  2049. /* Loop through the blocks */
  2050. while (len) {
  2051. cond_resched();
  2052. /* Check if we have a bad block, we do not erase bad blocks */
  2053. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  2054. printk(KERN_WARNING "%s: attempt to erase a bad block "
  2055. "at addr 0x%012llx\n",
  2056. __func__, (unsigned long long) addr);
  2057. instr->state = MTD_ERASE_FAILED;
  2058. return -EIO;
  2059. }
  2060. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  2061. onenand_invalidate_bufferram(mtd, addr, block_size);
  2062. ret = this->wait(mtd, FL_ERASING);
  2063. /* Check, if it is write protected */
  2064. if (ret) {
  2065. printk(KERN_ERR "%s: Failed erase, block %d\n",
  2066. __func__, onenand_block(this, addr));
  2067. instr->state = MTD_ERASE_FAILED;
  2068. instr->fail_addr = addr;
  2069. return -EIO;
  2070. }
  2071. len -= block_size;
  2072. addr += block_size;
  2073. if (region && addr == region_end) {
  2074. if (!len)
  2075. break;
  2076. region++;
  2077. block_size = region->erasesize;
  2078. region_end = region->offset + region->erasesize * region->numblocks;
  2079. if (len & (block_size - 1)) {
  2080. /* FIXME: This should be handled at MTD partitioning level. */
  2081. printk(KERN_ERR "%s: Unaligned address\n",
  2082. __func__);
  2083. return -EIO;
  2084. }
  2085. }
  2086. }
  2087. return 0;
  2088. }
  2089. /**
  2090. * onenand_erase - [MTD Interface] erase block(s)
  2091. * @param mtd MTD device structure
  2092. * @param instr erase instruction
  2093. *
  2094. * Erase one or more blocks
  2095. */
  2096. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2097. {
  2098. struct onenand_chip *this = mtd->priv;
  2099. unsigned int block_size;
  2100. loff_t addr = instr->addr;
  2101. loff_t len = instr->len;
  2102. int ret = 0;
  2103. struct mtd_erase_region_info *region = NULL;
  2104. loff_t region_offset = 0;
  2105. DEBUG(MTD_DEBUG_LEVEL3, "%s: start=0x%012llx, len=%llu\n", __func__,
  2106. (unsigned long long) instr->addr, (unsigned long long) instr->len);
  2107. /* Do not allow erase past end of device */
  2108. if (unlikely((len + addr) > mtd->size)) {
  2109. printk(KERN_ERR "%s: Erase past end of device\n", __func__);
  2110. return -EINVAL;
  2111. }
  2112. if (FLEXONENAND(this)) {
  2113. /* Find the eraseregion of this address */
  2114. int i = flexonenand_region(mtd, addr);
  2115. region = &mtd->eraseregions[i];
  2116. block_size = region->erasesize;
  2117. /* Start address within region must align on block boundary.
  2118. * Erase region's start offset is always block start address.
  2119. */
  2120. region_offset = region->offset;
  2121. } else
  2122. block_size = 1 << this->erase_shift;
  2123. /* Start address must align on block boundary */
  2124. if (unlikely((addr - region_offset) & (block_size - 1))) {
  2125. printk(KERN_ERR "%s: Unaligned address\n", __func__);
  2126. return -EINVAL;
  2127. }
  2128. /* Length must align on block boundary */
  2129. if (unlikely(len & (block_size - 1))) {
  2130. printk(KERN_ERR "%s: Length not block aligned\n", __func__);
  2131. return -EINVAL;
  2132. }
  2133. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  2134. /* Grab the lock and see if the device is available */
  2135. onenand_get_device(mtd, FL_ERASING);
  2136. if (ONENAND_IS_4KB_PAGE(this) || region ||
  2137. instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
  2138. /* region is set for Flex-OneNAND (no mb erase) */
  2139. ret = onenand_block_by_block_erase(mtd, instr,
  2140. region, block_size);
  2141. } else {
  2142. ret = onenand_multiblock_erase(mtd, instr, block_size);
  2143. }
  2144. /* Deselect and wake up anyone waiting on the device */
  2145. onenand_release_device(mtd);
  2146. /* Do call back function */
  2147. if (!ret) {
  2148. instr->state = MTD_ERASE_DONE;
  2149. mtd_erase_callback(instr);
  2150. }
  2151. return ret;
  2152. }
  2153. /**
  2154. * onenand_sync - [MTD Interface] sync
  2155. * @param mtd MTD device structure
  2156. *
  2157. * Sync is actually a wait for chip ready function
  2158. */
  2159. static void onenand_sync(struct mtd_info *mtd)
  2160. {
  2161. DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
  2162. /* Grab the lock and see if the device is available */
  2163. onenand_get_device(mtd, FL_SYNCING);
  2164. /* Release it and go back */
  2165. onenand_release_device(mtd);
  2166. }
  2167. /**
  2168. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  2169. * @param mtd MTD device structure
  2170. * @param ofs offset relative to mtd start
  2171. *
  2172. * Check whether the block is bad
  2173. */
  2174. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  2175. {
  2176. int ret;
  2177. /* Check for invalid offset */
  2178. if (ofs > mtd->size)
  2179. return -EINVAL;
  2180. onenand_get_device(mtd, FL_READING);
  2181. ret = onenand_block_isbad_nolock(mtd, ofs, 0);
  2182. onenand_release_device(mtd);
  2183. return ret;
  2184. }
  2185. /**
  2186. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  2187. * @param mtd MTD device structure
  2188. * @param ofs offset from device start
  2189. *
  2190. * This is the default implementation, which can be overridden by
  2191. * a hardware specific driver.
  2192. */
  2193. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2194. {
  2195. struct onenand_chip *this = mtd->priv;
  2196. struct bbm_info *bbm = this->bbm;
  2197. u_char buf[2] = {0, 0};
  2198. struct mtd_oob_ops ops = {
  2199. .mode = MTD_OOB_PLACE,
  2200. .ooblen = 2,
  2201. .oobbuf = buf,
  2202. .ooboffs = 0,
  2203. };
  2204. int block;
  2205. /* Get block number */
  2206. block = onenand_block(this, ofs);
  2207. if (bbm->bbt)
  2208. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  2209. /* We write two bytes, so we don't have to mess with 16-bit access */
  2210. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  2211. /* FIXME : What to do when marking SLC block in partition
  2212. * with MLC erasesize? For now, it is not advisable to
  2213. * create partitions containing both SLC and MLC regions.
  2214. */
  2215. return onenand_write_oob_nolock(mtd, ofs, &ops);
  2216. }
  2217. /**
  2218. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  2219. * @param mtd MTD device structure
  2220. * @param ofs offset relative to mtd start
  2221. *
  2222. * Mark the block as bad
  2223. */
  2224. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2225. {
  2226. struct onenand_chip *this = mtd->priv;
  2227. int ret;
  2228. ret = onenand_block_isbad(mtd, ofs);
  2229. if (ret) {
  2230. /* If it was bad already, return success and do nothing */
  2231. if (ret > 0)
  2232. return 0;
  2233. return ret;
  2234. }
  2235. onenand_get_device(mtd, FL_WRITING);
  2236. ret = this->block_markbad(mtd, ofs);
  2237. onenand_release_device(mtd);
  2238. return ret;
  2239. }
  2240. /**
  2241. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  2242. * @param mtd MTD device structure
  2243. * @param ofs offset relative to mtd start
  2244. * @param len number of bytes to lock or unlock
  2245. * @param cmd lock or unlock command
  2246. *
  2247. * Lock or unlock one or more blocks
  2248. */
  2249. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  2250. {
  2251. struct onenand_chip *this = mtd->priv;
  2252. int start, end, block, value, status;
  2253. int wp_status_mask;
  2254. start = onenand_block(this, ofs);
  2255. end = onenand_block(this, ofs + len) - 1;
  2256. if (cmd == ONENAND_CMD_LOCK)
  2257. wp_status_mask = ONENAND_WP_LS;
  2258. else
  2259. wp_status_mask = ONENAND_WP_US;
  2260. /* Continuous lock scheme */
  2261. if (this->options & ONENAND_HAS_CONT_LOCK) {
  2262. /* Set start block address */
  2263. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2264. /* Set end block address */
  2265. this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  2266. /* Write lock command */
  2267. this->command(mtd, cmd, 0, 0);
  2268. /* There's no return value */
  2269. this->wait(mtd, FL_LOCKING);
  2270. /* Sanity check */
  2271. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2272. & ONENAND_CTRL_ONGO)
  2273. continue;
  2274. /* Check lock status */
  2275. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2276. if (!(status & wp_status_mask))
  2277. printk(KERN_ERR "%s: wp status = 0x%x\n",
  2278. __func__, status);
  2279. return 0;
  2280. }
  2281. /* Block lock scheme */
  2282. for (block = start; block < end + 1; block++) {
  2283. /* Set block address */
  2284. value = onenand_block_address(this, block);
  2285. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2286. /* Select DataRAM for DDP */
  2287. value = onenand_bufferram_address(this, block);
  2288. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2289. /* Set start block address */
  2290. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2291. /* Write lock command */
  2292. this->command(mtd, cmd, 0, 0);
  2293. /* There's no return value */
  2294. this->wait(mtd, FL_LOCKING);
  2295. /* Sanity check */
  2296. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2297. & ONENAND_CTRL_ONGO)
  2298. continue;
  2299. /* Check lock status */
  2300. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2301. if (!(status & wp_status_mask))
  2302. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2303. __func__, block, status);
  2304. }
  2305. return 0;
  2306. }
  2307. /**
  2308. * onenand_lock - [MTD Interface] Lock block(s)
  2309. * @param mtd MTD device structure
  2310. * @param ofs offset relative to mtd start
  2311. * @param len number of bytes to unlock
  2312. *
  2313. * Lock one or more blocks
  2314. */
  2315. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2316. {
  2317. int ret;
  2318. onenand_get_device(mtd, FL_LOCKING);
  2319. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  2320. onenand_release_device(mtd);
  2321. return ret;
  2322. }
  2323. /**
  2324. * onenand_unlock - [MTD Interface] Unlock block(s)
  2325. * @param mtd MTD device structure
  2326. * @param ofs offset relative to mtd start
  2327. * @param len number of bytes to unlock
  2328. *
  2329. * Unlock one or more blocks
  2330. */
  2331. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2332. {
  2333. int ret;
  2334. onenand_get_device(mtd, FL_LOCKING);
  2335. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2336. onenand_release_device(mtd);
  2337. return ret;
  2338. }
  2339. /**
  2340. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  2341. * @param this onenand chip data structure
  2342. *
  2343. * Check lock status
  2344. */
  2345. static int onenand_check_lock_status(struct onenand_chip *this)
  2346. {
  2347. unsigned int value, block, status;
  2348. unsigned int end;
  2349. end = this->chipsize >> this->erase_shift;
  2350. for (block = 0; block < end; block++) {
  2351. /* Set block address */
  2352. value = onenand_block_address(this, block);
  2353. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2354. /* Select DataRAM for DDP */
  2355. value = onenand_bufferram_address(this, block);
  2356. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2357. /* Set start block address */
  2358. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2359. /* Check lock status */
  2360. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2361. if (!(status & ONENAND_WP_US)) {
  2362. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2363. __func__, block, status);
  2364. return 0;
  2365. }
  2366. }
  2367. return 1;
  2368. }
  2369. /**
  2370. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  2371. * @param mtd MTD device structure
  2372. *
  2373. * Unlock all blocks
  2374. */
  2375. static void onenand_unlock_all(struct mtd_info *mtd)
  2376. {
  2377. struct onenand_chip *this = mtd->priv;
  2378. loff_t ofs = 0;
  2379. loff_t len = mtd->size;
  2380. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  2381. /* Set start block address */
  2382. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2383. /* Write unlock command */
  2384. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  2385. /* There's no return value */
  2386. this->wait(mtd, FL_LOCKING);
  2387. /* Sanity check */
  2388. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2389. & ONENAND_CTRL_ONGO)
  2390. continue;
  2391. /* Don't check lock status */
  2392. if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
  2393. return;
  2394. /* Check lock status */
  2395. if (onenand_check_lock_status(this))
  2396. return;
  2397. /* Workaround for all block unlock in DDP */
  2398. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  2399. /* All blocks on another chip */
  2400. ofs = this->chipsize >> 1;
  2401. len = this->chipsize >> 1;
  2402. }
  2403. }
  2404. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2405. }
  2406. #ifdef CONFIG_MTD_ONENAND_OTP
  2407. /**
  2408. * onenand_otp_command - Send OTP specific command to OneNAND device
  2409. * @param mtd MTD device structure
  2410. * @param cmd the command to be sent
  2411. * @param addr offset to read from or write to
  2412. * @param len number of bytes to read or write
  2413. */
  2414. static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
  2415. size_t len)
  2416. {
  2417. struct onenand_chip *this = mtd->priv;
  2418. int value, block, page;
  2419. /* Address translation */
  2420. switch (cmd) {
  2421. case ONENAND_CMD_OTP_ACCESS:
  2422. block = (int) (addr >> this->erase_shift);
  2423. page = -1;
  2424. break;
  2425. default:
  2426. block = (int) (addr >> this->erase_shift);
  2427. page = (int) (addr >> this->page_shift);
  2428. if (ONENAND_IS_2PLANE(this)) {
  2429. /* Make the even block number */
  2430. block &= ~1;
  2431. /* Is it the odd plane? */
  2432. if (addr & this->writesize)
  2433. block++;
  2434. page >>= 1;
  2435. }
  2436. page &= this->page_mask;
  2437. break;
  2438. }
  2439. if (block != -1) {
  2440. /* Write 'DFS, FBA' of Flash */
  2441. value = onenand_block_address(this, block);
  2442. this->write_word(value, this->base +
  2443. ONENAND_REG_START_ADDRESS1);
  2444. }
  2445. if (page != -1) {
  2446. /* Now we use page size operation */
  2447. int sectors = 4, count = 4;
  2448. int dataram;
  2449. switch (cmd) {
  2450. default:
  2451. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  2452. cmd = ONENAND_CMD_2X_PROG;
  2453. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  2454. break;
  2455. }
  2456. /* Write 'FPA, FSA' of Flash */
  2457. value = onenand_page_address(page, sectors);
  2458. this->write_word(value, this->base +
  2459. ONENAND_REG_START_ADDRESS8);
  2460. /* Write 'BSA, BSC' of DataRAM */
  2461. value = onenand_buffer_address(dataram, sectors, count);
  2462. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  2463. }
  2464. /* Interrupt clear */
  2465. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  2466. /* Write command */
  2467. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  2468. return 0;
  2469. }
  2470. /**
  2471. * onenand_otp_write_oob_nolock - [Internal] OneNAND write out-of-band, specific to OTP
  2472. * @param mtd MTD device structure
  2473. * @param to offset to write to
  2474. * @param len number of bytes to write
  2475. * @param retlen pointer to variable to store the number of written bytes
  2476. * @param buf the data to write
  2477. *
  2478. * OneNAND write out-of-band only for OTP
  2479. */
  2480. static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  2481. struct mtd_oob_ops *ops)
  2482. {
  2483. struct onenand_chip *this = mtd->priv;
  2484. int column, ret = 0, oobsize;
  2485. int written = 0;
  2486. u_char *oobbuf;
  2487. size_t len = ops->ooblen;
  2488. const u_char *buf = ops->oobbuf;
  2489. int block, value, status;
  2490. to += ops->ooboffs;
  2491. /* Initialize retlen, in case of early exit */
  2492. ops->oobretlen = 0;
  2493. oobsize = mtd->oobsize;
  2494. column = to & (mtd->oobsize - 1);
  2495. oobbuf = this->oob_buf;
  2496. /* Loop until all data write */
  2497. while (written < len) {
  2498. int thislen = min_t(int, oobsize, len - written);
  2499. cond_resched();
  2500. block = (int) (to >> this->erase_shift);
  2501. /*
  2502. * Write 'DFS, FBA' of Flash
  2503. * Add: F100h DQ=DFS, FBA
  2504. */
  2505. value = onenand_block_address(this, block);
  2506. this->write_word(value, this->base +
  2507. ONENAND_REG_START_ADDRESS1);
  2508. /*
  2509. * Select DataRAM for DDP
  2510. * Add: F101h DQ=DBS
  2511. */
  2512. value = onenand_bufferram_address(this, block);
  2513. this->write_word(value, this->base +
  2514. ONENAND_REG_START_ADDRESS2);
  2515. ONENAND_SET_NEXT_BUFFERRAM(this);
  2516. /*
  2517. * Enter OTP access mode
  2518. */
  2519. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2520. this->wait(mtd, FL_OTPING);
  2521. /* We send data to spare ram with oobsize
  2522. * to prevent byte access */
  2523. memcpy(oobbuf + column, buf, thislen);
  2524. /*
  2525. * Write Data into DataRAM
  2526. * Add: 8th Word
  2527. * in sector0/spare/page0
  2528. * DQ=XXFCh
  2529. */
  2530. this->write_bufferram(mtd, ONENAND_SPARERAM,
  2531. oobbuf, 0, mtd->oobsize);
  2532. onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  2533. onenand_update_bufferram(mtd, to, 0);
  2534. if (ONENAND_IS_2PLANE(this)) {
  2535. ONENAND_SET_BUFFERRAM1(this);
  2536. onenand_update_bufferram(mtd, to + this->writesize, 0);
  2537. }
  2538. ret = this->wait(mtd, FL_WRITING);
  2539. if (ret) {
  2540. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  2541. break;
  2542. }
  2543. /* Exit OTP access mode */
  2544. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2545. this->wait(mtd, FL_RESETING);
  2546. status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  2547. status &= 0x60;
  2548. if (status == 0x60) {
  2549. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2550. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2551. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2552. } else if (status == 0x20) {
  2553. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2554. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2555. printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
  2556. } else if (status == 0x40) {
  2557. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2558. printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
  2559. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2560. } else {
  2561. printk(KERN_DEBUG "Reboot to check\n");
  2562. }
  2563. written += thislen;
  2564. if (written == len)
  2565. break;
  2566. to += mtd->writesize;
  2567. buf += thislen;
  2568. column = 0;
  2569. }
  2570. ops->oobretlen = written;
  2571. return ret;
  2572. }
  2573. /* Internal OTP operation */
  2574. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  2575. size_t *retlen, u_char *buf);
  2576. /**
  2577. * do_otp_read - [DEFAULT] Read OTP block area
  2578. * @param mtd MTD device structure
  2579. * @param from The offset to read
  2580. * @param len number of bytes to read
  2581. * @param retlen pointer to variable to store the number of readbytes
  2582. * @param buf the databuffer to put/get data
  2583. *
  2584. * Read OTP block area.
  2585. */
  2586. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  2587. size_t *retlen, u_char *buf)
  2588. {
  2589. struct onenand_chip *this = mtd->priv;
  2590. struct mtd_oob_ops ops = {
  2591. .len = len,
  2592. .ooblen = 0,
  2593. .datbuf = buf,
  2594. .oobbuf = NULL,
  2595. };
  2596. int ret;
  2597. /* Enter OTP access mode */
  2598. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2599. this->wait(mtd, FL_OTPING);
  2600. ret = ONENAND_IS_4KB_PAGE(this) ?
  2601. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  2602. onenand_read_ops_nolock(mtd, from, &ops);
  2603. /* Exit OTP access mode */
  2604. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2605. this->wait(mtd, FL_RESETING);
  2606. return ret;
  2607. }
  2608. /**
  2609. * do_otp_write - [DEFAULT] Write OTP block area
  2610. * @param mtd MTD device structure
  2611. * @param to The offset to write
  2612. * @param len number of bytes to write
  2613. * @param retlen pointer to variable to store the number of write bytes
  2614. * @param buf the databuffer to put/get data
  2615. *
  2616. * Write OTP block area.
  2617. */
  2618. static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  2619. size_t *retlen, u_char *buf)
  2620. {
  2621. struct onenand_chip *this = mtd->priv;
  2622. unsigned char *pbuf = buf;
  2623. int ret;
  2624. struct mtd_oob_ops ops;
  2625. /* Force buffer page aligned */
  2626. if (len < mtd->writesize) {
  2627. memcpy(this->page_buf, buf, len);
  2628. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  2629. pbuf = this->page_buf;
  2630. len = mtd->writesize;
  2631. }
  2632. /* Enter OTP access mode */
  2633. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2634. this->wait(mtd, FL_OTPING);
  2635. ops.len = len;
  2636. ops.ooblen = 0;
  2637. ops.datbuf = pbuf;
  2638. ops.oobbuf = NULL;
  2639. ret = onenand_write_ops_nolock(mtd, to, &ops);
  2640. *retlen = ops.retlen;
  2641. /* Exit OTP access mode */
  2642. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2643. this->wait(mtd, FL_RESETING);
  2644. return ret;
  2645. }
  2646. /**
  2647. * do_otp_lock - [DEFAULT] Lock OTP block area
  2648. * @param mtd MTD device structure
  2649. * @param from The offset to lock
  2650. * @param len number of bytes to lock
  2651. * @param retlen pointer to variable to store the number of lock bytes
  2652. * @param buf the databuffer to put/get data
  2653. *
  2654. * Lock OTP block area.
  2655. */
  2656. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  2657. size_t *retlen, u_char *buf)
  2658. {
  2659. struct onenand_chip *this = mtd->priv;
  2660. struct mtd_oob_ops ops;
  2661. int ret;
  2662. if (FLEXONENAND(this)) {
  2663. /* Enter OTP access mode */
  2664. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2665. this->wait(mtd, FL_OTPING);
  2666. /*
  2667. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2668. * main area of page 49.
  2669. */
  2670. ops.len = mtd->writesize;
  2671. ops.ooblen = 0;
  2672. ops.datbuf = buf;
  2673. ops.oobbuf = NULL;
  2674. ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
  2675. *retlen = ops.retlen;
  2676. /* Exit OTP access mode */
  2677. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2678. this->wait(mtd, FL_RESETING);
  2679. } else {
  2680. ops.mode = MTD_OOB_PLACE;
  2681. ops.ooblen = len;
  2682. ops.oobbuf = buf;
  2683. ops.ooboffs = 0;
  2684. ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
  2685. *retlen = ops.oobretlen;
  2686. }
  2687. return ret;
  2688. }
  2689. /**
  2690. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  2691. * @param mtd MTD device structure
  2692. * @param from The offset to read/write
  2693. * @param len number of bytes to read/write
  2694. * @param retlen pointer to variable to store the number of read bytes
  2695. * @param buf the databuffer to put/get data
  2696. * @param action do given action
  2697. * @param mode specify user and factory
  2698. *
  2699. * Handle OTP operation.
  2700. */
  2701. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  2702. size_t *retlen, u_char *buf,
  2703. otp_op_t action, int mode)
  2704. {
  2705. struct onenand_chip *this = mtd->priv;
  2706. int otp_pages;
  2707. int density;
  2708. int ret = 0;
  2709. *retlen = 0;
  2710. density = onenand_get_density(this->device_id);
  2711. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  2712. otp_pages = 20;
  2713. else
  2714. otp_pages = 50;
  2715. if (mode == MTD_OTP_FACTORY) {
  2716. from += mtd->writesize * otp_pages;
  2717. otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
  2718. }
  2719. /* Check User/Factory boundary */
  2720. if (mode == MTD_OTP_USER) {
  2721. if (mtd->writesize * otp_pages < from + len)
  2722. return 0;
  2723. } else {
  2724. if (mtd->writesize * otp_pages < len)
  2725. return 0;
  2726. }
  2727. onenand_get_device(mtd, FL_OTPING);
  2728. while (len > 0 && otp_pages > 0) {
  2729. if (!action) { /* OTP Info functions */
  2730. struct otp_info *otpinfo;
  2731. len -= sizeof(struct otp_info);
  2732. if (len <= 0) {
  2733. ret = -ENOSPC;
  2734. break;
  2735. }
  2736. otpinfo = (struct otp_info *) buf;
  2737. otpinfo->start = from;
  2738. otpinfo->length = mtd->writesize;
  2739. otpinfo->locked = 0;
  2740. from += mtd->writesize;
  2741. buf += sizeof(struct otp_info);
  2742. *retlen += sizeof(struct otp_info);
  2743. } else {
  2744. size_t tmp_retlen;
  2745. ret = action(mtd, from, len, &tmp_retlen, buf);
  2746. buf += tmp_retlen;
  2747. len -= tmp_retlen;
  2748. *retlen += tmp_retlen;
  2749. if (ret)
  2750. break;
  2751. }
  2752. otp_pages--;
  2753. }
  2754. onenand_release_device(mtd);
  2755. return ret;
  2756. }
  2757. /**
  2758. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  2759. * @param mtd MTD device structure
  2760. * @param buf the databuffer to put/get data
  2761. * @param len number of bytes to read
  2762. *
  2763. * Read factory OTP info.
  2764. */
  2765. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  2766. struct otp_info *buf, size_t len)
  2767. {
  2768. size_t retlen;
  2769. int ret;
  2770. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  2771. return ret ? : retlen;
  2772. }
  2773. /**
  2774. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  2775. * @param mtd MTD device structure
  2776. * @param from The offset to read
  2777. * @param len number of bytes to read
  2778. * @param retlen pointer to variable to store the number of read bytes
  2779. * @param buf the databuffer to put/get data
  2780. *
  2781. * Read factory OTP area.
  2782. */
  2783. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  2784. size_t len, size_t *retlen, u_char *buf)
  2785. {
  2786. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  2787. }
  2788. /**
  2789. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  2790. * @param mtd MTD device structure
  2791. * @param buf the databuffer to put/get data
  2792. * @param len number of bytes to read
  2793. *
  2794. * Read user OTP info.
  2795. */
  2796. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  2797. struct otp_info *buf, size_t len)
  2798. {
  2799. size_t retlen;
  2800. int ret;
  2801. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  2802. return ret ? : retlen;
  2803. }
  2804. /**
  2805. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  2806. * @param mtd MTD device structure
  2807. * @param from The offset to read
  2808. * @param len number of bytes to read
  2809. * @param retlen pointer to variable to store the number of read bytes
  2810. * @param buf the databuffer to put/get data
  2811. *
  2812. * Read user OTP area.
  2813. */
  2814. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2815. size_t len, size_t *retlen, u_char *buf)
  2816. {
  2817. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  2818. }
  2819. /**
  2820. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  2821. * @param mtd MTD device structure
  2822. * @param from The offset to write
  2823. * @param len number of bytes to write
  2824. * @param retlen pointer to variable to store the number of write bytes
  2825. * @param buf the databuffer to put/get data
  2826. *
  2827. * Write user OTP area.
  2828. */
  2829. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2830. size_t len, size_t *retlen, u_char *buf)
  2831. {
  2832. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  2833. }
  2834. /**
  2835. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  2836. * @param mtd MTD device structure
  2837. * @param from The offset to lock
  2838. * @param len number of bytes to unlock
  2839. *
  2840. * Write lock mark on spare area in page 0 in OTP block
  2841. */
  2842. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2843. size_t len)
  2844. {
  2845. struct onenand_chip *this = mtd->priv;
  2846. u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
  2847. size_t retlen;
  2848. int ret;
  2849. unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
  2850. memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
  2851. : mtd->oobsize);
  2852. /*
  2853. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  2854. * We write 16 bytes spare area instead of 2 bytes.
  2855. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2856. * main area of page 49.
  2857. */
  2858. from = 0;
  2859. len = FLEXONENAND(this) ? mtd->writesize : 16;
  2860. /*
  2861. * Note: OTP lock operation
  2862. * OTP block : 0xXXFC XX 1111 1100
  2863. * 1st block : 0xXXF3 (If chip support) XX 1111 0011
  2864. * Both : 0xXXF0 (If chip support) XX 1111 0000
  2865. */
  2866. if (FLEXONENAND(this))
  2867. otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
  2868. /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
  2869. if (otp == 1)
  2870. buf[otp_lock_offset] = 0xFC;
  2871. else if (otp == 2)
  2872. buf[otp_lock_offset] = 0xF3;
  2873. else if (otp == 3)
  2874. buf[otp_lock_offset] = 0xF0;
  2875. else if (otp != 0)
  2876. printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
  2877. ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
  2878. return ret ? : retlen;
  2879. }
  2880. #endif /* CONFIG_MTD_ONENAND_OTP */
  2881. /**
  2882. * onenand_check_features - Check and set OneNAND features
  2883. * @param mtd MTD data structure
  2884. *
  2885. * Check and set OneNAND features
  2886. * - lock scheme
  2887. * - two plane
  2888. */
  2889. static void onenand_check_features(struct mtd_info *mtd)
  2890. {
  2891. struct onenand_chip *this = mtd->priv;
  2892. unsigned int density, process, numbufs;
  2893. /* Lock scheme depends on density and process */
  2894. density = onenand_get_density(this->device_id);
  2895. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  2896. numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
  2897. /* Lock scheme */
  2898. switch (density) {
  2899. case ONENAND_DEVICE_DENSITY_4Gb:
  2900. if (ONENAND_IS_DDP(this))
  2901. this->options |= ONENAND_HAS_2PLANE;
  2902. else if (numbufs == 1) {
  2903. this->options |= ONENAND_HAS_4KB_PAGE;
  2904. this->options |= ONENAND_HAS_CACHE_PROGRAM;
  2905. }
  2906. case ONENAND_DEVICE_DENSITY_2Gb:
  2907. /* 2Gb DDP does not have 2 plane */
  2908. if (!ONENAND_IS_DDP(this))
  2909. this->options |= ONENAND_HAS_2PLANE;
  2910. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2911. case ONENAND_DEVICE_DENSITY_1Gb:
  2912. /* A-Die has all block unlock */
  2913. if (process)
  2914. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2915. break;
  2916. default:
  2917. /* Some OneNAND has continuous lock scheme */
  2918. if (!process)
  2919. this->options |= ONENAND_HAS_CONT_LOCK;
  2920. break;
  2921. }
  2922. /* The MLC has 4KiB pagesize. */
  2923. if (ONENAND_IS_MLC(this))
  2924. this->options |= ONENAND_HAS_4KB_PAGE;
  2925. if (ONENAND_IS_4KB_PAGE(this))
  2926. this->options &= ~ONENAND_HAS_2PLANE;
  2927. if (FLEXONENAND(this)) {
  2928. this->options &= ~ONENAND_HAS_CONT_LOCK;
  2929. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2930. }
  2931. if (this->options & ONENAND_HAS_CONT_LOCK)
  2932. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  2933. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  2934. printk(KERN_DEBUG "Chip support all block unlock\n");
  2935. if (this->options & ONENAND_HAS_2PLANE)
  2936. printk(KERN_DEBUG "Chip has 2 plane\n");
  2937. if (this->options & ONENAND_HAS_4KB_PAGE)
  2938. printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
  2939. if (this->options & ONENAND_HAS_CACHE_PROGRAM)
  2940. printk(KERN_DEBUG "Chip has cache program feature\n");
  2941. }
  2942. /**
  2943. * onenand_print_device_info - Print device & version ID
  2944. * @param device device ID
  2945. * @param version version ID
  2946. *
  2947. * Print device & version ID
  2948. */
  2949. static void onenand_print_device_info(int device, int version)
  2950. {
  2951. int vcc, demuxed, ddp, density, flexonenand;
  2952. vcc = device & ONENAND_DEVICE_VCC_MASK;
  2953. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  2954. ddp = device & ONENAND_DEVICE_IS_DDP;
  2955. density = onenand_get_density(device);
  2956. flexonenand = device & DEVICE_IS_FLEXONENAND;
  2957. printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  2958. demuxed ? "" : "Muxed ",
  2959. flexonenand ? "Flex-" : "",
  2960. ddp ? "(DDP)" : "",
  2961. (16 << density),
  2962. vcc ? "2.65/3.3" : "1.8",
  2963. device);
  2964. printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
  2965. }
  2966. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  2967. {ONENAND_MFR_SAMSUNG, "Samsung"},
  2968. {ONENAND_MFR_NUMONYX, "Numonyx"},
  2969. };
  2970. /**
  2971. * onenand_check_maf - Check manufacturer ID
  2972. * @param manuf manufacturer ID
  2973. *
  2974. * Check manufacturer ID
  2975. */
  2976. static int onenand_check_maf(int manuf)
  2977. {
  2978. int size = ARRAY_SIZE(onenand_manuf_ids);
  2979. char *name;
  2980. int i;
  2981. for (i = 0; i < size; i++)
  2982. if (manuf == onenand_manuf_ids[i].id)
  2983. break;
  2984. if (i < size)
  2985. name = onenand_manuf_ids[i].name;
  2986. else
  2987. name = "Unknown";
  2988. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2989. return (i == size);
  2990. }
  2991. /**
  2992. * flexonenand_get_boundary - Reads the SLC boundary
  2993. * @param onenand_info - onenand info structure
  2994. **/
  2995. static int flexonenand_get_boundary(struct mtd_info *mtd)
  2996. {
  2997. struct onenand_chip *this = mtd->priv;
  2998. unsigned die, bdry;
  2999. int ret, syscfg, locked;
  3000. /* Disable ECC */
  3001. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  3002. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  3003. for (die = 0; die < this->dies; die++) {
  3004. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  3005. this->wait(mtd, FL_SYNCING);
  3006. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3007. ret = this->wait(mtd, FL_READING);
  3008. bdry = this->read_word(this->base + ONENAND_DATARAM);
  3009. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  3010. locked = 0;
  3011. else
  3012. locked = 1;
  3013. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  3014. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  3015. ret = this->wait(mtd, FL_RESETING);
  3016. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  3017. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  3018. }
  3019. /* Enable ECC */
  3020. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3021. return 0;
  3022. }
  3023. /**
  3024. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  3025. * boundary[], diesize[], mtd->size, mtd->erasesize
  3026. * @param mtd - MTD device structure
  3027. */
  3028. static void flexonenand_get_size(struct mtd_info *mtd)
  3029. {
  3030. struct onenand_chip *this = mtd->priv;
  3031. int die, i, eraseshift, density;
  3032. int blksperdie, maxbdry;
  3033. loff_t ofs;
  3034. density = onenand_get_density(this->device_id);
  3035. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  3036. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3037. maxbdry = blksperdie - 1;
  3038. eraseshift = this->erase_shift - 1;
  3039. mtd->numeraseregions = this->dies << 1;
  3040. /* This fills up the device boundary */
  3041. flexonenand_get_boundary(mtd);
  3042. die = ofs = 0;
  3043. i = -1;
  3044. for (; die < this->dies; die++) {
  3045. if (!die || this->boundary[die-1] != maxbdry) {
  3046. i++;
  3047. mtd->eraseregions[i].offset = ofs;
  3048. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3049. mtd->eraseregions[i].numblocks =
  3050. this->boundary[die] + 1;
  3051. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3052. eraseshift++;
  3053. } else {
  3054. mtd->numeraseregions -= 1;
  3055. mtd->eraseregions[i].numblocks +=
  3056. this->boundary[die] + 1;
  3057. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  3058. }
  3059. if (this->boundary[die] != maxbdry) {
  3060. i++;
  3061. mtd->eraseregions[i].offset = ofs;
  3062. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3063. mtd->eraseregions[i].numblocks = maxbdry ^
  3064. this->boundary[die];
  3065. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3066. eraseshift--;
  3067. } else
  3068. mtd->numeraseregions -= 1;
  3069. }
  3070. /* Expose MLC erase size except when all blocks are SLC */
  3071. mtd->erasesize = 1 << this->erase_shift;
  3072. if (mtd->numeraseregions == 1)
  3073. mtd->erasesize >>= 1;
  3074. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  3075. for (i = 0; i < mtd->numeraseregions; i++)
  3076. printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
  3077. " numblocks: %04u]\n",
  3078. (unsigned int) mtd->eraseregions[i].offset,
  3079. mtd->eraseregions[i].erasesize,
  3080. mtd->eraseregions[i].numblocks);
  3081. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  3082. this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
  3083. this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
  3084. << (this->erase_shift - 1);
  3085. mtd->size += this->diesize[die];
  3086. }
  3087. }
  3088. /**
  3089. * flexonenand_check_blocks_erased - Check if blocks are erased
  3090. * @param mtd_info - mtd info structure
  3091. * @param start - first erase block to check
  3092. * @param end - last erase block to check
  3093. *
  3094. * Converting an unerased block from MLC to SLC
  3095. * causes byte values to change. Since both data and its ECC
  3096. * have changed, reads on the block give uncorrectable error.
  3097. * This might lead to the block being detected as bad.
  3098. *
  3099. * Avoid this by ensuring that the block to be converted is
  3100. * erased.
  3101. */
  3102. static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
  3103. {
  3104. struct onenand_chip *this = mtd->priv;
  3105. int i, ret;
  3106. int block;
  3107. struct mtd_oob_ops ops = {
  3108. .mode = MTD_OOB_PLACE,
  3109. .ooboffs = 0,
  3110. .ooblen = mtd->oobsize,
  3111. .datbuf = NULL,
  3112. .oobbuf = this->oob_buf,
  3113. };
  3114. loff_t addr;
  3115. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  3116. for (block = start; block <= end; block++) {
  3117. addr = flexonenand_addr(this, block);
  3118. if (onenand_block_isbad_nolock(mtd, addr, 0))
  3119. continue;
  3120. /*
  3121. * Since main area write results in ECC write to spare,
  3122. * it is sufficient to check only ECC bytes for change.
  3123. */
  3124. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  3125. if (ret)
  3126. return ret;
  3127. for (i = 0; i < mtd->oobsize; i++)
  3128. if (this->oob_buf[i] != 0xff)
  3129. break;
  3130. if (i != mtd->oobsize) {
  3131. printk(KERN_WARNING "%s: Block %d not erased.\n",
  3132. __func__, block);
  3133. return 1;
  3134. }
  3135. }
  3136. return 0;
  3137. }
  3138. /**
  3139. * flexonenand_set_boundary - Writes the SLC boundary
  3140. * @param mtd - mtd info structure
  3141. */
  3142. int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  3143. int boundary, int lock)
  3144. {
  3145. struct onenand_chip *this = mtd->priv;
  3146. int ret, density, blksperdie, old, new, thisboundary;
  3147. loff_t addr;
  3148. /* Change only once for SDP Flex-OneNAND */
  3149. if (die && (!ONENAND_IS_DDP(this)))
  3150. return 0;
  3151. /* boundary value of -1 indicates no required change */
  3152. if (boundary < 0 || boundary == this->boundary[die])
  3153. return 0;
  3154. density = onenand_get_density(this->device_id);
  3155. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  3156. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3157. if (boundary >= blksperdie) {
  3158. printk(KERN_ERR "%s: Invalid boundary value. "
  3159. "Boundary not changed.\n", __func__);
  3160. return -EINVAL;
  3161. }
  3162. /* Check if converting blocks are erased */
  3163. old = this->boundary[die] + (die * this->density_mask);
  3164. new = boundary + (die * this->density_mask);
  3165. ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
  3166. if (ret) {
  3167. printk(KERN_ERR "%s: Please erase blocks "
  3168. "before boundary change\n", __func__);
  3169. return ret;
  3170. }
  3171. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  3172. this->wait(mtd, FL_SYNCING);
  3173. /* Check is boundary is locked */
  3174. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3175. ret = this->wait(mtd, FL_READING);
  3176. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  3177. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  3178. printk(KERN_ERR "%s: boundary locked\n", __func__);
  3179. ret = 1;
  3180. goto out;
  3181. }
  3182. printk(KERN_INFO "Changing die %d boundary: %d%s\n",
  3183. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  3184. addr = die ? this->diesize[0] : 0;
  3185. boundary &= FLEXONENAND_PI_MASK;
  3186. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  3187. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  3188. ret = this->wait(mtd, FL_ERASING);
  3189. if (ret) {
  3190. printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
  3191. __func__, die);
  3192. goto out;
  3193. }
  3194. this->write_word(boundary, this->base + ONENAND_DATARAM);
  3195. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  3196. ret = this->wait(mtd, FL_WRITING);
  3197. if (ret) {
  3198. printk(KERN_ERR "%s: Failed PI write for Die %d\n",
  3199. __func__, die);
  3200. goto out;
  3201. }
  3202. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  3203. ret = this->wait(mtd, FL_WRITING);
  3204. out:
  3205. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  3206. this->wait(mtd, FL_RESETING);
  3207. if (!ret)
  3208. /* Recalculate device size on boundary change*/
  3209. flexonenand_get_size(mtd);
  3210. return ret;
  3211. }
  3212. /**
  3213. * onenand_chip_probe - [OneNAND Interface] The generic chip probe
  3214. * @param mtd MTD device structure
  3215. *
  3216. * OneNAND detection method:
  3217. * Compare the values from command with ones from register
  3218. */
  3219. static int onenand_chip_probe(struct mtd_info *mtd)
  3220. {
  3221. struct onenand_chip *this = mtd->priv;
  3222. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  3223. int syscfg;
  3224. /* Save system configuration 1 */
  3225. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  3226. /* Clear Sync. Burst Read mode to read BootRAM */
  3227. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
  3228. /* Send the command for reading device ID from BootRAM */
  3229. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  3230. /* Read manufacturer and device IDs from BootRAM */
  3231. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  3232. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  3233. /* Reset OneNAND to read default register values */
  3234. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  3235. /* Wait reset */
  3236. this->wait(mtd, FL_RESETING);
  3237. /* Restore system configuration 1 */
  3238. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3239. /* Check manufacturer ID */
  3240. if (onenand_check_maf(bram_maf_id))
  3241. return -ENXIO;
  3242. /* Read manufacturer and device IDs from Register */
  3243. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3244. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3245. /* Check OneNAND device */
  3246. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  3247. return -ENXIO;
  3248. return 0;
  3249. }
  3250. /**
  3251. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  3252. * @param mtd MTD device structure
  3253. */
  3254. static int onenand_probe(struct mtd_info *mtd)
  3255. {
  3256. struct onenand_chip *this = mtd->priv;
  3257. int maf_id, dev_id, ver_id;
  3258. int density;
  3259. int ret;
  3260. ret = this->chip_probe(mtd);
  3261. if (ret)
  3262. return ret;
  3263. /* Read manufacturer and device IDs from Register */
  3264. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3265. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3266. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  3267. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  3268. /* Flash device information */
  3269. onenand_print_device_info(dev_id, ver_id);
  3270. this->device_id = dev_id;
  3271. this->version_id = ver_id;
  3272. /* Check OneNAND features */
  3273. onenand_check_features(mtd);
  3274. density = onenand_get_density(dev_id);
  3275. if (FLEXONENAND(this)) {
  3276. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  3277. /* Maximum possible erase regions */
  3278. mtd->numeraseregions = this->dies << 1;
  3279. mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
  3280. * (this->dies << 1), GFP_KERNEL);
  3281. if (!mtd->eraseregions)
  3282. return -ENOMEM;
  3283. }
  3284. /*
  3285. * For Flex-OneNAND, chipsize represents maximum possible device size.
  3286. * mtd->size represents the actual device size.
  3287. */
  3288. this->chipsize = (16 << density) << 20;
  3289. /* OneNAND page size & block size */
  3290. /* The data buffer size is equal to page size */
  3291. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  3292. /* We use the full BufferRAM */
  3293. if (ONENAND_IS_4KB_PAGE(this))
  3294. mtd->writesize <<= 1;
  3295. mtd->oobsize = mtd->writesize >> 5;
  3296. /* Pages per a block are always 64 in OneNAND */
  3297. mtd->erasesize = mtd->writesize << 6;
  3298. /*
  3299. * Flex-OneNAND SLC area has 64 pages per block.
  3300. * Flex-OneNAND MLC area has 128 pages per block.
  3301. * Expose MLC erase size to find erase_shift and page_mask.
  3302. */
  3303. if (FLEXONENAND(this))
  3304. mtd->erasesize <<= 1;
  3305. this->erase_shift = ffs(mtd->erasesize) - 1;
  3306. this->page_shift = ffs(mtd->writesize) - 1;
  3307. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  3308. /* Set density mask. it is used for DDP */
  3309. if (ONENAND_IS_DDP(this))
  3310. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  3311. /* It's real page size */
  3312. this->writesize = mtd->writesize;
  3313. /* REVISIT: Multichip handling */
  3314. if (FLEXONENAND(this))
  3315. flexonenand_get_size(mtd);
  3316. else
  3317. mtd->size = this->chipsize;
  3318. /*
  3319. * We emulate the 4KiB page and 256KiB erase block size
  3320. * But oobsize is still 64 bytes.
  3321. * It is only valid if you turn on 2X program support,
  3322. * Otherwise it will be ignored by compiler.
  3323. */
  3324. if (ONENAND_IS_2PLANE(this)) {
  3325. mtd->writesize <<= 1;
  3326. mtd->erasesize <<= 1;
  3327. }
  3328. return 0;
  3329. }
  3330. /**
  3331. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  3332. * @param mtd MTD device structure
  3333. */
  3334. static int onenand_suspend(struct mtd_info *mtd)
  3335. {
  3336. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  3337. }
  3338. /**
  3339. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  3340. * @param mtd MTD device structure
  3341. */
  3342. static void onenand_resume(struct mtd_info *mtd)
  3343. {
  3344. struct onenand_chip *this = mtd->priv;
  3345. if (this->state == FL_PM_SUSPENDED)
  3346. onenand_release_device(mtd);
  3347. else
  3348. printk(KERN_ERR "%s: resume() called for the chip which is not "
  3349. "in suspended state\n", __func__);
  3350. }
  3351. /**
  3352. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  3353. * @param mtd MTD device structure
  3354. * @param maxchips Number of chips to scan for
  3355. *
  3356. * This fills out all the not initialized function pointers
  3357. * with the defaults.
  3358. * The flash ID is read and the mtd/chip structures are
  3359. * filled with the appropriate values.
  3360. */
  3361. int onenand_scan(struct mtd_info *mtd, int maxchips)
  3362. {
  3363. int i, ret;
  3364. struct onenand_chip *this = mtd->priv;
  3365. if (!this->read_word)
  3366. this->read_word = onenand_readw;
  3367. if (!this->write_word)
  3368. this->write_word = onenand_writew;
  3369. if (!this->command)
  3370. this->command = onenand_command;
  3371. if (!this->wait)
  3372. onenand_setup_wait(mtd);
  3373. if (!this->bbt_wait)
  3374. this->bbt_wait = onenand_bbt_wait;
  3375. if (!this->unlock_all)
  3376. this->unlock_all = onenand_unlock_all;
  3377. if (!this->chip_probe)
  3378. this->chip_probe = onenand_chip_probe;
  3379. if (!this->read_bufferram)
  3380. this->read_bufferram = onenand_read_bufferram;
  3381. if (!this->write_bufferram)
  3382. this->write_bufferram = onenand_write_bufferram;
  3383. if (!this->block_markbad)
  3384. this->block_markbad = onenand_default_block_markbad;
  3385. if (!this->scan_bbt)
  3386. this->scan_bbt = onenand_default_bbt;
  3387. if (onenand_probe(mtd))
  3388. return -ENXIO;
  3389. /* Set Sync. Burst Read after probing */
  3390. if (this->mmcontrol) {
  3391. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  3392. this->read_bufferram = onenand_sync_read_bufferram;
  3393. }
  3394. /* Allocate buffers, if necessary */
  3395. if (!this->page_buf) {
  3396. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3397. if (!this->page_buf) {
  3398. printk(KERN_ERR "%s: Can't allocate page_buf\n",
  3399. __func__);
  3400. return -ENOMEM;
  3401. }
  3402. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3403. this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3404. if (!this->verify_buf) {
  3405. kfree(this->page_buf);
  3406. return -ENOMEM;
  3407. }
  3408. #endif
  3409. this->options |= ONENAND_PAGEBUF_ALLOC;
  3410. }
  3411. if (!this->oob_buf) {
  3412. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  3413. if (!this->oob_buf) {
  3414. printk(KERN_ERR "%s: Can't allocate oob_buf\n",
  3415. __func__);
  3416. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3417. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  3418. kfree(this->page_buf);
  3419. }
  3420. return -ENOMEM;
  3421. }
  3422. this->options |= ONENAND_OOBBUF_ALLOC;
  3423. }
  3424. this->state = FL_READY;
  3425. init_waitqueue_head(&this->wq);
  3426. spin_lock_init(&this->chip_lock);
  3427. /*
  3428. * Allow subpage writes up to oobsize.
  3429. */
  3430. switch (mtd->oobsize) {
  3431. case 128:
  3432. if (FLEXONENAND(this)) {
  3433. this->ecclayout = &flexonenand_oob_128;
  3434. mtd->subpage_sft = 0;
  3435. } else {
  3436. this->ecclayout = &onenand_oob_128;
  3437. mtd->subpage_sft = 2;
  3438. }
  3439. break;
  3440. case 64:
  3441. this->ecclayout = &onenand_oob_64;
  3442. mtd->subpage_sft = 2;
  3443. break;
  3444. case 32:
  3445. this->ecclayout = &onenand_oob_32;
  3446. mtd->subpage_sft = 1;
  3447. break;
  3448. default:
  3449. printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
  3450. __func__, mtd->oobsize);
  3451. mtd->subpage_sft = 0;
  3452. /* To prevent kernel oops */
  3453. this->ecclayout = &onenand_oob_32;
  3454. break;
  3455. }
  3456. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3457. /*
  3458. * The number of bytes available for a client to place data into
  3459. * the out of band area
  3460. */
  3461. this->ecclayout->oobavail = 0;
  3462. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  3463. this->ecclayout->oobfree[i].length; i++)
  3464. this->ecclayout->oobavail +=
  3465. this->ecclayout->oobfree[i].length;
  3466. mtd->oobavail = this->ecclayout->oobavail;
  3467. mtd->ecclayout = this->ecclayout;
  3468. /* Fill in remaining MTD driver data */
  3469. mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
  3470. mtd->flags = MTD_CAP_NANDFLASH;
  3471. mtd->erase = onenand_erase;
  3472. mtd->point = NULL;
  3473. mtd->unpoint = NULL;
  3474. mtd->read = onenand_read;
  3475. mtd->write = onenand_write;
  3476. mtd->read_oob = onenand_read_oob;
  3477. mtd->write_oob = onenand_write_oob;
  3478. mtd->panic_write = onenand_panic_write;
  3479. #ifdef CONFIG_MTD_ONENAND_OTP
  3480. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  3481. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  3482. mtd->get_user_prot_info = onenand_get_user_prot_info;
  3483. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  3484. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  3485. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  3486. #endif
  3487. mtd->sync = onenand_sync;
  3488. mtd->lock = onenand_lock;
  3489. mtd->unlock = onenand_unlock;
  3490. mtd->suspend = onenand_suspend;
  3491. mtd->resume = onenand_resume;
  3492. mtd->block_isbad = onenand_block_isbad;
  3493. mtd->block_markbad = onenand_block_markbad;
  3494. mtd->owner = THIS_MODULE;
  3495. mtd->writebufsize = mtd->writesize;
  3496. /* Unlock whole block */
  3497. if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
  3498. this->unlock_all(mtd);
  3499. ret = this->scan_bbt(mtd);
  3500. if ((!FLEXONENAND(this)) || ret)
  3501. return ret;
  3502. /* Change Flex-OneNAND boundaries if required */
  3503. for (i = 0; i < MAX_DIES; i++)
  3504. flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
  3505. flex_bdry[(2 * i) + 1]);
  3506. return 0;
  3507. }
  3508. /**
  3509. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  3510. * @param mtd MTD device structure
  3511. */
  3512. void onenand_release(struct mtd_info *mtd)
  3513. {
  3514. struct onenand_chip *this = mtd->priv;
  3515. /* Deregister partitions */
  3516. mtd_device_unregister(mtd);
  3517. /* Free bad block table memory, if allocated */
  3518. if (this->bbm) {
  3519. struct bbm_info *bbm = this->bbm;
  3520. kfree(bbm->bbt);
  3521. kfree(this->bbm);
  3522. }
  3523. /* Buffers allocated by onenand_scan */
  3524. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3525. kfree(this->page_buf);
  3526. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3527. kfree(this->verify_buf);
  3528. #endif
  3529. }
  3530. if (this->options & ONENAND_OOBBUF_ALLOC)
  3531. kfree(this->oob_buf);
  3532. kfree(mtd->eraseregions);
  3533. }
  3534. EXPORT_SYMBOL_GPL(onenand_scan);
  3535. EXPORT_SYMBOL_GPL(onenand_release);
  3536. MODULE_LICENSE("GPL");
  3537. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  3538. MODULE_DESCRIPTION("Generic OneNAND flash driver code");