s3c2410.c 24 KB

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