s3c2410.c 23 KB

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