nand_base.c 72 KB

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