s3c2410.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. pr_debug("s3c2410_nand_correct_data(%p,%p,%p,%p)\n", mtd, dat, read_ecc, calc_ecc);
  271. pr_debug("eccs: read %02x,%02x,%02x vs calc %02x,%02x,%02x\n",
  272. read_ecc[0], read_ecc[1], read_ecc[2], calc_ecc[0], calc_ecc[1], calc_ecc[2]);
  273. if (read_ecc[0] == calc_ecc[0] && read_ecc[1] == calc_ecc[1] && read_ecc[2] == calc_ecc[2])
  274. return 0;
  275. /* we curently have no method for correcting the error */
  276. return -1;
  277. }
  278. /* ECC functions
  279. *
  280. * These allow the s3c2410 and s3c2440 to use the controller's ECC
  281. * generator block to ECC the data as it passes through]
  282. */
  283. static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  284. {
  285. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  286. unsigned long ctrl;
  287. ctrl = readl(info->regs + S3C2410_NFCONF);
  288. ctrl |= S3C2410_NFCONF_INITECC;
  289. writel(ctrl, info->regs + S3C2410_NFCONF);
  290. }
  291. static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  292. {
  293. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  294. unsigned long ctrl;
  295. ctrl = readl(info->regs + S3C2440_NFCONT);
  296. writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
  297. }
  298. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  299. {
  300. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  301. ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
  302. ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
  303. ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
  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. static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  308. {
  309. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  310. unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
  311. ecc_code[0] = ecc;
  312. ecc_code[1] = ecc >> 8;
  313. ecc_code[2] = ecc >> 16;
  314. pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
  315. return 0;
  316. }
  317. /* over-ride the standard functions for a little more speed. We can
  318. * use read/write block to move the data buffers to/from the controller
  319. */
  320. static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  321. {
  322. struct nand_chip *this = mtd->priv;
  323. readsb(this->IO_ADDR_R, buf, len);
  324. }
  325. static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  326. {
  327. struct nand_chip *this = mtd->priv;
  328. writesb(this->IO_ADDR_W, buf, len);
  329. }
  330. /* device management functions */
  331. static int s3c2410_nand_remove(struct platform_device *pdev)
  332. {
  333. struct s3c2410_nand_info *info = to_nand_info(pdev);
  334. platform_set_drvdata(pdev, NULL);
  335. if (info == NULL)
  336. return 0;
  337. /* first thing we need to do is release all our mtds
  338. * and their partitions, then go through freeing the
  339. * resources used
  340. */
  341. if (info->mtds != NULL) {
  342. struct s3c2410_nand_mtd *ptr = info->mtds;
  343. int mtdno;
  344. for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
  345. pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
  346. nand_release(&ptr->mtd);
  347. }
  348. kfree(info->mtds);
  349. }
  350. /* free the common resources */
  351. if (info->clk != NULL && !IS_ERR(info->clk)) {
  352. if (!allow_clk_stop(info))
  353. clk_disable(info->clk);
  354. clk_put(info->clk);
  355. }
  356. if (info->regs != NULL) {
  357. iounmap(info->regs);
  358. info->regs = NULL;
  359. }
  360. if (info->area != NULL) {
  361. release_resource(info->area);
  362. kfree(info->area);
  363. info->area = NULL;
  364. }
  365. kfree(info);
  366. return 0;
  367. }
  368. #ifdef CONFIG_MTD_PARTITIONS
  369. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  370. struct s3c2410_nand_mtd *mtd,
  371. struct s3c2410_nand_set *set)
  372. {
  373. if (set == NULL)
  374. return add_mtd_device(&mtd->mtd);
  375. if (set->nr_partitions > 0 && set->partitions != NULL) {
  376. return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
  377. }
  378. return add_mtd_device(&mtd->mtd);
  379. }
  380. #else
  381. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  382. struct s3c2410_nand_mtd *mtd,
  383. struct s3c2410_nand_set *set)
  384. {
  385. return add_mtd_device(&mtd->mtd);
  386. }
  387. #endif
  388. /* s3c2410_nand_init_chip
  389. *
  390. * init a single instance of an chip
  391. */
  392. static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
  393. struct s3c2410_nand_mtd *nmtd,
  394. struct s3c2410_nand_set *set)
  395. {
  396. struct nand_chip *chip = &nmtd->chip;
  397. void __iomem *regs = info->regs;
  398. chip->write_buf = s3c2410_nand_write_buf;
  399. chip->read_buf = s3c2410_nand_read_buf;
  400. chip->select_chip = s3c2410_nand_select_chip;
  401. chip->chip_delay = 50;
  402. chip->priv = nmtd;
  403. chip->options = 0;
  404. chip->controller = &info->controller;
  405. switch (info->cpu_type) {
  406. case TYPE_S3C2410:
  407. chip->IO_ADDR_W = regs + S3C2410_NFDATA;
  408. info->sel_reg = regs + S3C2410_NFCONF;
  409. info->sel_bit = S3C2410_NFCONF_nFCE;
  410. chip->cmd_ctrl = s3c2410_nand_hwcontrol;
  411. chip->dev_ready = s3c2410_nand_devready;
  412. break;
  413. case TYPE_S3C2440:
  414. chip->IO_ADDR_W = regs + S3C2440_NFDATA;
  415. info->sel_reg = regs + S3C2440_NFCONT;
  416. info->sel_bit = S3C2440_NFCONT_nFCE;
  417. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  418. chip->dev_ready = s3c2440_nand_devready;
  419. break;
  420. case TYPE_S3C2412:
  421. chip->IO_ADDR_W = regs + S3C2440_NFDATA;
  422. info->sel_reg = regs + S3C2440_NFCONT;
  423. info->sel_bit = S3C2412_NFCONT_nFCE0;
  424. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  425. chip->dev_ready = s3c2412_nand_devready;
  426. if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
  427. dev_info(info->device, "System booted from NAND\n");
  428. break;
  429. }
  430. chip->IO_ADDR_R = chip->IO_ADDR_W;
  431. nmtd->info = info;
  432. nmtd->mtd.priv = chip;
  433. nmtd->mtd.owner = THIS_MODULE;
  434. nmtd->set = set;
  435. if (hardware_ecc) {
  436. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  437. chip->ecc.correct = s3c2410_nand_correct_data;
  438. chip->ecc.mode = NAND_ECC_HW;
  439. chip->ecc.size = 512;
  440. chip->ecc.bytes = 3;
  441. chip->ecc.layout = &nand_hw_eccoob;
  442. switch (info->cpu_type) {
  443. case TYPE_S3C2410:
  444. chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
  445. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  446. break;
  447. case TYPE_S3C2412:
  448. case TYPE_S3C2440:
  449. chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
  450. chip->ecc.calculate = s3c2440_nand_calculate_ecc;
  451. break;
  452. }
  453. } else {
  454. chip->ecc.mode = NAND_ECC_SOFT;
  455. }
  456. }
  457. /* s3c2410_nand_probe
  458. *
  459. * called by device layer when it finds a device matching
  460. * one our driver can handled. This code checks to see if
  461. * it can allocate all necessary resources then calls the
  462. * nand layer to look for devices
  463. */
  464. static int s3c24xx_nand_probe(struct platform_device *pdev,
  465. enum s3c_cpu_type cpu_type)
  466. {
  467. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  468. struct s3c2410_nand_info *info;
  469. struct s3c2410_nand_mtd *nmtd;
  470. struct s3c2410_nand_set *sets;
  471. struct resource *res;
  472. int err = 0;
  473. int size;
  474. int nr_sets;
  475. int setno;
  476. pr_debug("s3c2410_nand_probe(%p)\n", pdev);
  477. info = kmalloc(sizeof(*info), GFP_KERNEL);
  478. if (info == NULL) {
  479. dev_err(&pdev->dev, "no memory for flash info\n");
  480. err = -ENOMEM;
  481. goto exit_error;
  482. }
  483. memzero(info, sizeof(*info));
  484. platform_set_drvdata(pdev, info);
  485. spin_lock_init(&info->controller.lock);
  486. init_waitqueue_head(&info->controller.wq);
  487. /* get the clock source and enable it */
  488. info->clk = clk_get(&pdev->dev, "nand");
  489. if (IS_ERR(info->clk)) {
  490. dev_err(&pdev->dev, "failed to get clock");
  491. err = -ENOENT;
  492. goto exit_error;
  493. }
  494. clk_enable(info->clk);
  495. /* allocate and map the resource */
  496. /* currently we assume we have the one resource */
  497. res = pdev->resource;
  498. size = res->end - res->start + 1;
  499. info->area = request_mem_region(res->start, size, pdev->name);
  500. if (info->area == NULL) {
  501. dev_err(&pdev->dev, "cannot reserve register region\n");
  502. err = -ENOENT;
  503. goto exit_error;
  504. }
  505. info->device = &pdev->dev;
  506. info->platform = plat;
  507. info->regs = ioremap(res->start, size);
  508. info->cpu_type = cpu_type;
  509. if (info->regs == NULL) {
  510. dev_err(&pdev->dev, "cannot reserve register region\n");
  511. err = -EIO;
  512. goto exit_error;
  513. }
  514. dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
  515. /* initialise the hardware */
  516. err = s3c2410_nand_inithw(info, pdev);
  517. if (err != 0)
  518. goto exit_error;
  519. sets = (plat != NULL) ? plat->sets : NULL;
  520. nr_sets = (plat != NULL) ? plat->nr_sets : 1;
  521. info->mtd_count = nr_sets;
  522. /* allocate our information */
  523. size = nr_sets * sizeof(*info->mtds);
  524. info->mtds = kmalloc(size, GFP_KERNEL);
  525. if (info->mtds == NULL) {
  526. dev_err(&pdev->dev, "failed to allocate mtd storage\n");
  527. err = -ENOMEM;
  528. goto exit_error;
  529. }
  530. memzero(info->mtds, size);
  531. /* initialise all possible chips */
  532. nmtd = info->mtds;
  533. for (setno = 0; setno < nr_sets; setno++, nmtd++) {
  534. pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
  535. s3c2410_nand_init_chip(info, nmtd, sets);
  536. nmtd->scan_res = nand_scan(&nmtd->mtd, (sets) ? sets->nr_chips : 1);
  537. if (nmtd->scan_res == 0) {
  538. s3c2410_nand_add_partition(info, nmtd, sets);
  539. }
  540. if (sets != NULL)
  541. sets++;
  542. }
  543. if (allow_clk_stop(info)) {
  544. dev_info(&pdev->dev, "clock idle support enabled\n");
  545. clk_disable(info->clk);
  546. }
  547. pr_debug("initialised ok\n");
  548. return 0;
  549. exit_error:
  550. s3c2410_nand_remove(pdev);
  551. if (err == 0)
  552. err = -EINVAL;
  553. return err;
  554. }
  555. /* PM Support */
  556. #ifdef CONFIG_PM
  557. static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
  558. {
  559. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  560. if (info) {
  561. if (!allow_clk_stop(info))
  562. clk_disable(info->clk);
  563. }
  564. return 0;
  565. }
  566. static int s3c24xx_nand_resume(struct platform_device *dev)
  567. {
  568. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  569. if (info) {
  570. clk_enable(info->clk);
  571. s3c2410_nand_inithw(info, dev);
  572. if (allow_clk_stop(info))
  573. clk_disable(info->clk);
  574. }
  575. return 0;
  576. }
  577. #else
  578. #define s3c24xx_nand_suspend NULL
  579. #define s3c24xx_nand_resume NULL
  580. #endif
  581. /* driver device registration */
  582. static int s3c2410_nand_probe(struct platform_device *dev)
  583. {
  584. return s3c24xx_nand_probe(dev, TYPE_S3C2410);
  585. }
  586. static int s3c2440_nand_probe(struct platform_device *dev)
  587. {
  588. return s3c24xx_nand_probe(dev, TYPE_S3C2440);
  589. }
  590. static int s3c2412_nand_probe(struct platform_device *dev)
  591. {
  592. return s3c24xx_nand_probe(dev, TYPE_S3C2412);
  593. }
  594. static struct platform_driver s3c2410_nand_driver = {
  595. .probe = s3c2410_nand_probe,
  596. .remove = s3c2410_nand_remove,
  597. .suspend = s3c24xx_nand_suspend,
  598. .resume = s3c24xx_nand_resume,
  599. .driver = {
  600. .name = "s3c2410-nand",
  601. .owner = THIS_MODULE,
  602. },
  603. };
  604. static struct platform_driver s3c2440_nand_driver = {
  605. .probe = s3c2440_nand_probe,
  606. .remove = s3c2410_nand_remove,
  607. .suspend = s3c24xx_nand_suspend,
  608. .resume = s3c24xx_nand_resume,
  609. .driver = {
  610. .name = "s3c2440-nand",
  611. .owner = THIS_MODULE,
  612. },
  613. };
  614. static struct platform_driver s3c2412_nand_driver = {
  615. .probe = s3c2412_nand_probe,
  616. .remove = s3c2410_nand_remove,
  617. .suspend = s3c24xx_nand_suspend,
  618. .resume = s3c24xx_nand_resume,
  619. .driver = {
  620. .name = "s3c2412-nand",
  621. .owner = THIS_MODULE,
  622. },
  623. };
  624. static int __init s3c2410_nand_init(void)
  625. {
  626. printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
  627. platform_driver_register(&s3c2412_nand_driver);
  628. platform_driver_register(&s3c2440_nand_driver);
  629. return platform_driver_register(&s3c2410_nand_driver);
  630. }
  631. static void __exit s3c2410_nand_exit(void)
  632. {
  633. platform_driver_unregister(&s3c2412_nand_driver);
  634. platform_driver_unregister(&s3c2440_nand_driver);
  635. platform_driver_unregister(&s3c2410_nand_driver);
  636. }
  637. module_init(s3c2410_nand_init);
  638. module_exit(s3c2410_nand_exit);
  639. MODULE_LICENSE("GPL");
  640. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  641. MODULE_DESCRIPTION("S3C24XX MTD NAND driver");