onenand_base.c 107 KB

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