nand_base.c 85 KB

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