s3c2410.c 22 KB

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