nand_base.c 87 KB

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