s3c2410.c 29 KB

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