s3c2410.c 25 KB

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