nand_base.c 91 KB

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