nand_base.c 75 KB

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