s3c2410.c 29 KB

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