s3c2410.c 29 KB

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