nand_base.c 59 KB

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