s3c2410.c 28 KB

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