s3c2410.c 29 KB

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