nand_base.c 95 KB

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