s3c2410.c 29 KB

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