onenand_base.c 103 KB

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