s3c2410.c 29 KB

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