s3c2410.c 29 KB

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