nand_base.c 91 KB

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