nand_base.c 90 KB

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