nand_base.c 110 KB

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