nand_base.c 108 KB

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