s3c2410.c 28 KB

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