s3c2410.c 28 KB

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