nand_base.c 111 KB

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