onenand_base.c 107 KB

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