s3c2410.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /* linux/drivers/mtd/nand/s3c2410.c
  2. *
  3. * Copyright (c) 2004,2005 Simtec Electronics
  4. * http://www.simtec.co.uk/products/SWLINUX/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * Samsung S3C2410/S3C240 NAND driver
  8. *
  9. * Changelog:
  10. * 21-Sep-2004 BJD Initial version
  11. * 23-Sep-2004 BJD Multiple device support
  12. * 28-Sep-2004 BJD Fixed ECC placement for Hardware mode
  13. * 12-Oct-2004 BJD Fixed errors in use of platform data
  14. * 18-Feb-2005 BJD Fix sparse errors
  15. * 14-Mar-2005 BJD Applied tglx's code reduction patch
  16. * 02-May-2005 BJD Fixed s3c2440 support
  17. * 02-May-2005 BJD Reduced hwcontrol decode
  18. * 20-Jun-2005 BJD Updated s3c2440 support, fixed timing bug
  19. * 08-Jul-2005 BJD Fix OOPS when no platform data supplied
  20. * 20-Oct-2005 BJD Fix timing calculation bug
  21. * 14-Jan-2006 BJD Allow clock to be stopped when idle
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  36. */
  37. #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
  38. #define DEBUG
  39. #endif
  40. #include <linux/module.h>
  41. #include <linux/types.h>
  42. #include <linux/init.h>
  43. #include <linux/kernel.h>
  44. #include <linux/string.h>
  45. #include <linux/ioport.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/delay.h>
  48. #include <linux/err.h>
  49. #include <linux/slab.h>
  50. #include <linux/clk.h>
  51. #include <linux/mtd/mtd.h>
  52. #include <linux/mtd/nand.h>
  53. #include <linux/mtd/nand_ecc.h>
  54. #include <linux/mtd/partitions.h>
  55. #include <asm/io.h>
  56. #include <asm/plat-s3c/regs-nand.h>
  57. #include <asm/plat-s3c/nand.h>
  58. #ifdef CONFIG_MTD_NAND_S3C2410_HWECC
  59. static int hardware_ecc = 1;
  60. #else
  61. static int hardware_ecc = 0;
  62. #endif
  63. #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
  64. static int clock_stop = 1;
  65. #else
  66. static const int clock_stop = 0;
  67. #endif
  68. /* new oob placement block for use with hardware ecc generation
  69. */
  70. static struct nand_ecclayout nand_hw_eccoob = {
  71. .eccbytes = 3,
  72. .eccpos = {0, 1, 2},
  73. .oobfree = {{8, 8}}
  74. };
  75. /* controller and mtd information */
  76. struct s3c2410_nand_info;
  77. struct s3c2410_nand_mtd {
  78. struct mtd_info mtd;
  79. struct nand_chip chip;
  80. struct s3c2410_nand_set *set;
  81. struct s3c2410_nand_info *info;
  82. int scan_res;
  83. };
  84. enum s3c_cpu_type {
  85. TYPE_S3C2410,
  86. TYPE_S3C2412,
  87. TYPE_S3C2440,
  88. };
  89. /* overview of the s3c2410 nand state */
  90. struct s3c2410_nand_info {
  91. /* mtd info */
  92. struct nand_hw_control controller;
  93. struct s3c2410_nand_mtd *mtds;
  94. struct s3c2410_platform_nand *platform;
  95. /* device info */
  96. struct device *device;
  97. struct resource *area;
  98. struct clk *clk;
  99. void __iomem *regs;
  100. void __iomem *sel_reg;
  101. int sel_bit;
  102. int mtd_count;
  103. unsigned long save_sel;
  104. enum s3c_cpu_type cpu_type;
  105. };
  106. /* conversion functions */
  107. static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
  108. {
  109. return container_of(mtd, struct s3c2410_nand_mtd, mtd);
  110. }
  111. static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
  112. {
  113. return s3c2410_nand_mtd_toours(mtd)->info;
  114. }
  115. static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
  116. {
  117. return platform_get_drvdata(dev);
  118. }
  119. static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
  120. {
  121. return dev->dev.platform_data;
  122. }
  123. static inline int allow_clk_stop(struct s3c2410_nand_info *info)
  124. {
  125. return clock_stop;
  126. }
  127. /* timing calculations */
  128. #define NS_IN_KHZ 1000000
  129. static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
  130. {
  131. int result;
  132. result = (wanted * clk) / NS_IN_KHZ;
  133. result++;
  134. pr_debug("result %d from %ld, %d\n", result, clk, wanted);
  135. if (result > max) {
  136. printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
  137. return -1;
  138. }
  139. if (result < 1)
  140. result = 1;
  141. return result;
  142. }
  143. #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
  144. /* controller setup */
  145. static int s3c2410_nand_inithw(struct s3c2410_nand_info *info,
  146. struct platform_device *pdev)
  147. {
  148. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  149. unsigned long clkrate = clk_get_rate(info->clk);
  150. int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
  151. int tacls, twrph0, twrph1;
  152. unsigned long cfg = 0;
  153. /* calculate the timing information for the controller */
  154. clkrate /= 1000; /* turn clock into kHz for ease of use */
  155. if (plat != NULL) {
  156. tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
  157. twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
  158. twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
  159. } else {
  160. /* default timings */
  161. tacls = tacls_max;
  162. twrph0 = 8;
  163. twrph1 = 8;
  164. }
  165. if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
  166. dev_err(info->device, "cannot get suitable timings\n");
  167. return -EINVAL;
  168. }
  169. dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
  170. tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
  171. switch (info->cpu_type) {
  172. case TYPE_S3C2410:
  173. cfg = S3C2410_NFCONF_EN;
  174. cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
  175. cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
  176. cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
  177. break;
  178. case TYPE_S3C2440:
  179. case TYPE_S3C2412:
  180. cfg = S3C2440_NFCONF_TACLS(tacls - 1);
  181. cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
  182. cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
  183. /* enable the controller and de-assert nFCE */
  184. writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
  185. }
  186. dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
  187. writel(cfg, info->regs + S3C2410_NFCONF);
  188. return 0;
  189. }
  190. /* select chip */
  191. static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
  192. {
  193. struct s3c2410_nand_info *info;
  194. struct s3c2410_nand_mtd *nmtd;
  195. struct nand_chip *this = mtd->priv;
  196. unsigned long cur;
  197. nmtd = this->priv;
  198. info = nmtd->info;
  199. if (chip != -1 && allow_clk_stop(info))
  200. clk_enable(info->clk);
  201. cur = readl(info->sel_reg);
  202. if (chip == -1) {
  203. cur |= info->sel_bit;
  204. } else {
  205. if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
  206. dev_err(info->device, "invalid chip %d\n", chip);
  207. return;
  208. }
  209. if (info->platform != NULL) {
  210. if (info->platform->select_chip != NULL)
  211. (info->platform->select_chip) (nmtd->set, chip);
  212. }
  213. cur &= ~info->sel_bit;
  214. }
  215. writel(cur, info->sel_reg);
  216. if (chip == -1 && allow_clk_stop(info))
  217. clk_disable(info->clk);
  218. }
  219. /* s3c2410_nand_hwcontrol
  220. *
  221. * Issue command and address cycles to the chip
  222. */
  223. static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  224. unsigned int ctrl)
  225. {
  226. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  227. if (cmd == NAND_CMD_NONE)
  228. return;
  229. if (ctrl & NAND_CLE)
  230. writeb(cmd, info->regs + S3C2410_NFCMD);
  231. else
  232. writeb(cmd, info->regs + S3C2410_NFADDR);
  233. }
  234. /* command and control functions */
  235. static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  236. unsigned int ctrl)
  237. {
  238. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  239. if (cmd == NAND_CMD_NONE)
  240. return;
  241. if (ctrl & NAND_CLE)
  242. writeb(cmd, info->regs + S3C2440_NFCMD);
  243. else
  244. writeb(cmd, info->regs + S3C2440_NFADDR);
  245. }
  246. /* s3c2410_nand_devready()
  247. *
  248. * returns 0 if the nand is busy, 1 if it is ready
  249. */
  250. static int s3c2410_nand_devready(struct mtd_info *mtd)
  251. {
  252. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  253. return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
  254. }
  255. static int s3c2440_nand_devready(struct mtd_info *mtd)
  256. {
  257. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  258. return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
  259. }
  260. static int s3c2412_nand_devready(struct mtd_info *mtd)
  261. {
  262. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  263. return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
  264. }
  265. /* ECC handling functions */
  266. static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  267. u_char *read_ecc, u_char *calc_ecc)
  268. {
  269. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  270. unsigned int diff0, diff1, diff2;
  271. unsigned int bit, byte;
  272. pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
  273. diff0 = read_ecc[0] ^ calc_ecc[0];
  274. diff1 = read_ecc[1] ^ calc_ecc[1];
  275. diff2 = read_ecc[2] ^ calc_ecc[2];
  276. pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
  277. __func__,
  278. read_ecc[0], read_ecc[1], read_ecc[2],
  279. calc_ecc[0], calc_ecc[1], calc_ecc[2],
  280. diff0, diff1, diff2);
  281. if (diff0 == 0 && diff1 == 0 && diff2 == 0)
  282. return 0; /* ECC is ok */
  283. /* sometimes people do not think about using the ECC, so check
  284. * to see if we have an 0xff,0xff,0xff read ECC and then ignore
  285. * the error, on the assumption that this is an un-eccd page.
  286. */
  287. if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
  288. && info->platform->ignore_unset_ecc)
  289. return 0;
  290. /* Can we correct this ECC (ie, one row and column change).
  291. * Note, this is similar to the 256 error code on smartmedia */
  292. if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
  293. ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
  294. ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
  295. /* calculate the bit position of the error */
  296. bit = ((diff2 >> 3) & 1) |
  297. ((diff2 >> 4) & 2) |
  298. ((diff2 >> 5) & 4);
  299. /* calculate the byte position of the error */
  300. byte = ((diff2 << 7) & 0x100) |
  301. ((diff1 << 0) & 0x80) |
  302. ((diff1 << 1) & 0x40) |
  303. ((diff1 << 2) & 0x20) |
  304. ((diff1 << 3) & 0x10) |
  305. ((diff0 >> 4) & 0x08) |
  306. ((diff0 >> 3) & 0x04) |
  307. ((diff0 >> 2) & 0x02) |
  308. ((diff0 >> 1) & 0x01);
  309. dev_dbg(info->device, "correcting error bit %d, byte %d\n",
  310. bit, byte);
  311. dat[byte] ^= (1 << bit);
  312. return 1;
  313. }
  314. /* if there is only one bit difference in the ECC, then
  315. * one of only a row or column parity has changed, which
  316. * means the error is most probably in the ECC itself */
  317. diff0 |= (diff1 << 8);
  318. diff0 |= (diff2 << 16);
  319. if ((diff0 & ~(1<<fls(diff0))) == 0)
  320. return 1;
  321. return -1;
  322. }
  323. /* ECC functions
  324. *
  325. * These allow the s3c2410 and s3c2440 to use the controller's ECC
  326. * generator block to ECC the data as it passes through]
  327. */
  328. static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  329. {
  330. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  331. unsigned long ctrl;
  332. ctrl = readl(info->regs + S3C2410_NFCONF);
  333. ctrl |= S3C2410_NFCONF_INITECC;
  334. writel(ctrl, info->regs + S3C2410_NFCONF);
  335. }
  336. static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  337. {
  338. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  339. unsigned long ctrl;
  340. ctrl = readl(info->regs + S3C2440_NFCONT);
  341. writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
  342. }
  343. static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  344. {
  345. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  346. unsigned long ctrl;
  347. ctrl = readl(info->regs + S3C2440_NFCONT);
  348. writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
  349. }
  350. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  351. {
  352. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  353. ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
  354. ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
  355. ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
  356. pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
  357. ecc_code[0], ecc_code[1], ecc_code[2]);
  358. return 0;
  359. }
  360. static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  361. {
  362. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  363. unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
  364. ecc_code[0] = ecc;
  365. ecc_code[1] = ecc >> 8;
  366. ecc_code[2] = ecc >> 16;
  367. pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
  368. return 0;
  369. }
  370. static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  371. {
  372. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  373. unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
  374. ecc_code[0] = ecc;
  375. ecc_code[1] = ecc >> 8;
  376. ecc_code[2] = ecc >> 16;
  377. pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
  378. return 0;
  379. }
  380. /* over-ride the standard functions for a little more speed. We can
  381. * use read/write block to move the data buffers to/from the controller
  382. */
  383. static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  384. {
  385. struct nand_chip *this = mtd->priv;
  386. readsb(this->IO_ADDR_R, buf, len);
  387. }
  388. static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  389. {
  390. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  391. readsl(info->regs + S3C2440_NFDATA, buf, len / 4);
  392. }
  393. static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  394. {
  395. struct nand_chip *this = mtd->priv;
  396. writesb(this->IO_ADDR_W, buf, len);
  397. }
  398. static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  399. {
  400. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  401. writesl(info->regs + S3C2440_NFDATA, buf, len / 4);
  402. }
  403. /* device management functions */
  404. static int s3c2410_nand_remove(struct platform_device *pdev)
  405. {
  406. struct s3c2410_nand_info *info = to_nand_info(pdev);
  407. platform_set_drvdata(pdev, NULL);
  408. if (info == NULL)
  409. return 0;
  410. /* first thing we need to do is release all our mtds
  411. * and their partitions, then go through freeing the
  412. * resources used
  413. */
  414. if (info->mtds != NULL) {
  415. struct s3c2410_nand_mtd *ptr = info->mtds;
  416. int mtdno;
  417. for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
  418. pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
  419. nand_release(&ptr->mtd);
  420. }
  421. kfree(info->mtds);
  422. }
  423. /* free the common resources */
  424. if (info->clk != NULL && !IS_ERR(info->clk)) {
  425. if (!allow_clk_stop(info))
  426. clk_disable(info->clk);
  427. clk_put(info->clk);
  428. }
  429. if (info->regs != NULL) {
  430. iounmap(info->regs);
  431. info->regs = NULL;
  432. }
  433. if (info->area != NULL) {
  434. release_resource(info->area);
  435. kfree(info->area);
  436. info->area = NULL;
  437. }
  438. kfree(info);
  439. return 0;
  440. }
  441. #ifdef CONFIG_MTD_PARTITIONS
  442. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  443. struct s3c2410_nand_mtd *mtd,
  444. struct s3c2410_nand_set *set)
  445. {
  446. if (set == NULL)
  447. return add_mtd_device(&mtd->mtd);
  448. if (set->nr_partitions > 0 && set->partitions != NULL) {
  449. return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
  450. }
  451. return add_mtd_device(&mtd->mtd);
  452. }
  453. #else
  454. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  455. struct s3c2410_nand_mtd *mtd,
  456. struct s3c2410_nand_set *set)
  457. {
  458. return add_mtd_device(&mtd->mtd);
  459. }
  460. #endif
  461. /* s3c2410_nand_init_chip
  462. *
  463. * init a single instance of an chip
  464. */
  465. static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
  466. struct s3c2410_nand_mtd *nmtd,
  467. struct s3c2410_nand_set *set)
  468. {
  469. struct nand_chip *chip = &nmtd->chip;
  470. void __iomem *regs = info->regs;
  471. chip->write_buf = s3c2410_nand_write_buf;
  472. chip->read_buf = s3c2410_nand_read_buf;
  473. chip->select_chip = s3c2410_nand_select_chip;
  474. chip->chip_delay = 50;
  475. chip->priv = nmtd;
  476. chip->options = 0;
  477. chip->controller = &info->controller;
  478. switch (info->cpu_type) {
  479. case TYPE_S3C2410:
  480. chip->IO_ADDR_W = regs + S3C2410_NFDATA;
  481. info->sel_reg = regs + S3C2410_NFCONF;
  482. info->sel_bit = S3C2410_NFCONF_nFCE;
  483. chip->cmd_ctrl = s3c2410_nand_hwcontrol;
  484. chip->dev_ready = s3c2410_nand_devready;
  485. break;
  486. case TYPE_S3C2440:
  487. chip->IO_ADDR_W = regs + S3C2440_NFDATA;
  488. info->sel_reg = regs + S3C2440_NFCONT;
  489. info->sel_bit = S3C2440_NFCONT_nFCE;
  490. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  491. chip->dev_ready = s3c2440_nand_devready;
  492. chip->read_buf = s3c2440_nand_read_buf;
  493. chip->write_buf = s3c2440_nand_write_buf;
  494. break;
  495. case TYPE_S3C2412:
  496. chip->IO_ADDR_W = regs + S3C2440_NFDATA;
  497. info->sel_reg = regs + S3C2440_NFCONT;
  498. info->sel_bit = S3C2412_NFCONT_nFCE0;
  499. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  500. chip->dev_ready = s3c2412_nand_devready;
  501. if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
  502. dev_info(info->device, "System booted from NAND\n");
  503. break;
  504. }
  505. chip->IO_ADDR_R = chip->IO_ADDR_W;
  506. nmtd->info = info;
  507. nmtd->mtd.priv = chip;
  508. nmtd->mtd.owner = THIS_MODULE;
  509. nmtd->set = set;
  510. if (hardware_ecc) {
  511. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  512. chip->ecc.correct = s3c2410_nand_correct_data;
  513. chip->ecc.mode = NAND_ECC_HW;
  514. switch (info->cpu_type) {
  515. case TYPE_S3C2410:
  516. chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
  517. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  518. break;
  519. case TYPE_S3C2412:
  520. chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
  521. chip->ecc.calculate = s3c2412_nand_calculate_ecc;
  522. break;
  523. case TYPE_S3C2440:
  524. chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
  525. chip->ecc.calculate = s3c2440_nand_calculate_ecc;
  526. break;
  527. }
  528. } else {
  529. chip->ecc.mode = NAND_ECC_SOFT;
  530. }
  531. if (set->ecc_layout != NULL)
  532. chip->ecc.layout = set->ecc_layout;
  533. if (set->disable_ecc)
  534. chip->ecc.mode = NAND_ECC_NONE;
  535. }
  536. /* s3c2410_nand_update_chip
  537. *
  538. * post-probe chip update, to change any items, such as the
  539. * layout for large page nand
  540. */
  541. static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
  542. struct s3c2410_nand_mtd *nmtd)
  543. {
  544. struct nand_chip *chip = &nmtd->chip;
  545. dev_dbg(info->device, "chip %p => page shift %d\n",
  546. chip, chip->page_shift);
  547. if (hardware_ecc) {
  548. /* change the behaviour depending on wether we are using
  549. * the large or small page nand device */
  550. if (chip->page_shift > 10) {
  551. chip->ecc.size = 256;
  552. chip->ecc.bytes = 3;
  553. } else {
  554. chip->ecc.size = 512;
  555. chip->ecc.bytes = 3;
  556. chip->ecc.layout = &nand_hw_eccoob;
  557. }
  558. }
  559. }
  560. /* s3c2410_nand_probe
  561. *
  562. * called by device layer when it finds a device matching
  563. * one our driver can handled. This code checks to see if
  564. * it can allocate all necessary resources then calls the
  565. * nand layer to look for devices
  566. */
  567. static int s3c24xx_nand_probe(struct platform_device *pdev,
  568. enum s3c_cpu_type cpu_type)
  569. {
  570. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  571. struct s3c2410_nand_info *info;
  572. struct s3c2410_nand_mtd *nmtd;
  573. struct s3c2410_nand_set *sets;
  574. struct resource *res;
  575. int err = 0;
  576. int size;
  577. int nr_sets;
  578. int setno;
  579. pr_debug("s3c2410_nand_probe(%p)\n", pdev);
  580. info = kmalloc(sizeof(*info), GFP_KERNEL);
  581. if (info == NULL) {
  582. dev_err(&pdev->dev, "no memory for flash info\n");
  583. err = -ENOMEM;
  584. goto exit_error;
  585. }
  586. memzero(info, sizeof(*info));
  587. platform_set_drvdata(pdev, info);
  588. spin_lock_init(&info->controller.lock);
  589. init_waitqueue_head(&info->controller.wq);
  590. /* get the clock source and enable it */
  591. info->clk = clk_get(&pdev->dev, "nand");
  592. if (IS_ERR(info->clk)) {
  593. dev_err(&pdev->dev, "failed to get clock\n");
  594. err = -ENOENT;
  595. goto exit_error;
  596. }
  597. clk_enable(info->clk);
  598. /* allocate and map the resource */
  599. /* currently we assume we have the one resource */
  600. res = pdev->resource;
  601. size = res->end - res->start + 1;
  602. info->area = request_mem_region(res->start, size, pdev->name);
  603. if (info->area == NULL) {
  604. dev_err(&pdev->dev, "cannot reserve register region\n");
  605. err = -ENOENT;
  606. goto exit_error;
  607. }
  608. info->device = &pdev->dev;
  609. info->platform = plat;
  610. info->regs = ioremap(res->start, size);
  611. info->cpu_type = cpu_type;
  612. if (info->regs == NULL) {
  613. dev_err(&pdev->dev, "cannot reserve register region\n");
  614. err = -EIO;
  615. goto exit_error;
  616. }
  617. dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
  618. /* initialise the hardware */
  619. err = s3c2410_nand_inithw(info, pdev);
  620. if (err != 0)
  621. goto exit_error;
  622. sets = (plat != NULL) ? plat->sets : NULL;
  623. nr_sets = (plat != NULL) ? plat->nr_sets : 1;
  624. info->mtd_count = nr_sets;
  625. /* allocate our information */
  626. size = nr_sets * sizeof(*info->mtds);
  627. info->mtds = kmalloc(size, GFP_KERNEL);
  628. if (info->mtds == NULL) {
  629. dev_err(&pdev->dev, "failed to allocate mtd storage\n");
  630. err = -ENOMEM;
  631. goto exit_error;
  632. }
  633. memzero(info->mtds, size);
  634. /* initialise all possible chips */
  635. nmtd = info->mtds;
  636. for (setno = 0; setno < nr_sets; setno++, nmtd++) {
  637. pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
  638. s3c2410_nand_init_chip(info, nmtd, sets);
  639. nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
  640. (sets) ? sets->nr_chips : 1);
  641. if (nmtd->scan_res == 0) {
  642. s3c2410_nand_update_chip(info, nmtd);
  643. nand_scan_tail(&nmtd->mtd);
  644. s3c2410_nand_add_partition(info, nmtd, sets);
  645. }
  646. if (sets != NULL)
  647. sets++;
  648. }
  649. if (allow_clk_stop(info)) {
  650. dev_info(&pdev->dev, "clock idle support enabled\n");
  651. clk_disable(info->clk);
  652. }
  653. pr_debug("initialised ok\n");
  654. return 0;
  655. exit_error:
  656. s3c2410_nand_remove(pdev);
  657. if (err == 0)
  658. err = -EINVAL;
  659. return err;
  660. }
  661. /* PM Support */
  662. #ifdef CONFIG_PM
  663. static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
  664. {
  665. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  666. if (info) {
  667. info->save_sel = readl(info->sel_reg);
  668. /* For the moment, we must ensure nFCE is high during
  669. * the time we are suspended. This really should be
  670. * handled by suspending the MTDs we are using, but
  671. * that is currently not the case. */
  672. writel(info->save_sel | info->sel_bit, info->sel_reg);
  673. if (!allow_clk_stop(info))
  674. clk_disable(info->clk);
  675. }
  676. return 0;
  677. }
  678. static int s3c24xx_nand_resume(struct platform_device *dev)
  679. {
  680. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  681. unsigned long sel;
  682. if (info) {
  683. clk_enable(info->clk);
  684. s3c2410_nand_inithw(info, dev);
  685. /* Restore the state of the nFCE line. */
  686. sel = readl(info->sel_reg);
  687. sel &= ~info->sel_bit;
  688. sel |= info->save_sel & info->sel_bit;
  689. writel(sel, info->sel_reg);
  690. if (allow_clk_stop(info))
  691. clk_disable(info->clk);
  692. }
  693. return 0;
  694. }
  695. #else
  696. #define s3c24xx_nand_suspend NULL
  697. #define s3c24xx_nand_resume NULL
  698. #endif
  699. /* driver device registration */
  700. static int s3c2410_nand_probe(struct platform_device *dev)
  701. {
  702. return s3c24xx_nand_probe(dev, TYPE_S3C2410);
  703. }
  704. static int s3c2440_nand_probe(struct platform_device *dev)
  705. {
  706. return s3c24xx_nand_probe(dev, TYPE_S3C2440);
  707. }
  708. static int s3c2412_nand_probe(struct platform_device *dev)
  709. {
  710. return s3c24xx_nand_probe(dev, TYPE_S3C2412);
  711. }
  712. static struct platform_driver s3c2410_nand_driver = {
  713. .probe = s3c2410_nand_probe,
  714. .remove = s3c2410_nand_remove,
  715. .suspend = s3c24xx_nand_suspend,
  716. .resume = s3c24xx_nand_resume,
  717. .driver = {
  718. .name = "s3c2410-nand",
  719. .owner = THIS_MODULE,
  720. },
  721. };
  722. static struct platform_driver s3c2440_nand_driver = {
  723. .probe = s3c2440_nand_probe,
  724. .remove = s3c2410_nand_remove,
  725. .suspend = s3c24xx_nand_suspend,
  726. .resume = s3c24xx_nand_resume,
  727. .driver = {
  728. .name = "s3c2440-nand",
  729. .owner = THIS_MODULE,
  730. },
  731. };
  732. static struct platform_driver s3c2412_nand_driver = {
  733. .probe = s3c2412_nand_probe,
  734. .remove = s3c2410_nand_remove,
  735. .suspend = s3c24xx_nand_suspend,
  736. .resume = s3c24xx_nand_resume,
  737. .driver = {
  738. .name = "s3c2412-nand",
  739. .owner = THIS_MODULE,
  740. },
  741. };
  742. static int __init s3c2410_nand_init(void)
  743. {
  744. printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
  745. platform_driver_register(&s3c2412_nand_driver);
  746. platform_driver_register(&s3c2440_nand_driver);
  747. return platform_driver_register(&s3c2410_nand_driver);
  748. }
  749. static void __exit s3c2410_nand_exit(void)
  750. {
  751. platform_driver_unregister(&s3c2412_nand_driver);
  752. platform_driver_unregister(&s3c2440_nand_driver);
  753. platform_driver_unregister(&s3c2410_nand_driver);
  754. }
  755. module_init(s3c2410_nand_init);
  756. module_exit(s3c2410_nand_exit);
  757. MODULE_LICENSE("GPL");
  758. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  759. MODULE_DESCRIPTION("S3C24XX MTD NAND driver");
  760. MODULE_ALIAS("platform:s3c2410-nand");
  761. MODULE_ALIAS("platform:s3c2412-nand");
  762. MODULE_ALIAS("platform:s3c2440-nand");