onenand_base.c 107 KB

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