nand_base.c 102 KB

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