s3c2410.c 28 KB

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