nand_base.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  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. !(mtd->ecc_stats.failed - stats.failed))
  1312. chip->pagebuf = realpage;
  1313. memcpy(buf, chip->buffers->databuf + col, bytes);
  1314. }
  1315. buf += bytes;
  1316. if (unlikely(oob)) {
  1317. int toread = min(oobreadlen, max_oobsize);
  1318. if (toread) {
  1319. oob = nand_transfer_oob(chip,
  1320. oob, ops, toread);
  1321. oobreadlen -= toread;
  1322. }
  1323. }
  1324. if (!(chip->options & NAND_NO_READRDY)) {
  1325. /*
  1326. * Apply delay or wait for ready/busy pin. Do
  1327. * this before the AUTOINCR check, so no
  1328. * problems arise if a chip which does auto
  1329. * increment is marked as NOAUTOINCR by the
  1330. * board driver.
  1331. */
  1332. if (!chip->dev_ready)
  1333. udelay(chip->chip_delay);
  1334. else
  1335. nand_wait_ready(mtd);
  1336. }
  1337. } else {
  1338. memcpy(buf, chip->buffers->databuf + col, bytes);
  1339. buf += bytes;
  1340. }
  1341. readlen -= bytes;
  1342. if (!readlen)
  1343. break;
  1344. /* For subsequent reads align to page boundary. */
  1345. col = 0;
  1346. /* Increment page address */
  1347. realpage++;
  1348. page = realpage & chip->pagemask;
  1349. /* Check, if we cross a chip boundary */
  1350. if (!page) {
  1351. chipnr++;
  1352. chip->select_chip(mtd, -1);
  1353. chip->select_chip(mtd, chipnr);
  1354. }
  1355. /* Check, if the chip supports auto page increment
  1356. * or if we have hit a block boundary.
  1357. */
  1358. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1359. sndcmd = 1;
  1360. }
  1361. ops->retlen = ops->len - (size_t) readlen;
  1362. if (oob)
  1363. ops->oobretlen = ops->ooblen - oobreadlen;
  1364. if (ret)
  1365. return ret;
  1366. if (mtd->ecc_stats.failed - stats.failed)
  1367. return -EBADMSG;
  1368. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1369. }
  1370. /**
  1371. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  1372. * @mtd: MTD device structure
  1373. * @from: offset to read from
  1374. * @len: number of bytes to read
  1375. * @retlen: pointer to variable to store the number of read bytes
  1376. * @buf: the databuffer to put data
  1377. *
  1378. * Get hold of the chip and call nand_do_read
  1379. */
  1380. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1381. size_t *retlen, uint8_t *buf)
  1382. {
  1383. struct nand_chip *chip = mtd->priv;
  1384. int ret;
  1385. /* Do not allow reads past end of device */
  1386. if ((from + len) > mtd->size)
  1387. return -EINVAL;
  1388. if (!len)
  1389. return 0;
  1390. nand_get_device(chip, mtd, FL_READING);
  1391. chip->ops.len = len;
  1392. chip->ops.datbuf = buf;
  1393. chip->ops.oobbuf = NULL;
  1394. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1395. *retlen = chip->ops.retlen;
  1396. nand_release_device(mtd);
  1397. return ret;
  1398. }
  1399. /**
  1400. * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
  1401. * @mtd: mtd info structure
  1402. * @chip: nand chip info structure
  1403. * @page: page number to read
  1404. * @sndcmd: flag whether to issue read command or not
  1405. */
  1406. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1407. int page, int sndcmd)
  1408. {
  1409. if (sndcmd) {
  1410. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1411. sndcmd = 0;
  1412. }
  1413. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1414. return sndcmd;
  1415. }
  1416. /**
  1417. * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
  1418. * with syndromes
  1419. * @mtd: mtd info structure
  1420. * @chip: nand chip info structure
  1421. * @page: page number to read
  1422. * @sndcmd: flag whether to issue read command or not
  1423. */
  1424. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1425. int page, int sndcmd)
  1426. {
  1427. uint8_t *buf = chip->oob_poi;
  1428. int length = mtd->oobsize;
  1429. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1430. int eccsize = chip->ecc.size;
  1431. uint8_t *bufpoi = buf;
  1432. int i, toread, sndrnd = 0, pos;
  1433. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1434. for (i = 0; i < chip->ecc.steps; i++) {
  1435. if (sndrnd) {
  1436. pos = eccsize + i * (eccsize + chunk);
  1437. if (mtd->writesize > 512)
  1438. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1439. else
  1440. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1441. } else
  1442. sndrnd = 1;
  1443. toread = min_t(int, length, chunk);
  1444. chip->read_buf(mtd, bufpoi, toread);
  1445. bufpoi += toread;
  1446. length -= toread;
  1447. }
  1448. if (length > 0)
  1449. chip->read_buf(mtd, bufpoi, length);
  1450. return 1;
  1451. }
  1452. /**
  1453. * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
  1454. * @mtd: mtd info structure
  1455. * @chip: nand chip info structure
  1456. * @page: page number to write
  1457. */
  1458. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1459. int page)
  1460. {
  1461. int status = 0;
  1462. const uint8_t *buf = chip->oob_poi;
  1463. int length = mtd->oobsize;
  1464. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1465. chip->write_buf(mtd, buf, length);
  1466. /* Send command to program the OOB data */
  1467. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1468. status = chip->waitfunc(mtd, chip);
  1469. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1470. }
  1471. /**
  1472. * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
  1473. * with syndrome - only for large page flash !
  1474. * @mtd: mtd info structure
  1475. * @chip: nand chip info structure
  1476. * @page: page number to write
  1477. */
  1478. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1479. struct nand_chip *chip, int page)
  1480. {
  1481. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1482. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1483. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1484. const uint8_t *bufpoi = chip->oob_poi;
  1485. /*
  1486. * data-ecc-data-ecc ... ecc-oob
  1487. * or
  1488. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1489. */
  1490. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1491. pos = steps * (eccsize + chunk);
  1492. steps = 0;
  1493. } else
  1494. pos = eccsize;
  1495. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1496. for (i = 0; i < steps; i++) {
  1497. if (sndcmd) {
  1498. if (mtd->writesize <= 512) {
  1499. uint32_t fill = 0xFFFFFFFF;
  1500. len = eccsize;
  1501. while (len > 0) {
  1502. int num = min_t(int, len, 4);
  1503. chip->write_buf(mtd, (uint8_t *)&fill,
  1504. num);
  1505. len -= num;
  1506. }
  1507. } else {
  1508. pos = eccsize + i * (eccsize + chunk);
  1509. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1510. }
  1511. } else
  1512. sndcmd = 1;
  1513. len = min_t(int, length, chunk);
  1514. chip->write_buf(mtd, bufpoi, len);
  1515. bufpoi += len;
  1516. length -= len;
  1517. }
  1518. if (length > 0)
  1519. chip->write_buf(mtd, bufpoi, length);
  1520. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1521. status = chip->waitfunc(mtd, chip);
  1522. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1523. }
  1524. /**
  1525. * nand_do_read_oob - [Intern] NAND read out-of-band
  1526. * @mtd: MTD device structure
  1527. * @from: offset to read from
  1528. * @ops: oob operations description structure
  1529. *
  1530. * NAND read out-of-band data from the spare area
  1531. */
  1532. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1533. struct mtd_oob_ops *ops)
  1534. {
  1535. int page, realpage, chipnr, sndcmd = 1;
  1536. struct nand_chip *chip = mtd->priv;
  1537. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1538. int readlen = ops->ooblen;
  1539. int len;
  1540. uint8_t *buf = ops->oobbuf;
  1541. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
  1542. __func__, (unsigned long long)from, readlen);
  1543. if (ops->mode == MTD_OOB_AUTO)
  1544. len = chip->ecc.layout->oobavail;
  1545. else
  1546. len = mtd->oobsize;
  1547. if (unlikely(ops->ooboffs >= len)) {
  1548. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
  1549. "outside oob\n", __func__);
  1550. return -EINVAL;
  1551. }
  1552. /* Do not allow reads past end of device */
  1553. if (unlikely(from >= mtd->size ||
  1554. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1555. (from >> chip->page_shift)) * len)) {
  1556. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
  1557. "of device\n", __func__);
  1558. return -EINVAL;
  1559. }
  1560. chipnr = (int)(from >> chip->chip_shift);
  1561. chip->select_chip(mtd, chipnr);
  1562. /* Shift to get page */
  1563. realpage = (int)(from >> chip->page_shift);
  1564. page = realpage & chip->pagemask;
  1565. while (1) {
  1566. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1567. len = min(len, readlen);
  1568. buf = nand_transfer_oob(chip, buf, ops, len);
  1569. if (!(chip->options & NAND_NO_READRDY)) {
  1570. /*
  1571. * Apply delay or wait for ready/busy pin. Do this
  1572. * before the AUTOINCR check, so no problems arise if a
  1573. * chip which does auto increment is marked as
  1574. * NOAUTOINCR by the board driver.
  1575. */
  1576. if (!chip->dev_ready)
  1577. udelay(chip->chip_delay);
  1578. else
  1579. nand_wait_ready(mtd);
  1580. }
  1581. readlen -= len;
  1582. if (!readlen)
  1583. break;
  1584. /* Increment page address */
  1585. realpage++;
  1586. page = realpage & chip->pagemask;
  1587. /* Check, if we cross a chip boundary */
  1588. if (!page) {
  1589. chipnr++;
  1590. chip->select_chip(mtd, -1);
  1591. chip->select_chip(mtd, chipnr);
  1592. }
  1593. /* Check, if the chip supports auto page increment
  1594. * or if we have hit a block boundary.
  1595. */
  1596. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1597. sndcmd = 1;
  1598. }
  1599. ops->oobretlen = ops->ooblen;
  1600. return 0;
  1601. }
  1602. /**
  1603. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1604. * @mtd: MTD device structure
  1605. * @from: offset to read from
  1606. * @ops: oob operation description structure
  1607. *
  1608. * NAND read data and/or out-of-band data
  1609. */
  1610. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1611. struct mtd_oob_ops *ops)
  1612. {
  1613. struct nand_chip *chip = mtd->priv;
  1614. int ret = -ENOTSUPP;
  1615. ops->retlen = 0;
  1616. /* Do not allow reads past end of device */
  1617. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1618. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
  1619. "beyond end of device\n", __func__);
  1620. return -EINVAL;
  1621. }
  1622. nand_get_device(chip, mtd, FL_READING);
  1623. switch (ops->mode) {
  1624. case MTD_OOB_PLACE:
  1625. case MTD_OOB_AUTO:
  1626. case MTD_OOB_RAW:
  1627. break;
  1628. default:
  1629. goto out;
  1630. }
  1631. if (!ops->datbuf)
  1632. ret = nand_do_read_oob(mtd, from, ops);
  1633. else
  1634. ret = nand_do_read_ops(mtd, from, ops);
  1635. out:
  1636. nand_release_device(mtd);
  1637. return ret;
  1638. }
  1639. /**
  1640. * nand_write_page_raw - [Intern] raw page write function
  1641. * @mtd: mtd info structure
  1642. * @chip: nand chip info structure
  1643. * @buf: data buffer
  1644. *
  1645. * Not for syndrome calculating ecc controllers, which use a special oob layout
  1646. */
  1647. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1648. const uint8_t *buf)
  1649. {
  1650. chip->write_buf(mtd, buf, mtd->writesize);
  1651. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1652. }
  1653. /**
  1654. * nand_write_page_raw_syndrome - [Intern] raw page write function
  1655. * @mtd: mtd info structure
  1656. * @chip: nand chip info structure
  1657. * @buf: data buffer
  1658. *
  1659. * We need a special oob layout and handling even when ECC isn't checked.
  1660. */
  1661. static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1662. struct nand_chip *chip,
  1663. const uint8_t *buf)
  1664. {
  1665. int eccsize = chip->ecc.size;
  1666. int eccbytes = chip->ecc.bytes;
  1667. uint8_t *oob = chip->oob_poi;
  1668. int steps, size;
  1669. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1670. chip->write_buf(mtd, buf, eccsize);
  1671. buf += eccsize;
  1672. if (chip->ecc.prepad) {
  1673. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1674. oob += chip->ecc.prepad;
  1675. }
  1676. chip->read_buf(mtd, oob, eccbytes);
  1677. oob += eccbytes;
  1678. if (chip->ecc.postpad) {
  1679. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1680. oob += chip->ecc.postpad;
  1681. }
  1682. }
  1683. size = mtd->oobsize - (oob - chip->oob_poi);
  1684. if (size)
  1685. chip->write_buf(mtd, oob, size);
  1686. }
  1687. /**
  1688. * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  1689. * @mtd: mtd info structure
  1690. * @chip: nand chip info structure
  1691. * @buf: data buffer
  1692. */
  1693. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1694. const uint8_t *buf)
  1695. {
  1696. int i, eccsize = chip->ecc.size;
  1697. int eccbytes = chip->ecc.bytes;
  1698. int eccsteps = chip->ecc.steps;
  1699. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1700. const uint8_t *p = buf;
  1701. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1702. /* Software ecc calculation */
  1703. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1704. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1705. for (i = 0; i < chip->ecc.total; i++)
  1706. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1707. chip->ecc.write_page_raw(mtd, chip, buf);
  1708. }
  1709. /**
  1710. * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  1711. * @mtd: mtd info structure
  1712. * @chip: nand chip info structure
  1713. * @buf: data buffer
  1714. */
  1715. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1716. const uint8_t *buf)
  1717. {
  1718. int i, eccsize = chip->ecc.size;
  1719. int eccbytes = chip->ecc.bytes;
  1720. int eccsteps = chip->ecc.steps;
  1721. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1722. const uint8_t *p = buf;
  1723. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1724. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1725. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1726. chip->write_buf(mtd, p, eccsize);
  1727. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1728. }
  1729. for (i = 0; i < chip->ecc.total; i++)
  1730. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1731. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1732. }
  1733. /**
  1734. * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  1735. * @mtd: mtd info structure
  1736. * @chip: nand chip info structure
  1737. * @buf: data buffer
  1738. *
  1739. * The hw generator calculates the error syndrome automatically. Therefor
  1740. * we need a special oob layout and handling.
  1741. */
  1742. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1743. struct nand_chip *chip, const uint8_t *buf)
  1744. {
  1745. int i, eccsize = chip->ecc.size;
  1746. int eccbytes = chip->ecc.bytes;
  1747. int eccsteps = chip->ecc.steps;
  1748. const uint8_t *p = buf;
  1749. uint8_t *oob = chip->oob_poi;
  1750. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1751. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1752. chip->write_buf(mtd, p, eccsize);
  1753. if (chip->ecc.prepad) {
  1754. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1755. oob += chip->ecc.prepad;
  1756. }
  1757. chip->ecc.calculate(mtd, p, oob);
  1758. chip->write_buf(mtd, oob, eccbytes);
  1759. oob += eccbytes;
  1760. if (chip->ecc.postpad) {
  1761. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1762. oob += chip->ecc.postpad;
  1763. }
  1764. }
  1765. /* Calculate remaining oob bytes */
  1766. i = mtd->oobsize - (oob - chip->oob_poi);
  1767. if (i)
  1768. chip->write_buf(mtd, oob, i);
  1769. }
  1770. /**
  1771. * nand_write_page - [REPLACEABLE] write one page
  1772. * @mtd: MTD device structure
  1773. * @chip: NAND chip descriptor
  1774. * @buf: the data to write
  1775. * @page: page number to write
  1776. * @cached: cached programming
  1777. * @raw: use _raw version of write_page
  1778. */
  1779. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1780. const uint8_t *buf, int page, int cached, int raw)
  1781. {
  1782. int status;
  1783. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1784. if (unlikely(raw))
  1785. chip->ecc.write_page_raw(mtd, chip, buf);
  1786. else
  1787. chip->ecc.write_page(mtd, chip, buf);
  1788. /*
  1789. * Cached progamming disabled for now, Not sure if its worth the
  1790. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  1791. */
  1792. cached = 0;
  1793. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1794. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1795. status = chip->waitfunc(mtd, chip);
  1796. /*
  1797. * See if operation failed and additional status checks are
  1798. * available
  1799. */
  1800. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1801. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1802. page);
  1803. if (status & NAND_STATUS_FAIL)
  1804. return -EIO;
  1805. } else {
  1806. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1807. status = chip->waitfunc(mtd, chip);
  1808. }
  1809. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1810. /* Send command to read back the data */
  1811. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1812. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1813. return -EIO;
  1814. #endif
  1815. return 0;
  1816. }
  1817. /**
  1818. * nand_fill_oob - [Internal] Transfer client buffer to oob
  1819. * @chip: nand chip structure
  1820. * @oob: oob data buffer
  1821. * @len: oob data write length
  1822. * @ops: oob ops structure
  1823. */
  1824. static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
  1825. struct mtd_oob_ops *ops)
  1826. {
  1827. switch (ops->mode) {
  1828. case MTD_OOB_PLACE:
  1829. case MTD_OOB_RAW:
  1830. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1831. return oob + len;
  1832. case MTD_OOB_AUTO: {
  1833. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1834. uint32_t boffs = 0, woffs = ops->ooboffs;
  1835. size_t bytes = 0;
  1836. for (; free->length && len; free++, len -= bytes) {
  1837. /* Write request not from offset 0 ? */
  1838. if (unlikely(woffs)) {
  1839. if (woffs >= free->length) {
  1840. woffs -= free->length;
  1841. continue;
  1842. }
  1843. boffs = free->offset + woffs;
  1844. bytes = min_t(size_t, len,
  1845. (free->length - woffs));
  1846. woffs = 0;
  1847. } else {
  1848. bytes = min_t(size_t, len, free->length);
  1849. boffs = free->offset;
  1850. }
  1851. memcpy(chip->oob_poi + boffs, oob, bytes);
  1852. oob += bytes;
  1853. }
  1854. return oob;
  1855. }
  1856. default:
  1857. BUG();
  1858. }
  1859. return NULL;
  1860. }
  1861. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  1862. /**
  1863. * nand_do_write_ops - [Internal] NAND write with ECC
  1864. * @mtd: MTD device structure
  1865. * @to: offset to write to
  1866. * @ops: oob operations description structure
  1867. *
  1868. * NAND write with ECC
  1869. */
  1870. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1871. struct mtd_oob_ops *ops)
  1872. {
  1873. int chipnr, realpage, page, blockmask, column;
  1874. struct nand_chip *chip = mtd->priv;
  1875. uint32_t writelen = ops->len;
  1876. uint32_t oobwritelen = ops->ooblen;
  1877. uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
  1878. mtd->oobavail : mtd->oobsize;
  1879. uint8_t *oob = ops->oobbuf;
  1880. uint8_t *buf = ops->datbuf;
  1881. int ret, subpage;
  1882. ops->retlen = 0;
  1883. if (!writelen)
  1884. return 0;
  1885. /* reject writes, which are not page aligned */
  1886. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1887. printk(KERN_NOTICE "%s: Attempt to write not "
  1888. "page aligned data\n", __func__);
  1889. return -EINVAL;
  1890. }
  1891. column = to & (mtd->writesize - 1);
  1892. subpage = column || (writelen & (mtd->writesize - 1));
  1893. if (subpage && oob)
  1894. return -EINVAL;
  1895. chipnr = (int)(to >> chip->chip_shift);
  1896. chip->select_chip(mtd, chipnr);
  1897. /* Check, if it is write protected */
  1898. if (nand_check_wp(mtd))
  1899. return -EIO;
  1900. realpage = (int)(to >> chip->page_shift);
  1901. page = realpage & chip->pagemask;
  1902. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1903. /* Invalidate the page cache, when we write to the cached page */
  1904. if (to <= (chip->pagebuf << chip->page_shift) &&
  1905. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1906. chip->pagebuf = -1;
  1907. /* If we're not given explicit OOB data, let it be 0xFF */
  1908. if (likely(!oob))
  1909. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1910. /* Don't allow multipage oob writes with offset */
  1911. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
  1912. return -EINVAL;
  1913. while (1) {
  1914. int bytes = mtd->writesize;
  1915. int cached = writelen > bytes && page != blockmask;
  1916. uint8_t *wbuf = buf;
  1917. /* Partial page write ? */
  1918. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1919. cached = 0;
  1920. bytes = min_t(int, bytes - column, (int) writelen);
  1921. chip->pagebuf = -1;
  1922. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1923. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1924. wbuf = chip->buffers->databuf;
  1925. }
  1926. if (unlikely(oob)) {
  1927. size_t len = min(oobwritelen, oobmaxlen);
  1928. oob = nand_fill_oob(chip, oob, len, ops);
  1929. oobwritelen -= len;
  1930. }
  1931. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1932. (ops->mode == MTD_OOB_RAW));
  1933. if (ret)
  1934. break;
  1935. writelen -= bytes;
  1936. if (!writelen)
  1937. break;
  1938. column = 0;
  1939. buf += bytes;
  1940. realpage++;
  1941. page = realpage & chip->pagemask;
  1942. /* Check, if we cross a chip boundary */
  1943. if (!page) {
  1944. chipnr++;
  1945. chip->select_chip(mtd, -1);
  1946. chip->select_chip(mtd, chipnr);
  1947. }
  1948. }
  1949. ops->retlen = ops->len - writelen;
  1950. if (unlikely(oob))
  1951. ops->oobretlen = ops->ooblen;
  1952. return ret;
  1953. }
  1954. /**
  1955. * panic_nand_write - [MTD Interface] NAND write with ECC
  1956. * @mtd: MTD device structure
  1957. * @to: offset to write to
  1958. * @len: number of bytes to write
  1959. * @retlen: pointer to variable to store the number of written bytes
  1960. * @buf: the data to write
  1961. *
  1962. * NAND write with ECC. Used when performing writes in interrupt context, this
  1963. * may for example be called by mtdoops when writing an oops while in panic.
  1964. */
  1965. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1966. size_t *retlen, const uint8_t *buf)
  1967. {
  1968. struct nand_chip *chip = mtd->priv;
  1969. int ret;
  1970. /* Do not allow reads past end of device */
  1971. if ((to + len) > mtd->size)
  1972. return -EINVAL;
  1973. if (!len)
  1974. return 0;
  1975. /* Wait for the device to get ready. */
  1976. panic_nand_wait(mtd, chip, 400);
  1977. /* Grab the device. */
  1978. panic_nand_get_device(chip, mtd, FL_WRITING);
  1979. chip->ops.len = len;
  1980. chip->ops.datbuf = (uint8_t *)buf;
  1981. chip->ops.oobbuf = NULL;
  1982. ret = nand_do_write_ops(mtd, to, &chip->ops);
  1983. *retlen = chip->ops.retlen;
  1984. return ret;
  1985. }
  1986. /**
  1987. * nand_write - [MTD Interface] NAND write with ECC
  1988. * @mtd: MTD device structure
  1989. * @to: offset to write to
  1990. * @len: number of bytes to write
  1991. * @retlen: pointer to variable to store the number of written bytes
  1992. * @buf: the data to write
  1993. *
  1994. * NAND write with ECC
  1995. */
  1996. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1997. size_t *retlen, const uint8_t *buf)
  1998. {
  1999. struct nand_chip *chip = mtd->priv;
  2000. int ret;
  2001. /* Do not allow reads past end of device */
  2002. if ((to + len) > mtd->size)
  2003. return -EINVAL;
  2004. if (!len)
  2005. return 0;
  2006. nand_get_device(chip, mtd, FL_WRITING);
  2007. chip->ops.len = len;
  2008. chip->ops.datbuf = (uint8_t *)buf;
  2009. chip->ops.oobbuf = NULL;
  2010. ret = nand_do_write_ops(mtd, to, &chip->ops);
  2011. *retlen = chip->ops.retlen;
  2012. nand_release_device(mtd);
  2013. return ret;
  2014. }
  2015. /**
  2016. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2017. * @mtd: MTD device structure
  2018. * @to: offset to write to
  2019. * @ops: oob operation description structure
  2020. *
  2021. * NAND write out-of-band
  2022. */
  2023. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2024. struct mtd_oob_ops *ops)
  2025. {
  2026. int chipnr, page, status, len;
  2027. struct nand_chip *chip = mtd->priv;
  2028. DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
  2029. __func__, (unsigned int)to, (int)ops->ooblen);
  2030. if (ops->mode == MTD_OOB_AUTO)
  2031. len = chip->ecc.layout->oobavail;
  2032. else
  2033. len = mtd->oobsize;
  2034. /* Do not allow write past end of page */
  2035. if ((ops->ooboffs + ops->ooblen) > len) {
  2036. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
  2037. "past end of page\n", __func__);
  2038. return -EINVAL;
  2039. }
  2040. if (unlikely(ops->ooboffs >= len)) {
  2041. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
  2042. "write outside oob\n", __func__);
  2043. return -EINVAL;
  2044. }
  2045. /* Do not allow reads past end of device */
  2046. if (unlikely(to >= mtd->size ||
  2047. ops->ooboffs + ops->ooblen >
  2048. ((mtd->size >> chip->page_shift) -
  2049. (to >> chip->page_shift)) * len)) {
  2050. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
  2051. "end of device\n", __func__);
  2052. return -EINVAL;
  2053. }
  2054. chipnr = (int)(to >> chip->chip_shift);
  2055. chip->select_chip(mtd, chipnr);
  2056. /* Shift to get page */
  2057. page = (int)(to >> chip->page_shift);
  2058. /*
  2059. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2060. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2061. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2062. * it in the doc2000 driver in August 1999. dwmw2.
  2063. */
  2064. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2065. /* Check, if it is write protected */
  2066. if (nand_check_wp(mtd))
  2067. return -EROFS;
  2068. /* Invalidate the page cache, if we write to the cached page */
  2069. if (page == chip->pagebuf)
  2070. chip->pagebuf = -1;
  2071. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2072. nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
  2073. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2074. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2075. if (status)
  2076. return status;
  2077. ops->oobretlen = ops->ooblen;
  2078. return 0;
  2079. }
  2080. /**
  2081. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2082. * @mtd: MTD device structure
  2083. * @to: offset to write to
  2084. * @ops: oob operation description structure
  2085. */
  2086. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2087. struct mtd_oob_ops *ops)
  2088. {
  2089. struct nand_chip *chip = mtd->priv;
  2090. int ret = -ENOTSUPP;
  2091. ops->retlen = 0;
  2092. /* Do not allow writes past end of device */
  2093. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2094. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
  2095. "end of device\n", __func__);
  2096. return -EINVAL;
  2097. }
  2098. nand_get_device(chip, mtd, FL_WRITING);
  2099. switch (ops->mode) {
  2100. case MTD_OOB_PLACE:
  2101. case MTD_OOB_AUTO:
  2102. case MTD_OOB_RAW:
  2103. break;
  2104. default:
  2105. goto out;
  2106. }
  2107. if (!ops->datbuf)
  2108. ret = nand_do_write_oob(mtd, to, ops);
  2109. else
  2110. ret = nand_do_write_ops(mtd, to, ops);
  2111. out:
  2112. nand_release_device(mtd);
  2113. return ret;
  2114. }
  2115. /**
  2116. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  2117. * @mtd: MTD device structure
  2118. * @page: the page address of the block which will be erased
  2119. *
  2120. * Standard erase command for NAND chips
  2121. */
  2122. static void single_erase_cmd(struct mtd_info *mtd, int page)
  2123. {
  2124. struct nand_chip *chip = mtd->priv;
  2125. /* Send commands to erase a block */
  2126. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2127. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2128. }
  2129. /**
  2130. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  2131. * @mtd: MTD device structure
  2132. * @page: the page address of the block which will be erased
  2133. *
  2134. * AND multi block erase command function
  2135. * Erase 4 consecutive blocks
  2136. */
  2137. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  2138. {
  2139. struct nand_chip *chip = mtd->priv;
  2140. /* Send commands to erase a block */
  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_ERASE1, -1, page);
  2145. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2146. }
  2147. /**
  2148. * nand_erase - [MTD Interface] erase block(s)
  2149. * @mtd: MTD device structure
  2150. * @instr: erase instruction
  2151. *
  2152. * Erase one ore more blocks
  2153. */
  2154. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2155. {
  2156. return nand_erase_nand(mtd, instr, 0);
  2157. }
  2158. #define BBT_PAGE_MASK 0xffffff3f
  2159. /**
  2160. * nand_erase_nand - [Internal] erase block(s)
  2161. * @mtd: MTD device structure
  2162. * @instr: erase instruction
  2163. * @allowbbt: allow erasing the bbt area
  2164. *
  2165. * Erase one ore more blocks
  2166. */
  2167. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2168. int allowbbt)
  2169. {
  2170. int page, status, pages_per_block, ret, chipnr;
  2171. struct nand_chip *chip = mtd->priv;
  2172. loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
  2173. unsigned int bbt_masked_page = 0xffffffff;
  2174. loff_t len;
  2175. DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
  2176. __func__, (unsigned long long)instr->addr,
  2177. (unsigned long long)instr->len);
  2178. if (check_offs_len(mtd, instr->addr, instr->len))
  2179. return -EINVAL;
  2180. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  2181. /* Grab the lock and see if the device is available */
  2182. nand_get_device(chip, mtd, FL_ERASING);
  2183. /* Shift to get first page */
  2184. page = (int)(instr->addr >> chip->page_shift);
  2185. chipnr = (int)(instr->addr >> chip->chip_shift);
  2186. /* Calculate pages in each block */
  2187. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2188. /* Select the NAND device */
  2189. chip->select_chip(mtd, chipnr);
  2190. /* Check, if it is write protected */
  2191. if (nand_check_wp(mtd)) {
  2192. DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
  2193. __func__);
  2194. instr->state = MTD_ERASE_FAILED;
  2195. goto erase_exit;
  2196. }
  2197. /*
  2198. * If BBT requires refresh, set the BBT page mask to see if the BBT
  2199. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  2200. * can not be matched. This is also done when the bbt is actually
  2201. * erased to avoid recusrsive updates
  2202. */
  2203. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  2204. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  2205. /* Loop through the pages */
  2206. len = instr->len;
  2207. instr->state = MTD_ERASING;
  2208. while (len) {
  2209. /*
  2210. * heck if we have a bad block, we do not erase bad blocks !
  2211. */
  2212. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2213. chip->page_shift, 0, allowbbt)) {
  2214. printk(KERN_WARNING "%s: attempt to erase a bad block "
  2215. "at page 0x%08x\n", __func__, page);
  2216. instr->state = MTD_ERASE_FAILED;
  2217. goto erase_exit;
  2218. }
  2219. /*
  2220. * Invalidate the page cache, if we erase the block which
  2221. * contains the current cached page
  2222. */
  2223. if (page <= chip->pagebuf && chip->pagebuf <
  2224. (page + pages_per_block))
  2225. chip->pagebuf = -1;
  2226. chip->erase_cmd(mtd, page & chip->pagemask);
  2227. status = chip->waitfunc(mtd, chip);
  2228. /*
  2229. * See if operation failed and additional status checks are
  2230. * available
  2231. */
  2232. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2233. status = chip->errstat(mtd, chip, FL_ERASING,
  2234. status, page);
  2235. /* See if block erase succeeded */
  2236. if (status & NAND_STATUS_FAIL) {
  2237. DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
  2238. "page 0x%08x\n", __func__, page);
  2239. instr->state = MTD_ERASE_FAILED;
  2240. instr->fail_addr =
  2241. ((loff_t)page << chip->page_shift);
  2242. goto erase_exit;
  2243. }
  2244. /*
  2245. * If BBT requires refresh, set the BBT rewrite flag to the
  2246. * page being erased
  2247. */
  2248. if (bbt_masked_page != 0xffffffff &&
  2249. (page & BBT_PAGE_MASK) == bbt_masked_page)
  2250. rewrite_bbt[chipnr] =
  2251. ((loff_t)page << chip->page_shift);
  2252. /* Increment page address and decrement length */
  2253. len -= (1 << chip->phys_erase_shift);
  2254. page += pages_per_block;
  2255. /* Check, if we cross a chip boundary */
  2256. if (len && !(page & chip->pagemask)) {
  2257. chipnr++;
  2258. chip->select_chip(mtd, -1);
  2259. chip->select_chip(mtd, chipnr);
  2260. /*
  2261. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  2262. * page mask to see if this BBT should be rewritten
  2263. */
  2264. if (bbt_masked_page != 0xffffffff &&
  2265. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  2266. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  2267. BBT_PAGE_MASK;
  2268. }
  2269. }
  2270. instr->state = MTD_ERASE_DONE;
  2271. erase_exit:
  2272. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2273. /* Deselect and wake up anyone waiting on the device */
  2274. nand_release_device(mtd);
  2275. /* Do call back function */
  2276. if (!ret)
  2277. mtd_erase_callback(instr);
  2278. /*
  2279. * If BBT requires refresh and erase was successful, rewrite any
  2280. * selected bad block tables
  2281. */
  2282. if (bbt_masked_page == 0xffffffff || ret)
  2283. return ret;
  2284. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  2285. if (!rewrite_bbt[chipnr])
  2286. continue;
  2287. /* update the BBT for chip */
  2288. DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
  2289. "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
  2290. rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
  2291. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  2292. }
  2293. /* Return more or less happy */
  2294. return ret;
  2295. }
  2296. /**
  2297. * nand_sync - [MTD Interface] sync
  2298. * @mtd: MTD device structure
  2299. *
  2300. * Sync is actually a wait for chip ready function
  2301. */
  2302. static void nand_sync(struct mtd_info *mtd)
  2303. {
  2304. struct nand_chip *chip = mtd->priv;
  2305. DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
  2306. /* Grab the lock and see if the device is available */
  2307. nand_get_device(chip, mtd, FL_SYNCING);
  2308. /* Release it and go back */
  2309. nand_release_device(mtd);
  2310. }
  2311. /**
  2312. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2313. * @mtd: MTD device structure
  2314. * @offs: offset relative to mtd start
  2315. */
  2316. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2317. {
  2318. /* Check for invalid offset */
  2319. if (offs > mtd->size)
  2320. return -EINVAL;
  2321. return nand_block_checkbad(mtd, offs, 1, 0);
  2322. }
  2323. /**
  2324. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2325. * @mtd: MTD device structure
  2326. * @ofs: offset relative to mtd start
  2327. */
  2328. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2329. {
  2330. struct nand_chip *chip = mtd->priv;
  2331. int ret;
  2332. ret = nand_block_isbad(mtd, ofs);
  2333. if (ret) {
  2334. /* If it was bad already, return success and do nothing. */
  2335. if (ret > 0)
  2336. return 0;
  2337. return ret;
  2338. }
  2339. return chip->block_markbad(mtd, ofs);
  2340. }
  2341. /**
  2342. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2343. * @mtd: MTD device structure
  2344. */
  2345. static int nand_suspend(struct mtd_info *mtd)
  2346. {
  2347. struct nand_chip *chip = mtd->priv;
  2348. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2349. }
  2350. /**
  2351. * nand_resume - [MTD Interface] Resume the NAND flash
  2352. * @mtd: MTD device structure
  2353. */
  2354. static void nand_resume(struct mtd_info *mtd)
  2355. {
  2356. struct nand_chip *chip = mtd->priv;
  2357. if (chip->state == FL_PM_SUSPENDED)
  2358. nand_release_device(mtd);
  2359. else
  2360. printk(KERN_ERR "%s called for a chip which is not "
  2361. "in suspended state\n", __func__);
  2362. }
  2363. /*
  2364. * Set default functions
  2365. */
  2366. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2367. {
  2368. /* check for proper chip_delay setup, set 20us if not */
  2369. if (!chip->chip_delay)
  2370. chip->chip_delay = 20;
  2371. /* check, if a user supplied command function given */
  2372. if (chip->cmdfunc == NULL)
  2373. chip->cmdfunc = nand_command;
  2374. /* check, if a user supplied wait function given */
  2375. if (chip->waitfunc == NULL)
  2376. chip->waitfunc = nand_wait;
  2377. if (!chip->select_chip)
  2378. chip->select_chip = nand_select_chip;
  2379. if (!chip->read_byte)
  2380. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2381. if (!chip->read_word)
  2382. chip->read_word = nand_read_word;
  2383. if (!chip->block_bad)
  2384. chip->block_bad = nand_block_bad;
  2385. if (!chip->block_markbad)
  2386. chip->block_markbad = nand_default_block_markbad;
  2387. if (!chip->write_buf)
  2388. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2389. if (!chip->read_buf)
  2390. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2391. if (!chip->verify_buf)
  2392. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2393. if (!chip->scan_bbt)
  2394. chip->scan_bbt = nand_default_bbt;
  2395. if (!chip->controller) {
  2396. chip->controller = &chip->hwcontrol;
  2397. spin_lock_init(&chip->controller->lock);
  2398. init_waitqueue_head(&chip->controller->wq);
  2399. }
  2400. }
  2401. /*
  2402. * sanitize ONFI strings so we can safely print them
  2403. */
  2404. static void sanitize_string(uint8_t *s, size_t len)
  2405. {
  2406. ssize_t i;
  2407. /* null terminate */
  2408. s[len - 1] = 0;
  2409. /* remove non printable chars */
  2410. for (i = 0; i < len - 1; i++) {
  2411. if (s[i] < ' ' || s[i] > 127)
  2412. s[i] = '?';
  2413. }
  2414. /* remove trailing spaces */
  2415. strim(s);
  2416. }
  2417. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2418. {
  2419. int i;
  2420. while (len--) {
  2421. crc ^= *p++ << 8;
  2422. for (i = 0; i < 8; i++)
  2423. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2424. }
  2425. return crc;
  2426. }
  2427. /*
  2428. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise
  2429. */
  2430. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2431. int busw)
  2432. {
  2433. struct nand_onfi_params *p = &chip->onfi_params;
  2434. int i;
  2435. int val;
  2436. /* try ONFI for unknow chip or LP */
  2437. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2438. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2439. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2440. return 0;
  2441. printk(KERN_INFO "ONFI flash detected\n");
  2442. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2443. for (i = 0; i < 3; i++) {
  2444. chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
  2445. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2446. le16_to_cpu(p->crc)) {
  2447. printk(KERN_INFO "ONFI param page %d valid\n", i);
  2448. break;
  2449. }
  2450. }
  2451. if (i == 3)
  2452. return 0;
  2453. /* check version */
  2454. val = le16_to_cpu(p->revision);
  2455. if (val == 1 || val > (1 << 4)) {
  2456. printk(KERN_INFO "%s: unsupported ONFI version: %d\n",
  2457. __func__, val);
  2458. return 0;
  2459. }
  2460. if (val & (1 << 4))
  2461. chip->onfi_version = 22;
  2462. else if (val & (1 << 3))
  2463. chip->onfi_version = 21;
  2464. else if (val & (1 << 2))
  2465. chip->onfi_version = 20;
  2466. else
  2467. chip->onfi_version = 10;
  2468. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2469. sanitize_string(p->model, sizeof(p->model));
  2470. if (!mtd->name)
  2471. mtd->name = p->model;
  2472. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2473. mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
  2474. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2475. chip->chipsize = le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
  2476. busw = 0;
  2477. if (le16_to_cpu(p->features) & 1)
  2478. busw = NAND_BUSWIDTH_16;
  2479. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2480. chip->options |= (NAND_NO_READRDY |
  2481. NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
  2482. return 1;
  2483. }
  2484. /*
  2485. * Get the flash and manufacturer id and lookup if the type is supported
  2486. */
  2487. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2488. struct nand_chip *chip,
  2489. int busw,
  2490. int *maf_id, int *dev_id,
  2491. struct nand_flash_dev *type)
  2492. {
  2493. int i, maf_idx;
  2494. u8 id_data[8];
  2495. int ret;
  2496. /* Select the device */
  2497. chip->select_chip(mtd, 0);
  2498. /*
  2499. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2500. * after power-up
  2501. */
  2502. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2503. /* Send the command for reading device ID */
  2504. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2505. /* Read manufacturer and device IDs */
  2506. *maf_id = chip->read_byte(mtd);
  2507. *dev_id = chip->read_byte(mtd);
  2508. /* Try again to make sure, as some systems the bus-hold or other
  2509. * interface concerns can cause random data which looks like a
  2510. * possibly credible NAND flash to appear. If the two results do
  2511. * not match, ignore the device completely.
  2512. */
  2513. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2514. for (i = 0; i < 2; i++)
  2515. id_data[i] = chip->read_byte(mtd);
  2516. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  2517. printk(KERN_INFO "%s: second ID read did not match "
  2518. "%02x,%02x against %02x,%02x\n", __func__,
  2519. *maf_id, *dev_id, id_data[0], id_data[1]);
  2520. return ERR_PTR(-ENODEV);
  2521. }
  2522. if (!type)
  2523. type = nand_flash_ids;
  2524. for (; type->name != NULL; type++)
  2525. if (*dev_id == type->id)
  2526. break;
  2527. chip->onfi_version = 0;
  2528. if (!type->name || !type->pagesize) {
  2529. /* Check is chip is ONFI compliant */
  2530. ret = nand_flash_detect_onfi(mtd, chip, busw);
  2531. if (ret)
  2532. goto ident_done;
  2533. }
  2534. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2535. /* Read entire ID string */
  2536. for (i = 0; i < 8; i++)
  2537. id_data[i] = chip->read_byte(mtd);
  2538. if (!type->name)
  2539. return ERR_PTR(-ENODEV);
  2540. if (!mtd->name)
  2541. mtd->name = type->name;
  2542. chip->chipsize = (uint64_t)type->chipsize << 20;
  2543. if (!type->pagesize && chip->init_size) {
  2544. /* set the pagesize, oobsize, erasesize by the driver*/
  2545. busw = chip->init_size(mtd, chip, id_data);
  2546. } else if (!type->pagesize) {
  2547. int extid;
  2548. /* The 3rd id byte holds MLC / multichip data */
  2549. chip->cellinfo = id_data[2];
  2550. /* The 4th id byte is the important one */
  2551. extid = id_data[3];
  2552. /*
  2553. * Field definitions are in the following datasheets:
  2554. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2555. * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
  2556. *
  2557. * Check for wraparound + Samsung ID + nonzero 6th byte
  2558. * to decide what to do.
  2559. */
  2560. if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
  2561. id_data[0] == NAND_MFR_SAMSUNG &&
  2562. (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2563. id_data[5] != 0x00) {
  2564. /* Calc pagesize */
  2565. mtd->writesize = 2048 << (extid & 0x03);
  2566. extid >>= 2;
  2567. /* Calc oobsize */
  2568. switch (extid & 0x03) {
  2569. case 1:
  2570. mtd->oobsize = 128;
  2571. break;
  2572. case 2:
  2573. mtd->oobsize = 218;
  2574. break;
  2575. case 3:
  2576. mtd->oobsize = 400;
  2577. break;
  2578. default:
  2579. mtd->oobsize = 436;
  2580. break;
  2581. }
  2582. extid >>= 2;
  2583. /* Calc blocksize */
  2584. mtd->erasesize = (128 * 1024) <<
  2585. (((extid >> 1) & 0x04) | (extid & 0x03));
  2586. busw = 0;
  2587. } else {
  2588. /* Calc pagesize */
  2589. mtd->writesize = 1024 << (extid & 0x03);
  2590. extid >>= 2;
  2591. /* Calc oobsize */
  2592. mtd->oobsize = (8 << (extid & 0x01)) *
  2593. (mtd->writesize >> 9);
  2594. extid >>= 2;
  2595. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2596. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2597. extid >>= 2;
  2598. /* Get buswidth information */
  2599. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2600. }
  2601. } else {
  2602. /*
  2603. * Old devices have chip data hardcoded in the device id table
  2604. */
  2605. mtd->erasesize = type->erasesize;
  2606. mtd->writesize = type->pagesize;
  2607. mtd->oobsize = mtd->writesize / 32;
  2608. busw = type->options & NAND_BUSWIDTH_16;
  2609. /*
  2610. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  2611. * some Spansion chips have erasesize that conflicts with size
  2612. * listed in nand_ids table
  2613. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  2614. */
  2615. if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
  2616. id_data[5] == 0x00 && id_data[6] == 0x00 &&
  2617. id_data[7] == 0x00 && mtd->writesize == 512) {
  2618. mtd->erasesize = 128 * 1024;
  2619. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  2620. }
  2621. }
  2622. /* Get chip options, preserve non chip based options */
  2623. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2624. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2625. /* Check if chip is a not a samsung device. Do not clear the
  2626. * options for chips which are not having an extended id.
  2627. */
  2628. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2629. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2630. ident_done:
  2631. /*
  2632. * Set chip as a default. Board drivers can override it, if necessary
  2633. */
  2634. chip->options |= NAND_NO_AUTOINCR;
  2635. /* Try to identify manufacturer */
  2636. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2637. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2638. break;
  2639. }
  2640. /*
  2641. * Check, if buswidth is correct. Hardware drivers should set
  2642. * chip correct !
  2643. */
  2644. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2645. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2646. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2647. *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2648. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  2649. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2650. busw ? 16 : 8);
  2651. return ERR_PTR(-EINVAL);
  2652. }
  2653. /* Calculate the address shift from the page size */
  2654. chip->page_shift = ffs(mtd->writesize) - 1;
  2655. /* Convert chipsize to number of pages per chip -1. */
  2656. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2657. chip->bbt_erase_shift = chip->phys_erase_shift =
  2658. ffs(mtd->erasesize) - 1;
  2659. if (chip->chipsize & 0xffffffff)
  2660. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2661. else {
  2662. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  2663. chip->chip_shift += 32 - 1;
  2664. }
  2665. /* Set the bad block position */
  2666. if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
  2667. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  2668. else
  2669. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  2670. /*
  2671. * Bad block marker is stored in the last page of each block
  2672. * on Samsung and Hynix MLC devices; stored in first two pages
  2673. * of each block on Micron devices with 2KiB pages and on
  2674. * SLC Samsung, Hynix, Toshiba and AMD/Spansion. All others scan
  2675. * only the first page.
  2676. */
  2677. if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2678. (*maf_id == NAND_MFR_SAMSUNG ||
  2679. *maf_id == NAND_MFR_HYNIX))
  2680. chip->options |= NAND_BBT_SCANLASTPAGE;
  2681. else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2682. (*maf_id == NAND_MFR_SAMSUNG ||
  2683. *maf_id == NAND_MFR_HYNIX ||
  2684. *maf_id == NAND_MFR_TOSHIBA ||
  2685. *maf_id == NAND_MFR_AMD)) ||
  2686. (mtd->writesize == 2048 &&
  2687. *maf_id == NAND_MFR_MICRON))
  2688. chip->options |= NAND_BBT_SCAN2NDPAGE;
  2689. /*
  2690. * Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6
  2691. */
  2692. if (!(busw & NAND_BUSWIDTH_16) &&
  2693. *maf_id == NAND_MFR_STMICRO &&
  2694. mtd->writesize == 2048) {
  2695. chip->options |= NAND_BBT_SCANBYTE1AND6;
  2696. chip->badblockpos = 0;
  2697. }
  2698. /* Check for AND chips with 4 page planes */
  2699. if (chip->options & NAND_4PAGE_ARRAY)
  2700. chip->erase_cmd = multi_erase_cmd;
  2701. else
  2702. chip->erase_cmd = single_erase_cmd;
  2703. /* Do not replace user supplied command function ! */
  2704. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2705. chip->cmdfunc = nand_command_lp;
  2706. /* TODO onfi flash name */
  2707. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2708. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
  2709. nand_manuf_ids[maf_idx].name,
  2710. chip->onfi_version ? type->name : chip->onfi_params.model);
  2711. return type;
  2712. }
  2713. /**
  2714. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2715. * @mtd: MTD device structure
  2716. * @maxchips: Number of chips to scan for
  2717. * @table: Alternative NAND ID table
  2718. *
  2719. * This is the first phase of the normal nand_scan() function. It
  2720. * reads the flash ID and sets up MTD fields accordingly.
  2721. *
  2722. * The mtd->owner field must be set to the module of the caller.
  2723. */
  2724. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  2725. struct nand_flash_dev *table)
  2726. {
  2727. int i, busw, nand_maf_id, nand_dev_id;
  2728. struct nand_chip *chip = mtd->priv;
  2729. struct nand_flash_dev *type;
  2730. /* Get buswidth to select the correct functions */
  2731. busw = chip->options & NAND_BUSWIDTH_16;
  2732. /* Set the default functions */
  2733. nand_set_defaults(chip, busw);
  2734. /* Read the flash type */
  2735. type = nand_get_flash_type(mtd, chip, busw,
  2736. &nand_maf_id, &nand_dev_id, table);
  2737. if (IS_ERR(type)) {
  2738. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  2739. printk(KERN_WARNING "No NAND device found.\n");
  2740. chip->select_chip(mtd, -1);
  2741. return PTR_ERR(type);
  2742. }
  2743. /* Check for a chip array */
  2744. for (i = 1; i < maxchips; i++) {
  2745. chip->select_chip(mtd, i);
  2746. /* See comment in nand_get_flash_type for reset */
  2747. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2748. /* Send the command for reading device ID */
  2749. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2750. /* Read manufacturer and device IDs */
  2751. if (nand_maf_id != chip->read_byte(mtd) ||
  2752. nand_dev_id != chip->read_byte(mtd))
  2753. break;
  2754. }
  2755. if (i > 1)
  2756. printk(KERN_INFO "%d NAND chips detected\n", i);
  2757. /* Store the number of chips and calc total size for mtd */
  2758. chip->numchips = i;
  2759. mtd->size = i * chip->chipsize;
  2760. return 0;
  2761. }
  2762. EXPORT_SYMBOL(nand_scan_ident);
  2763. /**
  2764. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2765. * @mtd: MTD device structure
  2766. *
  2767. * This is the second phase of the normal nand_scan() function. It
  2768. * fills out all the uninitialized function pointers with the defaults
  2769. * and scans for a bad block table if appropriate.
  2770. */
  2771. int nand_scan_tail(struct mtd_info *mtd)
  2772. {
  2773. int i;
  2774. struct nand_chip *chip = mtd->priv;
  2775. if (!(chip->options & NAND_OWN_BUFFERS))
  2776. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2777. if (!chip->buffers)
  2778. return -ENOMEM;
  2779. /* Set the internal oob buffer location, just after the page data */
  2780. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2781. /*
  2782. * If no default placement scheme is given, select an appropriate one
  2783. */
  2784. if (!chip->ecc.layout) {
  2785. switch (mtd->oobsize) {
  2786. case 8:
  2787. chip->ecc.layout = &nand_oob_8;
  2788. break;
  2789. case 16:
  2790. chip->ecc.layout = &nand_oob_16;
  2791. break;
  2792. case 64:
  2793. chip->ecc.layout = &nand_oob_64;
  2794. break;
  2795. case 128:
  2796. chip->ecc.layout = &nand_oob_128;
  2797. break;
  2798. default:
  2799. printk(KERN_WARNING "No oob scheme defined for "
  2800. "oobsize %d\n", mtd->oobsize);
  2801. BUG();
  2802. }
  2803. }
  2804. if (!chip->write_page)
  2805. chip->write_page = nand_write_page;
  2806. /*
  2807. * check ECC mode, default to software if 3byte/512byte hardware ECC is
  2808. * selected and we have 256 byte pagesize fallback to software ECC
  2809. */
  2810. switch (chip->ecc.mode) {
  2811. case NAND_ECC_HW_OOB_FIRST:
  2812. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  2813. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2814. !chip->ecc.hwctl) {
  2815. printk(KERN_WARNING "No ECC functions supplied; "
  2816. "Hardware ECC not possible\n");
  2817. BUG();
  2818. }
  2819. if (!chip->ecc.read_page)
  2820. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  2821. case NAND_ECC_HW:
  2822. /* Use standard hwecc read page function ? */
  2823. if (!chip->ecc.read_page)
  2824. chip->ecc.read_page = nand_read_page_hwecc;
  2825. if (!chip->ecc.write_page)
  2826. chip->ecc.write_page = nand_write_page_hwecc;
  2827. if (!chip->ecc.read_page_raw)
  2828. chip->ecc.read_page_raw = nand_read_page_raw;
  2829. if (!chip->ecc.write_page_raw)
  2830. chip->ecc.write_page_raw = nand_write_page_raw;
  2831. if (!chip->ecc.read_oob)
  2832. chip->ecc.read_oob = nand_read_oob_std;
  2833. if (!chip->ecc.write_oob)
  2834. chip->ecc.write_oob = nand_write_oob_std;
  2835. case NAND_ECC_HW_SYNDROME:
  2836. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2837. !chip->ecc.hwctl) &&
  2838. (!chip->ecc.read_page ||
  2839. chip->ecc.read_page == nand_read_page_hwecc ||
  2840. !chip->ecc.write_page ||
  2841. chip->ecc.write_page == nand_write_page_hwecc)) {
  2842. printk(KERN_WARNING "No ECC functions supplied; "
  2843. "Hardware ECC not possible\n");
  2844. BUG();
  2845. }
  2846. /* Use standard syndrome read/write page function ? */
  2847. if (!chip->ecc.read_page)
  2848. chip->ecc.read_page = nand_read_page_syndrome;
  2849. if (!chip->ecc.write_page)
  2850. chip->ecc.write_page = nand_write_page_syndrome;
  2851. if (!chip->ecc.read_page_raw)
  2852. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2853. if (!chip->ecc.write_page_raw)
  2854. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2855. if (!chip->ecc.read_oob)
  2856. chip->ecc.read_oob = nand_read_oob_syndrome;
  2857. if (!chip->ecc.write_oob)
  2858. chip->ecc.write_oob = nand_write_oob_syndrome;
  2859. if (mtd->writesize >= chip->ecc.size)
  2860. break;
  2861. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2862. "%d byte page size, fallback to SW ECC\n",
  2863. chip->ecc.size, mtd->writesize);
  2864. chip->ecc.mode = NAND_ECC_SOFT;
  2865. case NAND_ECC_SOFT:
  2866. chip->ecc.calculate = nand_calculate_ecc;
  2867. chip->ecc.correct = nand_correct_data;
  2868. chip->ecc.read_page = nand_read_page_swecc;
  2869. chip->ecc.read_subpage = nand_read_subpage;
  2870. chip->ecc.write_page = nand_write_page_swecc;
  2871. chip->ecc.read_page_raw = nand_read_page_raw;
  2872. chip->ecc.write_page_raw = nand_write_page_raw;
  2873. chip->ecc.read_oob = nand_read_oob_std;
  2874. chip->ecc.write_oob = nand_write_oob_std;
  2875. if (!chip->ecc.size)
  2876. chip->ecc.size = 256;
  2877. chip->ecc.bytes = 3;
  2878. break;
  2879. case NAND_ECC_NONE:
  2880. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2881. "This is not recommended !!\n");
  2882. chip->ecc.read_page = nand_read_page_raw;
  2883. chip->ecc.write_page = nand_write_page_raw;
  2884. chip->ecc.read_oob = nand_read_oob_std;
  2885. chip->ecc.read_page_raw = nand_read_page_raw;
  2886. chip->ecc.write_page_raw = nand_write_page_raw;
  2887. chip->ecc.write_oob = nand_write_oob_std;
  2888. chip->ecc.size = mtd->writesize;
  2889. chip->ecc.bytes = 0;
  2890. break;
  2891. default:
  2892. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2893. chip->ecc.mode);
  2894. BUG();
  2895. }
  2896. /*
  2897. * The number of bytes available for a client to place data into
  2898. * the out of band area
  2899. */
  2900. chip->ecc.layout->oobavail = 0;
  2901. for (i = 0; chip->ecc.layout->oobfree[i].length
  2902. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2903. chip->ecc.layout->oobavail +=
  2904. chip->ecc.layout->oobfree[i].length;
  2905. mtd->oobavail = chip->ecc.layout->oobavail;
  2906. /*
  2907. * Set the number of read / write steps for one page depending on ECC
  2908. * mode
  2909. */
  2910. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2911. if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2912. printk(KERN_WARNING "Invalid ecc parameters\n");
  2913. BUG();
  2914. }
  2915. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2916. /*
  2917. * Allow subpage writes up to ecc.steps. Not possible for MLC
  2918. * FLASH.
  2919. */
  2920. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2921. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2922. switch (chip->ecc.steps) {
  2923. case 2:
  2924. mtd->subpage_sft = 1;
  2925. break;
  2926. case 4:
  2927. case 8:
  2928. case 16:
  2929. mtd->subpage_sft = 2;
  2930. break;
  2931. }
  2932. }
  2933. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2934. /* Initialize state */
  2935. chip->state = FL_READY;
  2936. /* De-select the device */
  2937. chip->select_chip(mtd, -1);
  2938. /* Invalidate the pagebuffer reference */
  2939. chip->pagebuf = -1;
  2940. /* Fill in remaining MTD driver data */
  2941. mtd->type = MTD_NANDFLASH;
  2942. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  2943. MTD_CAP_NANDFLASH;
  2944. mtd->erase = nand_erase;
  2945. mtd->point = NULL;
  2946. mtd->unpoint = NULL;
  2947. mtd->read = nand_read;
  2948. mtd->write = nand_write;
  2949. mtd->panic_write = panic_nand_write;
  2950. mtd->read_oob = nand_read_oob;
  2951. mtd->write_oob = nand_write_oob;
  2952. mtd->sync = nand_sync;
  2953. mtd->lock = NULL;
  2954. mtd->unlock = NULL;
  2955. mtd->suspend = nand_suspend;
  2956. mtd->resume = nand_resume;
  2957. mtd->block_isbad = nand_block_isbad;
  2958. mtd->block_markbad = nand_block_markbad;
  2959. /* propagate ecc.layout to mtd_info */
  2960. mtd->ecclayout = chip->ecc.layout;
  2961. /* Check, if we should skip the bad block table scan */
  2962. if (chip->options & NAND_SKIP_BBTSCAN)
  2963. return 0;
  2964. /* Build bad block table */
  2965. return chip->scan_bbt(mtd);
  2966. }
  2967. EXPORT_SYMBOL(nand_scan_tail);
  2968. /* is_module_text_address() isn't exported, and it's mostly a pointless
  2969. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  2970. * to call us from in-kernel code if the core NAND support is modular. */
  2971. #ifdef MODULE
  2972. #define caller_is_module() (1)
  2973. #else
  2974. #define caller_is_module() \
  2975. is_module_text_address((unsigned long)__builtin_return_address(0))
  2976. #endif
  2977. /**
  2978. * nand_scan - [NAND Interface] Scan for the NAND device
  2979. * @mtd: MTD device structure
  2980. * @maxchips: Number of chips to scan for
  2981. *
  2982. * This fills out all the uninitialized function pointers
  2983. * with the defaults.
  2984. * The flash ID is read and the mtd/chip structures are
  2985. * filled with the appropriate values.
  2986. * The mtd->owner field must be set to the module of the caller
  2987. *
  2988. */
  2989. int nand_scan(struct mtd_info *mtd, int maxchips)
  2990. {
  2991. int ret;
  2992. /* Many callers got this wrong, so check for it for a while... */
  2993. if (!mtd->owner && caller_is_module()) {
  2994. printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
  2995. __func__);
  2996. BUG();
  2997. }
  2998. ret = nand_scan_ident(mtd, maxchips, NULL);
  2999. if (!ret)
  3000. ret = nand_scan_tail(mtd);
  3001. return ret;
  3002. }
  3003. EXPORT_SYMBOL(nand_scan);
  3004. /**
  3005. * nand_release - [NAND Interface] Free resources held by the NAND device
  3006. * @mtd: MTD device structure
  3007. */
  3008. void nand_release(struct mtd_info *mtd)
  3009. {
  3010. struct nand_chip *chip = mtd->priv;
  3011. #ifdef CONFIG_MTD_PARTITIONS
  3012. /* Deregister partitions */
  3013. del_mtd_partitions(mtd);
  3014. #endif
  3015. /* Deregister the device */
  3016. del_mtd_device(mtd);
  3017. /* Free bad block table memory */
  3018. kfree(chip->bbt);
  3019. if (!(chip->options & NAND_OWN_BUFFERS))
  3020. kfree(chip->buffers);
  3021. /* Free bad block descriptor memory */
  3022. if (chip->badblock_pattern && chip->badblock_pattern->options
  3023. & NAND_BBT_DYNAMICSTRUCT)
  3024. kfree(chip->badblock_pattern);
  3025. }
  3026. EXPORT_SYMBOL_GPL(nand_release);
  3027. static int __init nand_base_init(void)
  3028. {
  3029. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  3030. return 0;
  3031. }
  3032. static void __exit nand_base_exit(void)
  3033. {
  3034. led_trigger_unregister_simple(nand_led_trigger);
  3035. }
  3036. module_init(nand_base_init);
  3037. module_exit(nand_base_exit);
  3038. MODULE_LICENSE("GPL");
  3039. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3040. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3041. MODULE_DESCRIPTION("Generic NAND flash driver code");