s3c2410.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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_sel;
  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. /* sometimes people do not think about using the ECC, so check
  286. * to see if we have an 0xff,0xff,0xff read ECC and then ignore
  287. * the error, on the assumption that this is an un-eccd page.
  288. */
  289. if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
  290. && info->platform->ignore_unset_ecc)
  291. return 0;
  292. /* Can we correct this ECC (ie, one row and column change).
  293. * Note, this is similar to the 256 error code on smartmedia */
  294. if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
  295. ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
  296. ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
  297. /* calculate the bit position of the error */
  298. bit = ((diff2 >> 3) & 1) |
  299. ((diff2 >> 4) & 2) |
  300. ((diff2 >> 5) & 4);
  301. /* calculate the byte position of the error */
  302. byte = ((diff2 << 7) & 0x100) |
  303. ((diff1 << 0) & 0x80) |
  304. ((diff1 << 1) & 0x40) |
  305. ((diff1 << 2) & 0x20) |
  306. ((diff1 << 3) & 0x10) |
  307. ((diff0 >> 4) & 0x08) |
  308. ((diff0 >> 3) & 0x04) |
  309. ((diff0 >> 2) & 0x02) |
  310. ((diff0 >> 1) & 0x01);
  311. dev_dbg(info->device, "correcting error bit %d, byte %d\n",
  312. bit, byte);
  313. dat[byte] ^= (1 << bit);
  314. return 1;
  315. }
  316. /* if there is only one bit difference in the ECC, then
  317. * one of only a row or column parity has changed, which
  318. * means the error is most probably in the ECC itself */
  319. diff0 |= (diff1 << 8);
  320. diff0 |= (diff2 << 16);
  321. if ((diff0 & ~(1<<fls(diff0))) == 0)
  322. return 1;
  323. return -1;
  324. }
  325. /* ECC functions
  326. *
  327. * These allow the s3c2410 and s3c2440 to use the controller's ECC
  328. * generator block to ECC the data as it passes through]
  329. */
  330. static void s3c2410_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 + S3C2410_NFCONF);
  335. ctrl |= S3C2410_NFCONF_INITECC;
  336. writel(ctrl, info->regs + S3C2410_NFCONF);
  337. }
  338. static void s3c2412_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 | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
  344. }
  345. static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  346. {
  347. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  348. unsigned long ctrl;
  349. ctrl = readl(info->regs + S3C2440_NFCONT);
  350. writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
  351. }
  352. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  353. {
  354. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  355. ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
  356. ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
  357. ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
  358. pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
  359. ecc_code[0], ecc_code[1], ecc_code[2]);
  360. return 0;
  361. }
  362. static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  363. {
  364. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  365. unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
  366. ecc_code[0] = ecc;
  367. ecc_code[1] = ecc >> 8;
  368. ecc_code[2] = ecc >> 16;
  369. pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
  370. return 0;
  371. }
  372. static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  373. {
  374. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  375. unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
  376. ecc_code[0] = ecc;
  377. ecc_code[1] = ecc >> 8;
  378. ecc_code[2] = ecc >> 16;
  379. pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
  380. return 0;
  381. }
  382. /* over-ride the standard functions for a little more speed. We can
  383. * use read/write block to move the data buffers to/from the controller
  384. */
  385. static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  386. {
  387. struct nand_chip *this = mtd->priv;
  388. readsb(this->IO_ADDR_R, buf, len);
  389. }
  390. static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  391. {
  392. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  393. readsl(info->regs + S3C2440_NFDATA, buf, len / 4);
  394. }
  395. static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  396. {
  397. struct nand_chip *this = mtd->priv;
  398. writesb(this->IO_ADDR_W, buf, len);
  399. }
  400. static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  401. {
  402. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  403. writesl(info->regs + S3C2440_NFDATA, buf, len / 4);
  404. }
  405. /* device management functions */
  406. static int s3c2410_nand_remove(struct platform_device *pdev)
  407. {
  408. struct s3c2410_nand_info *info = to_nand_info(pdev);
  409. platform_set_drvdata(pdev, NULL);
  410. if (info == NULL)
  411. return 0;
  412. /* first thing we need to do is release all our mtds
  413. * and their partitions, then go through freeing the
  414. * resources used
  415. */
  416. if (info->mtds != NULL) {
  417. struct s3c2410_nand_mtd *ptr = info->mtds;
  418. int mtdno;
  419. for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
  420. pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
  421. nand_release(&ptr->mtd);
  422. }
  423. kfree(info->mtds);
  424. }
  425. /* free the common resources */
  426. if (info->clk != NULL && !IS_ERR(info->clk)) {
  427. if (!allow_clk_stop(info))
  428. clk_disable(info->clk);
  429. clk_put(info->clk);
  430. }
  431. if (info->regs != NULL) {
  432. iounmap(info->regs);
  433. info->regs = NULL;
  434. }
  435. if (info->area != NULL) {
  436. release_resource(info->area);
  437. kfree(info->area);
  438. info->area = NULL;
  439. }
  440. kfree(info);
  441. return 0;
  442. }
  443. #ifdef CONFIG_MTD_PARTITIONS
  444. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  445. struct s3c2410_nand_mtd *mtd,
  446. struct s3c2410_nand_set *set)
  447. {
  448. if (set == NULL)
  449. return add_mtd_device(&mtd->mtd);
  450. if (set->nr_partitions > 0 && set->partitions != NULL) {
  451. return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
  452. }
  453. return add_mtd_device(&mtd->mtd);
  454. }
  455. #else
  456. static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
  457. struct s3c2410_nand_mtd *mtd,
  458. struct s3c2410_nand_set *set)
  459. {
  460. return add_mtd_device(&mtd->mtd);
  461. }
  462. #endif
  463. /* s3c2410_nand_init_chip
  464. *
  465. * init a single instance of an chip
  466. */
  467. static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
  468. struct s3c2410_nand_mtd *nmtd,
  469. struct s3c2410_nand_set *set)
  470. {
  471. struct nand_chip *chip = &nmtd->chip;
  472. void __iomem *regs = info->regs;
  473. chip->write_buf = s3c2410_nand_write_buf;
  474. chip->read_buf = s3c2410_nand_read_buf;
  475. chip->select_chip = s3c2410_nand_select_chip;
  476. chip->chip_delay = 50;
  477. chip->priv = nmtd;
  478. chip->options = 0;
  479. chip->controller = &info->controller;
  480. switch (info->cpu_type) {
  481. case TYPE_S3C2410:
  482. chip->IO_ADDR_W = regs + S3C2410_NFDATA;
  483. info->sel_reg = regs + S3C2410_NFCONF;
  484. info->sel_bit = S3C2410_NFCONF_nFCE;
  485. chip->cmd_ctrl = s3c2410_nand_hwcontrol;
  486. chip->dev_ready = s3c2410_nand_devready;
  487. break;
  488. case TYPE_S3C2440:
  489. chip->IO_ADDR_W = regs + S3C2440_NFDATA;
  490. info->sel_reg = regs + S3C2440_NFCONT;
  491. info->sel_bit = S3C2440_NFCONT_nFCE;
  492. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  493. chip->dev_ready = s3c2440_nand_devready;
  494. chip->read_buf = s3c2440_nand_read_buf;
  495. chip->write_buf = s3c2440_nand_write_buf;
  496. break;
  497. case TYPE_S3C2412:
  498. chip->IO_ADDR_W = regs + S3C2440_NFDATA;
  499. info->sel_reg = regs + S3C2440_NFCONT;
  500. info->sel_bit = S3C2412_NFCONT_nFCE0;
  501. chip->cmd_ctrl = s3c2440_nand_hwcontrol;
  502. chip->dev_ready = s3c2412_nand_devready;
  503. if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
  504. dev_info(info->device, "System booted from NAND\n");
  505. break;
  506. }
  507. chip->IO_ADDR_R = chip->IO_ADDR_W;
  508. nmtd->info = info;
  509. nmtd->mtd.priv = chip;
  510. nmtd->mtd.owner = THIS_MODULE;
  511. nmtd->set = set;
  512. if (hardware_ecc) {
  513. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  514. chip->ecc.correct = s3c2410_nand_correct_data;
  515. chip->ecc.mode = NAND_ECC_HW;
  516. switch (info->cpu_type) {
  517. case TYPE_S3C2410:
  518. chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
  519. chip->ecc.calculate = s3c2410_nand_calculate_ecc;
  520. break;
  521. case TYPE_S3C2412:
  522. chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
  523. chip->ecc.calculate = s3c2412_nand_calculate_ecc;
  524. break;
  525. case TYPE_S3C2440:
  526. chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
  527. chip->ecc.calculate = s3c2440_nand_calculate_ecc;
  528. break;
  529. }
  530. } else {
  531. chip->ecc.mode = NAND_ECC_SOFT;
  532. }
  533. if (set->ecc_layout != NULL)
  534. chip->ecc.layout = set->ecc_layout;
  535. if (set->disable_ecc)
  536. chip->ecc.mode = NAND_ECC_NONE;
  537. }
  538. /* s3c2410_nand_update_chip
  539. *
  540. * post-probe chip update, to change any items, such as the
  541. * layout for large page nand
  542. */
  543. static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
  544. struct s3c2410_nand_mtd *nmtd)
  545. {
  546. struct nand_chip *chip = &nmtd->chip;
  547. printk("%s: chip %p: %d\n", __func__, chip, chip->page_shift);
  548. if (hardware_ecc) {
  549. /* change the behaviour depending on wether we are using
  550. * the large or small page nand device */
  551. if (chip->page_shift > 10) {
  552. chip->ecc.size = 256;
  553. chip->ecc.bytes = 3;
  554. } else {
  555. chip->ecc.size = 512;
  556. chip->ecc.bytes = 3;
  557. chip->ecc.layout = &nand_hw_eccoob;
  558. }
  559. }
  560. }
  561. /* s3c2410_nand_probe
  562. *
  563. * called by device layer when it finds a device matching
  564. * one our driver can handled. This code checks to see if
  565. * it can allocate all necessary resources then calls the
  566. * nand layer to look for devices
  567. */
  568. static int s3c24xx_nand_probe(struct platform_device *pdev,
  569. enum s3c_cpu_type cpu_type)
  570. {
  571. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  572. struct s3c2410_nand_info *info;
  573. struct s3c2410_nand_mtd *nmtd;
  574. struct s3c2410_nand_set *sets;
  575. struct resource *res;
  576. int err = 0;
  577. int size;
  578. int nr_sets;
  579. int setno;
  580. pr_debug("s3c2410_nand_probe(%p)\n", pdev);
  581. info = kmalloc(sizeof(*info), GFP_KERNEL);
  582. if (info == NULL) {
  583. dev_err(&pdev->dev, "no memory for flash info\n");
  584. err = -ENOMEM;
  585. goto exit_error;
  586. }
  587. memzero(info, sizeof(*info));
  588. platform_set_drvdata(pdev, info);
  589. spin_lock_init(&info->controller.lock);
  590. init_waitqueue_head(&info->controller.wq);
  591. /* get the clock source and enable it */
  592. info->clk = clk_get(&pdev->dev, "nand");
  593. if (IS_ERR(info->clk)) {
  594. dev_err(&pdev->dev, "failed to get clock\n");
  595. err = -ENOENT;
  596. goto exit_error;
  597. }
  598. clk_enable(info->clk);
  599. /* allocate and map the resource */
  600. /* currently we assume we have the one resource */
  601. res = pdev->resource;
  602. size = res->end - res->start + 1;
  603. info->area = request_mem_region(res->start, size, pdev->name);
  604. if (info->area == NULL) {
  605. dev_err(&pdev->dev, "cannot reserve register region\n");
  606. err = -ENOENT;
  607. goto exit_error;
  608. }
  609. info->device = &pdev->dev;
  610. info->platform = plat;
  611. info->regs = ioremap(res->start, size);
  612. info->cpu_type = cpu_type;
  613. if (info->regs == NULL) {
  614. dev_err(&pdev->dev, "cannot reserve register region\n");
  615. err = -EIO;
  616. goto exit_error;
  617. }
  618. dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
  619. /* initialise the hardware */
  620. err = s3c2410_nand_inithw(info, pdev);
  621. if (err != 0)
  622. goto exit_error;
  623. sets = (plat != NULL) ? plat->sets : NULL;
  624. nr_sets = (plat != NULL) ? plat->nr_sets : 1;
  625. info->mtd_count = nr_sets;
  626. /* allocate our information */
  627. size = nr_sets * sizeof(*info->mtds);
  628. info->mtds = kmalloc(size, GFP_KERNEL);
  629. if (info->mtds == NULL) {
  630. dev_err(&pdev->dev, "failed to allocate mtd storage\n");
  631. err = -ENOMEM;
  632. goto exit_error;
  633. }
  634. memzero(info->mtds, size);
  635. /* initialise all possible chips */
  636. nmtd = info->mtds;
  637. for (setno = 0; setno < nr_sets; setno++, nmtd++) {
  638. pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
  639. s3c2410_nand_init_chip(info, nmtd, sets);
  640. nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
  641. (sets) ? sets->nr_chips : 1);
  642. if (nmtd->scan_res == 0) {
  643. s3c2410_nand_update_chip(info, nmtd);
  644. nand_scan_tail(&nmtd->mtd);
  645. s3c2410_nand_add_partition(info, nmtd, sets);
  646. }
  647. if (sets != NULL)
  648. sets++;
  649. }
  650. if (allow_clk_stop(info)) {
  651. dev_info(&pdev->dev, "clock idle support enabled\n");
  652. clk_disable(info->clk);
  653. }
  654. pr_debug("initialised ok\n");
  655. return 0;
  656. exit_error:
  657. s3c2410_nand_remove(pdev);
  658. if (err == 0)
  659. err = -EINVAL;
  660. return err;
  661. }
  662. /* PM Support */
  663. #ifdef CONFIG_PM
  664. static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
  665. {
  666. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  667. if (info) {
  668. info->save_sel = readl(info->sel_reg);
  669. /* For the moment, we must ensure nFCE is high during
  670. * the time we are suspended. This really should be
  671. * handled by suspending the MTDs we are using, but
  672. * that is currently not the case. */
  673. writel(info->save_sel | info->sel_bit, info->sel_reg);
  674. if (!allow_clk_stop(info))
  675. clk_disable(info->clk);
  676. }
  677. return 0;
  678. }
  679. static int s3c24xx_nand_resume(struct platform_device *dev)
  680. {
  681. struct s3c2410_nand_info *info = platform_get_drvdata(dev);
  682. unsigned long sel;
  683. if (info) {
  684. clk_enable(info->clk);
  685. s3c2410_nand_inithw(info, dev);
  686. /* Restore the state of the nFCE line. */
  687. sel = readl(info->sel_reg);
  688. sel &= ~info->sel_bit;
  689. sel |= info->save_sel & info->sel_bit;
  690. writel(sel, info->sel_reg);
  691. if (allow_clk_stop(info))
  692. clk_disable(info->clk);
  693. }
  694. return 0;
  695. }
  696. #else
  697. #define s3c24xx_nand_suspend NULL
  698. #define s3c24xx_nand_resume NULL
  699. #endif
  700. /* driver device registration */
  701. static int s3c2410_nand_probe(struct platform_device *dev)
  702. {
  703. return s3c24xx_nand_probe(dev, TYPE_S3C2410);
  704. }
  705. static int s3c2440_nand_probe(struct platform_device *dev)
  706. {
  707. return s3c24xx_nand_probe(dev, TYPE_S3C2440);
  708. }
  709. static int s3c2412_nand_probe(struct platform_device *dev)
  710. {
  711. return s3c24xx_nand_probe(dev, TYPE_S3C2412);
  712. }
  713. static struct platform_driver s3c2410_nand_driver = {
  714. .probe = s3c2410_nand_probe,
  715. .remove = s3c2410_nand_remove,
  716. .suspend = s3c24xx_nand_suspend,
  717. .resume = s3c24xx_nand_resume,
  718. .driver = {
  719. .name = "s3c2410-nand",
  720. .owner = THIS_MODULE,
  721. },
  722. };
  723. static struct platform_driver s3c2440_nand_driver = {
  724. .probe = s3c2440_nand_probe,
  725. .remove = s3c2410_nand_remove,
  726. .suspend = s3c24xx_nand_suspend,
  727. .resume = s3c24xx_nand_resume,
  728. .driver = {
  729. .name = "s3c2440-nand",
  730. .owner = THIS_MODULE,
  731. },
  732. };
  733. static struct platform_driver s3c2412_nand_driver = {
  734. .probe = s3c2412_nand_probe,
  735. .remove = s3c2410_nand_remove,
  736. .suspend = s3c24xx_nand_suspend,
  737. .resume = s3c24xx_nand_resume,
  738. .driver = {
  739. .name = "s3c2412-nand",
  740. .owner = THIS_MODULE,
  741. },
  742. };
  743. static int __init s3c2410_nand_init(void)
  744. {
  745. printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
  746. platform_driver_register(&s3c2412_nand_driver);
  747. platform_driver_register(&s3c2440_nand_driver);
  748. return platform_driver_register(&s3c2410_nand_driver);
  749. }
  750. static void __exit s3c2410_nand_exit(void)
  751. {
  752. platform_driver_unregister(&s3c2412_nand_driver);
  753. platform_driver_unregister(&s3c2440_nand_driver);
  754. platform_driver_unregister(&s3c2410_nand_driver);
  755. }
  756. module_init(s3c2410_nand_init);
  757. module_exit(s3c2410_nand_exit);
  758. MODULE_LICENSE("GPL");
  759. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  760. MODULE_DESCRIPTION("S3C24XX MTD NAND driver");
  761. MODULE_ALIAS("platform:s3c2410-nand");
  762. MODULE_ALIAS("platform:s3c2412-nand");
  763. MODULE_ALIAS("platform:s3c2440-nand");