nand_base.c 95 KB

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