nand_base.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246
  1. /*
  2. * Overview:
  3. * This is the generic MTD driver for NAND flash devices. It should be
  4. * capable of working with almost all NAND chips currently available.
  5. *
  6. * Additional technical information is available on
  7. * http://www.linux-mtd.infradead.org/doc/nand.html
  8. *
  9. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  10. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  11. *
  12. * Credits:
  13. * David Woodhouse for adding multichip support
  14. *
  15. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  16. * rework for 2K page size chips
  17. *
  18. * TODO:
  19. * Enable cached programming for 2k page size chips
  20. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  21. * if we have HW ECC support.
  22. * BBT table is not serialized, has to be fixed
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License version 2 as
  26. * published by the Free Software Foundation.
  27. *
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <common.h>
  31. #if CONFIG_IS_ENABLED(OF_CONTROL)
  32. #include <fdtdec.h>
  33. #endif
  34. #include <malloc.h>
  35. #include <watchdog.h>
  36. #include <linux/err.h>
  37. #include <linux/compat.h>
  38. #include <linux/mtd/mtd.h>
  39. #include <linux/mtd/nand.h>
  40. #include <linux/mtd/nand_ecc.h>
  41. #include <linux/mtd/nand_bch.h>
  42. #ifdef CONFIG_MTD_PARTITIONS
  43. #include <linux/mtd/partitions.h>
  44. #endif
  45. #include <asm/io.h>
  46. #include <linux/errno.h>
  47. /* Define default oob placement schemes for large and small page devices */
  48. static struct nand_ecclayout nand_oob_8 = {
  49. .eccbytes = 3,
  50. .eccpos = {0, 1, 2},
  51. .oobfree = {
  52. {.offset = 3,
  53. .length = 2},
  54. {.offset = 6,
  55. .length = 2} }
  56. };
  57. static struct nand_ecclayout nand_oob_16 = {
  58. .eccbytes = 6,
  59. .eccpos = {0, 1, 2, 3, 6, 7},
  60. .oobfree = {
  61. {.offset = 8,
  62. . length = 8} }
  63. };
  64. static struct nand_ecclayout nand_oob_64 = {
  65. .eccbytes = 24,
  66. .eccpos = {
  67. 40, 41, 42, 43, 44, 45, 46, 47,
  68. 48, 49, 50, 51, 52, 53, 54, 55,
  69. 56, 57, 58, 59, 60, 61, 62, 63},
  70. .oobfree = {
  71. {.offset = 2,
  72. .length = 38} }
  73. };
  74. static struct nand_ecclayout nand_oob_128 = {
  75. .eccbytes = 48,
  76. .eccpos = {
  77. 80, 81, 82, 83, 84, 85, 86, 87,
  78. 88, 89, 90, 91, 92, 93, 94, 95,
  79. 96, 97, 98, 99, 100, 101, 102, 103,
  80. 104, 105, 106, 107, 108, 109, 110, 111,
  81. 112, 113, 114, 115, 116, 117, 118, 119,
  82. 120, 121, 122, 123, 124, 125, 126, 127},
  83. .oobfree = {
  84. {.offset = 2,
  85. .length = 78} }
  86. };
  87. static int nand_get_device(struct mtd_info *mtd, int new_state);
  88. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  89. struct mtd_oob_ops *ops);
  90. /*
  91. * For devices which display every fart in the system on a separate LED. Is
  92. * compiled away when LED support is disabled.
  93. */
  94. DEFINE_LED_TRIGGER(nand_led_trigger);
  95. static int check_offs_len(struct mtd_info *mtd,
  96. loff_t ofs, uint64_t len)
  97. {
  98. struct nand_chip *chip = mtd_to_nand(mtd);
  99. int ret = 0;
  100. /* Start address must align on block boundary */
  101. if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
  102. pr_debug("%s: unaligned address\n", __func__);
  103. ret = -EINVAL;
  104. }
  105. /* Length must align on block boundary */
  106. if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
  107. pr_debug("%s: length not block aligned\n", __func__);
  108. ret = -EINVAL;
  109. }
  110. return ret;
  111. }
  112. /**
  113. * nand_release_device - [GENERIC] release chip
  114. * @mtd: MTD device structure
  115. *
  116. * Release chip lock and wake up anyone waiting on the device.
  117. */
  118. static void nand_release_device(struct mtd_info *mtd)
  119. {
  120. struct nand_chip *chip = mtd_to_nand(mtd);
  121. /* De-select the NAND device */
  122. chip->select_chip(mtd, -1);
  123. }
  124. /**
  125. * nand_read_byte - [DEFAULT] read one byte from the chip
  126. * @mtd: MTD device structure
  127. *
  128. * Default read function for 8bit buswidth
  129. */
  130. uint8_t nand_read_byte(struct mtd_info *mtd)
  131. {
  132. struct nand_chip *chip = mtd_to_nand(mtd);
  133. return readb(chip->IO_ADDR_R);
  134. }
  135. /**
  136. * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  137. * @mtd: MTD device structure
  138. *
  139. * Default read function for 16bit buswidth with endianness conversion.
  140. *
  141. */
  142. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  143. {
  144. struct nand_chip *chip = mtd_to_nand(mtd);
  145. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  146. }
  147. /**
  148. * nand_read_word - [DEFAULT] read one word from the chip
  149. * @mtd: MTD device structure
  150. *
  151. * Default read function for 16bit buswidth without endianness conversion.
  152. */
  153. static u16 nand_read_word(struct mtd_info *mtd)
  154. {
  155. struct nand_chip *chip = mtd_to_nand(mtd);
  156. return readw(chip->IO_ADDR_R);
  157. }
  158. /**
  159. * nand_select_chip - [DEFAULT] control CE line
  160. * @mtd: MTD device structure
  161. * @chipnr: chipnumber to select, -1 for deselect
  162. *
  163. * Default select function for 1 chip devices.
  164. */
  165. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  166. {
  167. struct nand_chip *chip = mtd_to_nand(mtd);
  168. switch (chipnr) {
  169. case -1:
  170. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  171. break;
  172. case 0:
  173. break;
  174. default:
  175. BUG();
  176. }
  177. }
  178. /**
  179. * nand_write_byte - [DEFAULT] write single byte to chip
  180. * @mtd: MTD device structure
  181. * @byte: value to write
  182. *
  183. * Default function to write a byte to I/O[7:0]
  184. */
  185. static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
  186. {
  187. struct nand_chip *chip = mtd_to_nand(mtd);
  188. chip->write_buf(mtd, &byte, 1);
  189. }
  190. /**
  191. * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
  192. * @mtd: MTD device structure
  193. * @byte: value to write
  194. *
  195. * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
  196. */
  197. static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
  198. {
  199. struct nand_chip *chip = mtd_to_nand(mtd);
  200. uint16_t word = byte;
  201. /*
  202. * It's not entirely clear what should happen to I/O[15:8] when writing
  203. * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
  204. *
  205. * When the host supports a 16-bit bus width, only data is
  206. * transferred at the 16-bit width. All address and command line
  207. * transfers shall use only the lower 8-bits of the data bus. During
  208. * command transfers, the host may place any value on the upper
  209. * 8-bits of the data bus. During address transfers, the host shall
  210. * set the upper 8-bits of the data bus to 00h.
  211. *
  212. * One user of the write_byte callback is nand_onfi_set_features. The
  213. * four parameters are specified to be written to I/O[7:0], but this is
  214. * neither an address nor a command transfer. Let's assume a 0 on the
  215. * upper I/O lines is OK.
  216. */
  217. chip->write_buf(mtd, (uint8_t *)&word, 2);
  218. }
  219. #if !defined(CONFIG_BLACKFIN)
  220. static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
  221. {
  222. int i;
  223. for (i = 0; i < len; i++)
  224. writeb(buf[i], addr);
  225. }
  226. static void ioread8_rep(void *addr, uint8_t *buf, int len)
  227. {
  228. int i;
  229. for (i = 0; i < len; i++)
  230. buf[i] = readb(addr);
  231. }
  232. static void ioread16_rep(void *addr, void *buf, int len)
  233. {
  234. int i;
  235. u16 *p = (u16 *) buf;
  236. for (i = 0; i < len; i++)
  237. p[i] = readw(addr);
  238. }
  239. static void iowrite16_rep(void *addr, void *buf, int len)
  240. {
  241. int i;
  242. u16 *p = (u16 *) buf;
  243. for (i = 0; i < len; i++)
  244. writew(p[i], addr);
  245. }
  246. #endif
  247. /**
  248. * nand_write_buf - [DEFAULT] write buffer to chip
  249. * @mtd: MTD device structure
  250. * @buf: data buffer
  251. * @len: number of bytes to write
  252. *
  253. * Default write function for 8bit buswidth.
  254. */
  255. void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  256. {
  257. struct nand_chip *chip = mtd_to_nand(mtd);
  258. iowrite8_rep(chip->IO_ADDR_W, buf, len);
  259. }
  260. /**
  261. * nand_read_buf - [DEFAULT] read chip data into buffer
  262. * @mtd: MTD device structure
  263. * @buf: buffer to store date
  264. * @len: number of bytes to read
  265. *
  266. * Default read function for 8bit buswidth.
  267. */
  268. void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  269. {
  270. struct nand_chip *chip = mtd_to_nand(mtd);
  271. ioread8_rep(chip->IO_ADDR_R, buf, len);
  272. }
  273. /**
  274. * nand_write_buf16 - [DEFAULT] write buffer to chip
  275. * @mtd: MTD device structure
  276. * @buf: data buffer
  277. * @len: number of bytes to write
  278. *
  279. * Default write function for 16bit buswidth.
  280. */
  281. void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  282. {
  283. struct nand_chip *chip = mtd_to_nand(mtd);
  284. u16 *p = (u16 *) buf;
  285. iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
  286. }
  287. /**
  288. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  289. * @mtd: MTD device structure
  290. * @buf: buffer to store date
  291. * @len: number of bytes to read
  292. *
  293. * Default read function for 16bit buswidth.
  294. */
  295. void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  296. {
  297. struct nand_chip *chip = mtd_to_nand(mtd);
  298. u16 *p = (u16 *) buf;
  299. ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
  300. }
  301. /**
  302. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  303. * @mtd: MTD device structure
  304. * @ofs: offset from device start
  305. *
  306. * Check, if the block is bad.
  307. */
  308. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
  309. {
  310. int page, res = 0, i = 0;
  311. struct nand_chip *chip = mtd_to_nand(mtd);
  312. u16 bad;
  313. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  314. ofs += mtd->erasesize - mtd->writesize;
  315. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  316. do {
  317. if (chip->options & NAND_BUSWIDTH_16) {
  318. chip->cmdfunc(mtd, NAND_CMD_READOOB,
  319. chip->badblockpos & 0xFE, page);
  320. bad = cpu_to_le16(chip->read_word(mtd));
  321. if (chip->badblockpos & 0x1)
  322. bad >>= 8;
  323. else
  324. bad &= 0xFF;
  325. } else {
  326. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
  327. page);
  328. bad = chip->read_byte(mtd);
  329. }
  330. if (likely(chip->badblockbits == 8))
  331. res = bad != 0xFF;
  332. else
  333. res = hweight8(bad) < chip->badblockbits;
  334. ofs += mtd->writesize;
  335. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  336. i++;
  337. } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
  338. return res;
  339. }
  340. /**
  341. * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
  342. * @mtd: MTD device structure
  343. * @ofs: offset from device start
  344. *
  345. * This is the default implementation, which can be overridden by a hardware
  346. * specific driver. It provides the details for writing a bad block marker to a
  347. * block.
  348. */
  349. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  350. {
  351. struct nand_chip *chip = mtd_to_nand(mtd);
  352. struct mtd_oob_ops ops;
  353. uint8_t buf[2] = { 0, 0 };
  354. int ret = 0, res, i = 0;
  355. memset(&ops, 0, sizeof(ops));
  356. ops.oobbuf = buf;
  357. ops.ooboffs = chip->badblockpos;
  358. if (chip->options & NAND_BUSWIDTH_16) {
  359. ops.ooboffs &= ~0x01;
  360. ops.len = ops.ooblen = 2;
  361. } else {
  362. ops.len = ops.ooblen = 1;
  363. }
  364. ops.mode = MTD_OPS_PLACE_OOB;
  365. /* Write to first/last page(s) if necessary */
  366. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  367. ofs += mtd->erasesize - mtd->writesize;
  368. do {
  369. res = nand_do_write_oob(mtd, ofs, &ops);
  370. if (!ret)
  371. ret = res;
  372. i++;
  373. ofs += mtd->writesize;
  374. } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
  375. return ret;
  376. }
  377. /**
  378. * nand_block_markbad_lowlevel - mark a block bad
  379. * @mtd: MTD device structure
  380. * @ofs: offset from device start
  381. *
  382. * This function performs the generic NAND bad block marking steps (i.e., bad
  383. * block table(s) and/or marker(s)). We only allow the hardware driver to
  384. * specify how to write bad block markers to OOB (chip->block_markbad).
  385. *
  386. * We try operations in the following order:
  387. * (1) erase the affected block, to allow OOB marker to be written cleanly
  388. * (2) write bad block marker to OOB area of affected block (unless flag
  389. * NAND_BBT_NO_OOB_BBM is present)
  390. * (3) update the BBT
  391. * Note that we retain the first error encountered in (2) or (3), finish the
  392. * procedures, and dump the error in the end.
  393. */
  394. static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
  395. {
  396. struct nand_chip *chip = mtd_to_nand(mtd);
  397. int res, ret = 0;
  398. if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
  399. struct erase_info einfo;
  400. /* Attempt erase before marking OOB */
  401. memset(&einfo, 0, sizeof(einfo));
  402. einfo.mtd = mtd;
  403. einfo.addr = ofs;
  404. einfo.len = 1ULL << chip->phys_erase_shift;
  405. nand_erase_nand(mtd, &einfo, 0);
  406. /* Write bad block marker to OOB */
  407. nand_get_device(mtd, FL_WRITING);
  408. ret = chip->block_markbad(mtd, ofs);
  409. nand_release_device(mtd);
  410. }
  411. /* Mark block bad in BBT */
  412. if (chip->bbt) {
  413. res = nand_markbad_bbt(mtd, ofs);
  414. if (!ret)
  415. ret = res;
  416. }
  417. if (!ret)
  418. mtd->ecc_stats.badblocks++;
  419. return ret;
  420. }
  421. /**
  422. * nand_check_wp - [GENERIC] check if the chip is write protected
  423. * @mtd: MTD device structure
  424. *
  425. * Check, if the device is write protected. The function expects, that the
  426. * device is already selected.
  427. */
  428. static int nand_check_wp(struct mtd_info *mtd)
  429. {
  430. struct nand_chip *chip = mtd_to_nand(mtd);
  431. /* Broken xD cards report WP despite being writable */
  432. if (chip->options & NAND_BROKEN_XD)
  433. return 0;
  434. /* Check the WP bit */
  435. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  436. return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  437. }
  438. /**
  439. * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
  440. * @mtd: MTD device structure
  441. * @ofs: offset from device start
  442. *
  443. * Check if the block is marked as reserved.
  444. */
  445. static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  446. {
  447. struct nand_chip *chip = mtd_to_nand(mtd);
  448. if (!chip->bbt)
  449. return 0;
  450. /* Return info from the table */
  451. return nand_isreserved_bbt(mtd, ofs);
  452. }
  453. /**
  454. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  455. * @mtd: MTD device structure
  456. * @ofs: offset from device start
  457. * @allowbbt: 1, if its allowed to access the bbt area
  458. *
  459. * Check, if the block is bad. Either by reading the bad block table or
  460. * calling of the scan function.
  461. */
  462. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  463. {
  464. struct nand_chip *chip = mtd_to_nand(mtd);
  465. if (!(chip->options & NAND_SKIP_BBTSCAN) &&
  466. !(chip->options & NAND_BBT_SCANNED)) {
  467. chip->options |= NAND_BBT_SCANNED;
  468. chip->scan_bbt(mtd);
  469. }
  470. if (!chip->bbt)
  471. return chip->block_bad(mtd, ofs);
  472. /* Return info from the table */
  473. return nand_isbad_bbt(mtd, ofs, allowbbt);
  474. }
  475. /**
  476. * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
  477. * @mtd: MTD device structure
  478. *
  479. * Wait for the ready pin after a command, and warn if a timeout occurs.
  480. */
  481. void nand_wait_ready(struct mtd_info *mtd)
  482. {
  483. struct nand_chip *chip = mtd_to_nand(mtd);
  484. u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
  485. u32 time_start;
  486. time_start = get_timer(0);
  487. /* Wait until command is processed or timeout occurs */
  488. while (get_timer(time_start) < timeo) {
  489. if (chip->dev_ready)
  490. if (chip->dev_ready(mtd))
  491. break;
  492. }
  493. if (!chip->dev_ready(mtd))
  494. pr_warn("timeout while waiting for chip to become ready\n");
  495. }
  496. EXPORT_SYMBOL_GPL(nand_wait_ready);
  497. /**
  498. * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
  499. * @mtd: MTD device structure
  500. * @timeo: Timeout in ms
  501. *
  502. * Wait for status ready (i.e. command done) or timeout.
  503. */
  504. static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
  505. {
  506. register struct nand_chip *chip = mtd_to_nand(mtd);
  507. u32 time_start;
  508. timeo = (CONFIG_SYS_HZ * timeo) / 1000;
  509. time_start = get_timer(0);
  510. while (get_timer(time_start) < timeo) {
  511. if ((chip->read_byte(mtd) & NAND_STATUS_READY))
  512. break;
  513. WATCHDOG_RESET();
  514. }
  515. };
  516. /**
  517. * nand_command - [DEFAULT] Send command to NAND device
  518. * @mtd: MTD device structure
  519. * @command: the command to be sent
  520. * @column: the column address for this command, -1 if none
  521. * @page_addr: the page address for this command, -1 if none
  522. *
  523. * Send command to NAND device. This function is used for small page devices
  524. * (512 Bytes per page).
  525. */
  526. static void nand_command(struct mtd_info *mtd, unsigned int command,
  527. int column, int page_addr)
  528. {
  529. register struct nand_chip *chip = mtd_to_nand(mtd);
  530. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  531. /* Write out the command to the device */
  532. if (command == NAND_CMD_SEQIN) {
  533. int readcmd;
  534. if (column >= mtd->writesize) {
  535. /* OOB area */
  536. column -= mtd->writesize;
  537. readcmd = NAND_CMD_READOOB;
  538. } else if (column < 256) {
  539. /* First 256 bytes --> READ0 */
  540. readcmd = NAND_CMD_READ0;
  541. } else {
  542. column -= 256;
  543. readcmd = NAND_CMD_READ1;
  544. }
  545. chip->cmd_ctrl(mtd, readcmd, ctrl);
  546. ctrl &= ~NAND_CTRL_CHANGE;
  547. }
  548. chip->cmd_ctrl(mtd, command, ctrl);
  549. /* Address cycle, when necessary */
  550. ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
  551. /* Serially input address */
  552. if (column != -1) {
  553. /* Adjust columns for 16 bit buswidth */
  554. if (chip->options & NAND_BUSWIDTH_16 &&
  555. !nand_opcode_8bits(command))
  556. column >>= 1;
  557. chip->cmd_ctrl(mtd, column, ctrl);
  558. ctrl &= ~NAND_CTRL_CHANGE;
  559. }
  560. if (page_addr != -1) {
  561. chip->cmd_ctrl(mtd, page_addr, ctrl);
  562. ctrl &= ~NAND_CTRL_CHANGE;
  563. chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
  564. /* One more address cycle for devices > 32MiB */
  565. if (chip->chipsize > (32 << 20))
  566. chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
  567. }
  568. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  569. /*
  570. * Program and erase have their own busy handlers status and sequential
  571. * in needs no delay
  572. */
  573. switch (command) {
  574. case NAND_CMD_PAGEPROG:
  575. case NAND_CMD_ERASE1:
  576. case NAND_CMD_ERASE2:
  577. case NAND_CMD_SEQIN:
  578. case NAND_CMD_STATUS:
  579. return;
  580. case NAND_CMD_RESET:
  581. if (chip->dev_ready)
  582. break;
  583. udelay(chip->chip_delay);
  584. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  585. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  586. chip->cmd_ctrl(mtd,
  587. NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  588. /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
  589. nand_wait_status_ready(mtd, 250);
  590. return;
  591. /* This applies to read commands */
  592. default:
  593. /*
  594. * If we don't have access to the busy pin, we apply the given
  595. * command delay
  596. */
  597. if (!chip->dev_ready) {
  598. udelay(chip->chip_delay);
  599. return;
  600. }
  601. }
  602. /*
  603. * Apply this short delay always to ensure that we do wait tWB in
  604. * any case on any machine.
  605. */
  606. ndelay(100);
  607. nand_wait_ready(mtd);
  608. }
  609. /**
  610. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  611. * @mtd: MTD device structure
  612. * @command: the command to be sent
  613. * @column: the column address for this command, -1 if none
  614. * @page_addr: the page address for this command, -1 if none
  615. *
  616. * Send command to NAND device. This is the version for the new large page
  617. * devices. We don't have the separate regions as we have in the small page
  618. * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
  619. */
  620. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  621. int column, int page_addr)
  622. {
  623. register struct nand_chip *chip = mtd_to_nand(mtd);
  624. /* Emulate NAND_CMD_READOOB */
  625. if (command == NAND_CMD_READOOB) {
  626. column += mtd->writesize;
  627. command = NAND_CMD_READ0;
  628. }
  629. /* Command latch cycle */
  630. chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  631. if (column != -1 || page_addr != -1) {
  632. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  633. /* Serially input address */
  634. if (column != -1) {
  635. /* Adjust columns for 16 bit buswidth */
  636. if (chip->options & NAND_BUSWIDTH_16 &&
  637. !nand_opcode_8bits(command))
  638. column >>= 1;
  639. chip->cmd_ctrl(mtd, column, ctrl);
  640. ctrl &= ~NAND_CTRL_CHANGE;
  641. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  642. }
  643. if (page_addr != -1) {
  644. chip->cmd_ctrl(mtd, page_addr, ctrl);
  645. chip->cmd_ctrl(mtd, page_addr >> 8,
  646. NAND_NCE | NAND_ALE);
  647. /* One more address cycle for devices > 128MiB */
  648. if (chip->chipsize > (128 << 20))
  649. chip->cmd_ctrl(mtd, page_addr >> 16,
  650. NAND_NCE | NAND_ALE);
  651. }
  652. }
  653. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  654. /*
  655. * Program and erase have their own busy handlers status, sequential
  656. * in and status need no delay.
  657. */
  658. switch (command) {
  659. case NAND_CMD_CACHEDPROG:
  660. case NAND_CMD_PAGEPROG:
  661. case NAND_CMD_ERASE1:
  662. case NAND_CMD_ERASE2:
  663. case NAND_CMD_SEQIN:
  664. case NAND_CMD_RNDIN:
  665. case NAND_CMD_STATUS:
  666. return;
  667. case NAND_CMD_RESET:
  668. if (chip->dev_ready)
  669. break;
  670. udelay(chip->chip_delay);
  671. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  672. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  673. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  674. NAND_NCE | NAND_CTRL_CHANGE);
  675. /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
  676. nand_wait_status_ready(mtd, 250);
  677. return;
  678. case NAND_CMD_RNDOUT:
  679. /* No ready / busy check necessary */
  680. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  681. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  682. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  683. NAND_NCE | NAND_CTRL_CHANGE);
  684. return;
  685. case NAND_CMD_READ0:
  686. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  687. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  688. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  689. NAND_NCE | NAND_CTRL_CHANGE);
  690. /* This applies to read commands */
  691. default:
  692. /*
  693. * If we don't have access to the busy pin, we apply the given
  694. * command delay.
  695. */
  696. if (!chip->dev_ready) {
  697. udelay(chip->chip_delay);
  698. return;
  699. }
  700. }
  701. /*
  702. * Apply this short delay always to ensure that we do wait tWB in
  703. * any case on any machine.
  704. */
  705. ndelay(100);
  706. nand_wait_ready(mtd);
  707. }
  708. /**
  709. * panic_nand_get_device - [GENERIC] Get chip for selected access
  710. * @chip: the nand chip descriptor
  711. * @mtd: MTD device structure
  712. * @new_state: the state which is requested
  713. *
  714. * Used when in panic, no locks are taken.
  715. */
  716. static void panic_nand_get_device(struct nand_chip *chip,
  717. struct mtd_info *mtd, int new_state)
  718. {
  719. /* Hardware controller shared among independent devices */
  720. chip->controller->active = chip;
  721. chip->state = new_state;
  722. }
  723. /**
  724. * nand_get_device - [GENERIC] Get chip for selected access
  725. * @mtd: MTD device structure
  726. * @new_state: the state which is requested
  727. *
  728. * Get the device and lock it for exclusive access
  729. */
  730. static int
  731. nand_get_device(struct mtd_info *mtd, int new_state)
  732. {
  733. struct nand_chip *chip = mtd_to_nand(mtd);
  734. chip->state = new_state;
  735. return 0;
  736. }
  737. /**
  738. * panic_nand_wait - [GENERIC] wait until the command is done
  739. * @mtd: MTD device structure
  740. * @chip: NAND chip structure
  741. * @timeo: timeout
  742. *
  743. * Wait for command done. This is a helper function for nand_wait used when
  744. * we are in interrupt context. May happen when in panic and trying to write
  745. * an oops through mtdoops.
  746. */
  747. static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
  748. unsigned long timeo)
  749. {
  750. int i;
  751. for (i = 0; i < timeo; i++) {
  752. if (chip->dev_ready) {
  753. if (chip->dev_ready(mtd))
  754. break;
  755. } else {
  756. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  757. break;
  758. }
  759. mdelay(1);
  760. }
  761. }
  762. /**
  763. * nand_wait - [DEFAULT] wait until the command is done
  764. * @mtd: MTD device structure
  765. * @chip: NAND chip structure
  766. *
  767. * Wait for command done. This applies to erase and program only.
  768. */
  769. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  770. {
  771. int status;
  772. unsigned long timeo = 400;
  773. led_trigger_event(nand_led_trigger, LED_FULL);
  774. /*
  775. * Apply this short delay always to ensure that we do wait tWB in any
  776. * case on any machine.
  777. */
  778. ndelay(100);
  779. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  780. u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
  781. u32 time_start;
  782. time_start = get_timer(0);
  783. while (get_timer(time_start) < timer) {
  784. if (chip->dev_ready) {
  785. if (chip->dev_ready(mtd))
  786. break;
  787. } else {
  788. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  789. break;
  790. }
  791. }
  792. led_trigger_event(nand_led_trigger, LED_OFF);
  793. status = (int)chip->read_byte(mtd);
  794. /* This can happen if in case of timeout or buggy dev_ready */
  795. WARN_ON(!(status & NAND_STATUS_READY));
  796. return status;
  797. }
  798. #define BITS_PER_BYTE 8
  799. /**
  800. * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
  801. * @buf: buffer to test
  802. * @len: buffer length
  803. * @bitflips_threshold: maximum number of bitflips
  804. *
  805. * Check if a buffer contains only 0xff, which means the underlying region
  806. * has been erased and is ready to be programmed.
  807. * The bitflips_threshold specify the maximum number of bitflips before
  808. * considering the region is not erased.
  809. * Note: The logic of this function has been extracted from the memweight
  810. * implementation, except that nand_check_erased_buf function exit before
  811. * testing the whole buffer if the number of bitflips exceed the
  812. * bitflips_threshold value.
  813. *
  814. * Returns a positive number of bitflips less than or equal to
  815. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  816. * threshold.
  817. */
  818. static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
  819. {
  820. const unsigned char *bitmap = buf;
  821. int bitflips = 0;
  822. int weight;
  823. for (; len && ((uintptr_t)bitmap) % sizeof(long);
  824. len--, bitmap++) {
  825. weight = hweight8(*bitmap);
  826. bitflips += BITS_PER_BYTE - weight;
  827. if (unlikely(bitflips > bitflips_threshold))
  828. return -EBADMSG;
  829. }
  830. for (; len >= 4; len -= 4, bitmap += 4) {
  831. weight = hweight32(*((u32 *)bitmap));
  832. bitflips += 32 - weight;
  833. if (unlikely(bitflips > bitflips_threshold))
  834. return -EBADMSG;
  835. }
  836. for (; len > 0; len--, bitmap++) {
  837. weight = hweight8(*bitmap);
  838. bitflips += BITS_PER_BYTE - weight;
  839. if (unlikely(bitflips > bitflips_threshold))
  840. return -EBADMSG;
  841. }
  842. return bitflips;
  843. }
  844. /**
  845. * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
  846. * 0xff data
  847. * @data: data buffer to test
  848. * @datalen: data length
  849. * @ecc: ECC buffer
  850. * @ecclen: ECC length
  851. * @extraoob: extra OOB buffer
  852. * @extraooblen: extra OOB length
  853. * @bitflips_threshold: maximum number of bitflips
  854. *
  855. * Check if a data buffer and its associated ECC and OOB data contains only
  856. * 0xff pattern, which means the underlying region has been erased and is
  857. * ready to be programmed.
  858. * The bitflips_threshold specify the maximum number of bitflips before
  859. * considering the region as not erased.
  860. *
  861. * Note:
  862. * 1/ ECC algorithms are working on pre-defined block sizes which are usually
  863. * different from the NAND page size. When fixing bitflips, ECC engines will
  864. * report the number of errors per chunk, and the NAND core infrastructure
  865. * expect you to return the maximum number of bitflips for the whole page.
  866. * This is why you should always use this function on a single chunk and
  867. * not on the whole page. After checking each chunk you should update your
  868. * max_bitflips value accordingly.
  869. * 2/ When checking for bitflips in erased pages you should not only check
  870. * the payload data but also their associated ECC data, because a user might
  871. * have programmed almost all bits to 1 but a few. In this case, we
  872. * shouldn't consider the chunk as erased, and checking ECC bytes prevent
  873. * this case.
  874. * 3/ The extraoob argument is optional, and should be used if some of your OOB
  875. * data are protected by the ECC engine.
  876. * It could also be used if you support subpages and want to attach some
  877. * extra OOB data to an ECC chunk.
  878. *
  879. * Returns a positive number of bitflips less than or equal to
  880. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  881. * threshold. In case of success, the passed buffers are filled with 0xff.
  882. */
  883. int nand_check_erased_ecc_chunk(void *data, int datalen,
  884. void *ecc, int ecclen,
  885. void *extraoob, int extraooblen,
  886. int bitflips_threshold)
  887. {
  888. int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
  889. data_bitflips = nand_check_erased_buf(data, datalen,
  890. bitflips_threshold);
  891. if (data_bitflips < 0)
  892. return data_bitflips;
  893. bitflips_threshold -= data_bitflips;
  894. ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
  895. if (ecc_bitflips < 0)
  896. return ecc_bitflips;
  897. bitflips_threshold -= ecc_bitflips;
  898. extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
  899. bitflips_threshold);
  900. if (extraoob_bitflips < 0)
  901. return extraoob_bitflips;
  902. if (data_bitflips)
  903. memset(data, 0xff, datalen);
  904. if (ecc_bitflips)
  905. memset(ecc, 0xff, ecclen);
  906. if (extraoob_bitflips)
  907. memset(extraoob, 0xff, extraooblen);
  908. return data_bitflips + ecc_bitflips + extraoob_bitflips;
  909. }
  910. EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
  911. /**
  912. * nand_read_page_raw - [INTERN] read raw page data without ecc
  913. * @mtd: mtd info structure
  914. * @chip: nand chip info structure
  915. * @buf: buffer to store read data
  916. * @oob_required: caller requires OOB data read to chip->oob_poi
  917. * @page: page number to read
  918. *
  919. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  920. */
  921. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  922. uint8_t *buf, int oob_required, int page)
  923. {
  924. chip->read_buf(mtd, buf, mtd->writesize);
  925. if (oob_required)
  926. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  927. return 0;
  928. }
  929. /**
  930. * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
  931. * @mtd: mtd info structure
  932. * @chip: nand chip info structure
  933. * @buf: buffer to store read data
  934. * @oob_required: caller requires OOB data read to chip->oob_poi
  935. * @page: page number to read
  936. *
  937. * We need a special oob layout and handling even when OOB isn't used.
  938. */
  939. static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
  940. struct nand_chip *chip, uint8_t *buf,
  941. int oob_required, int page)
  942. {
  943. int eccsize = chip->ecc.size;
  944. int eccbytes = chip->ecc.bytes;
  945. uint8_t *oob = chip->oob_poi;
  946. int steps, size;
  947. for (steps = chip->ecc.steps; steps > 0; steps--) {
  948. chip->read_buf(mtd, buf, eccsize);
  949. buf += eccsize;
  950. if (chip->ecc.prepad) {
  951. chip->read_buf(mtd, oob, chip->ecc.prepad);
  952. oob += chip->ecc.prepad;
  953. }
  954. chip->read_buf(mtd, oob, eccbytes);
  955. oob += eccbytes;
  956. if (chip->ecc.postpad) {
  957. chip->read_buf(mtd, oob, chip->ecc.postpad);
  958. oob += chip->ecc.postpad;
  959. }
  960. }
  961. size = mtd->oobsize - (oob - chip->oob_poi);
  962. if (size)
  963. chip->read_buf(mtd, oob, size);
  964. return 0;
  965. }
  966. /**
  967. * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
  968. * @mtd: mtd info structure
  969. * @chip: nand chip info structure
  970. * @buf: buffer to store read data
  971. * @oob_required: caller requires OOB data read to chip->oob_poi
  972. * @page: page number to read
  973. */
  974. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  975. uint8_t *buf, int oob_required, int page)
  976. {
  977. int i, eccsize = chip->ecc.size;
  978. int eccbytes = chip->ecc.bytes;
  979. int eccsteps = chip->ecc.steps;
  980. uint8_t *p = buf;
  981. uint8_t *ecc_calc = chip->buffers->ecccalc;
  982. uint8_t *ecc_code = chip->buffers->ecccode;
  983. uint32_t *eccpos = chip->ecc.layout->eccpos;
  984. unsigned int max_bitflips = 0;
  985. chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
  986. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  987. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  988. for (i = 0; i < chip->ecc.total; i++)
  989. ecc_code[i] = chip->oob_poi[eccpos[i]];
  990. eccsteps = chip->ecc.steps;
  991. p = buf;
  992. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  993. int stat;
  994. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  995. if (stat < 0) {
  996. mtd->ecc_stats.failed++;
  997. } else {
  998. mtd->ecc_stats.corrected += stat;
  999. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1000. }
  1001. }
  1002. return max_bitflips;
  1003. }
  1004. /**
  1005. * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
  1006. * @mtd: mtd info structure
  1007. * @chip: nand chip info structure
  1008. * @data_offs: offset of requested data within the page
  1009. * @readlen: data length
  1010. * @bufpoi: buffer to store read data
  1011. * @page: page number to read
  1012. */
  1013. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1014. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
  1015. int page)
  1016. {
  1017. int start_step, end_step, num_steps;
  1018. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1019. uint8_t *p;
  1020. int data_col_addr, i, gaps = 0;
  1021. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1022. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1023. int index;
  1024. unsigned int max_bitflips = 0;
  1025. /* Column address within the page aligned to ECC size (256bytes) */
  1026. start_step = data_offs / chip->ecc.size;
  1027. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1028. num_steps = end_step - start_step + 1;
  1029. index = start_step * chip->ecc.bytes;
  1030. /* Data size aligned to ECC ecc.size */
  1031. datafrag_len = num_steps * chip->ecc.size;
  1032. eccfrag_len = num_steps * chip->ecc.bytes;
  1033. data_col_addr = start_step * chip->ecc.size;
  1034. /* If we read not a page aligned data */
  1035. if (data_col_addr != 0)
  1036. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1037. p = bufpoi + data_col_addr;
  1038. chip->read_buf(mtd, p, datafrag_len);
  1039. /* Calculate ECC */
  1040. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1041. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1042. /*
  1043. * The performance is faster if we position offsets according to
  1044. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1045. */
  1046. for (i = 0; i < eccfrag_len - 1; i++) {
  1047. if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
  1048. gaps = 1;
  1049. break;
  1050. }
  1051. }
  1052. if (gaps) {
  1053. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1054. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1055. } else {
  1056. /*
  1057. * Send the command to read the particular ECC bytes take care
  1058. * about buswidth alignment in read_buf.
  1059. */
  1060. aligned_pos = eccpos[index] & ~(busw - 1);
  1061. aligned_len = eccfrag_len;
  1062. if (eccpos[index] & (busw - 1))
  1063. aligned_len++;
  1064. if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1065. aligned_len++;
  1066. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1067. mtd->writesize + aligned_pos, -1);
  1068. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1069. }
  1070. for (i = 0; i < eccfrag_len; i++)
  1071. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1072. p = bufpoi + data_col_addr;
  1073. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1074. int stat;
  1075. stat = chip->ecc.correct(mtd, p,
  1076. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1077. if (stat == -EBADMSG &&
  1078. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1079. /* check for empty pages with bitflips */
  1080. stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1081. &chip->buffers->ecccode[i],
  1082. chip->ecc.bytes,
  1083. NULL, 0,
  1084. chip->ecc.strength);
  1085. }
  1086. if (stat < 0) {
  1087. mtd->ecc_stats.failed++;
  1088. } else {
  1089. mtd->ecc_stats.corrected += stat;
  1090. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1091. }
  1092. }
  1093. return max_bitflips;
  1094. }
  1095. /**
  1096. * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
  1097. * @mtd: mtd info structure
  1098. * @chip: nand chip info structure
  1099. * @buf: buffer to store read data
  1100. * @oob_required: caller requires OOB data read to chip->oob_poi
  1101. * @page: page number to read
  1102. *
  1103. * Not for syndrome calculating ECC controllers which need a special oob layout.
  1104. */
  1105. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1106. uint8_t *buf, int oob_required, int page)
  1107. {
  1108. int i, eccsize = chip->ecc.size;
  1109. int eccbytes = chip->ecc.bytes;
  1110. int eccsteps = chip->ecc.steps;
  1111. uint8_t *p = buf;
  1112. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1113. uint8_t *ecc_code = chip->buffers->ecccode;
  1114. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1115. unsigned int max_bitflips = 0;
  1116. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1117. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1118. chip->read_buf(mtd, p, eccsize);
  1119. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1120. }
  1121. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1122. for (i = 0; i < chip->ecc.total; i++)
  1123. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1124. eccsteps = chip->ecc.steps;
  1125. p = buf;
  1126. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1127. int stat;
  1128. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1129. if (stat == -EBADMSG &&
  1130. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1131. /* check for empty pages with bitflips */
  1132. stat = nand_check_erased_ecc_chunk(p, eccsize,
  1133. &ecc_code[i], eccbytes,
  1134. NULL, 0,
  1135. chip->ecc.strength);
  1136. }
  1137. if (stat < 0) {
  1138. mtd->ecc_stats.failed++;
  1139. } else {
  1140. mtd->ecc_stats.corrected += stat;
  1141. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1142. }
  1143. }
  1144. return max_bitflips;
  1145. }
  1146. /**
  1147. * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
  1148. * @mtd: mtd info structure
  1149. * @chip: nand chip info structure
  1150. * @buf: buffer to store read data
  1151. * @oob_required: caller requires OOB data read to chip->oob_poi
  1152. * @page: page number to read
  1153. *
  1154. * Hardware ECC for large page chips, require OOB to be read first. For this
  1155. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1156. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1157. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1158. * the data area, by overwriting the NAND manufacturer bad block markings.
  1159. */
  1160. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1161. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  1162. {
  1163. int i, eccsize = chip->ecc.size;
  1164. int eccbytes = chip->ecc.bytes;
  1165. int eccsteps = chip->ecc.steps;
  1166. uint8_t *p = buf;
  1167. uint8_t *ecc_code = chip->buffers->ecccode;
  1168. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1169. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1170. unsigned int max_bitflips = 0;
  1171. /* Read the OOB area first */
  1172. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1173. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1174. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1175. for (i = 0; i < chip->ecc.total; i++)
  1176. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1177. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1178. int stat;
  1179. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1180. chip->read_buf(mtd, p, eccsize);
  1181. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1182. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1183. if (stat == -EBADMSG &&
  1184. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1185. /* check for empty pages with bitflips */
  1186. stat = nand_check_erased_ecc_chunk(p, eccsize,
  1187. &ecc_code[i], eccbytes,
  1188. NULL, 0,
  1189. chip->ecc.strength);
  1190. }
  1191. if (stat < 0) {
  1192. mtd->ecc_stats.failed++;
  1193. } else {
  1194. mtd->ecc_stats.corrected += stat;
  1195. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1196. }
  1197. }
  1198. return max_bitflips;
  1199. }
  1200. /**
  1201. * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
  1202. * @mtd: mtd info structure
  1203. * @chip: nand chip info structure
  1204. * @buf: buffer to store read data
  1205. * @oob_required: caller requires OOB data read to chip->oob_poi
  1206. * @page: page number to read
  1207. *
  1208. * The hw generator calculates the error syndrome automatically. Therefore we
  1209. * need a special oob layout and handling.
  1210. */
  1211. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1212. uint8_t *buf, int oob_required, int page)
  1213. {
  1214. int i, eccsize = chip->ecc.size;
  1215. int eccbytes = chip->ecc.bytes;
  1216. int eccsteps = chip->ecc.steps;
  1217. int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  1218. uint8_t *p = buf;
  1219. uint8_t *oob = chip->oob_poi;
  1220. unsigned int max_bitflips = 0;
  1221. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1222. int stat;
  1223. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1224. chip->read_buf(mtd, p, eccsize);
  1225. if (chip->ecc.prepad) {
  1226. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1227. oob += chip->ecc.prepad;
  1228. }
  1229. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1230. chip->read_buf(mtd, oob, eccbytes);
  1231. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1232. oob += eccbytes;
  1233. if (chip->ecc.postpad) {
  1234. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1235. oob += chip->ecc.postpad;
  1236. }
  1237. if (stat == -EBADMSG &&
  1238. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1239. /* check for empty pages with bitflips */
  1240. stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1241. oob - eccpadbytes,
  1242. eccpadbytes,
  1243. NULL, 0,
  1244. chip->ecc.strength);
  1245. }
  1246. if (stat < 0) {
  1247. mtd->ecc_stats.failed++;
  1248. } else {
  1249. mtd->ecc_stats.corrected += stat;
  1250. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1251. }
  1252. }
  1253. /* Calculate remaining oob bytes */
  1254. i = mtd->oobsize - (oob - chip->oob_poi);
  1255. if (i)
  1256. chip->read_buf(mtd, oob, i);
  1257. return max_bitflips;
  1258. }
  1259. /**
  1260. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  1261. * @chip: nand chip structure
  1262. * @oob: oob destination address
  1263. * @ops: oob ops structure
  1264. * @len: size of oob to transfer
  1265. */
  1266. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1267. struct mtd_oob_ops *ops, size_t len)
  1268. {
  1269. switch (ops->mode) {
  1270. case MTD_OPS_PLACE_OOB:
  1271. case MTD_OPS_RAW:
  1272. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1273. return oob + len;
  1274. case MTD_OPS_AUTO_OOB: {
  1275. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1276. uint32_t boffs = 0, roffs = ops->ooboffs;
  1277. size_t bytes = 0;
  1278. for (; free->length && len; free++, len -= bytes) {
  1279. /* Read request not from offset 0? */
  1280. if (unlikely(roffs)) {
  1281. if (roffs >= free->length) {
  1282. roffs -= free->length;
  1283. continue;
  1284. }
  1285. boffs = free->offset + roffs;
  1286. bytes = min_t(size_t, len,
  1287. (free->length - roffs));
  1288. roffs = 0;
  1289. } else {
  1290. bytes = min_t(size_t, len, free->length);
  1291. boffs = free->offset;
  1292. }
  1293. memcpy(oob, chip->oob_poi + boffs, bytes);
  1294. oob += bytes;
  1295. }
  1296. return oob;
  1297. }
  1298. default:
  1299. BUG();
  1300. }
  1301. return NULL;
  1302. }
  1303. /**
  1304. * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
  1305. * @mtd: MTD device structure
  1306. * @retry_mode: the retry mode to use
  1307. *
  1308. * Some vendors supply a special command to shift the Vt threshold, to be used
  1309. * when there are too many bitflips in a page (i.e., ECC error). After setting
  1310. * a new threshold, the host should retry reading the page.
  1311. */
  1312. static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
  1313. {
  1314. struct nand_chip *chip = mtd_to_nand(mtd);
  1315. pr_debug("setting READ RETRY mode %d\n", retry_mode);
  1316. if (retry_mode >= chip->read_retries)
  1317. return -EINVAL;
  1318. if (!chip->setup_read_retry)
  1319. return -EOPNOTSUPP;
  1320. return chip->setup_read_retry(mtd, retry_mode);
  1321. }
  1322. /**
  1323. * nand_do_read_ops - [INTERN] Read data with ECC
  1324. * @mtd: MTD device structure
  1325. * @from: offset to read from
  1326. * @ops: oob ops structure
  1327. *
  1328. * Internal function. Called with chip held.
  1329. */
  1330. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1331. struct mtd_oob_ops *ops)
  1332. {
  1333. int chipnr, page, realpage, col, bytes, aligned, oob_required;
  1334. struct nand_chip *chip = mtd_to_nand(mtd);
  1335. int ret = 0;
  1336. uint32_t readlen = ops->len;
  1337. uint32_t oobreadlen = ops->ooblen;
  1338. uint32_t max_oobsize = mtd_oobavail(mtd, ops);
  1339. uint8_t *bufpoi, *oob, *buf;
  1340. int use_bufpoi;
  1341. unsigned int max_bitflips = 0;
  1342. int retry_mode = 0;
  1343. bool ecc_fail = false;
  1344. chipnr = (int)(from >> chip->chip_shift);
  1345. chip->select_chip(mtd, chipnr);
  1346. realpage = (int)(from >> chip->page_shift);
  1347. page = realpage & chip->pagemask;
  1348. col = (int)(from & (mtd->writesize - 1));
  1349. buf = ops->datbuf;
  1350. oob = ops->oobbuf;
  1351. oob_required = oob ? 1 : 0;
  1352. while (1) {
  1353. unsigned int ecc_failures = mtd->ecc_stats.failed;
  1354. WATCHDOG_RESET();
  1355. bytes = min(mtd->writesize - col, readlen);
  1356. aligned = (bytes == mtd->writesize);
  1357. if (!aligned)
  1358. use_bufpoi = 1;
  1359. else
  1360. use_bufpoi = 0;
  1361. /* Is the current page in the buffer? */
  1362. if (realpage != chip->pagebuf || oob) {
  1363. bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
  1364. if (use_bufpoi && aligned)
  1365. pr_debug("%s: using read bounce buffer for buf@%p\n",
  1366. __func__, buf);
  1367. read_retry:
  1368. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1369. /*
  1370. * Now read the page into the buffer. Absent an error,
  1371. * the read methods return max bitflips per ecc step.
  1372. */
  1373. if (unlikely(ops->mode == MTD_OPS_RAW))
  1374. ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
  1375. oob_required,
  1376. page);
  1377. else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
  1378. !oob)
  1379. ret = chip->ecc.read_subpage(mtd, chip,
  1380. col, bytes, bufpoi,
  1381. page);
  1382. else
  1383. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1384. oob_required, page);
  1385. if (ret < 0) {
  1386. if (use_bufpoi)
  1387. /* Invalidate page cache */
  1388. chip->pagebuf = -1;
  1389. break;
  1390. }
  1391. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  1392. /* Transfer not aligned data */
  1393. if (use_bufpoi) {
  1394. if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
  1395. !(mtd->ecc_stats.failed - ecc_failures) &&
  1396. (ops->mode != MTD_OPS_RAW)) {
  1397. chip->pagebuf = realpage;
  1398. chip->pagebuf_bitflips = ret;
  1399. } else {
  1400. /* Invalidate page cache */
  1401. chip->pagebuf = -1;
  1402. }
  1403. memcpy(buf, chip->buffers->databuf + col, bytes);
  1404. }
  1405. if (unlikely(oob)) {
  1406. int toread = min(oobreadlen, max_oobsize);
  1407. if (toread) {
  1408. oob = nand_transfer_oob(chip,
  1409. oob, ops, toread);
  1410. oobreadlen -= toread;
  1411. }
  1412. }
  1413. if (chip->options & NAND_NEED_READRDY) {
  1414. /* Apply delay or wait for ready/busy pin */
  1415. if (!chip->dev_ready)
  1416. udelay(chip->chip_delay);
  1417. else
  1418. nand_wait_ready(mtd);
  1419. }
  1420. if (mtd->ecc_stats.failed - ecc_failures) {
  1421. if (retry_mode + 1 < chip->read_retries) {
  1422. retry_mode++;
  1423. ret = nand_setup_read_retry(mtd,
  1424. retry_mode);
  1425. if (ret < 0)
  1426. break;
  1427. /* Reset failures; retry */
  1428. mtd->ecc_stats.failed = ecc_failures;
  1429. goto read_retry;
  1430. } else {
  1431. /* No more retry modes; real failure */
  1432. ecc_fail = true;
  1433. }
  1434. }
  1435. buf += bytes;
  1436. } else {
  1437. memcpy(buf, chip->buffers->databuf + col, bytes);
  1438. buf += bytes;
  1439. max_bitflips = max_t(unsigned int, max_bitflips,
  1440. chip->pagebuf_bitflips);
  1441. }
  1442. readlen -= bytes;
  1443. /* Reset to retry mode 0 */
  1444. if (retry_mode) {
  1445. ret = nand_setup_read_retry(mtd, 0);
  1446. if (ret < 0)
  1447. break;
  1448. retry_mode = 0;
  1449. }
  1450. if (!readlen)
  1451. break;
  1452. /* For subsequent reads align to page boundary */
  1453. col = 0;
  1454. /* Increment page address */
  1455. realpage++;
  1456. page = realpage & chip->pagemask;
  1457. /* Check, if we cross a chip boundary */
  1458. if (!page) {
  1459. chipnr++;
  1460. chip->select_chip(mtd, -1);
  1461. chip->select_chip(mtd, chipnr);
  1462. }
  1463. }
  1464. chip->select_chip(mtd, -1);
  1465. ops->retlen = ops->len - (size_t) readlen;
  1466. if (oob)
  1467. ops->oobretlen = ops->ooblen - oobreadlen;
  1468. if (ret < 0)
  1469. return ret;
  1470. if (ecc_fail)
  1471. return -EBADMSG;
  1472. return max_bitflips;
  1473. }
  1474. /**
  1475. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1476. * @mtd: MTD device structure
  1477. * @from: offset to read from
  1478. * @len: number of bytes to read
  1479. * @retlen: pointer to variable to store the number of read bytes
  1480. * @buf: the databuffer to put data
  1481. *
  1482. * Get hold of the chip and call nand_do_read.
  1483. */
  1484. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1485. size_t *retlen, uint8_t *buf)
  1486. {
  1487. struct mtd_oob_ops ops;
  1488. int ret;
  1489. nand_get_device(mtd, FL_READING);
  1490. memset(&ops, 0, sizeof(ops));
  1491. ops.len = len;
  1492. ops.datbuf = buf;
  1493. ops.mode = MTD_OPS_PLACE_OOB;
  1494. ret = nand_do_read_ops(mtd, from, &ops);
  1495. *retlen = ops.retlen;
  1496. nand_release_device(mtd);
  1497. return ret;
  1498. }
  1499. /**
  1500. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1501. * @mtd: mtd info structure
  1502. * @chip: nand chip info structure
  1503. * @page: page number to read
  1504. */
  1505. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1506. int page)
  1507. {
  1508. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1509. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1510. return 0;
  1511. }
  1512. /**
  1513. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1514. * with syndromes
  1515. * @mtd: mtd info structure
  1516. * @chip: nand chip info structure
  1517. * @page: page number to read
  1518. */
  1519. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1520. int page)
  1521. {
  1522. int length = mtd->oobsize;
  1523. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1524. int eccsize = chip->ecc.size;
  1525. uint8_t *bufpoi = chip->oob_poi;
  1526. int i, toread, sndrnd = 0, pos;
  1527. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1528. for (i = 0; i < chip->ecc.steps; i++) {
  1529. if (sndrnd) {
  1530. pos = eccsize + i * (eccsize + chunk);
  1531. if (mtd->writesize > 512)
  1532. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1533. else
  1534. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1535. } else
  1536. sndrnd = 1;
  1537. toread = min_t(int, length, chunk);
  1538. chip->read_buf(mtd, bufpoi, toread);
  1539. bufpoi += toread;
  1540. length -= toread;
  1541. }
  1542. if (length > 0)
  1543. chip->read_buf(mtd, bufpoi, length);
  1544. return 0;
  1545. }
  1546. /**
  1547. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1548. * @mtd: mtd info structure
  1549. * @chip: nand chip info structure
  1550. * @page: page number to write
  1551. */
  1552. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1553. int page)
  1554. {
  1555. int status = 0;
  1556. const uint8_t *buf = chip->oob_poi;
  1557. int length = mtd->oobsize;
  1558. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1559. chip->write_buf(mtd, buf, length);
  1560. /* Send command to program the OOB data */
  1561. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1562. status = chip->waitfunc(mtd, chip);
  1563. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1564. }
  1565. /**
  1566. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1567. * with syndrome - only for large page flash
  1568. * @mtd: mtd info structure
  1569. * @chip: nand chip info structure
  1570. * @page: page number to write
  1571. */
  1572. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1573. struct nand_chip *chip, int page)
  1574. {
  1575. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1576. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1577. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1578. const uint8_t *bufpoi = chip->oob_poi;
  1579. /*
  1580. * data-ecc-data-ecc ... ecc-oob
  1581. * or
  1582. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1583. */
  1584. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1585. pos = steps * (eccsize + chunk);
  1586. steps = 0;
  1587. } else
  1588. pos = eccsize;
  1589. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1590. for (i = 0; i < steps; i++) {
  1591. if (sndcmd) {
  1592. if (mtd->writesize <= 512) {
  1593. uint32_t fill = 0xFFFFFFFF;
  1594. len = eccsize;
  1595. while (len > 0) {
  1596. int num = min_t(int, len, 4);
  1597. chip->write_buf(mtd, (uint8_t *)&fill,
  1598. num);
  1599. len -= num;
  1600. }
  1601. } else {
  1602. pos = eccsize + i * (eccsize + chunk);
  1603. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1604. }
  1605. } else
  1606. sndcmd = 1;
  1607. len = min_t(int, length, chunk);
  1608. chip->write_buf(mtd, bufpoi, len);
  1609. bufpoi += len;
  1610. length -= len;
  1611. }
  1612. if (length > 0)
  1613. chip->write_buf(mtd, bufpoi, length);
  1614. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1615. status = chip->waitfunc(mtd, chip);
  1616. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1617. }
  1618. /**
  1619. * nand_do_read_oob - [INTERN] NAND read out-of-band
  1620. * @mtd: MTD device structure
  1621. * @from: offset to read from
  1622. * @ops: oob operations description structure
  1623. *
  1624. * NAND read out-of-band data from the spare area.
  1625. */
  1626. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1627. struct mtd_oob_ops *ops)
  1628. {
  1629. int page, realpage, chipnr;
  1630. struct nand_chip *chip = mtd_to_nand(mtd);
  1631. struct mtd_ecc_stats stats;
  1632. int readlen = ops->ooblen;
  1633. int len;
  1634. uint8_t *buf = ops->oobbuf;
  1635. int ret = 0;
  1636. pr_debug("%s: from = 0x%08Lx, len = %i\n",
  1637. __func__, (unsigned long long)from, readlen);
  1638. stats = mtd->ecc_stats;
  1639. len = mtd_oobavail(mtd, ops);
  1640. if (unlikely(ops->ooboffs >= len)) {
  1641. pr_debug("%s: attempt to start read outside oob\n",
  1642. __func__);
  1643. return -EINVAL;
  1644. }
  1645. /* Do not allow reads past end of device */
  1646. if (unlikely(from >= mtd->size ||
  1647. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1648. (from >> chip->page_shift)) * len)) {
  1649. pr_debug("%s: attempt to read beyond end of device\n",
  1650. __func__);
  1651. return -EINVAL;
  1652. }
  1653. chipnr = (int)(from >> chip->chip_shift);
  1654. chip->select_chip(mtd, chipnr);
  1655. /* Shift to get page */
  1656. realpage = (int)(from >> chip->page_shift);
  1657. page = realpage & chip->pagemask;
  1658. while (1) {
  1659. WATCHDOG_RESET();
  1660. if (ops->mode == MTD_OPS_RAW)
  1661. ret = chip->ecc.read_oob_raw(mtd, chip, page);
  1662. else
  1663. ret = chip->ecc.read_oob(mtd, chip, page);
  1664. if (ret < 0)
  1665. break;
  1666. len = min(len, readlen);
  1667. buf = nand_transfer_oob(chip, buf, ops, len);
  1668. if (chip->options & NAND_NEED_READRDY) {
  1669. /* Apply delay or wait for ready/busy pin */
  1670. if (!chip->dev_ready)
  1671. udelay(chip->chip_delay);
  1672. else
  1673. nand_wait_ready(mtd);
  1674. }
  1675. readlen -= len;
  1676. if (!readlen)
  1677. break;
  1678. /* Increment page address */
  1679. realpage++;
  1680. page = realpage & chip->pagemask;
  1681. /* Check, if we cross a chip boundary */
  1682. if (!page) {
  1683. chipnr++;
  1684. chip->select_chip(mtd, -1);
  1685. chip->select_chip(mtd, chipnr);
  1686. }
  1687. }
  1688. chip->select_chip(mtd, -1);
  1689. ops->oobretlen = ops->ooblen - readlen;
  1690. if (ret < 0)
  1691. return ret;
  1692. if (mtd->ecc_stats.failed - stats.failed)
  1693. return -EBADMSG;
  1694. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1695. }
  1696. /**
  1697. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1698. * @mtd: MTD device structure
  1699. * @from: offset to read from
  1700. * @ops: oob operation description structure
  1701. *
  1702. * NAND read data and/or out-of-band data.
  1703. */
  1704. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1705. struct mtd_oob_ops *ops)
  1706. {
  1707. int ret = -ENOTSUPP;
  1708. ops->retlen = 0;
  1709. /* Do not allow reads past end of device */
  1710. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1711. pr_debug("%s: attempt to read beyond end of device\n",
  1712. __func__);
  1713. return -EINVAL;
  1714. }
  1715. nand_get_device(mtd, FL_READING);
  1716. switch (ops->mode) {
  1717. case MTD_OPS_PLACE_OOB:
  1718. case MTD_OPS_AUTO_OOB:
  1719. case MTD_OPS_RAW:
  1720. break;
  1721. default:
  1722. goto out;
  1723. }
  1724. if (!ops->datbuf)
  1725. ret = nand_do_read_oob(mtd, from, ops);
  1726. else
  1727. ret = nand_do_read_ops(mtd, from, ops);
  1728. out:
  1729. nand_release_device(mtd);
  1730. return ret;
  1731. }
  1732. /**
  1733. * nand_write_page_raw - [INTERN] raw page write function
  1734. * @mtd: mtd info structure
  1735. * @chip: nand chip info structure
  1736. * @buf: data buffer
  1737. * @oob_required: must write chip->oob_poi to OOB
  1738. * @page: page number to write
  1739. *
  1740. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1741. */
  1742. static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1743. const uint8_t *buf, int oob_required, int page)
  1744. {
  1745. chip->write_buf(mtd, buf, mtd->writesize);
  1746. if (oob_required)
  1747. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1748. return 0;
  1749. }
  1750. /**
  1751. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  1752. * @mtd: mtd info structure
  1753. * @chip: nand chip info structure
  1754. * @buf: data buffer
  1755. * @oob_required: must write chip->oob_poi to OOB
  1756. * @page: page number to write
  1757. *
  1758. * We need a special oob layout and handling even when ECC isn't checked.
  1759. */
  1760. static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1761. struct nand_chip *chip,
  1762. const uint8_t *buf, int oob_required,
  1763. int page)
  1764. {
  1765. int eccsize = chip->ecc.size;
  1766. int eccbytes = chip->ecc.bytes;
  1767. uint8_t *oob = chip->oob_poi;
  1768. int steps, size;
  1769. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1770. chip->write_buf(mtd, buf, eccsize);
  1771. buf += eccsize;
  1772. if (chip->ecc.prepad) {
  1773. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1774. oob += chip->ecc.prepad;
  1775. }
  1776. chip->write_buf(mtd, oob, eccbytes);
  1777. oob += eccbytes;
  1778. if (chip->ecc.postpad) {
  1779. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1780. oob += chip->ecc.postpad;
  1781. }
  1782. }
  1783. size = mtd->oobsize - (oob - chip->oob_poi);
  1784. if (size)
  1785. chip->write_buf(mtd, oob, size);
  1786. return 0;
  1787. }
  1788. /**
  1789. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  1790. * @mtd: mtd info structure
  1791. * @chip: nand chip info structure
  1792. * @buf: data buffer
  1793. * @oob_required: must write chip->oob_poi to OOB
  1794. * @page: page number to write
  1795. */
  1796. static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1797. const uint8_t *buf, int oob_required,
  1798. int page)
  1799. {
  1800. int i, eccsize = chip->ecc.size;
  1801. int eccbytes = chip->ecc.bytes;
  1802. int eccsteps = chip->ecc.steps;
  1803. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1804. const uint8_t *p = buf;
  1805. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1806. /* Software ECC calculation */
  1807. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1808. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1809. for (i = 0; i < chip->ecc.total; i++)
  1810. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1811. return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
  1812. }
  1813. /**
  1814. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  1815. * @mtd: mtd info structure
  1816. * @chip: nand chip info structure
  1817. * @buf: data buffer
  1818. * @oob_required: must write chip->oob_poi to OOB
  1819. * @page: page number to write
  1820. */
  1821. static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1822. const uint8_t *buf, int oob_required,
  1823. int page)
  1824. {
  1825. int i, eccsize = chip->ecc.size;
  1826. int eccbytes = chip->ecc.bytes;
  1827. int eccsteps = chip->ecc.steps;
  1828. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1829. const uint8_t *p = buf;
  1830. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1831. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1832. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1833. chip->write_buf(mtd, p, eccsize);
  1834. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1835. }
  1836. for (i = 0; i < chip->ecc.total; i++)
  1837. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1838. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1839. return 0;
  1840. }
  1841. /**
  1842. * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
  1843. * @mtd: mtd info structure
  1844. * @chip: nand chip info structure
  1845. * @offset: column address of subpage within the page
  1846. * @data_len: data length
  1847. * @buf: data buffer
  1848. * @oob_required: must write chip->oob_poi to OOB
  1849. * @page: page number to write
  1850. */
  1851. static int nand_write_subpage_hwecc(struct mtd_info *mtd,
  1852. struct nand_chip *chip, uint32_t offset,
  1853. uint32_t data_len, const uint8_t *buf,
  1854. int oob_required, int page)
  1855. {
  1856. uint8_t *oob_buf = chip->oob_poi;
  1857. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1858. int ecc_size = chip->ecc.size;
  1859. int ecc_bytes = chip->ecc.bytes;
  1860. int ecc_steps = chip->ecc.steps;
  1861. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1862. uint32_t start_step = offset / ecc_size;
  1863. uint32_t end_step = (offset + data_len - 1) / ecc_size;
  1864. int oob_bytes = mtd->oobsize / ecc_steps;
  1865. int step, i;
  1866. for (step = 0; step < ecc_steps; step++) {
  1867. /* configure controller for WRITE access */
  1868. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1869. /* write data (untouched subpages already masked by 0xFF) */
  1870. chip->write_buf(mtd, buf, ecc_size);
  1871. /* mask ECC of un-touched subpages by padding 0xFF */
  1872. if ((step < start_step) || (step > end_step))
  1873. memset(ecc_calc, 0xff, ecc_bytes);
  1874. else
  1875. chip->ecc.calculate(mtd, buf, ecc_calc);
  1876. /* mask OOB of un-touched subpages by padding 0xFF */
  1877. /* if oob_required, preserve OOB metadata of written subpage */
  1878. if (!oob_required || (step < start_step) || (step > end_step))
  1879. memset(oob_buf, 0xff, oob_bytes);
  1880. buf += ecc_size;
  1881. ecc_calc += ecc_bytes;
  1882. oob_buf += oob_bytes;
  1883. }
  1884. /* copy calculated ECC for whole page to chip->buffer->oob */
  1885. /* this include masked-value(0xFF) for unwritten subpages */
  1886. ecc_calc = chip->buffers->ecccalc;
  1887. for (i = 0; i < chip->ecc.total; i++)
  1888. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1889. /* write OOB buffer to NAND device */
  1890. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1891. return 0;
  1892. }
  1893. /**
  1894. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  1895. * @mtd: mtd info structure
  1896. * @chip: nand chip info structure
  1897. * @buf: data buffer
  1898. * @oob_required: must write chip->oob_poi to OOB
  1899. * @page: page number to write
  1900. *
  1901. * The hw generator calculates the error syndrome automatically. Therefore we
  1902. * need a special oob layout and handling.
  1903. */
  1904. static int nand_write_page_syndrome(struct mtd_info *mtd,
  1905. struct nand_chip *chip,
  1906. const uint8_t *buf, int oob_required,
  1907. int page)
  1908. {
  1909. int i, eccsize = chip->ecc.size;
  1910. int eccbytes = chip->ecc.bytes;
  1911. int eccsteps = chip->ecc.steps;
  1912. const uint8_t *p = buf;
  1913. uint8_t *oob = chip->oob_poi;
  1914. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1915. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1916. chip->write_buf(mtd, p, eccsize);
  1917. if (chip->ecc.prepad) {
  1918. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1919. oob += chip->ecc.prepad;
  1920. }
  1921. chip->ecc.calculate(mtd, p, oob);
  1922. chip->write_buf(mtd, oob, eccbytes);
  1923. oob += eccbytes;
  1924. if (chip->ecc.postpad) {
  1925. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1926. oob += chip->ecc.postpad;
  1927. }
  1928. }
  1929. /* Calculate remaining oob bytes */
  1930. i = mtd->oobsize - (oob - chip->oob_poi);
  1931. if (i)
  1932. chip->write_buf(mtd, oob, i);
  1933. return 0;
  1934. }
  1935. /**
  1936. * nand_write_page - [REPLACEABLE] write one page
  1937. * @mtd: MTD device structure
  1938. * @chip: NAND chip descriptor
  1939. * @offset: address offset within the page
  1940. * @data_len: length of actual data to be written
  1941. * @buf: the data to write
  1942. * @oob_required: must write chip->oob_poi to OOB
  1943. * @page: page number to write
  1944. * @cached: cached programming
  1945. * @raw: use _raw version of write_page
  1946. */
  1947. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1948. uint32_t offset, int data_len, const uint8_t *buf,
  1949. int oob_required, int page, int cached, int raw)
  1950. {
  1951. int status, subpage;
  1952. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  1953. chip->ecc.write_subpage)
  1954. subpage = offset || (data_len < mtd->writesize);
  1955. else
  1956. subpage = 0;
  1957. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1958. if (unlikely(raw))
  1959. status = chip->ecc.write_page_raw(mtd, chip, buf,
  1960. oob_required, page);
  1961. else if (subpage)
  1962. status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
  1963. buf, oob_required, page);
  1964. else
  1965. status = chip->ecc.write_page(mtd, chip, buf, oob_required,
  1966. page);
  1967. if (status < 0)
  1968. return status;
  1969. /*
  1970. * Cached progamming disabled for now. Not sure if it's worth the
  1971. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  1972. */
  1973. cached = 0;
  1974. if (!cached || !NAND_HAS_CACHEPROG(chip)) {
  1975. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1976. status = chip->waitfunc(mtd, chip);
  1977. /*
  1978. * See if operation failed and additional status checks are
  1979. * available.
  1980. */
  1981. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1982. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1983. page);
  1984. if (status & NAND_STATUS_FAIL)
  1985. return -EIO;
  1986. } else {
  1987. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1988. status = chip->waitfunc(mtd, chip);
  1989. }
  1990. return 0;
  1991. }
  1992. /**
  1993. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  1994. * @mtd: MTD device structure
  1995. * @oob: oob data buffer
  1996. * @len: oob data write length
  1997. * @ops: oob ops structure
  1998. */
  1999. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  2000. struct mtd_oob_ops *ops)
  2001. {
  2002. struct nand_chip *chip = mtd_to_nand(mtd);
  2003. /*
  2004. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  2005. * data from a previous OOB read.
  2006. */
  2007. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2008. switch (ops->mode) {
  2009. case MTD_OPS_PLACE_OOB:
  2010. case MTD_OPS_RAW:
  2011. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  2012. return oob + len;
  2013. case MTD_OPS_AUTO_OOB: {
  2014. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  2015. uint32_t boffs = 0, woffs = ops->ooboffs;
  2016. size_t bytes = 0;
  2017. for (; free->length && len; free++, len -= bytes) {
  2018. /* Write request not from offset 0? */
  2019. if (unlikely(woffs)) {
  2020. if (woffs >= free->length) {
  2021. woffs -= free->length;
  2022. continue;
  2023. }
  2024. boffs = free->offset + woffs;
  2025. bytes = min_t(size_t, len,
  2026. (free->length - woffs));
  2027. woffs = 0;
  2028. } else {
  2029. bytes = min_t(size_t, len, free->length);
  2030. boffs = free->offset;
  2031. }
  2032. memcpy(chip->oob_poi + boffs, oob, bytes);
  2033. oob += bytes;
  2034. }
  2035. return oob;
  2036. }
  2037. default:
  2038. BUG();
  2039. }
  2040. return NULL;
  2041. }
  2042. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  2043. /**
  2044. * nand_do_write_ops - [INTERN] NAND write with ECC
  2045. * @mtd: MTD device structure
  2046. * @to: offset to write to
  2047. * @ops: oob operations description structure
  2048. *
  2049. * NAND write with ECC.
  2050. */
  2051. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  2052. struct mtd_oob_ops *ops)
  2053. {
  2054. int chipnr, realpage, page, blockmask, column;
  2055. struct nand_chip *chip = mtd_to_nand(mtd);
  2056. uint32_t writelen = ops->len;
  2057. uint32_t oobwritelen = ops->ooblen;
  2058. uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
  2059. uint8_t *oob = ops->oobbuf;
  2060. uint8_t *buf = ops->datbuf;
  2061. int ret;
  2062. int oob_required = oob ? 1 : 0;
  2063. ops->retlen = 0;
  2064. if (!writelen)
  2065. return 0;
  2066. /* Reject writes, which are not page aligned */
  2067. if (NOTALIGNED(to)) {
  2068. pr_notice("%s: attempt to write non page aligned data\n",
  2069. __func__);
  2070. return -EINVAL;
  2071. }
  2072. column = to & (mtd->writesize - 1);
  2073. chipnr = (int)(to >> chip->chip_shift);
  2074. chip->select_chip(mtd, chipnr);
  2075. /* Check, if it is write protected */
  2076. if (nand_check_wp(mtd)) {
  2077. ret = -EIO;
  2078. goto err_out;
  2079. }
  2080. realpage = (int)(to >> chip->page_shift);
  2081. page = realpage & chip->pagemask;
  2082. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  2083. /* Invalidate the page cache, when we write to the cached page */
  2084. if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
  2085. ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
  2086. chip->pagebuf = -1;
  2087. /* Don't allow multipage oob writes with offset */
  2088. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
  2089. ret = -EINVAL;
  2090. goto err_out;
  2091. }
  2092. while (1) {
  2093. int bytes = mtd->writesize;
  2094. int cached = writelen > bytes && page != blockmask;
  2095. uint8_t *wbuf = buf;
  2096. int use_bufpoi;
  2097. int part_pagewr = (column || writelen < mtd->writesize);
  2098. if (part_pagewr)
  2099. use_bufpoi = 1;
  2100. else
  2101. use_bufpoi = 0;
  2102. WATCHDOG_RESET();
  2103. /* Partial page write?, or need to use bounce buffer */
  2104. if (use_bufpoi) {
  2105. pr_debug("%s: using write bounce buffer for buf@%p\n",
  2106. __func__, buf);
  2107. cached = 0;
  2108. if (part_pagewr)
  2109. bytes = min_t(int, bytes - column, writelen);
  2110. chip->pagebuf = -1;
  2111. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  2112. memcpy(&chip->buffers->databuf[column], buf, bytes);
  2113. wbuf = chip->buffers->databuf;
  2114. }
  2115. if (unlikely(oob)) {
  2116. size_t len = min(oobwritelen, oobmaxlen);
  2117. oob = nand_fill_oob(mtd, oob, len, ops);
  2118. oobwritelen -= len;
  2119. } else {
  2120. /* We still need to erase leftover OOB data */
  2121. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2122. }
  2123. ret = chip->write_page(mtd, chip, column, bytes, wbuf,
  2124. oob_required, page, cached,
  2125. (ops->mode == MTD_OPS_RAW));
  2126. if (ret)
  2127. break;
  2128. writelen -= bytes;
  2129. if (!writelen)
  2130. break;
  2131. column = 0;
  2132. buf += bytes;
  2133. realpage++;
  2134. page = realpage & chip->pagemask;
  2135. /* Check, if we cross a chip boundary */
  2136. if (!page) {
  2137. chipnr++;
  2138. chip->select_chip(mtd, -1);
  2139. chip->select_chip(mtd, chipnr);
  2140. }
  2141. }
  2142. ops->retlen = ops->len - writelen;
  2143. if (unlikely(oob))
  2144. ops->oobretlen = ops->ooblen;
  2145. err_out:
  2146. chip->select_chip(mtd, -1);
  2147. return ret;
  2148. }
  2149. /**
  2150. * panic_nand_write - [MTD Interface] NAND write with ECC
  2151. * @mtd: MTD device structure
  2152. * @to: offset to write to
  2153. * @len: number of bytes to write
  2154. * @retlen: pointer to variable to store the number of written bytes
  2155. * @buf: the data to write
  2156. *
  2157. * NAND write with ECC. Used when performing writes in interrupt context, this
  2158. * may for example be called by mtdoops when writing an oops while in panic.
  2159. */
  2160. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2161. size_t *retlen, const uint8_t *buf)
  2162. {
  2163. struct nand_chip *chip = mtd_to_nand(mtd);
  2164. struct mtd_oob_ops ops;
  2165. int ret;
  2166. /* Wait for the device to get ready */
  2167. panic_nand_wait(mtd, chip, 400);
  2168. /* Grab the device */
  2169. panic_nand_get_device(chip, mtd, FL_WRITING);
  2170. memset(&ops, 0, sizeof(ops));
  2171. ops.len = len;
  2172. ops.datbuf = (uint8_t *)buf;
  2173. ops.mode = MTD_OPS_PLACE_OOB;
  2174. ret = nand_do_write_ops(mtd, to, &ops);
  2175. *retlen = ops.retlen;
  2176. return ret;
  2177. }
  2178. /**
  2179. * nand_write - [MTD Interface] NAND write with ECC
  2180. * @mtd: MTD device structure
  2181. * @to: offset to write to
  2182. * @len: number of bytes to write
  2183. * @retlen: pointer to variable to store the number of written bytes
  2184. * @buf: the data to write
  2185. *
  2186. * NAND write with ECC.
  2187. */
  2188. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2189. size_t *retlen, const uint8_t *buf)
  2190. {
  2191. struct mtd_oob_ops ops;
  2192. int ret;
  2193. nand_get_device(mtd, FL_WRITING);
  2194. memset(&ops, 0, sizeof(ops));
  2195. ops.len = len;
  2196. ops.datbuf = (uint8_t *)buf;
  2197. ops.mode = MTD_OPS_PLACE_OOB;
  2198. ret = nand_do_write_ops(mtd, to, &ops);
  2199. *retlen = ops.retlen;
  2200. nand_release_device(mtd);
  2201. return ret;
  2202. }
  2203. /**
  2204. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2205. * @mtd: MTD device structure
  2206. * @to: offset to write to
  2207. * @ops: oob operation description structure
  2208. *
  2209. * NAND write out-of-band.
  2210. */
  2211. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2212. struct mtd_oob_ops *ops)
  2213. {
  2214. int chipnr, page, status, len;
  2215. struct nand_chip *chip = mtd_to_nand(mtd);
  2216. pr_debug("%s: to = 0x%08x, len = %i\n",
  2217. __func__, (unsigned int)to, (int)ops->ooblen);
  2218. len = mtd_oobavail(mtd, ops);
  2219. /* Do not allow write past end of page */
  2220. if ((ops->ooboffs + ops->ooblen) > len) {
  2221. pr_debug("%s: attempt to write past end of page\n",
  2222. __func__);
  2223. return -EINVAL;
  2224. }
  2225. if (unlikely(ops->ooboffs >= len)) {
  2226. pr_debug("%s: attempt to start write outside oob\n",
  2227. __func__);
  2228. return -EINVAL;
  2229. }
  2230. /* Do not allow write past end of device */
  2231. if (unlikely(to >= mtd->size ||
  2232. ops->ooboffs + ops->ooblen >
  2233. ((mtd->size >> chip->page_shift) -
  2234. (to >> chip->page_shift)) * len)) {
  2235. pr_debug("%s: attempt to write beyond end of device\n",
  2236. __func__);
  2237. return -EINVAL;
  2238. }
  2239. chipnr = (int)(to >> chip->chip_shift);
  2240. chip->select_chip(mtd, chipnr);
  2241. /* Shift to get page */
  2242. page = (int)(to >> chip->page_shift);
  2243. /*
  2244. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2245. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2246. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2247. * it in the doc2000 driver in August 1999. dwmw2.
  2248. */
  2249. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2250. /* Check, if it is write protected */
  2251. if (nand_check_wp(mtd)) {
  2252. chip->select_chip(mtd, -1);
  2253. return -EROFS;
  2254. }
  2255. /* Invalidate the page cache, if we write to the cached page */
  2256. if (page == chip->pagebuf)
  2257. chip->pagebuf = -1;
  2258. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2259. if (ops->mode == MTD_OPS_RAW)
  2260. status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
  2261. else
  2262. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2263. chip->select_chip(mtd, -1);
  2264. if (status)
  2265. return status;
  2266. ops->oobretlen = ops->ooblen;
  2267. return 0;
  2268. }
  2269. /**
  2270. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2271. * @mtd: MTD device structure
  2272. * @to: offset to write to
  2273. * @ops: oob operation description structure
  2274. */
  2275. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2276. struct mtd_oob_ops *ops)
  2277. {
  2278. int ret = -ENOTSUPP;
  2279. ops->retlen = 0;
  2280. /* Do not allow writes past end of device */
  2281. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2282. pr_debug("%s: attempt to write beyond end of device\n",
  2283. __func__);
  2284. return -EINVAL;
  2285. }
  2286. nand_get_device(mtd, FL_WRITING);
  2287. switch (ops->mode) {
  2288. case MTD_OPS_PLACE_OOB:
  2289. case MTD_OPS_AUTO_OOB:
  2290. case MTD_OPS_RAW:
  2291. break;
  2292. default:
  2293. goto out;
  2294. }
  2295. if (!ops->datbuf)
  2296. ret = nand_do_write_oob(mtd, to, ops);
  2297. else
  2298. ret = nand_do_write_ops(mtd, to, ops);
  2299. out:
  2300. nand_release_device(mtd);
  2301. return ret;
  2302. }
  2303. /**
  2304. * single_erase - [GENERIC] NAND standard block erase command function
  2305. * @mtd: MTD device structure
  2306. * @page: the page address of the block which will be erased
  2307. *
  2308. * Standard erase command for NAND chips. Returns NAND status.
  2309. */
  2310. static int single_erase(struct mtd_info *mtd, int page)
  2311. {
  2312. struct nand_chip *chip = mtd_to_nand(mtd);
  2313. /* Send commands to erase a block */
  2314. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2315. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2316. return chip->waitfunc(mtd, chip);
  2317. }
  2318. /**
  2319. * nand_erase - [MTD Interface] erase block(s)
  2320. * @mtd: MTD device structure
  2321. * @instr: erase instruction
  2322. *
  2323. * Erase one ore more blocks.
  2324. */
  2325. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2326. {
  2327. return nand_erase_nand(mtd, instr, 0);
  2328. }
  2329. /**
  2330. * nand_erase_nand - [INTERN] erase block(s)
  2331. * @mtd: MTD device structure
  2332. * @instr: erase instruction
  2333. * @allowbbt: allow erasing the bbt area
  2334. *
  2335. * Erase one ore more blocks.
  2336. */
  2337. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2338. int allowbbt)
  2339. {
  2340. int page, status, pages_per_block, ret, chipnr;
  2341. struct nand_chip *chip = mtd_to_nand(mtd);
  2342. loff_t len;
  2343. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2344. __func__, (unsigned long long)instr->addr,
  2345. (unsigned long long)instr->len);
  2346. if (check_offs_len(mtd, instr->addr, instr->len))
  2347. return -EINVAL;
  2348. /* Grab the lock and see if the device is available */
  2349. nand_get_device(mtd, FL_ERASING);
  2350. /* Shift to get first page */
  2351. page = (int)(instr->addr >> chip->page_shift);
  2352. chipnr = (int)(instr->addr >> chip->chip_shift);
  2353. /* Calculate pages in each block */
  2354. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2355. /* Select the NAND device */
  2356. chip->select_chip(mtd, chipnr);
  2357. /* Check, if it is write protected */
  2358. if (nand_check_wp(mtd)) {
  2359. pr_debug("%s: device is write protected!\n",
  2360. __func__);
  2361. instr->state = MTD_ERASE_FAILED;
  2362. goto erase_exit;
  2363. }
  2364. /* Loop through the pages */
  2365. len = instr->len;
  2366. instr->state = MTD_ERASING;
  2367. while (len) {
  2368. WATCHDOG_RESET();
  2369. /* Check if we have a bad block, we do not erase bad blocks! */
  2370. if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
  2371. chip->page_shift, allowbbt)) {
  2372. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2373. __func__, page);
  2374. instr->state = MTD_ERASE_FAILED;
  2375. goto erase_exit;
  2376. }
  2377. /*
  2378. * Invalidate the page cache, if we erase the block which
  2379. * contains the current cached page.
  2380. */
  2381. if (page <= chip->pagebuf && chip->pagebuf <
  2382. (page + pages_per_block))
  2383. chip->pagebuf = -1;
  2384. status = chip->erase(mtd, page & chip->pagemask);
  2385. /*
  2386. * See if operation failed and additional status checks are
  2387. * available
  2388. */
  2389. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2390. status = chip->errstat(mtd, chip, FL_ERASING,
  2391. status, page);
  2392. /* See if block erase succeeded */
  2393. if (status & NAND_STATUS_FAIL) {
  2394. pr_debug("%s: failed erase, page 0x%08x\n",
  2395. __func__, page);
  2396. instr->state = MTD_ERASE_FAILED;
  2397. instr->fail_addr =
  2398. ((loff_t)page << chip->page_shift);
  2399. goto erase_exit;
  2400. }
  2401. /* Increment page address and decrement length */
  2402. len -= (1ULL << chip->phys_erase_shift);
  2403. page += pages_per_block;
  2404. /* Check, if we cross a chip boundary */
  2405. if (len && !(page & chip->pagemask)) {
  2406. chipnr++;
  2407. chip->select_chip(mtd, -1);
  2408. chip->select_chip(mtd, chipnr);
  2409. }
  2410. }
  2411. instr->state = MTD_ERASE_DONE;
  2412. erase_exit:
  2413. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2414. /* Deselect and wake up anyone waiting on the device */
  2415. chip->select_chip(mtd, -1);
  2416. nand_release_device(mtd);
  2417. /* Do call back function */
  2418. if (!ret)
  2419. mtd_erase_callback(instr);
  2420. /* Return more or less happy */
  2421. return ret;
  2422. }
  2423. /**
  2424. * nand_sync - [MTD Interface] sync
  2425. * @mtd: MTD device structure
  2426. *
  2427. * Sync is actually a wait for chip ready function.
  2428. */
  2429. static void nand_sync(struct mtd_info *mtd)
  2430. {
  2431. pr_debug("%s: called\n", __func__);
  2432. /* Grab the lock and see if the device is available */
  2433. nand_get_device(mtd, FL_SYNCING);
  2434. /* Release it and go back */
  2435. nand_release_device(mtd);
  2436. }
  2437. /**
  2438. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2439. * @mtd: MTD device structure
  2440. * @offs: offset relative to mtd start
  2441. */
  2442. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2443. {
  2444. struct nand_chip *chip = mtd_to_nand(mtd);
  2445. int chipnr = (int)(offs >> chip->chip_shift);
  2446. int ret;
  2447. /* Select the NAND device */
  2448. nand_get_device(mtd, FL_READING);
  2449. chip->select_chip(mtd, chipnr);
  2450. ret = nand_block_checkbad(mtd, offs, 0);
  2451. chip->select_chip(mtd, -1);
  2452. nand_release_device(mtd);
  2453. return ret;
  2454. }
  2455. /**
  2456. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2457. * @mtd: MTD device structure
  2458. * @ofs: offset relative to mtd start
  2459. */
  2460. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2461. {
  2462. int ret;
  2463. ret = nand_block_isbad(mtd, ofs);
  2464. if (ret) {
  2465. /* If it was bad already, return success and do nothing */
  2466. if (ret > 0)
  2467. return 0;
  2468. return ret;
  2469. }
  2470. return nand_block_markbad_lowlevel(mtd, ofs);
  2471. }
  2472. /**
  2473. * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
  2474. * @mtd: MTD device structure
  2475. * @chip: nand chip info structure
  2476. * @addr: feature address.
  2477. * @subfeature_param: the subfeature parameters, a four bytes array.
  2478. */
  2479. static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
  2480. int addr, uint8_t *subfeature_param)
  2481. {
  2482. int status;
  2483. int i;
  2484. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2485. if (!chip->onfi_version ||
  2486. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2487. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2488. return -EINVAL;
  2489. #endif
  2490. chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
  2491. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2492. chip->write_byte(mtd, subfeature_param[i]);
  2493. status = chip->waitfunc(mtd, chip);
  2494. if (status & NAND_STATUS_FAIL)
  2495. return -EIO;
  2496. return 0;
  2497. }
  2498. /**
  2499. * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
  2500. * @mtd: MTD device structure
  2501. * @chip: nand chip info structure
  2502. * @addr: feature address.
  2503. * @subfeature_param: the subfeature parameters, a four bytes array.
  2504. */
  2505. static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
  2506. int addr, uint8_t *subfeature_param)
  2507. {
  2508. int i;
  2509. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2510. if (!chip->onfi_version ||
  2511. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2512. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2513. return -EINVAL;
  2514. #endif
  2515. chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
  2516. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2517. *subfeature_param++ = chip->read_byte(mtd);
  2518. return 0;
  2519. }
  2520. /* Set default functions */
  2521. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2522. {
  2523. /* check for proper chip_delay setup, set 20us if not */
  2524. if (!chip->chip_delay)
  2525. chip->chip_delay = 20;
  2526. /* check, if a user supplied command function given */
  2527. if (chip->cmdfunc == NULL)
  2528. chip->cmdfunc = nand_command;
  2529. /* check, if a user supplied wait function given */
  2530. if (chip->waitfunc == NULL)
  2531. chip->waitfunc = nand_wait;
  2532. if (!chip->select_chip)
  2533. chip->select_chip = nand_select_chip;
  2534. /* set for ONFI nand */
  2535. if (!chip->onfi_set_features)
  2536. chip->onfi_set_features = nand_onfi_set_features;
  2537. if (!chip->onfi_get_features)
  2538. chip->onfi_get_features = nand_onfi_get_features;
  2539. /* If called twice, pointers that depend on busw may need to be reset */
  2540. if (!chip->read_byte || chip->read_byte == nand_read_byte)
  2541. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2542. if (!chip->read_word)
  2543. chip->read_word = nand_read_word;
  2544. if (!chip->block_bad)
  2545. chip->block_bad = nand_block_bad;
  2546. if (!chip->block_markbad)
  2547. chip->block_markbad = nand_default_block_markbad;
  2548. if (!chip->write_buf || chip->write_buf == nand_write_buf)
  2549. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2550. if (!chip->write_byte || chip->write_byte == nand_write_byte)
  2551. chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
  2552. if (!chip->read_buf || chip->read_buf == nand_read_buf)
  2553. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2554. if (!chip->scan_bbt)
  2555. chip->scan_bbt = nand_default_bbt;
  2556. if (!chip->controller) {
  2557. chip->controller = &chip->hwcontrol;
  2558. spin_lock_init(&chip->controller->lock);
  2559. init_waitqueue_head(&chip->controller->wq);
  2560. }
  2561. }
  2562. /* Sanitize ONFI strings so we can safely print them */
  2563. static void sanitize_string(char *s, size_t len)
  2564. {
  2565. ssize_t i;
  2566. /* Null terminate */
  2567. s[len - 1] = 0;
  2568. /* Remove non printable chars */
  2569. for (i = 0; i < len - 1; i++) {
  2570. if (s[i] < ' ' || s[i] > 127)
  2571. s[i] = '?';
  2572. }
  2573. /* Remove trailing spaces */
  2574. strim(s);
  2575. }
  2576. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2577. {
  2578. int i;
  2579. while (len--) {
  2580. crc ^= *p++ << 8;
  2581. for (i = 0; i < 8; i++)
  2582. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2583. }
  2584. return crc;
  2585. }
  2586. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2587. /* Parse the Extended Parameter Page. */
  2588. static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
  2589. struct nand_chip *chip, struct nand_onfi_params *p)
  2590. {
  2591. struct onfi_ext_param_page *ep;
  2592. struct onfi_ext_section *s;
  2593. struct onfi_ext_ecc_info *ecc;
  2594. uint8_t *cursor;
  2595. int ret = -EINVAL;
  2596. int len;
  2597. int i;
  2598. len = le16_to_cpu(p->ext_param_page_length) * 16;
  2599. ep = kmalloc(len, GFP_KERNEL);
  2600. if (!ep)
  2601. return -ENOMEM;
  2602. /* Send our own NAND_CMD_PARAM. */
  2603. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2604. /* Use the Change Read Column command to skip the ONFI param pages. */
  2605. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  2606. sizeof(*p) * p->num_of_param_pages , -1);
  2607. /* Read out the Extended Parameter Page. */
  2608. chip->read_buf(mtd, (uint8_t *)ep, len);
  2609. if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
  2610. != le16_to_cpu(ep->crc))) {
  2611. pr_debug("fail in the CRC.\n");
  2612. goto ext_out;
  2613. }
  2614. /*
  2615. * Check the signature.
  2616. * Do not strictly follow the ONFI spec, maybe changed in future.
  2617. */
  2618. if (strncmp((char *)ep->sig, "EPPS", 4)) {
  2619. pr_debug("The signature is invalid.\n");
  2620. goto ext_out;
  2621. }
  2622. /* find the ECC section. */
  2623. cursor = (uint8_t *)(ep + 1);
  2624. for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
  2625. s = ep->sections + i;
  2626. if (s->type == ONFI_SECTION_TYPE_2)
  2627. break;
  2628. cursor += s->length * 16;
  2629. }
  2630. if (i == ONFI_EXT_SECTION_MAX) {
  2631. pr_debug("We can not find the ECC section.\n");
  2632. goto ext_out;
  2633. }
  2634. /* get the info we want. */
  2635. ecc = (struct onfi_ext_ecc_info *)cursor;
  2636. if (!ecc->codeword_size) {
  2637. pr_debug("Invalid codeword size\n");
  2638. goto ext_out;
  2639. }
  2640. chip->ecc_strength_ds = ecc->ecc_bits;
  2641. chip->ecc_step_ds = 1 << ecc->codeword_size;
  2642. ret = 0;
  2643. ext_out:
  2644. kfree(ep);
  2645. return ret;
  2646. }
  2647. static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
  2648. {
  2649. struct nand_chip *chip = mtd_to_nand(mtd);
  2650. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  2651. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  2652. feature);
  2653. }
  2654. /*
  2655. * Configure chip properties from Micron vendor-specific ONFI table
  2656. */
  2657. static void nand_onfi_detect_micron(struct nand_chip *chip,
  2658. struct nand_onfi_params *p)
  2659. {
  2660. struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
  2661. if (le16_to_cpu(p->vendor_revision) < 1)
  2662. return;
  2663. chip->read_retries = micron->read_retry_options;
  2664. chip->setup_read_retry = nand_setup_read_retry_micron;
  2665. }
  2666. /*
  2667. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2668. */
  2669. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2670. int *busw)
  2671. {
  2672. struct nand_onfi_params *p = &chip->onfi_params;
  2673. int i, j;
  2674. int val;
  2675. /* Try ONFI for unknown chip or LP */
  2676. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2677. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2678. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2679. return 0;
  2680. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2681. for (i = 0; i < 3; i++) {
  2682. for (j = 0; j < sizeof(*p); j++)
  2683. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  2684. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2685. le16_to_cpu(p->crc)) {
  2686. break;
  2687. }
  2688. }
  2689. if (i == 3) {
  2690. pr_err("Could not find valid ONFI parameter page; aborting\n");
  2691. return 0;
  2692. }
  2693. /* Check version */
  2694. val = le16_to_cpu(p->revision);
  2695. if (val & (1 << 5))
  2696. chip->onfi_version = 23;
  2697. else if (val & (1 << 4))
  2698. chip->onfi_version = 22;
  2699. else if (val & (1 << 3))
  2700. chip->onfi_version = 21;
  2701. else if (val & (1 << 2))
  2702. chip->onfi_version = 20;
  2703. else if (val & (1 << 1))
  2704. chip->onfi_version = 10;
  2705. if (!chip->onfi_version) {
  2706. pr_info("unsupported ONFI version: %d\n", val);
  2707. return 0;
  2708. }
  2709. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2710. sanitize_string(p->model, sizeof(p->model));
  2711. if (!mtd->name)
  2712. mtd->name = p->model;
  2713. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2714. /*
  2715. * pages_per_block and blocks_per_lun may not be a power-of-2 size
  2716. * (don't ask me who thought of this...). MTD assumes that these
  2717. * dimensions will be power-of-2, so just truncate the remaining area.
  2718. */
  2719. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2720. mtd->erasesize *= mtd->writesize;
  2721. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2722. /* See erasesize comment */
  2723. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2724. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2725. chip->bits_per_cell = p->bits_per_cell;
  2726. if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
  2727. *busw = NAND_BUSWIDTH_16;
  2728. else
  2729. *busw = 0;
  2730. if (p->ecc_bits != 0xff) {
  2731. chip->ecc_strength_ds = p->ecc_bits;
  2732. chip->ecc_step_ds = 512;
  2733. } else if (chip->onfi_version >= 21 &&
  2734. (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
  2735. /*
  2736. * The nand_flash_detect_ext_param_page() uses the
  2737. * Change Read Column command which maybe not supported
  2738. * by the chip->cmdfunc. So try to update the chip->cmdfunc
  2739. * now. We do not replace user supplied command function.
  2740. */
  2741. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2742. chip->cmdfunc = nand_command_lp;
  2743. /* The Extended Parameter Page is supported since ONFI 2.1. */
  2744. if (nand_flash_detect_ext_param_page(mtd, chip, p))
  2745. pr_warn("Failed to detect ONFI extended param page\n");
  2746. } else {
  2747. pr_warn("Could not retrieve ONFI ECC requirements\n");
  2748. }
  2749. if (p->jedec_id == NAND_MFR_MICRON)
  2750. nand_onfi_detect_micron(chip, p);
  2751. return 1;
  2752. }
  2753. #else
  2754. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2755. int *busw)
  2756. {
  2757. return 0;
  2758. }
  2759. #endif
  2760. /*
  2761. * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
  2762. */
  2763. static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
  2764. int *busw)
  2765. {
  2766. struct nand_jedec_params *p = &chip->jedec_params;
  2767. struct jedec_ecc_info *ecc;
  2768. int val;
  2769. int i, j;
  2770. /* Try JEDEC for unknown chip or LP */
  2771. chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
  2772. if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
  2773. chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
  2774. chip->read_byte(mtd) != 'C')
  2775. return 0;
  2776. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
  2777. for (i = 0; i < 3; i++) {
  2778. for (j = 0; j < sizeof(*p); j++)
  2779. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  2780. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
  2781. le16_to_cpu(p->crc))
  2782. break;
  2783. }
  2784. if (i == 3) {
  2785. pr_err("Could not find valid JEDEC parameter page; aborting\n");
  2786. return 0;
  2787. }
  2788. /* Check version */
  2789. val = le16_to_cpu(p->revision);
  2790. if (val & (1 << 2))
  2791. chip->jedec_version = 10;
  2792. else if (val & (1 << 1))
  2793. chip->jedec_version = 1; /* vendor specific version */
  2794. if (!chip->jedec_version) {
  2795. pr_info("unsupported JEDEC version: %d\n", val);
  2796. return 0;
  2797. }
  2798. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2799. sanitize_string(p->model, sizeof(p->model));
  2800. if (!mtd->name)
  2801. mtd->name = p->model;
  2802. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2803. /* Please reference to the comment for nand_flash_detect_onfi. */
  2804. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2805. mtd->erasesize *= mtd->writesize;
  2806. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2807. /* Please reference to the comment for nand_flash_detect_onfi. */
  2808. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2809. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2810. chip->bits_per_cell = p->bits_per_cell;
  2811. if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
  2812. *busw = NAND_BUSWIDTH_16;
  2813. else
  2814. *busw = 0;
  2815. /* ECC info */
  2816. ecc = &p->ecc_info[0];
  2817. if (ecc->codeword_size >= 9) {
  2818. chip->ecc_strength_ds = ecc->ecc_bits;
  2819. chip->ecc_step_ds = 1 << ecc->codeword_size;
  2820. } else {
  2821. pr_warn("Invalid codeword size\n");
  2822. }
  2823. return 1;
  2824. }
  2825. /*
  2826. * nand_id_has_period - Check if an ID string has a given wraparound period
  2827. * @id_data: the ID string
  2828. * @arrlen: the length of the @id_data array
  2829. * @period: the period of repitition
  2830. *
  2831. * Check if an ID string is repeated within a given sequence of bytes at
  2832. * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
  2833. * period of 3). This is a helper function for nand_id_len(). Returns non-zero
  2834. * if the repetition has a period of @period; otherwise, returns zero.
  2835. */
  2836. static int nand_id_has_period(u8 *id_data, int arrlen, int period)
  2837. {
  2838. int i, j;
  2839. for (i = 0; i < period; i++)
  2840. for (j = i + period; j < arrlen; j += period)
  2841. if (id_data[i] != id_data[j])
  2842. return 0;
  2843. return 1;
  2844. }
  2845. /*
  2846. * nand_id_len - Get the length of an ID string returned by CMD_READID
  2847. * @id_data: the ID string
  2848. * @arrlen: the length of the @id_data array
  2849. * Returns the length of the ID string, according to known wraparound/trailing
  2850. * zero patterns. If no pattern exists, returns the length of the array.
  2851. */
  2852. static int nand_id_len(u8 *id_data, int arrlen)
  2853. {
  2854. int last_nonzero, period;
  2855. /* Find last non-zero byte */
  2856. for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
  2857. if (id_data[last_nonzero])
  2858. break;
  2859. /* All zeros */
  2860. if (last_nonzero < 0)
  2861. return 0;
  2862. /* Calculate wraparound period */
  2863. for (period = 1; period < arrlen; period++)
  2864. if (nand_id_has_period(id_data, arrlen, period))
  2865. break;
  2866. /* There's a repeated pattern */
  2867. if (period < arrlen)
  2868. return period;
  2869. /* There are trailing zeros */
  2870. if (last_nonzero < arrlen - 1)
  2871. return last_nonzero + 1;
  2872. /* No pattern detected */
  2873. return arrlen;
  2874. }
  2875. /* Extract the bits of per cell from the 3rd byte of the extended ID */
  2876. static int nand_get_bits_per_cell(u8 cellinfo)
  2877. {
  2878. int bits;
  2879. bits = cellinfo & NAND_CI_CELLTYPE_MSK;
  2880. bits >>= NAND_CI_CELLTYPE_SHIFT;
  2881. return bits + 1;
  2882. }
  2883. /*
  2884. * Many new NAND share similar device ID codes, which represent the size of the
  2885. * chip. The rest of the parameters must be decoded according to generic or
  2886. * manufacturer-specific "extended ID" decoding patterns.
  2887. */
  2888. static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
  2889. u8 id_data[8], int *busw)
  2890. {
  2891. int extid, id_len;
  2892. /* The 3rd id byte holds MLC / multichip data */
  2893. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  2894. /* The 4th id byte is the important one */
  2895. extid = id_data[3];
  2896. id_len = nand_id_len(id_data, 8);
  2897. /*
  2898. * Field definitions are in the following datasheets:
  2899. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2900. * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
  2901. * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
  2902. *
  2903. * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
  2904. * ID to decide what to do.
  2905. */
  2906. if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
  2907. !nand_is_slc(chip) && id_data[5] != 0x00) {
  2908. /* Calc pagesize */
  2909. mtd->writesize = 2048 << (extid & 0x03);
  2910. extid >>= 2;
  2911. /* Calc oobsize */
  2912. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  2913. case 1:
  2914. mtd->oobsize = 128;
  2915. break;
  2916. case 2:
  2917. mtd->oobsize = 218;
  2918. break;
  2919. case 3:
  2920. mtd->oobsize = 400;
  2921. break;
  2922. case 4:
  2923. mtd->oobsize = 436;
  2924. break;
  2925. case 5:
  2926. mtd->oobsize = 512;
  2927. break;
  2928. case 6:
  2929. mtd->oobsize = 640;
  2930. break;
  2931. case 7:
  2932. default: /* Other cases are "reserved" (unknown) */
  2933. mtd->oobsize = 1024;
  2934. break;
  2935. }
  2936. extid >>= 2;
  2937. /* Calc blocksize */
  2938. mtd->erasesize = (128 * 1024) <<
  2939. (((extid >> 1) & 0x04) | (extid & 0x03));
  2940. *busw = 0;
  2941. } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
  2942. !nand_is_slc(chip)) {
  2943. unsigned int tmp;
  2944. /* Calc pagesize */
  2945. mtd->writesize = 2048 << (extid & 0x03);
  2946. extid >>= 2;
  2947. /* Calc oobsize */
  2948. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  2949. case 0:
  2950. mtd->oobsize = 128;
  2951. break;
  2952. case 1:
  2953. mtd->oobsize = 224;
  2954. break;
  2955. case 2:
  2956. mtd->oobsize = 448;
  2957. break;
  2958. case 3:
  2959. mtd->oobsize = 64;
  2960. break;
  2961. case 4:
  2962. mtd->oobsize = 32;
  2963. break;
  2964. case 5:
  2965. mtd->oobsize = 16;
  2966. break;
  2967. default:
  2968. mtd->oobsize = 640;
  2969. break;
  2970. }
  2971. extid >>= 2;
  2972. /* Calc blocksize */
  2973. tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
  2974. if (tmp < 0x03)
  2975. mtd->erasesize = (128 * 1024) << tmp;
  2976. else if (tmp == 0x03)
  2977. mtd->erasesize = 768 * 1024;
  2978. else
  2979. mtd->erasesize = (64 * 1024) << tmp;
  2980. *busw = 0;
  2981. } else {
  2982. /* Calc pagesize */
  2983. mtd->writesize = 1024 << (extid & 0x03);
  2984. extid >>= 2;
  2985. /* Calc oobsize */
  2986. mtd->oobsize = (8 << (extid & 0x01)) *
  2987. (mtd->writesize >> 9);
  2988. extid >>= 2;
  2989. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2990. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2991. extid >>= 2;
  2992. /* Get buswidth information */
  2993. *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2994. /*
  2995. * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
  2996. * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
  2997. * follows:
  2998. * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
  2999. * 110b -> 24nm
  3000. * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
  3001. */
  3002. if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
  3003. nand_is_slc(chip) &&
  3004. (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
  3005. !(id_data[4] & 0x80) /* !BENAND */) {
  3006. mtd->oobsize = 32 * mtd->writesize >> 9;
  3007. }
  3008. }
  3009. }
  3010. /*
  3011. * Old devices have chip data hardcoded in the device ID table. nand_decode_id
  3012. * decodes a matching ID table entry and assigns the MTD size parameters for
  3013. * the chip.
  3014. */
  3015. static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
  3016. struct nand_flash_dev *type, u8 id_data[8],
  3017. int *busw)
  3018. {
  3019. int maf_id = id_data[0];
  3020. mtd->erasesize = type->erasesize;
  3021. mtd->writesize = type->pagesize;
  3022. mtd->oobsize = mtd->writesize / 32;
  3023. *busw = type->options & NAND_BUSWIDTH_16;
  3024. /* All legacy ID NAND are small-page, SLC */
  3025. chip->bits_per_cell = 1;
  3026. /*
  3027. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  3028. * some Spansion chips have erasesize that conflicts with size
  3029. * listed in nand_ids table.
  3030. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  3031. */
  3032. if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
  3033. && id_data[6] == 0x00 && id_data[7] == 0x00
  3034. && mtd->writesize == 512) {
  3035. mtd->erasesize = 128 * 1024;
  3036. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  3037. }
  3038. }
  3039. /*
  3040. * Set the bad block marker/indicator (BBM/BBI) patterns according to some
  3041. * heuristic patterns using various detected parameters (e.g., manufacturer,
  3042. * page size, cell-type information).
  3043. */
  3044. static void nand_decode_bbm_options(struct mtd_info *mtd,
  3045. struct nand_chip *chip, u8 id_data[8])
  3046. {
  3047. int maf_id = id_data[0];
  3048. /* Set the bad block position */
  3049. if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
  3050. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  3051. else
  3052. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  3053. /*
  3054. * Bad block marker is stored in the last page of each block on Samsung
  3055. * and Hynix MLC devices; stored in first two pages of each block on
  3056. * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
  3057. * AMD/Spansion, and Macronix. All others scan only the first page.
  3058. */
  3059. if (!nand_is_slc(chip) &&
  3060. (maf_id == NAND_MFR_SAMSUNG ||
  3061. maf_id == NAND_MFR_HYNIX))
  3062. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  3063. else if ((nand_is_slc(chip) &&
  3064. (maf_id == NAND_MFR_SAMSUNG ||
  3065. maf_id == NAND_MFR_HYNIX ||
  3066. maf_id == NAND_MFR_TOSHIBA ||
  3067. maf_id == NAND_MFR_AMD ||
  3068. maf_id == NAND_MFR_MACRONIX)) ||
  3069. (mtd->writesize == 2048 &&
  3070. maf_id == NAND_MFR_MICRON))
  3071. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  3072. }
  3073. static inline bool is_full_id_nand(struct nand_flash_dev *type)
  3074. {
  3075. return type->id_len;
  3076. }
  3077. static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
  3078. struct nand_flash_dev *type, u8 *id_data, int *busw)
  3079. {
  3080. if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
  3081. mtd->writesize = type->pagesize;
  3082. mtd->erasesize = type->erasesize;
  3083. mtd->oobsize = type->oobsize;
  3084. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  3085. chip->chipsize = (uint64_t)type->chipsize << 20;
  3086. chip->options |= type->options;
  3087. chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
  3088. chip->ecc_step_ds = NAND_ECC_STEP(type);
  3089. chip->onfi_timing_mode_default =
  3090. type->onfi_timing_mode_default;
  3091. *busw = type->options & NAND_BUSWIDTH_16;
  3092. if (!mtd->name)
  3093. mtd->name = type->name;
  3094. return true;
  3095. }
  3096. return false;
  3097. }
  3098. /*
  3099. * Get the flash and manufacturer id and lookup if the type is supported.
  3100. */
  3101. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  3102. struct nand_chip *chip,
  3103. int *maf_id, int *dev_id,
  3104. struct nand_flash_dev *type)
  3105. {
  3106. int busw;
  3107. int i, maf_idx;
  3108. u8 id_data[8];
  3109. /* Select the device */
  3110. chip->select_chip(mtd, 0);
  3111. /*
  3112. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  3113. * after power-up.
  3114. */
  3115. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3116. /* Send the command for reading device ID */
  3117. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3118. /* Read manufacturer and device IDs */
  3119. *maf_id = chip->read_byte(mtd);
  3120. *dev_id = chip->read_byte(mtd);
  3121. /*
  3122. * Try again to make sure, as some systems the bus-hold or other
  3123. * interface concerns can cause random data which looks like a
  3124. * possibly credible NAND flash to appear. If the two results do
  3125. * not match, ignore the device completely.
  3126. */
  3127. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3128. /* Read entire ID string */
  3129. for (i = 0; i < 8; i++)
  3130. id_data[i] = chip->read_byte(mtd);
  3131. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  3132. pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
  3133. *maf_id, *dev_id, id_data[0], id_data[1]);
  3134. return ERR_PTR(-ENODEV);
  3135. }
  3136. if (!type)
  3137. type = nand_flash_ids;
  3138. for (; type->name != NULL; type++) {
  3139. if (is_full_id_nand(type)) {
  3140. if (find_full_id_nand(mtd, chip, type, id_data, &busw))
  3141. goto ident_done;
  3142. } else if (*dev_id == type->dev_id) {
  3143. break;
  3144. }
  3145. }
  3146. chip->onfi_version = 0;
  3147. if (!type->name || !type->pagesize) {
  3148. /* Check if the chip is ONFI compliant */
  3149. if (nand_flash_detect_onfi(mtd, chip, &busw))
  3150. goto ident_done;
  3151. /* Check if the chip is JEDEC compliant */
  3152. if (nand_flash_detect_jedec(mtd, chip, &busw))
  3153. goto ident_done;
  3154. }
  3155. if (!type->name)
  3156. return ERR_PTR(-ENODEV);
  3157. if (!mtd->name)
  3158. mtd->name = type->name;
  3159. chip->chipsize = (uint64_t)type->chipsize << 20;
  3160. if (!type->pagesize) {
  3161. /* Decode parameters from extended ID */
  3162. nand_decode_ext_id(mtd, chip, id_data, &busw);
  3163. } else {
  3164. nand_decode_id(mtd, chip, type, id_data, &busw);
  3165. }
  3166. /* Get chip options */
  3167. chip->options |= type->options;
  3168. /*
  3169. * Check if chip is not a Samsung device. Do not clear the
  3170. * options for chips which do not have an extended id.
  3171. */
  3172. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  3173. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  3174. ident_done:
  3175. /* Try to identify manufacturer */
  3176. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  3177. if (nand_manuf_ids[maf_idx].id == *maf_id)
  3178. break;
  3179. }
  3180. if (chip->options & NAND_BUSWIDTH_AUTO) {
  3181. WARN_ON(chip->options & NAND_BUSWIDTH_16);
  3182. chip->options |= busw;
  3183. nand_set_defaults(chip, busw);
  3184. } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  3185. /*
  3186. * Check, if buswidth is correct. Hardware drivers should set
  3187. * chip correct!
  3188. */
  3189. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3190. *maf_id, *dev_id);
  3191. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
  3192. pr_warn("bus width %d instead %d bit\n",
  3193. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  3194. busw ? 16 : 8);
  3195. return ERR_PTR(-EINVAL);
  3196. }
  3197. nand_decode_bbm_options(mtd, chip, id_data);
  3198. /* Calculate the address shift from the page size */
  3199. chip->page_shift = ffs(mtd->writesize) - 1;
  3200. /* Convert chipsize to number of pages per chip -1 */
  3201. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  3202. chip->bbt_erase_shift = chip->phys_erase_shift =
  3203. ffs(mtd->erasesize) - 1;
  3204. if (chip->chipsize & 0xffffffff)
  3205. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  3206. else {
  3207. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  3208. chip->chip_shift += 32 - 1;
  3209. }
  3210. chip->badblockbits = 8;
  3211. chip->erase = single_erase;
  3212. /* Do not replace user supplied command function! */
  3213. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  3214. chip->cmdfunc = nand_command_lp;
  3215. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3216. *maf_id, *dev_id);
  3217. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  3218. if (chip->onfi_version)
  3219. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3220. chip->onfi_params.model);
  3221. else if (chip->jedec_version)
  3222. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3223. chip->jedec_params.model);
  3224. else
  3225. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3226. type->name);
  3227. #else
  3228. if (chip->jedec_version)
  3229. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3230. chip->jedec_params.model);
  3231. else
  3232. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3233. type->name);
  3234. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3235. type->name);
  3236. #endif
  3237. pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
  3238. (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
  3239. mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
  3240. return type;
  3241. }
  3242. #if CONFIG_IS_ENABLED(OF_CONTROL)
  3243. DECLARE_GLOBAL_DATA_PTR;
  3244. static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
  3245. {
  3246. int ret, ecc_mode = -1, ecc_strength, ecc_step;
  3247. const void *blob = gd->fdt_blob;
  3248. const char *str;
  3249. ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
  3250. if (ret == 16)
  3251. chip->options |= NAND_BUSWIDTH_16;
  3252. if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
  3253. chip->bbt_options |= NAND_BBT_USE_FLASH;
  3254. str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
  3255. if (str) {
  3256. if (!strcmp(str, "none"))
  3257. ecc_mode = NAND_ECC_NONE;
  3258. else if (!strcmp(str, "soft"))
  3259. ecc_mode = NAND_ECC_SOFT;
  3260. else if (!strcmp(str, "hw"))
  3261. ecc_mode = NAND_ECC_HW;
  3262. else if (!strcmp(str, "hw_syndrome"))
  3263. ecc_mode = NAND_ECC_HW_SYNDROME;
  3264. else if (!strcmp(str, "hw_oob_first"))
  3265. ecc_mode = NAND_ECC_HW_OOB_FIRST;
  3266. else if (!strcmp(str, "soft_bch"))
  3267. ecc_mode = NAND_ECC_SOFT_BCH;
  3268. }
  3269. ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
  3270. ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
  3271. if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
  3272. (!(ecc_step >= 0) && ecc_strength >= 0)) {
  3273. pr_err("must set both strength and step size in DT\n");
  3274. return -EINVAL;
  3275. }
  3276. if (ecc_mode >= 0)
  3277. chip->ecc.mode = ecc_mode;
  3278. if (ecc_strength >= 0)
  3279. chip->ecc.strength = ecc_strength;
  3280. if (ecc_step > 0)
  3281. chip->ecc.size = ecc_step;
  3282. return 0;
  3283. }
  3284. #else
  3285. static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
  3286. {
  3287. return 0;
  3288. }
  3289. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  3290. /**
  3291. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  3292. * @mtd: MTD device structure
  3293. * @maxchips: number of chips to scan for
  3294. * @table: alternative NAND ID table
  3295. *
  3296. * This is the first phase of the normal nand_scan() function. It reads the
  3297. * flash ID and sets up MTD fields accordingly.
  3298. *
  3299. */
  3300. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  3301. struct nand_flash_dev *table)
  3302. {
  3303. int i, nand_maf_id, nand_dev_id;
  3304. struct nand_chip *chip = mtd_to_nand(mtd);
  3305. struct nand_flash_dev *type;
  3306. int ret;
  3307. if (chip->flash_node) {
  3308. ret = nand_dt_init(mtd, chip, chip->flash_node);
  3309. if (ret)
  3310. return ret;
  3311. }
  3312. /* Set the default functions */
  3313. nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
  3314. /* Read the flash type */
  3315. type = nand_get_flash_type(mtd, chip, &nand_maf_id,
  3316. &nand_dev_id, table);
  3317. if (IS_ERR(type)) {
  3318. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  3319. pr_warn("No NAND device found\n");
  3320. chip->select_chip(mtd, -1);
  3321. return PTR_ERR(type);
  3322. }
  3323. chip->select_chip(mtd, -1);
  3324. /* Check for a chip array */
  3325. for (i = 1; i < maxchips; i++) {
  3326. chip->select_chip(mtd, i);
  3327. /* See comment in nand_get_flash_type for reset */
  3328. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  3329. /* Send the command for reading device ID */
  3330. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3331. /* Read manufacturer and device IDs */
  3332. if (nand_maf_id != chip->read_byte(mtd) ||
  3333. nand_dev_id != chip->read_byte(mtd)) {
  3334. chip->select_chip(mtd, -1);
  3335. break;
  3336. }
  3337. chip->select_chip(mtd, -1);
  3338. }
  3339. #ifdef DEBUG
  3340. if (i > 1)
  3341. pr_info("%d chips detected\n", i);
  3342. #endif
  3343. /* Store the number of chips and calc total size for mtd */
  3344. chip->numchips = i;
  3345. mtd->size = i * chip->chipsize;
  3346. return 0;
  3347. }
  3348. EXPORT_SYMBOL(nand_scan_ident);
  3349. /*
  3350. * Check if the chip configuration meet the datasheet requirements.
  3351. * If our configuration corrects A bits per B bytes and the minimum
  3352. * required correction level is X bits per Y bytes, then we must ensure
  3353. * both of the following are true:
  3354. *
  3355. * (1) A / B >= X / Y
  3356. * (2) A >= X
  3357. *
  3358. * Requirement (1) ensures we can correct for the required bitflip density.
  3359. * Requirement (2) ensures we can correct even when all bitflips are clumped
  3360. * in the same sector.
  3361. */
  3362. static bool nand_ecc_strength_good(struct mtd_info *mtd)
  3363. {
  3364. struct nand_chip *chip = mtd_to_nand(mtd);
  3365. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3366. int corr, ds_corr;
  3367. if (ecc->size == 0 || chip->ecc_step_ds == 0)
  3368. /* Not enough information */
  3369. return true;
  3370. /*
  3371. * We get the number of corrected bits per page to compare
  3372. * the correction density.
  3373. */
  3374. corr = (mtd->writesize * ecc->strength) / ecc->size;
  3375. ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
  3376. return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
  3377. }
  3378. /**
  3379. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  3380. * @mtd: MTD device structure
  3381. *
  3382. * This is the second phase of the normal nand_scan() function. It fills out
  3383. * all the uninitialized function pointers with the defaults and scans for a
  3384. * bad block table if appropriate.
  3385. */
  3386. int nand_scan_tail(struct mtd_info *mtd)
  3387. {
  3388. int i;
  3389. struct nand_chip *chip = mtd_to_nand(mtd);
  3390. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3391. struct nand_buffers *nbuf;
  3392. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  3393. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  3394. !(chip->bbt_options & NAND_BBT_USE_FLASH));
  3395. if (!(chip->options & NAND_OWN_BUFFERS)) {
  3396. nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
  3397. chip->buffers = nbuf;
  3398. } else {
  3399. if (!chip->buffers)
  3400. return -ENOMEM;
  3401. }
  3402. /* Set the internal oob buffer location, just after the page data */
  3403. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  3404. /*
  3405. * If no default placement scheme is given, select an appropriate one.
  3406. */
  3407. if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  3408. switch (mtd->oobsize) {
  3409. case 8:
  3410. ecc->layout = &nand_oob_8;
  3411. break;
  3412. case 16:
  3413. ecc->layout = &nand_oob_16;
  3414. break;
  3415. case 64:
  3416. ecc->layout = &nand_oob_64;
  3417. break;
  3418. case 128:
  3419. ecc->layout = &nand_oob_128;
  3420. break;
  3421. default:
  3422. pr_warn("No oob scheme defined for oobsize %d\n",
  3423. mtd->oobsize);
  3424. BUG();
  3425. }
  3426. }
  3427. if (!chip->write_page)
  3428. chip->write_page = nand_write_page;
  3429. /*
  3430. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  3431. * selected and we have 256 byte pagesize fallback to software ECC
  3432. */
  3433. switch (ecc->mode) {
  3434. case NAND_ECC_HW_OOB_FIRST:
  3435. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  3436. if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
  3437. pr_warn("No ECC functions supplied; hardware ECC not possible\n");
  3438. BUG();
  3439. }
  3440. if (!ecc->read_page)
  3441. ecc->read_page = nand_read_page_hwecc_oob_first;
  3442. case NAND_ECC_HW:
  3443. /* Use standard hwecc read page function? */
  3444. if (!ecc->read_page)
  3445. ecc->read_page = nand_read_page_hwecc;
  3446. if (!ecc->write_page)
  3447. ecc->write_page = nand_write_page_hwecc;
  3448. if (!ecc->read_page_raw)
  3449. ecc->read_page_raw = nand_read_page_raw;
  3450. if (!ecc->write_page_raw)
  3451. ecc->write_page_raw = nand_write_page_raw;
  3452. if (!ecc->read_oob)
  3453. ecc->read_oob = nand_read_oob_std;
  3454. if (!ecc->write_oob)
  3455. ecc->write_oob = nand_write_oob_std;
  3456. if (!ecc->read_subpage)
  3457. ecc->read_subpage = nand_read_subpage;
  3458. if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
  3459. ecc->write_subpage = nand_write_subpage_hwecc;
  3460. case NAND_ECC_HW_SYNDROME:
  3461. if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
  3462. (!ecc->read_page ||
  3463. ecc->read_page == nand_read_page_hwecc ||
  3464. !ecc->write_page ||
  3465. ecc->write_page == nand_write_page_hwecc)) {
  3466. pr_warn("No ECC functions supplied; hardware ECC not possible\n");
  3467. BUG();
  3468. }
  3469. /* Use standard syndrome read/write page function? */
  3470. if (!ecc->read_page)
  3471. ecc->read_page = nand_read_page_syndrome;
  3472. if (!ecc->write_page)
  3473. ecc->write_page = nand_write_page_syndrome;
  3474. if (!ecc->read_page_raw)
  3475. ecc->read_page_raw = nand_read_page_raw_syndrome;
  3476. if (!ecc->write_page_raw)
  3477. ecc->write_page_raw = nand_write_page_raw_syndrome;
  3478. if (!ecc->read_oob)
  3479. ecc->read_oob = nand_read_oob_syndrome;
  3480. if (!ecc->write_oob)
  3481. ecc->write_oob = nand_write_oob_syndrome;
  3482. if (mtd->writesize >= ecc->size) {
  3483. if (!ecc->strength) {
  3484. pr_warn("Driver must set ecc.strength when using hardware ECC\n");
  3485. BUG();
  3486. }
  3487. break;
  3488. }
  3489. pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
  3490. ecc->size, mtd->writesize);
  3491. ecc->mode = NAND_ECC_SOFT;
  3492. case NAND_ECC_SOFT:
  3493. ecc->calculate = nand_calculate_ecc;
  3494. ecc->correct = nand_correct_data;
  3495. ecc->read_page = nand_read_page_swecc;
  3496. ecc->read_subpage = nand_read_subpage;
  3497. ecc->write_page = nand_write_page_swecc;
  3498. ecc->read_page_raw = nand_read_page_raw;
  3499. ecc->write_page_raw = nand_write_page_raw;
  3500. ecc->read_oob = nand_read_oob_std;
  3501. ecc->write_oob = nand_write_oob_std;
  3502. if (!ecc->size)
  3503. ecc->size = 256;
  3504. ecc->bytes = 3;
  3505. ecc->strength = 1;
  3506. break;
  3507. case NAND_ECC_SOFT_BCH:
  3508. if (!mtd_nand_has_bch()) {
  3509. pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  3510. BUG();
  3511. }
  3512. ecc->calculate = nand_bch_calculate_ecc;
  3513. ecc->correct = nand_bch_correct_data;
  3514. ecc->read_page = nand_read_page_swecc;
  3515. ecc->read_subpage = nand_read_subpage;
  3516. ecc->write_page = nand_write_page_swecc;
  3517. ecc->read_page_raw = nand_read_page_raw;
  3518. ecc->write_page_raw = nand_write_page_raw;
  3519. ecc->read_oob = nand_read_oob_std;
  3520. ecc->write_oob = nand_write_oob_std;
  3521. /*
  3522. * Board driver should supply ecc.size and ecc.strength values
  3523. * to select how many bits are correctable. Otherwise, default
  3524. * to 4 bits for large page devices.
  3525. */
  3526. if (!ecc->size && (mtd->oobsize >= 64)) {
  3527. ecc->size = 512;
  3528. ecc->strength = 4;
  3529. }
  3530. /* See nand_bch_init() for details. */
  3531. ecc->bytes = 0;
  3532. ecc->priv = nand_bch_init(mtd);
  3533. if (!ecc->priv) {
  3534. pr_warn("BCH ECC initialization failed!\n");
  3535. BUG();
  3536. }
  3537. break;
  3538. case NAND_ECC_NONE:
  3539. pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
  3540. ecc->read_page = nand_read_page_raw;
  3541. ecc->write_page = nand_write_page_raw;
  3542. ecc->read_oob = nand_read_oob_std;
  3543. ecc->read_page_raw = nand_read_page_raw;
  3544. ecc->write_page_raw = nand_write_page_raw;
  3545. ecc->write_oob = nand_write_oob_std;
  3546. ecc->size = mtd->writesize;
  3547. ecc->bytes = 0;
  3548. ecc->strength = 0;
  3549. break;
  3550. default:
  3551. pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
  3552. BUG();
  3553. }
  3554. /* For many systems, the standard OOB write also works for raw */
  3555. if (!ecc->read_oob_raw)
  3556. ecc->read_oob_raw = ecc->read_oob;
  3557. if (!ecc->write_oob_raw)
  3558. ecc->write_oob_raw = ecc->write_oob;
  3559. /*
  3560. * The number of bytes available for a client to place data into
  3561. * the out of band area.
  3562. */
  3563. mtd->oobavail = 0;
  3564. if (ecc->layout) {
  3565. for (i = 0; ecc->layout->oobfree[i].length; i++)
  3566. mtd->oobavail += ecc->layout->oobfree[i].length;
  3567. }
  3568. /* ECC sanity check: warn if it's too weak */
  3569. if (!nand_ecc_strength_good(mtd))
  3570. pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  3571. mtd->name);
  3572. /*
  3573. * Set the number of read / write steps for one page depending on ECC
  3574. * mode.
  3575. */
  3576. ecc->steps = mtd->writesize / ecc->size;
  3577. if (ecc->steps * ecc->size != mtd->writesize) {
  3578. pr_warn("Invalid ECC parameters\n");
  3579. BUG();
  3580. }
  3581. ecc->total = ecc->steps * ecc->bytes;
  3582. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  3583. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
  3584. switch (ecc->steps) {
  3585. case 2:
  3586. mtd->subpage_sft = 1;
  3587. break;
  3588. case 4:
  3589. case 8:
  3590. case 16:
  3591. mtd->subpage_sft = 2;
  3592. break;
  3593. }
  3594. }
  3595. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3596. /* Initialize state */
  3597. chip->state = FL_READY;
  3598. /* Invalidate the pagebuffer reference */
  3599. chip->pagebuf = -1;
  3600. /* Large page NAND with SOFT_ECC should support subpage reads */
  3601. switch (ecc->mode) {
  3602. case NAND_ECC_SOFT:
  3603. case NAND_ECC_SOFT_BCH:
  3604. if (chip->page_shift > 9)
  3605. chip->options |= NAND_SUBPAGE_READ;
  3606. break;
  3607. default:
  3608. break;
  3609. }
  3610. /* Fill in remaining MTD driver data */
  3611. mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
  3612. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  3613. MTD_CAP_NANDFLASH;
  3614. mtd->_erase = nand_erase;
  3615. mtd->_read = nand_read;
  3616. mtd->_write = nand_write;
  3617. mtd->_panic_write = panic_nand_write;
  3618. mtd->_read_oob = nand_read_oob;
  3619. mtd->_write_oob = nand_write_oob;
  3620. mtd->_sync = nand_sync;
  3621. mtd->_lock = NULL;
  3622. mtd->_unlock = NULL;
  3623. mtd->_block_isreserved = nand_block_isreserved;
  3624. mtd->_block_isbad = nand_block_isbad;
  3625. mtd->_block_markbad = nand_block_markbad;
  3626. mtd->writebufsize = mtd->writesize;
  3627. /* propagate ecc info to mtd_info */
  3628. mtd->ecclayout = ecc->layout;
  3629. mtd->ecc_strength = ecc->strength;
  3630. mtd->ecc_step_size = ecc->size;
  3631. /*
  3632. * Initialize bitflip_threshold to its default prior scan_bbt() call.
  3633. * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
  3634. * properly set.
  3635. */
  3636. if (!mtd->bitflip_threshold)
  3637. mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
  3638. return 0;
  3639. }
  3640. EXPORT_SYMBOL(nand_scan_tail);
  3641. /**
  3642. * nand_scan - [NAND Interface] Scan for the NAND device
  3643. * @mtd: MTD device structure
  3644. * @maxchips: number of chips to scan for
  3645. *
  3646. * This fills out all the uninitialized function pointers with the defaults.
  3647. * The flash ID is read and the mtd/chip structures are filled with the
  3648. * appropriate values.
  3649. */
  3650. int nand_scan(struct mtd_info *mtd, int maxchips)
  3651. {
  3652. int ret;
  3653. ret = nand_scan_ident(mtd, maxchips, NULL);
  3654. if (!ret)
  3655. ret = nand_scan_tail(mtd);
  3656. return ret;
  3657. }
  3658. EXPORT_SYMBOL(nand_scan);
  3659. MODULE_LICENSE("GPL");
  3660. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3661. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3662. MODULE_DESCRIPTION("Generic NAND flash driver code");