nand_base.c 68 KB

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