s3c2410.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. #define PFX "s3c2410-nand: "
  61. #ifdef CONFIG_MTD_NAND_S3C2410_HWECC
  62. static int hardware_ecc = 1;
  63. #else
  64. static int hardware_ecc = 0;
  65. #endif
  66. #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
  67. static int clock_stop = 1;
  68. #else
  69. static const int clock_stop = 0;
  70. #endif
  71. /* new oob placement block for use with hardware ecc generation
  72. */
  73. static struct nand_ecclayout nand_hw_eccoob = {
  74. .eccbytes = 3,
  75. .eccpos = {0, 1, 2},
  76. .oobfree = {{8, 8}}
  77. };
  78. /* controller and mtd information */
  79. struct s3c2410_nand_info;
  80. struct s3c2410_nand_mtd {
  81. struct mtd_info mtd;
  82. struct nand_chip chip;
  83. struct s3c2410_nand_set *set;
  84. struct s3c2410_nand_info *info;
  85. int scan_res;
  86. };
  87. /* overview of the s3c2410 nand state */
  88. struct s3c2410_nand_info {
  89. /* mtd info */
  90. struct nand_hw_control controller;
  91. struct s3c2410_nand_mtd *mtds;
  92. struct s3c2410_platform_nand *platform;
  93. /* device info */
  94. struct device *device;
  95. struct resource *area;
  96. struct clk *clk;
  97. void __iomem *regs;
  98. int mtd_count;
  99. unsigned char is_s3c2440;
  100. };
  101. /* conversion functions */
  102. static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
  103. {
  104. return container_of(mtd, struct s3c2410_nand_mtd, mtd);
  105. }
  106. static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
  107. {
  108. return s3c2410_nand_mtd_toours(mtd)->info;
  109. }
  110. static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
  111. {
  112. return platform_get_drvdata(dev);
  113. }
  114. static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
  115. {
  116. return dev->dev.platform_data;
  117. }
  118. static inline int allow_clk_stop(struct s3c2410_nand_info *info)
  119. {
  120. return clock_stop;
  121. }
  122. /* timing calculations */
  123. #define NS_IN_KHZ 1000000
  124. static int s3c2410_nand_calc_rate(int wanted, unsigned long clk, int max)
  125. {
  126. int result;
  127. result = (wanted * clk) / NS_IN_KHZ;
  128. result++;
  129. pr_debug("result %d from %ld, %d\n", result, clk, wanted);
  130. if (result > max) {
  131. printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
  132. return -1;
  133. }
  134. if (result < 1)
  135. result = 1;
  136. return result;
  137. }
  138. #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
  139. /* controller setup */
  140. static int s3c2410_nand_inithw(struct s3c2410_nand_info *info, struct platform_device *pdev)
  141. {
  142. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  143. unsigned long clkrate = clk_get_rate(info->clk);
  144. int tacls, twrph0, twrph1;
  145. unsigned long cfg;
  146. /* calculate the timing information for the controller */
  147. clkrate /= 1000; /* turn clock into kHz for ease of use */
  148. if (plat != NULL) {
  149. tacls = s3c2410_nand_calc_rate(plat->tacls, clkrate, 4);
  150. twrph0 = s3c2410_nand_calc_rate(plat->twrph0, clkrate, 8);
  151. twrph1 = s3c2410_nand_calc_rate(plat->twrph1, clkrate, 8);
  152. } else {
  153. /* default timings */
  154. tacls = 4;
  155. twrph0 = 8;
  156. twrph1 = 8;
  157. }
  158. if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
  159. printk(KERN_ERR PFX "cannot get timings suitable for board\n");
  160. return -EINVAL;
  161. }
  162. printk(KERN_INFO PFX "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
  163. tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
  164. if (!info->is_s3c2440) {
  165. cfg = S3C2410_NFCONF_EN;
  166. cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
  167. cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
  168. cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
  169. } else {
  170. cfg = S3C2440_NFCONF_TACLS(tacls - 1);
  171. cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
  172. cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
  173. /* enable the controller and de-assert nFCE */
  174. writel(S3C2440_NFCONT_ENABLE | S3C2440_NFCONT_ENABLE,
  175. info->regs + S3C2440_NFCONT);
  176. }
  177. pr_debug(PFX "NF_CONF is 0x%lx\n", cfg);
  178. writel(cfg, info->regs + S3C2410_NFCONF);
  179. return 0;
  180. }
  181. /* select chip */
  182. static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
  183. {
  184. struct s3c2410_nand_info *info;
  185. struct s3c2410_nand_mtd *nmtd;
  186. struct nand_chip *this = mtd->priv;
  187. void __iomem *reg;
  188. unsigned long cur;
  189. unsigned long bit;
  190. nmtd = this->priv;
  191. info = nmtd->info;
  192. bit = (info->is_s3c2440) ? S3C2440_NFCONT_nFCE : S3C2410_NFCONF_nFCE;
  193. reg = info->regs + ((info->is_s3c2440) ? S3C2440_NFCONT : S3C2410_NFCONF);
  194. if (chip != -1 && allow_clk_stop(info))
  195. clk_enable(info->clk);
  196. cur = readl(reg);
  197. if (chip == -1) {
  198. cur |= bit;
  199. } else {
  200. if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
  201. printk(KERN_ERR PFX "chip %d out of range\n", chip);
  202. return;
  203. }
  204. if (info->platform != NULL) {
  205. if (info->platform->select_chip != NULL)
  206. (info->platform->select_chip) (nmtd->set, chip);
  207. }
  208. cur &= ~bit;
  209. }
  210. writel(cur, reg);
  211. if (chip == -1 && allow_clk_stop(info))
  212. clk_disable(info->clk);
  213. }
  214. /* command and control functions
  215. *
  216. * Note, these all use tglx's method of changing the IO_ADDR_W field
  217. * to make the code simpler, and use the nand layer's code to issue the
  218. * command and address sequences via the proper IO ports.
  219. *
  220. */
  221. static void s3c2410_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. struct nand_chip *chip = mtd->priv;
  226. if (cmd == NAND_CMD_NONE)
  227. return;
  228. if (ctrl & NAND_CLE)
  229. writeb(cmd, info->regs + S3C2410_NFCMD);
  230. else
  231. writeb(cmd, info->regs + S3C2410_NFADDR);
  232. }
  233. /* command and control functions */
  234. static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  235. unsigned int ctrl)
  236. {
  237. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  238. struct nand_chip *chip = mtd->priv;
  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. if (info->is_s3c2440)
  254. return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
  255. return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
  256. }
  257. /* ECC handling functions */
  258. static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  259. {
  260. pr_debug("s3c2410_nand_correct_data(%p,%p,%p,%p)\n", mtd, dat, read_ecc, calc_ecc);
  261. pr_debug("eccs: read %02x,%02x,%02x vs calc %02x,%02x,%02x\n",
  262. read_ecc[0], read_ecc[1], read_ecc[2], calc_ecc[0], calc_ecc[1], calc_ecc[2]);
  263. if (read_ecc[0] == calc_ecc[0] && read_ecc[1] == calc_ecc[1] && read_ecc[2] == calc_ecc[2])
  264. return 0;
  265. /* we curently have no method for correcting the error */
  266. return -1;
  267. }
  268. /* ECC functions
  269. *
  270. * These allow the s3c2410 and s3c2440 to use the controller's ECC
  271. * generator block to ECC the data as it passes through]
  272. */
  273. static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  274. {
  275. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  276. unsigned long ctrl;
  277. ctrl = readl(info->regs + S3C2410_NFCONF);
  278. ctrl |= S3C2410_NFCONF_INITECC;
  279. writel(ctrl, info->regs + S3C2410_NFCONF);
  280. }
  281. static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  282. {
  283. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  284. unsigned long ctrl;
  285. ctrl = readl(info->regs + S3C2440_NFCONT);
  286. writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
  287. }
  288. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  289. {
  290. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  291. ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
  292. ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
  293. ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
  294. pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
  295. return 0;
  296. }
  297. static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  298. {
  299. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  300. unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
  301. ecc_code[0] = ecc;
  302. ecc_code[1] = ecc >> 8;
  303. ecc_code[2] = ecc >> 16;
  304. pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
  305. return 0;
  306. }
  307. /* over-ride the standard functions for a little more speed. We can
  308. * use read/write block to move the data buffers to/from the controller
  309. */
  310. static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  311. {
  312. struct nand_chip *this = mtd->priv;
  313. readsb(this->IO_ADDR_R, buf, len);
  314. }
  315. static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  316. {
  317. struct nand_chip *this = mtd->priv;
  318. writesb(this->IO_ADDR_W, buf, len);
  319. }
  320. /* device management functions */
  321. static int s3c2410_nand_remove(struct platform_device *pdev)
  322. {
  323. struct s3c2410_nand_info *info = to_nand_info(pdev);
  324. platform_set_drvdata(pdev, NULL);
  325. if (info == NULL)
  326. return 0;
  327. /* first thing we need to do is release all our mtds
  328. * and their partitions, then go through freeing the
  329. * resources used
  330. */
  331. if (info->mtds != NULL) {
  332. struct s3c2410_nand_mtd *ptr = info->mtds;
  333. int mtdno;
  334. for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
  335. pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
  336. nand_release(&ptr->mtd);
  337. }
  338. kfree(info->mtds);
  339. }
  340. /* free the common resources */
  341. if (info->clk != NULL && !IS_ERR(info->clk)) {
  342. if (!allow_clk_stop(info))
  343. clk_disable(info->clk);
  344. clk_put(info->clk);
  345. }
  346. if (info->regs != NULL) {
  347. iounmap(info->regs);
  348. info->regs = NULL;
  349. }
  350. if (info->area != NULL) {
  351. release_resource(info->area);
  352. kfree(info->area);
  353. info->area = NULL;
  354. }
  355. kfree(info);
  356. return 0;
  357. }
  358. #ifdef CONFIG_MTD_PARTITIONS
  359. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  360. struct s3c2410_nand_mtd *mtd,
  361. struct s3c2410_nand_set *set)
  362. {
  363. if (set == NULL)
  364. return add_mtd_device(&mtd->mtd);
  365. if (set->nr_partitions > 0 && set->partitions != NULL) {
  366. return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
  367. }
  368. return add_mtd_device(&mtd->mtd);
  369. }
  370. #else
  371. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  372. struct s3c2410_nand_mtd *mtd,
  373. struct s3c2410_nand_set *set)
  374. {
  375. return add_mtd_device(&mtd->mtd);
  376. }
  377. #endif
  378. /* s3c2410_nand_init_chip
  379. *
  380. * init a single instance of an chip
  381. */
  382. static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
  383. struct s3c2410_nand_mtd *nmtd,
  384. struct s3c2410_nand_set *set)
  385. {
  386. struct nand_chip *chip = &nmtd->chip;
  387. chip->IO_ADDR_R = info->regs + S3C2410_NFDATA;
  388. chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
  389. chip->cmd_ctrl = s3c2410_nand_hwcontrol;
  390. chip->dev_ready = s3c2410_nand_devready;
  391. chip->write_buf = s3c2410_nand_write_buf;
  392. chip->read_buf = s3c2410_nand_read_buf;
  393. chip->select_chip = s3c2410_nand_select_chip;
  394. chip->chip_delay = 50;
  395. chip->priv = nmtd;
  396. chip->options = 0;
  397. chip->controller = &info->controller;
  398. if (info->is_s3c2440) {
  399. chip->IO_ADDR_R = info->regs + S3C2440_NFDATA;
  400. chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
  401. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  402. }
  403. nmtd->info = info;
  404. nmtd->mtd.priv = chip;
  405. nmtd->mtd.owner = THIS_MODULE;
  406. nmtd->set = set;
  407. if (hardware_ecc) {
  408. chip->ecc.correct = s3c2410_nand_correct_data;
  409. chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
  410. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  411. chip->ecc.mode = NAND_ECC_HW;
  412. chip->ecc.size = 512;
  413. chip->ecc.bytes = 3;
  414. chip->ecc.layout = &nand_hw_eccoob;
  415. if (info->is_s3c2440) {
  416. chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
  417. chip->ecc.calculate = s3c2440_nand_calculate_ecc;
  418. }
  419. } else {
  420. chip->ecc.mode = NAND_ECC_SOFT;
  421. }
  422. }
  423. /* s3c2410_nand_probe
  424. *
  425. * called by device layer when it finds a device matching
  426. * one our driver can handled. This code checks to see if
  427. * it can allocate all necessary resources then calls the
  428. * nand layer to look for devices
  429. */
  430. static int s3c24xx_nand_probe(struct platform_device *pdev, int is_s3c2440)
  431. {
  432. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  433. struct s3c2410_nand_info *info;
  434. struct s3c2410_nand_mtd *nmtd;
  435. struct s3c2410_nand_set *sets;
  436. struct resource *res;
  437. int err = 0;
  438. int size;
  439. int nr_sets;
  440. int setno;
  441. pr_debug("s3c2410_nand_probe(%p)\n", pdev);
  442. info = kmalloc(sizeof(*info), GFP_KERNEL);
  443. if (info == NULL) {
  444. dev_err(&pdev->dev, "no memory for flash info\n");
  445. err = -ENOMEM;
  446. goto exit_error;
  447. }
  448. memzero(info, sizeof(*info));
  449. platform_set_drvdata(pdev, info);
  450. spin_lock_init(&info->controller.lock);
  451. init_waitqueue_head(&info->controller.wq);
  452. /* get the clock source and enable it */
  453. info->clk = clk_get(&pdev->dev, "nand");
  454. if (IS_ERR(info->clk)) {
  455. dev_err(&pdev->dev, "failed to get clock");
  456. err = -ENOENT;
  457. goto exit_error;
  458. }
  459. clk_enable(info->clk);
  460. /* allocate and map the resource */
  461. /* currently we assume we have the one resource */
  462. res = pdev->resource;
  463. size = res->end - res->start + 1;
  464. info->area = request_mem_region(res->start, size, pdev->name);
  465. if (info->area == NULL) {
  466. dev_err(&pdev->dev, "cannot reserve register region\n");
  467. err = -ENOENT;
  468. goto exit_error;
  469. }
  470. info->device = &pdev->dev;
  471. info->platform = plat;
  472. info->regs = ioremap(res->start, size);
  473. info->is_s3c2440 = is_s3c2440;
  474. if (info->regs == NULL) {
  475. dev_err(&pdev->dev, "cannot reserve register region\n");
  476. err = -EIO;
  477. goto exit_error;
  478. }
  479. dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
  480. /* initialise the hardware */
  481. err = s3c2410_nand_inithw(info, pdev);
  482. if (err != 0)
  483. goto exit_error;
  484. sets = (plat != NULL) ? plat->sets : NULL;
  485. nr_sets = (plat != NULL) ? plat->nr_sets : 1;
  486. info->mtd_count = nr_sets;
  487. /* allocate our information */
  488. size = nr_sets * sizeof(*info->mtds);
  489. info->mtds = kmalloc(size, GFP_KERNEL);
  490. if (info->mtds == NULL) {
  491. dev_err(&pdev->dev, "failed to allocate mtd storage\n");
  492. err = -ENOMEM;
  493. goto exit_error;
  494. }
  495. memzero(info->mtds, size);
  496. /* initialise all possible chips */
  497. nmtd = info->mtds;
  498. for (setno = 0; setno < nr_sets; setno++, nmtd++) {
  499. pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
  500. s3c2410_nand_init_chip(info, nmtd, sets);
  501. nmtd->scan_res = nand_scan(&nmtd->mtd, (sets) ? sets->nr_chips : 1);
  502. if (nmtd->scan_res == 0) {
  503. s3c2410_nand_add_partition(info, nmtd, sets);
  504. }
  505. if (sets != NULL)
  506. sets++;
  507. }
  508. if (allow_clk_stop(info)) {
  509. dev_info(&pdev->dev, "clock idle support enabled\n");
  510. clk_disable(info->clk);
  511. }
  512. pr_debug("initialised ok\n");
  513. return 0;
  514. exit_error:
  515. s3c2410_nand_remove(pdev);
  516. if (err == 0)
  517. err = -EINVAL;
  518. return err;
  519. }
  520. /* PM Support */
  521. #ifdef CONFIG_PM
  522. static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
  523. {
  524. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  525. if (info) {
  526. if (!allow_clk_stop(info))
  527. clk_disable(info->clk);
  528. }
  529. return 0;
  530. }
  531. static int s3c24xx_nand_resume(struct platform_device *dev)
  532. {
  533. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  534. if (info) {
  535. clk_enable(info->clk);
  536. s3c2410_nand_inithw(info, dev);
  537. if (allow_clk_stop(info))
  538. clk_disable(info->clk);
  539. }
  540. return 0;
  541. }
  542. #else
  543. #define s3c24xx_nand_suspend NULL
  544. #define s3c24xx_nand_resume NULL
  545. #endif
  546. /* driver device registration */
  547. static int s3c2410_nand_probe(struct platform_device *dev)
  548. {
  549. return s3c24xx_nand_probe(dev, 0);
  550. }
  551. static int s3c2440_nand_probe(struct platform_device *dev)
  552. {
  553. return s3c24xx_nand_probe(dev, 1);
  554. }
  555. static struct platform_driver s3c2410_nand_driver = {
  556. .probe = s3c2410_nand_probe,
  557. .remove = s3c2410_nand_remove,
  558. .suspend = s3c24xx_nand_suspend,
  559. .resume = s3c24xx_nand_resume,
  560. .driver = {
  561. .name = "s3c2410-nand",
  562. .owner = THIS_MODULE,
  563. },
  564. };
  565. static struct platform_driver s3c2440_nand_driver = {
  566. .probe = s3c2440_nand_probe,
  567. .remove = s3c2410_nand_remove,
  568. .suspend = s3c24xx_nand_suspend,
  569. .resume = s3c24xx_nand_resume,
  570. .driver = {
  571. .name = "s3c2440-nand",
  572. .owner = THIS_MODULE,
  573. },
  574. };
  575. static int __init s3c2410_nand_init(void)
  576. {
  577. printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
  578. platform_driver_register(&s3c2440_nand_driver);
  579. return platform_driver_register(&s3c2410_nand_driver);
  580. }
  581. static void __exit s3c2410_nand_exit(void)
  582. {
  583. platform_driver_unregister(&s3c2440_nand_driver);
  584. platform_driver_unregister(&s3c2410_nand_driver);
  585. }
  586. module_init(s3c2410_nand_init);
  587. module_exit(s3c2410_nand_exit);
  588. MODULE_LICENSE("GPL");
  589. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  590. MODULE_DESCRIPTION("S3C24XX MTD NAND driver");