nand_base.c 110 KB

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