onenand_base.c 108 KB

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