nand_base.c 65 KB

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