s3c2410.c 28 KB

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