nand_base.c 92 KB

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