s3c2410.c 23 KB

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