onenand_base.c 106 KB

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