]>
Commit | Line | Data |
---|---|---|
43a0a45a | 1 | /* |
7e74a507 BD |
2 | * Copyright © 2004-2008 Simtec Electronics |
3 | * http://armlinux.simtec.co.uk/ | |
fdf2fd52 | 4 | * Ben Dooks <[email protected]> |
1da177e4 | 5 | * |
7e74a507 | 6 | * Samsung S3C2410/S3C2440/S3C2412 NAND driver |
1da177e4 | 7 | * |
1da177e4 LT |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | */ | |
22 | ||
92aeb5d2 SK |
23 | #define pr_fmt(fmt) "nand-s3c2410: " fmt |
24 | ||
1da177e4 LT |
25 | #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG |
26 | #define DEBUG | |
27 | #endif | |
28 | ||
29 | #include <linux/module.h> | |
30 | #include <linux/types.h> | |
1da177e4 LT |
31 | #include <linux/kernel.h> |
32 | #include <linux/string.h> | |
d2a89be8 | 33 | #include <linux/io.h> |
1da177e4 | 34 | #include <linux/ioport.h> |
d052d1be | 35 | #include <linux/platform_device.h> |
1da177e4 LT |
36 | #include <linux/delay.h> |
37 | #include <linux/err.h> | |
4e57b681 | 38 | #include <linux/slab.h> |
f8ce2547 | 39 | #include <linux/clk.h> |
30821fee | 40 | #include <linux/cpufreq.h> |
1c825ad1 SP |
41 | #include <linux/of.h> |
42 | #include <linux/of_device.h> | |
1da177e4 LT |
43 | |
44 | #include <linux/mtd/mtd.h> | |
d4092d76 | 45 | #include <linux/mtd/rawnand.h> |
1da177e4 LT |
46 | #include <linux/mtd/nand_ecc.h> |
47 | #include <linux/mtd/partitions.h> | |
48 | ||
436d42c6 | 49 | #include <linux/platform_data/mtd-nand-s3c2410.h> |
1da177e4 | 50 | |
02d01862 SK |
51 | #define S3C2410_NFREG(x) (x) |
52 | ||
53 | #define S3C2410_NFCONF S3C2410_NFREG(0x00) | |
54 | #define S3C2410_NFCMD S3C2410_NFREG(0x04) | |
55 | #define S3C2410_NFADDR S3C2410_NFREG(0x08) | |
56 | #define S3C2410_NFDATA S3C2410_NFREG(0x0C) | |
57 | #define S3C2410_NFSTAT S3C2410_NFREG(0x10) | |
58 | #define S3C2410_NFECC S3C2410_NFREG(0x14) | |
59 | #define S3C2440_NFCONT S3C2410_NFREG(0x04) | |
60 | #define S3C2440_NFCMD S3C2410_NFREG(0x08) | |
61 | #define S3C2440_NFADDR S3C2410_NFREG(0x0C) | |
62 | #define S3C2440_NFDATA S3C2410_NFREG(0x10) | |
63 | #define S3C2440_NFSTAT S3C2410_NFREG(0x20) | |
64 | #define S3C2440_NFMECC0 S3C2410_NFREG(0x2C) | |
65 | #define S3C2412_NFSTAT S3C2410_NFREG(0x28) | |
66 | #define S3C2412_NFMECC0 S3C2410_NFREG(0x34) | |
67 | #define S3C2410_NFCONF_EN (1<<15) | |
68 | #define S3C2410_NFCONF_INITECC (1<<12) | |
69 | #define S3C2410_NFCONF_nFCE (1<<11) | |
70 | #define S3C2410_NFCONF_TACLS(x) ((x)<<8) | |
71 | #define S3C2410_NFCONF_TWRPH0(x) ((x)<<4) | |
72 | #define S3C2410_NFCONF_TWRPH1(x) ((x)<<0) | |
73 | #define S3C2410_NFSTAT_BUSY (1<<0) | |
74 | #define S3C2440_NFCONF_TACLS(x) ((x)<<12) | |
75 | #define S3C2440_NFCONF_TWRPH0(x) ((x)<<8) | |
76 | #define S3C2440_NFCONF_TWRPH1(x) ((x)<<4) | |
77 | #define S3C2440_NFCONT_INITECC (1<<4) | |
78 | #define S3C2440_NFCONT_nFCE (1<<1) | |
79 | #define S3C2440_NFCONT_ENABLE (1<<0) | |
80 | #define S3C2440_NFSTAT_READY (1<<0) | |
81 | #define S3C2412_NFCONF_NANDBOOT (1<<31) | |
82 | #define S3C2412_NFCONT_INIT_MAIN_ECC (1<<5) | |
83 | #define S3C2412_NFCONT_nFCE0 (1<<1) | |
84 | #define S3C2412_NFSTAT_READY (1<<0) | |
85 | ||
1da177e4 LT |
86 | /* new oob placement block for use with hardware ecc generation |
87 | */ | |
bf01e06b BB |
88 | static int s3c2410_ooblayout_ecc(struct mtd_info *mtd, int section, |
89 | struct mtd_oob_region *oobregion) | |
90 | { | |
91 | if (section) | |
92 | return -ERANGE; | |
93 | ||
94 | oobregion->offset = 0; | |
95 | oobregion->length = 3; | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | static int s3c2410_ooblayout_free(struct mtd_info *mtd, int section, | |
101 | struct mtd_oob_region *oobregion) | |
102 | { | |
103 | if (section) | |
104 | return -ERANGE; | |
105 | ||
106 | oobregion->offset = 8; | |
107 | oobregion->length = 8; | |
108 | ||
109 | return 0; | |
110 | } | |
1da177e4 | 111 | |
bf01e06b BB |
112 | static const struct mtd_ooblayout_ops s3c2410_ooblayout_ops = { |
113 | .ecc = s3c2410_ooblayout_ecc, | |
114 | .free = s3c2410_ooblayout_free, | |
1da177e4 LT |
115 | }; |
116 | ||
117 | /* controller and mtd information */ | |
118 | ||
119 | struct s3c2410_nand_info; | |
120 | ||
3db72151 BD |
121 | /** |
122 | * struct s3c2410_nand_mtd - driver MTD structure | |
123 | * @mtd: The MTD instance to pass to the MTD layer. | |
124 | * @chip: The NAND chip information. | |
125 | * @set: The platform information supplied for this set of NAND chips. | |
126 | * @info: Link back to the hardware information. | |
3db72151 | 127 | */ |
1da177e4 | 128 | struct s3c2410_nand_mtd { |
1da177e4 LT |
129 | struct nand_chip chip; |
130 | struct s3c2410_nand_set *set; | |
131 | struct s3c2410_nand_info *info; | |
1da177e4 LT |
132 | }; |
133 | ||
2c06a082 BD |
134 | enum s3c_cpu_type { |
135 | TYPE_S3C2410, | |
136 | TYPE_S3C2412, | |
137 | TYPE_S3C2440, | |
138 | }; | |
139 | ||
ac497c16 JP |
140 | enum s3c_nand_clk_state { |
141 | CLOCK_DISABLE = 0, | |
142 | CLOCK_ENABLE, | |
143 | CLOCK_SUSPEND, | |
144 | }; | |
145 | ||
1da177e4 LT |
146 | /* overview of the s3c2410 nand state */ |
147 | ||
3db72151 BD |
148 | /** |
149 | * struct s3c2410_nand_info - NAND controller state. | |
150 | * @mtds: An array of MTD instances on this controoler. | |
151 | * @platform: The platform data for this board. | |
152 | * @device: The platform device we bound to. | |
3db72151 | 153 | * @clk: The clock resource for this controller. |
6f32a3e2 | 154 | * @regs: The area mapped for the hardware registers. |
3db72151 BD |
155 | * @sel_reg: Pointer to the register controlling the NAND selection. |
156 | * @sel_bit: The bit in @sel_reg to select the NAND chip. | |
157 | * @mtd_count: The number of MTDs created from this controller. | |
158 | * @save_sel: The contents of @sel_reg to be saved over suspend. | |
159 | * @clk_rate: The clock rate from @clk. | |
ac497c16 | 160 | * @clk_state: The current clock state. |
3db72151 BD |
161 | * @cpu_type: The exact type of this controller. |
162 | */ | |
1da177e4 LT |
163 | struct s3c2410_nand_info { |
164 | /* mtd info */ | |
7da45139 | 165 | struct nand_controller controller; |
1da177e4 LT |
166 | struct s3c2410_nand_mtd *mtds; |
167 | struct s3c2410_platform_nand *platform; | |
168 | ||
169 | /* device info */ | |
170 | struct device *device; | |
1da177e4 | 171 | struct clk *clk; |
fdf2fd52 | 172 | void __iomem *regs; |
2c06a082 BD |
173 | void __iomem *sel_reg; |
174 | int sel_bit; | |
1da177e4 | 175 | int mtd_count; |
09160832 | 176 | unsigned long save_sel; |
30821fee | 177 | unsigned long clk_rate; |
ac497c16 | 178 | enum s3c_nand_clk_state clk_state; |
03680b1e | 179 | |
2c06a082 | 180 | enum s3c_cpu_type cpu_type; |
30821fee | 181 | |
d9ca77f0 | 182 | #ifdef CONFIG_ARM_S3C24XX_CPUFREQ |
30821fee BD |
183 | struct notifier_block freq_transition; |
184 | #endif | |
1da177e4 LT |
185 | }; |
186 | ||
1c825ad1 SP |
187 | struct s3c24XX_nand_devtype_data { |
188 | enum s3c_cpu_type type; | |
189 | }; | |
190 | ||
191 | static const struct s3c24XX_nand_devtype_data s3c2410_nand_devtype_data = { | |
192 | .type = TYPE_S3C2410, | |
193 | }; | |
194 | ||
195 | static const struct s3c24XX_nand_devtype_data s3c2412_nand_devtype_data = { | |
196 | .type = TYPE_S3C2412, | |
197 | }; | |
198 | ||
199 | static const struct s3c24XX_nand_devtype_data s3c2440_nand_devtype_data = { | |
200 | .type = TYPE_S3C2440, | |
201 | }; | |
202 | ||
1da177e4 LT |
203 | /* conversion functions */ |
204 | ||
205 | static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd) | |
206 | { | |
7208b997 BB |
207 | return container_of(mtd_to_nand(mtd), struct s3c2410_nand_mtd, |
208 | chip); | |
1da177e4 LT |
209 | } |
210 | ||
211 | static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd) | |
212 | { | |
213 | return s3c2410_nand_mtd_toours(mtd)->info; | |
214 | } | |
215 | ||
3ae5eaec | 216 | static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev) |
1da177e4 | 217 | { |
3ae5eaec | 218 | return platform_get_drvdata(dev); |
1da177e4 LT |
219 | } |
220 | ||
3ae5eaec | 221 | static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev) |
1da177e4 | 222 | { |
453810b7 | 223 | return dev_get_platdata(&dev->dev); |
1da177e4 LT |
224 | } |
225 | ||
ac497c16 | 226 | static inline int allow_clk_suspend(struct s3c2410_nand_info *info) |
d1fef3c5 | 227 | { |
a68c5ec8 SK |
228 | #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP |
229 | return 1; | |
230 | #else | |
231 | return 0; | |
232 | #endif | |
d1fef3c5 BD |
233 | } |
234 | ||
ac497c16 JP |
235 | /** |
236 | * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock. | |
237 | * @info: The controller instance. | |
238 | * @new_state: State to which clock should be set. | |
239 | */ | |
240 | static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info, | |
241 | enum s3c_nand_clk_state new_state) | |
242 | { | |
243 | if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND) | |
244 | return; | |
245 | ||
246 | if (info->clk_state == CLOCK_ENABLE) { | |
247 | if (new_state != CLOCK_ENABLE) | |
887957b4 | 248 | clk_disable_unprepare(info->clk); |
ac497c16 JP |
249 | } else { |
250 | if (new_state == CLOCK_ENABLE) | |
887957b4 | 251 | clk_prepare_enable(info->clk); |
ac497c16 JP |
252 | } |
253 | ||
254 | info->clk_state = new_state; | |
255 | } | |
256 | ||
1da177e4 LT |
257 | /* timing calculations */ |
258 | ||
cfd320fb | 259 | #define NS_IN_KHZ 1000000 |
1da177e4 | 260 | |
3db72151 BD |
261 | /** |
262 | * s3c_nand_calc_rate - calculate timing data. | |
263 | * @wanted: The cycle time in nanoseconds. | |
264 | * @clk: The clock rate in kHz. | |
265 | * @max: The maximum divider value. | |
266 | * | |
267 | * Calculate the timing value from the given parameters. | |
268 | */ | |
2c06a082 | 269 | static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max) |
1da177e4 LT |
270 | { |
271 | int result; | |
272 | ||
947391cf | 273 | result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ); |
1da177e4 LT |
274 | |
275 | pr_debug("result %d from %ld, %d\n", result, clk, wanted); | |
276 | ||
277 | if (result > max) { | |
92aeb5d2 SK |
278 | pr_err("%d ns is too big for current clock rate %ld\n", |
279 | wanted, clk); | |
1da177e4 LT |
280 | return -1; |
281 | } | |
282 | ||
283 | if (result < 1) | |
284 | result = 1; | |
285 | ||
286 | return result; | |
287 | } | |
288 | ||
54cd0208 | 289 | #define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk)) |
1da177e4 LT |
290 | |
291 | /* controller setup */ | |
292 | ||
3db72151 BD |
293 | /** |
294 | * s3c2410_nand_setrate - setup controller timing information. | |
295 | * @info: The controller instance. | |
296 | * | |
297 | * Given the information supplied by the platform, calculate and set | |
298 | * the necessary timing registers in the hardware to generate the | |
299 | * necessary timing cycles to the hardware. | |
300 | */ | |
30821fee | 301 | static int s3c2410_nand_setrate(struct s3c2410_nand_info *info) |
1da177e4 | 302 | { |
30821fee | 303 | struct s3c2410_platform_nand *plat = info->platform; |
2c06a082 | 304 | int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4; |
cfd320fb | 305 | int tacls, twrph0, twrph1; |
30821fee | 306 | unsigned long clkrate = clk_get_rate(info->clk); |
2612e523 | 307 | unsigned long uninitialized_var(set), cfg, uninitialized_var(mask); |
30821fee | 308 | unsigned long flags; |
1da177e4 LT |
309 | |
310 | /* calculate the timing information for the controller */ | |
311 | ||
30821fee | 312 | info->clk_rate = clkrate; |
cfd320fb BD |
313 | clkrate /= 1000; /* turn clock into kHz for ease of use */ |
314 | ||
1da177e4 | 315 | if (plat != NULL) { |
2c06a082 BD |
316 | tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max); |
317 | twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8); | |
318 | twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8); | |
1da177e4 LT |
319 | } else { |
320 | /* default timings */ | |
2c06a082 | 321 | tacls = tacls_max; |
1da177e4 LT |
322 | twrph0 = 8; |
323 | twrph1 = 8; | |
324 | } | |
61b03bd7 | 325 | |
1da177e4 | 326 | if (tacls < 0 || twrph0 < 0 || twrph1 < 0) { |
99974c62 | 327 | dev_err(info->device, "cannot get suitable timings\n"); |
1da177e4 LT |
328 | return -EINVAL; |
329 | } | |
330 | ||
99974c62 | 331 | dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n", |
54cd0208 SK |
332 | tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), |
333 | twrph1, to_ns(twrph1, clkrate)); | |
1da177e4 | 334 | |
30821fee BD |
335 | switch (info->cpu_type) { |
336 | case TYPE_S3C2410: | |
337 | mask = (S3C2410_NFCONF_TACLS(3) | | |
338 | S3C2410_NFCONF_TWRPH0(7) | | |
339 | S3C2410_NFCONF_TWRPH1(7)); | |
340 | set = S3C2410_NFCONF_EN; | |
341 | set |= S3C2410_NFCONF_TACLS(tacls - 1); | |
342 | set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1); | |
343 | set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1); | |
344 | break; | |
345 | ||
346 | case TYPE_S3C2440: | |
347 | case TYPE_S3C2412: | |
a755a385 PK |
348 | mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) | |
349 | S3C2440_NFCONF_TWRPH0(7) | | |
350 | S3C2440_NFCONF_TWRPH1(7)); | |
30821fee BD |
351 | |
352 | set = S3C2440_NFCONF_TACLS(tacls - 1); | |
353 | set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1); | |
354 | set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1); | |
355 | break; | |
356 | ||
357 | default: | |
30821fee BD |
358 | BUG(); |
359 | } | |
360 | ||
30821fee BD |
361 | local_irq_save(flags); |
362 | ||
363 | cfg = readl(info->regs + S3C2410_NFCONF); | |
364 | cfg &= ~mask; | |
365 | cfg |= set; | |
366 | writel(cfg, info->regs + S3C2410_NFCONF); | |
367 | ||
368 | local_irq_restore(flags); | |
369 | ||
ae7304e5 AG |
370 | dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg); |
371 | ||
30821fee BD |
372 | return 0; |
373 | } | |
374 | ||
3db72151 BD |
375 | /** |
376 | * s3c2410_nand_inithw - basic hardware initialisation | |
377 | * @info: The hardware state. | |
378 | * | |
379 | * Do the basic initialisation of the hardware, using s3c2410_nand_setrate() | |
380 | * to setup the hardware access speeds and set the controller to be enabled. | |
381 | */ | |
30821fee BD |
382 | static int s3c2410_nand_inithw(struct s3c2410_nand_info *info) |
383 | { | |
384 | int ret; | |
385 | ||
386 | ret = s3c2410_nand_setrate(info); | |
387 | if (ret < 0) | |
388 | return ret; | |
389 | ||
54cd0208 SK |
390 | switch (info->cpu_type) { |
391 | case TYPE_S3C2410: | |
30821fee | 392 | default: |
2c06a082 BD |
393 | break; |
394 | ||
54cd0208 SK |
395 | case TYPE_S3C2440: |
396 | case TYPE_S3C2412: | |
d1fef3c5 BD |
397 | /* enable the controller and de-assert nFCE */ |
398 | ||
2c06a082 | 399 | writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT); |
a4f957f1 | 400 | } |
1da177e4 | 401 | |
1da177e4 LT |
402 | return 0; |
403 | } | |
404 | ||
3db72151 BD |
405 | /** |
406 | * s3c2410_nand_select_chip - select the given nand chip | |
407 | * @mtd: The MTD instance for this chip. | |
408 | * @chip: The chip number. | |
409 | * | |
410 | * This is called by the MTD layer to either select a given chip for the | |
411 | * @mtd instance, or to indicate that the access has finished and the | |
412 | * chip can be de-selected. | |
413 | * | |
414 | * The routine ensures that the nFCE line is correctly setup, and any | |
415 | * platform specific selection code is called to route nFCE to the specific | |
416 | * chip. | |
417 | */ | |
1da177e4 LT |
418 | static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip) |
419 | { | |
420 | struct s3c2410_nand_info *info; | |
61b03bd7 | 421 | struct s3c2410_nand_mtd *nmtd; |
4bd4ebcc | 422 | struct nand_chip *this = mtd_to_nand(mtd); |
1da177e4 LT |
423 | unsigned long cur; |
424 | ||
d699ed25 | 425 | nmtd = nand_get_controller_data(this); |
1da177e4 LT |
426 | info = nmtd->info; |
427 | ||
ac497c16 JP |
428 | if (chip != -1) |
429 | s3c2410_nand_clk_set_state(info, CLOCK_ENABLE); | |
d1fef3c5 | 430 | |
2c06a082 | 431 | cur = readl(info->sel_reg); |
1da177e4 LT |
432 | |
433 | if (chip == -1) { | |
2c06a082 | 434 | cur |= info->sel_bit; |
1da177e4 | 435 | } else { |
fb8d82a8 | 436 | if (nmtd->set != NULL && chip > nmtd->set->nr_chips) { |
99974c62 | 437 | dev_err(info->device, "invalid chip %d\n", chip); |
1da177e4 LT |
438 | return; |
439 | } | |
440 | ||
441 | if (info->platform != NULL) { | |
442 | if (info->platform->select_chip != NULL) | |
e0c7d767 | 443 | (info->platform->select_chip) (nmtd->set, chip); |
1da177e4 LT |
444 | } |
445 | ||
2c06a082 | 446 | cur &= ~info->sel_bit; |
1da177e4 LT |
447 | } |
448 | ||
2c06a082 | 449 | writel(cur, info->sel_reg); |
d1fef3c5 | 450 | |
ac497c16 JP |
451 | if (chip == -1) |
452 | s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND); | |
1da177e4 LT |
453 | } |
454 | ||
ad3b5fb7 | 455 | /* s3c2410_nand_hwcontrol |
a4f957f1 | 456 | * |
ad3b5fb7 | 457 | * Issue command and address cycles to the chip |
a4f957f1 | 458 | */ |
1da177e4 | 459 | |
7abd3ef9 | 460 | static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
f9068876 | 461 | unsigned int ctrl) |
1da177e4 LT |
462 | { |
463 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
c9ac5977 | 464 | |
7abd3ef9 TG |
465 | if (cmd == NAND_CMD_NONE) |
466 | return; | |
467 | ||
f9068876 | 468 | if (ctrl & NAND_CLE) |
7abd3ef9 TG |
469 | writeb(cmd, info->regs + S3C2410_NFCMD); |
470 | else | |
471 | writeb(cmd, info->regs + S3C2410_NFADDR); | |
a4f957f1 BD |
472 | } |
473 | ||
474 | /* command and control functions */ | |
475 | ||
f9068876 DW |
476 | static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
477 | unsigned int ctrl) | |
a4f957f1 BD |
478 | { |
479 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
1da177e4 | 480 | |
7abd3ef9 TG |
481 | if (cmd == NAND_CMD_NONE) |
482 | return; | |
483 | ||
f9068876 | 484 | if (ctrl & NAND_CLE) |
7abd3ef9 TG |
485 | writeb(cmd, info->regs + S3C2440_NFCMD); |
486 | else | |
487 | writeb(cmd, info->regs + S3C2440_NFADDR); | |
1da177e4 LT |
488 | } |
489 | ||
1da177e4 LT |
490 | /* s3c2410_nand_devready() |
491 | * | |
492 | * returns 0 if the nand is busy, 1 if it is ready | |
493 | */ | |
494 | ||
495 | static int s3c2410_nand_devready(struct mtd_info *mtd) | |
496 | { | |
497 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
1da177e4 LT |
498 | return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY; |
499 | } | |
500 | ||
2c06a082 BD |
501 | static int s3c2440_nand_devready(struct mtd_info *mtd) |
502 | { | |
503 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
504 | return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY; | |
505 | } | |
506 | ||
507 | static int s3c2412_nand_devready(struct mtd_info *mtd) | |
508 | { | |
509 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
510 | return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY; | |
511 | } | |
512 | ||
1da177e4 LT |
513 | /* ECC handling functions */ |
514 | ||
2c06a082 BD |
515 | static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
516 | u_char *read_ecc, u_char *calc_ecc) | |
1da177e4 | 517 | { |
a2593247 BD |
518 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
519 | unsigned int diff0, diff1, diff2; | |
520 | unsigned int bit, byte; | |
521 | ||
522 | pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc); | |
523 | ||
524 | diff0 = read_ecc[0] ^ calc_ecc[0]; | |
525 | diff1 = read_ecc[1] ^ calc_ecc[1]; | |
526 | diff2 = read_ecc[2] ^ calc_ecc[2]; | |
527 | ||
13e85974 AS |
528 | pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n", |
529 | __func__, 3, read_ecc, 3, calc_ecc, | |
a2593247 BD |
530 | diff0, diff1, diff2); |
531 | ||
532 | if (diff0 == 0 && diff1 == 0 && diff2 == 0) | |
533 | return 0; /* ECC is ok */ | |
534 | ||
c45c6c68 BD |
535 | /* sometimes people do not think about using the ECC, so check |
536 | * to see if we have an 0xff,0xff,0xff read ECC and then ignore | |
537 | * the error, on the assumption that this is an un-eccd page. | |
538 | */ | |
539 | if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff | |
540 | && info->platform->ignore_unset_ecc) | |
541 | return 0; | |
542 | ||
a2593247 BD |
543 | /* Can we correct this ECC (ie, one row and column change). |
544 | * Note, this is similar to the 256 error code on smartmedia */ | |
545 | ||
546 | if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 && | |
547 | ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 && | |
548 | ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) { | |
549 | /* calculate the bit position of the error */ | |
550 | ||
d0bf3793 MR |
551 | bit = ((diff2 >> 3) & 1) | |
552 | ((diff2 >> 4) & 2) | | |
553 | ((diff2 >> 5) & 4); | |
1da177e4 | 554 | |
a2593247 | 555 | /* calculate the byte position of the error */ |
1da177e4 | 556 | |
d0bf3793 MR |
557 | byte = ((diff2 << 7) & 0x100) | |
558 | ((diff1 << 0) & 0x80) | | |
559 | ((diff1 << 1) & 0x40) | | |
560 | ((diff1 << 2) & 0x20) | | |
561 | ((diff1 << 3) & 0x10) | | |
562 | ((diff0 >> 4) & 0x08) | | |
563 | ((diff0 >> 3) & 0x04) | | |
564 | ((diff0 >> 2) & 0x02) | | |
565 | ((diff0 >> 1) & 0x01); | |
a2593247 BD |
566 | |
567 | dev_dbg(info->device, "correcting error bit %d, byte %d\n", | |
568 | bit, byte); | |
569 | ||
570 | dat[byte] ^= (1 << bit); | |
571 | return 1; | |
572 | } | |
573 | ||
574 | /* if there is only one bit difference in the ECC, then | |
575 | * one of only a row or column parity has changed, which | |
576 | * means the error is most probably in the ECC itself */ | |
577 | ||
578 | diff0 |= (diff1 << 8); | |
579 | diff0 |= (diff2 << 16); | |
580 | ||
03a97550 ZZ |
581 | /* equal to "(diff0 & ~(1 << __ffs(diff0)))" */ |
582 | if ((diff0 & (diff0 - 1)) == 0) | |
a2593247 BD |
583 | return 1; |
584 | ||
4fac9f69 | 585 | return -1; |
1da177e4 LT |
586 | } |
587 | ||
a4f957f1 BD |
588 | /* ECC functions |
589 | * | |
590 | * These allow the s3c2410 and s3c2440 to use the controller's ECC | |
591 | * generator block to ECC the data as it passes through] | |
592 | */ | |
593 | ||
ec47636c | 594 | static void s3c2410_nand_enable_hwecc(struct nand_chip *chip, int mode) |
1da177e4 | 595 | { |
ec47636c | 596 | struct s3c2410_nand_info *info; |
1da177e4 LT |
597 | unsigned long ctrl; |
598 | ||
ec47636c | 599 | info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip)); |
1da177e4 LT |
600 | ctrl = readl(info->regs + S3C2410_NFCONF); |
601 | ctrl |= S3C2410_NFCONF_INITECC; | |
602 | writel(ctrl, info->regs + S3C2410_NFCONF); | |
603 | } | |
604 | ||
ec47636c | 605 | static void s3c2412_nand_enable_hwecc(struct nand_chip *chip, int mode) |
4f659923 | 606 | { |
ec47636c | 607 | struct s3c2410_nand_info *info; |
4f659923 MC |
608 | unsigned long ctrl; |
609 | ||
ec47636c | 610 | info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip)); |
4f659923 | 611 | ctrl = readl(info->regs + S3C2440_NFCONT); |
f938bc56 SK |
612 | writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, |
613 | info->regs + S3C2440_NFCONT); | |
4f659923 MC |
614 | } |
615 | ||
ec47636c | 616 | static void s3c2440_nand_enable_hwecc(struct nand_chip *chip, int mode) |
a4f957f1 | 617 | { |
ec47636c | 618 | struct s3c2410_nand_info *info; |
a4f957f1 BD |
619 | unsigned long ctrl; |
620 | ||
ec47636c | 621 | info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip)); |
a4f957f1 BD |
622 | ctrl = readl(info->regs + S3C2440_NFCONT); |
623 | writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT); | |
624 | } | |
625 | ||
f938bc56 SK |
626 | static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
627 | u_char *ecc_code) | |
1da177e4 LT |
628 | { |
629 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
630 | ||
631 | ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0); | |
632 | ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1); | |
633 | ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2); | |
634 | ||
13e85974 | 635 | pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code); |
1da177e4 LT |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
f938bc56 SK |
640 | static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
641 | u_char *ecc_code) | |
4f659923 MC |
642 | { |
643 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
644 | unsigned long ecc = readl(info->regs + S3C2412_NFMECC0); | |
645 | ||
646 | ecc_code[0] = ecc; | |
647 | ecc_code[1] = ecc >> 8; | |
648 | ecc_code[2] = ecc >> 16; | |
649 | ||
13e85974 | 650 | pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code); |
4f659923 MC |
651 | |
652 | return 0; | |
653 | } | |
654 | ||
f938bc56 SK |
655 | static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
656 | u_char *ecc_code) | |
a4f957f1 BD |
657 | { |
658 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
659 | unsigned long ecc = readl(info->regs + S3C2440_NFMECC0); | |
660 | ||
661 | ecc_code[0] = ecc; | |
662 | ecc_code[1] = ecc >> 8; | |
663 | ecc_code[2] = ecc >> 16; | |
664 | ||
71d54f38 | 665 | pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff); |
a4f957f1 BD |
666 | |
667 | return 0; | |
668 | } | |
669 | ||
a4f957f1 BD |
670 | /* over-ride the standard functions for a little more speed. We can |
671 | * use read/write block to move the data buffers to/from the controller | |
672 | */ | |
1da177e4 LT |
673 | |
674 | static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) | |
675 | { | |
4bd4ebcc | 676 | struct nand_chip *this = mtd_to_nand(mtd); |
1da177e4 LT |
677 | readsb(this->IO_ADDR_R, buf, len); |
678 | } | |
679 | ||
b773bb2e MR |
680 | static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
681 | { | |
682 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
dea2aa6f BD |
683 | |
684 | readsl(info->regs + S3C2440_NFDATA, buf, len >> 2); | |
685 | ||
686 | /* cleanup if we've got less than a word to do */ | |
687 | if (len & 3) { | |
688 | buf += len & ~3; | |
689 | ||
690 | for (; len & 3; len--) | |
691 | *buf++ = readb(info->regs + S3C2440_NFDATA); | |
692 | } | |
b773bb2e MR |
693 | } |
694 | ||
f938bc56 SK |
695 | static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, |
696 | int len) | |
1da177e4 | 697 | { |
4bd4ebcc | 698 | struct nand_chip *this = mtd_to_nand(mtd); |
1da177e4 LT |
699 | writesb(this->IO_ADDR_W, buf, len); |
700 | } | |
701 | ||
f938bc56 SK |
702 | static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, |
703 | int len) | |
b773bb2e MR |
704 | { |
705 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
dea2aa6f BD |
706 | |
707 | writesl(info->regs + S3C2440_NFDATA, buf, len >> 2); | |
708 | ||
709 | /* cleanup any fractional write */ | |
710 | if (len & 3) { | |
711 | buf += len & ~3; | |
712 | ||
713 | for (; len & 3; len--, buf++) | |
714 | writeb(*buf, info->regs + S3C2440_NFDATA); | |
715 | } | |
b773bb2e MR |
716 | } |
717 | ||
30821fee BD |
718 | /* cpufreq driver support */ |
719 | ||
d9ca77f0 | 720 | #ifdef CONFIG_ARM_S3C24XX_CPUFREQ |
30821fee BD |
721 | |
722 | static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb, | |
723 | unsigned long val, void *data) | |
724 | { | |
725 | struct s3c2410_nand_info *info; | |
726 | unsigned long newclk; | |
727 | ||
728 | info = container_of(nb, struct s3c2410_nand_info, freq_transition); | |
729 | newclk = clk_get_rate(info->clk); | |
730 | ||
731 | if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) || | |
732 | (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) { | |
733 | s3c2410_nand_setrate(info); | |
734 | } | |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
739 | static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info) | |
740 | { | |
741 | info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition; | |
742 | ||
743 | return cpufreq_register_notifier(&info->freq_transition, | |
744 | CPUFREQ_TRANSITION_NOTIFIER); | |
745 | } | |
746 | ||
f938bc56 SK |
747 | static inline void |
748 | s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info) | |
30821fee BD |
749 | { |
750 | cpufreq_unregister_notifier(&info->freq_transition, | |
751 | CPUFREQ_TRANSITION_NOTIFIER); | |
752 | } | |
753 | ||
754 | #else | |
755 | static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info) | |
756 | { | |
757 | return 0; | |
758 | } | |
759 | ||
f938bc56 SK |
760 | static inline void |
761 | s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info) | |
30821fee BD |
762 | { |
763 | } | |
764 | #endif | |
765 | ||
1da177e4 LT |
766 | /* device management functions */ |
767 | ||
ec0482e6 | 768 | static int s3c24xx_nand_remove(struct platform_device *pdev) |
1da177e4 | 769 | { |
3ae5eaec | 770 | struct s3c2410_nand_info *info = to_nand_info(pdev); |
1da177e4 | 771 | |
61b03bd7 | 772 | if (info == NULL) |
1da177e4 LT |
773 | return 0; |
774 | ||
30821fee BD |
775 | s3c2410_nand_cpufreq_deregister(info); |
776 | ||
777 | /* Release all our mtds and their partitions, then go through | |
778 | * freeing the resources used | |
1da177e4 | 779 | */ |
61b03bd7 | 780 | |
1da177e4 LT |
781 | if (info->mtds != NULL) { |
782 | struct s3c2410_nand_mtd *ptr = info->mtds; | |
783 | int mtdno; | |
784 | ||
785 | for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) { | |
786 | pr_debug("releasing mtd %d (%p)\n", mtdno, ptr); | |
59ac276f | 787 | nand_release(&ptr->chip); |
1da177e4 | 788 | } |
1da177e4 LT |
789 | } |
790 | ||
791 | /* free the common resources */ | |
792 | ||
6f32a3e2 | 793 | if (!IS_ERR(info->clk)) |
ac497c16 | 794 | s3c2410_nand_clk_set_state(info, CLOCK_DISABLE); |
1da177e4 LT |
795 | |
796 | return 0; | |
797 | } | |
798 | ||
1da177e4 LT |
799 | static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info, |
800 | struct s3c2410_nand_mtd *mtd, | |
801 | struct s3c2410_nand_set *set) | |
802 | { | |
ded4c55d | 803 | if (set) { |
7208b997 | 804 | struct mtd_info *mtdinfo = nand_to_mtd(&mtd->chip); |
ed27f028 | 805 | |
7208b997 BB |
806 | mtdinfo->name = set->name; |
807 | ||
29597ca1 RM |
808 | return mtd_device_register(mtdinfo, set->partitions, |
809 | set->nr_partitions); | |
ded4c55d SK |
810 | } |
811 | ||
812 | return -ENODEV; | |
1da177e4 | 813 | } |
1da177e4 | 814 | |
104e442a BB |
815 | static int s3c2410_nand_setup_data_interface(struct mtd_info *mtd, int csline, |
816 | const struct nand_data_interface *conf) | |
1c825ad1 SP |
817 | { |
818 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
819 | struct s3c2410_platform_nand *pdata = info->platform; | |
820 | const struct nand_sdr_timings *timings; | |
821 | int tacls; | |
822 | ||
823 | timings = nand_get_sdr_timings(conf); | |
824 | if (IS_ERR(timings)) | |
825 | return -ENOTSUPP; | |
826 | ||
827 | tacls = timings->tCLS_min - timings->tWP_min; | |
828 | if (tacls < 0) | |
829 | tacls = 0; | |
830 | ||
831 | pdata->tacls = DIV_ROUND_UP(tacls, 1000); | |
832 | pdata->twrph0 = DIV_ROUND_UP(timings->tWP_min, 1000); | |
833 | pdata->twrph1 = DIV_ROUND_UP(timings->tCLH_min, 1000); | |
834 | ||
835 | return s3c2410_nand_setrate(info); | |
836 | } | |
837 | ||
3db72151 BD |
838 | /** |
839 | * s3c2410_nand_init_chip - initialise a single instance of an chip | |
840 | * @info: The base NAND controller the chip is on. | |
841 | * @nmtd: The new controller MTD instance to fill in. | |
842 | * @set: The information passed from the board specific platform data. | |
1da177e4 | 843 | * |
3db72151 BD |
844 | * Initialise the given @nmtd from the information in @info and @set. This |
845 | * readies the structure for use with the MTD layer functions by ensuring | |
846 | * all pointers are setup and the necessary control routines selected. | |
847 | */ | |
1da177e4 LT |
848 | static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info, |
849 | struct s3c2410_nand_mtd *nmtd, | |
850 | struct s3c2410_nand_set *set) | |
851 | { | |
1c825ad1 | 852 | struct device_node *np = info->device->of_node; |
1da177e4 | 853 | struct nand_chip *chip = &nmtd->chip; |
2c06a082 | 854 | void __iomem *regs = info->regs; |
1da177e4 | 855 | |
1c825ad1 SP |
856 | nand_set_flash_node(chip, set->of_node); |
857 | ||
1da177e4 LT |
858 | chip->write_buf = s3c2410_nand_write_buf; |
859 | chip->read_buf = s3c2410_nand_read_buf; | |
860 | chip->select_chip = s3c2410_nand_select_chip; | |
861 | chip->chip_delay = 50; | |
d699ed25 | 862 | nand_set_controller_data(chip, nmtd); |
74218fed | 863 | chip->options = set->options; |
1da177e4 LT |
864 | chip->controller = &info->controller; |
865 | ||
1c825ad1 SP |
866 | /* |
867 | * let's keep behavior unchanged for legacy boards booting via pdata and | |
868 | * auto-detect timings only when booting with a device tree. | |
869 | */ | |
870 | if (np) | |
871 | chip->setup_data_interface = s3c2410_nand_setup_data_interface; | |
872 | ||
2c06a082 BD |
873 | switch (info->cpu_type) { |
874 | case TYPE_S3C2410: | |
875 | chip->IO_ADDR_W = regs + S3C2410_NFDATA; | |
876 | info->sel_reg = regs + S3C2410_NFCONF; | |
877 | info->sel_bit = S3C2410_NFCONF_nFCE; | |
878 | chip->cmd_ctrl = s3c2410_nand_hwcontrol; | |
879 | chip->dev_ready = s3c2410_nand_devready; | |
880 | break; | |
881 | ||
882 | case TYPE_S3C2440: | |
883 | chip->IO_ADDR_W = regs + S3C2440_NFDATA; | |
884 | info->sel_reg = regs + S3C2440_NFCONT; | |
885 | info->sel_bit = S3C2440_NFCONT_nFCE; | |
886 | chip->cmd_ctrl = s3c2440_nand_hwcontrol; | |
887 | chip->dev_ready = s3c2440_nand_devready; | |
b773bb2e MR |
888 | chip->read_buf = s3c2440_nand_read_buf; |
889 | chip->write_buf = s3c2440_nand_write_buf; | |
2c06a082 BD |
890 | break; |
891 | ||
892 | case TYPE_S3C2412: | |
893 | chip->IO_ADDR_W = regs + S3C2440_NFDATA; | |
894 | info->sel_reg = regs + S3C2440_NFCONT; | |
895 | info->sel_bit = S3C2412_NFCONT_nFCE0; | |
896 | chip->cmd_ctrl = s3c2440_nand_hwcontrol; | |
897 | chip->dev_ready = s3c2412_nand_devready; | |
898 | ||
899 | if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT) | |
900 | dev_info(info->device, "System booted from NAND\n"); | |
901 | ||
902 | break; | |
54cd0208 | 903 | } |
2c06a082 BD |
904 | |
905 | chip->IO_ADDR_R = chip->IO_ADDR_W; | |
a4f957f1 | 906 | |
1da177e4 | 907 | nmtd->info = info; |
1da177e4 LT |
908 | nmtd->set = set; |
909 | ||
e9f66ae2 | 910 | chip->ecc.mode = info->platform->ecc_mode; |
9db41f9e | 911 | |
1c825ad1 SP |
912 | /* |
913 | * If you use u-boot BBT creation code, specifying this flag will | |
914 | * let the kernel fish out the BBT from the NAND. | |
915 | */ | |
916 | if (set->flash_bbt) | |
bb9ebd4e | 917 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
1da177e4 LT |
918 | } |
919 | ||
3db72151 | 920 | /** |
12748318 MR |
921 | * s3c2410_nand_attach_chip - Init the ECC engine after NAND scan |
922 | * @chip: The NAND chip | |
71d54f38 | 923 | * |
12748318 MR |
924 | * This hook is called by the core after the identification of the NAND chip, |
925 | * once the relevant per-chip information is up to date.. This call ensure that | |
3db72151 BD |
926 | * we update the internal state accordingly. |
927 | * | |
928 | * The internal state is currently limited to the ECC state information. | |
929 | */ | |
12748318 | 930 | static int s3c2410_nand_attach_chip(struct nand_chip *chip) |
71d54f38 | 931 | { |
12748318 MR |
932 | struct mtd_info *mtd = nand_to_mtd(chip); |
933 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); | |
71d54f38 | 934 | |
e9f66ae2 | 935 | switch (chip->ecc.mode) { |
71d54f38 | 936 | |
e9f66ae2 SP |
937 | case NAND_ECC_NONE: |
938 | dev_info(info->device, "ECC disabled\n"); | |
939 | break; | |
940 | ||
941 | case NAND_ECC_SOFT: | |
942 | /* | |
943 | * This driver expects Hamming based ECC when ecc_mode is set | |
944 | * to NAND_ECC_SOFT. Force ecc.algo to NAND_ECC_HAMMING to | |
945 | * avoid adding an extra ecc_algo field to | |
946 | * s3c2410_platform_nand. | |
947 | */ | |
948 | chip->ecc.algo = NAND_ECC_HAMMING; | |
949 | dev_info(info->device, "soft ECC\n"); | |
950 | break; | |
951 | ||
952 | case NAND_ECC_HW: | |
953 | chip->ecc.calculate = s3c2410_nand_calculate_ecc; | |
954 | chip->ecc.correct = s3c2410_nand_correct_data; | |
955 | chip->ecc.strength = 1; | |
956 | ||
957 | switch (info->cpu_type) { | |
958 | case TYPE_S3C2410: | |
959 | chip->ecc.hwctl = s3c2410_nand_enable_hwecc; | |
960 | chip->ecc.calculate = s3c2410_nand_calculate_ecc; | |
961 | break; | |
962 | ||
963 | case TYPE_S3C2412: | |
964 | chip->ecc.hwctl = s3c2412_nand_enable_hwecc; | |
965 | chip->ecc.calculate = s3c2412_nand_calculate_ecc; | |
966 | break; | |
967 | ||
968 | case TYPE_S3C2440: | |
969 | chip->ecc.hwctl = s3c2440_nand_enable_hwecc; | |
970 | chip->ecc.calculate = s3c2440_nand_calculate_ecc; | |
971 | break; | |
972 | } | |
973 | ||
974 | dev_dbg(info->device, "chip %p => page shift %d\n", | |
975 | chip, chip->page_shift); | |
8c3e843d | 976 | |
48fc7f7e | 977 | /* change the behaviour depending on whether we are using |
71d54f38 | 978 | * the large or small page nand device */ |
e9f66ae2 SP |
979 | if (chip->page_shift > 10) { |
980 | chip->ecc.size = 256; | |
981 | chip->ecc.bytes = 3; | |
982 | } else { | |
983 | chip->ecc.size = 512; | |
984 | chip->ecc.bytes = 3; | |
985 | mtd_set_ooblayout(nand_to_mtd(chip), | |
986 | &s3c2410_ooblayout_ops); | |
987 | } | |
71d54f38 | 988 | |
e9f66ae2 SP |
989 | dev_info(info->device, "hardware ECC\n"); |
990 | break; | |
991 | ||
992 | default: | |
993 | dev_err(info->device, "invalid ECC mode!\n"); | |
994 | return -EINVAL; | |
71d54f38 | 995 | } |
e9f66ae2 | 996 | |
1c825ad1 SP |
997 | if (chip->bbt_options & NAND_BBT_USE_FLASH) |
998 | chip->options |= NAND_SKIP_BBTSCAN; | |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
12748318 MR |
1003 | static const struct nand_controller_ops s3c24xx_nand_controller_ops = { |
1004 | .attach_chip = s3c2410_nand_attach_chip, | |
1005 | }; | |
1006 | ||
1c825ad1 SP |
1007 | static const struct of_device_id s3c24xx_nand_dt_ids[] = { |
1008 | { | |
1009 | .compatible = "samsung,s3c2410-nand", | |
1010 | .data = &s3c2410_nand_devtype_data, | |
1011 | }, { | |
1012 | /* also compatible with s3c6400 */ | |
1013 | .compatible = "samsung,s3c2412-nand", | |
1014 | .data = &s3c2412_nand_devtype_data, | |
1015 | }, { | |
1016 | .compatible = "samsung,s3c2440-nand", | |
1017 | .data = &s3c2440_nand_devtype_data, | |
1018 | }, | |
1019 | { /* sentinel */ } | |
1020 | }; | |
1021 | MODULE_DEVICE_TABLE(of, s3c24xx_nand_dt_ids); | |
1022 | ||
1023 | static int s3c24xx_nand_probe_dt(struct platform_device *pdev) | |
1024 | { | |
1025 | const struct s3c24XX_nand_devtype_data *devtype_data; | |
1026 | struct s3c2410_platform_nand *pdata; | |
1027 | struct s3c2410_nand_info *info = platform_get_drvdata(pdev); | |
1028 | struct device_node *np = pdev->dev.of_node, *child; | |
1029 | struct s3c2410_nand_set *sets; | |
1030 | ||
1031 | devtype_data = of_device_get_match_data(&pdev->dev); | |
1032 | if (!devtype_data) | |
1033 | return -ENODEV; | |
1034 | ||
1035 | info->cpu_type = devtype_data->type; | |
1036 | ||
1037 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); | |
1038 | if (!pdata) | |
1039 | return -ENOMEM; | |
1040 | ||
1041 | pdev->dev.platform_data = pdata; | |
1042 | ||
1043 | pdata->nr_sets = of_get_child_count(np); | |
1044 | if (!pdata->nr_sets) | |
1045 | return 0; | |
1046 | ||
a86854d0 | 1047 | sets = devm_kcalloc(&pdev->dev, pdata->nr_sets, sizeof(*sets), |
1c825ad1 SP |
1048 | GFP_KERNEL); |
1049 | if (!sets) | |
1050 | return -ENOMEM; | |
1051 | ||
1052 | pdata->sets = sets; | |
1053 | ||
1054 | for_each_available_child_of_node(np, child) { | |
1055 | sets->name = (char *)child->name; | |
1056 | sets->of_node = child; | |
1057 | sets->nr_chips = 1; | |
1058 | ||
1059 | of_node_get(child); | |
1060 | ||
1061 | sets++; | |
1062 | } | |
1063 | ||
1064 | return 0; | |
1065 | } | |
1066 | ||
1067 | static int s3c24xx_nand_probe_pdata(struct platform_device *pdev) | |
1068 | { | |
1069 | struct s3c2410_nand_info *info = platform_get_drvdata(pdev); | |
1070 | ||
1071 | info->cpu_type = platform_get_device_id(pdev)->driver_data; | |
1072 | ||
e9f66ae2 | 1073 | return 0; |
71d54f38 BD |
1074 | } |
1075 | ||
ec0482e6 | 1076 | /* s3c24xx_nand_probe |
1da177e4 LT |
1077 | * |
1078 | * called by device layer when it finds a device matching | |
1079 | * one our driver can handled. This code checks to see if | |
1080 | * it can allocate all necessary resources then calls the | |
1081 | * nand layer to look for devices | |
1082 | */ | |
ec0482e6 | 1083 | static int s3c24xx_nand_probe(struct platform_device *pdev) |
1da177e4 | 1084 | { |
1c825ad1 | 1085 | struct s3c2410_platform_nand *plat; |
1da177e4 LT |
1086 | struct s3c2410_nand_info *info; |
1087 | struct s3c2410_nand_mtd *nmtd; | |
1088 | struct s3c2410_nand_set *sets; | |
1089 | struct resource *res; | |
1090 | int err = 0; | |
1091 | int size; | |
1092 | int nr_sets; | |
1093 | int setno; | |
1094 | ||
6f32a3e2 | 1095 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
1da177e4 | 1096 | if (info == NULL) { |
1da177e4 LT |
1097 | err = -ENOMEM; |
1098 | goto exit_error; | |
1099 | } | |
1100 | ||
3ae5eaec | 1101 | platform_set_drvdata(pdev, info); |
1da177e4 | 1102 | |
7da45139 | 1103 | nand_controller_init(&info->controller); |
12748318 | 1104 | info->controller.ops = &s3c24xx_nand_controller_ops; |
1da177e4 LT |
1105 | |
1106 | /* get the clock source and enable it */ | |
1107 | ||
6f32a3e2 | 1108 | info->clk = devm_clk_get(&pdev->dev, "nand"); |
1da177e4 | 1109 | if (IS_ERR(info->clk)) { |
898eb71c | 1110 | dev_err(&pdev->dev, "failed to get clock\n"); |
1da177e4 LT |
1111 | err = -ENOENT; |
1112 | goto exit_error; | |
1113 | } | |
1114 | ||
ac497c16 | 1115 | s3c2410_nand_clk_set_state(info, CLOCK_ENABLE); |
1da177e4 | 1116 | |
1c825ad1 SP |
1117 | if (pdev->dev.of_node) |
1118 | err = s3c24xx_nand_probe_dt(pdev); | |
1119 | else | |
1120 | err = s3c24xx_nand_probe_pdata(pdev); | |
1121 | ||
1122 | if (err) | |
1123 | goto exit_error; | |
1124 | ||
1125 | plat = to_nand_plat(pdev); | |
1126 | ||
1da177e4 LT |
1127 | /* allocate and map the resource */ |
1128 | ||
a4f957f1 | 1129 | /* currently we assume we have the one resource */ |
6f32a3e2 | 1130 | res = pdev->resource; |
fc161c4e | 1131 | size = resource_size(res); |
1da177e4 | 1132 | |
6f32a3e2 SK |
1133 | info->device = &pdev->dev; |
1134 | info->platform = plat; | |
1da177e4 | 1135 | |
b0de774c TR |
1136 | info->regs = devm_ioremap_resource(&pdev->dev, res); |
1137 | if (IS_ERR(info->regs)) { | |
1138 | err = PTR_ERR(info->regs); | |
1da177e4 | 1139 | goto exit_error; |
61b03bd7 | 1140 | } |
1da177e4 | 1141 | |
3ae5eaec | 1142 | dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs); |
1da177e4 | 1143 | |
bdc4e58d BB |
1144 | if (!plat->sets || plat->nr_sets < 1) { |
1145 | err = -EINVAL; | |
1146 | goto exit_error; | |
1147 | } | |
1148 | ||
1149 | sets = plat->sets; | |
1150 | nr_sets = plat->nr_sets; | |
1da177e4 LT |
1151 | |
1152 | info->mtd_count = nr_sets; | |
1153 | ||
1154 | /* allocate our information */ | |
1155 | ||
1156 | size = nr_sets * sizeof(*info->mtds); | |
6f32a3e2 | 1157 | info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
1da177e4 | 1158 | if (info->mtds == NULL) { |
1da177e4 LT |
1159 | err = -ENOMEM; |
1160 | goto exit_error; | |
1161 | } | |
1162 | ||
1da177e4 LT |
1163 | /* initialise all possible chips */ |
1164 | ||
1165 | nmtd = info->mtds; | |
1166 | ||
bdc4e58d | 1167 | for (setno = 0; setno < nr_sets; setno++, nmtd++, sets++) { |
7208b997 BB |
1168 | struct mtd_info *mtd = nand_to_mtd(&nmtd->chip); |
1169 | ||
f938bc56 SK |
1170 | pr_debug("initialising set %d (%p, info %p)\n", |
1171 | setno, nmtd, info); | |
61b03bd7 | 1172 | |
7208b997 | 1173 | mtd->dev.parent = &pdev->dev; |
1da177e4 LT |
1174 | s3c2410_nand_init_chip(info, nmtd, sets); |
1175 | ||
00ad378f | 1176 | err = nand_scan(&nmtd->chip, sets ? sets->nr_chips : 1); |
bb00ff2f MR |
1177 | if (err) |
1178 | goto exit_error; | |
1179 | ||
1180 | s3c2410_nand_add_partition(info, nmtd, sets); | |
1da177e4 | 1181 | } |
61b03bd7 | 1182 | |
1c825ad1 SP |
1183 | /* initialise the hardware */ |
1184 | err = s3c2410_nand_inithw(info); | |
1185 | if (err != 0) | |
1186 | goto exit_error; | |
1187 | ||
30821fee BD |
1188 | err = s3c2410_nand_cpufreq_register(info); |
1189 | if (err < 0) { | |
1190 | dev_err(&pdev->dev, "failed to init cpufreq support\n"); | |
1191 | goto exit_error; | |
1192 | } | |
1193 | ||
ac497c16 | 1194 | if (allow_clk_suspend(info)) { |
d1fef3c5 | 1195 | dev_info(&pdev->dev, "clock idle support enabled\n"); |
ac497c16 | 1196 | s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND); |
d1fef3c5 BD |
1197 | } |
1198 | ||
1da177e4 LT |
1199 | return 0; |
1200 | ||
1201 | exit_error: | |
ec0482e6 | 1202 | s3c24xx_nand_remove(pdev); |
1da177e4 LT |
1203 | |
1204 | if (err == 0) | |
1205 | err = -EINVAL; | |
1206 | return err; | |
1207 | } | |
1208 | ||
d1fef3c5 BD |
1209 | /* PM Support */ |
1210 | #ifdef CONFIG_PM | |
1211 | ||
1212 | static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm) | |
1213 | { | |
1214 | struct s3c2410_nand_info *info = platform_get_drvdata(dev); | |
1215 | ||
1216 | if (info) { | |
09160832 | 1217 | info->save_sel = readl(info->sel_reg); |
03680b1e BD |
1218 | |
1219 | /* For the moment, we must ensure nFCE is high during | |
1220 | * the time we are suspended. This really should be | |
1221 | * handled by suspending the MTDs we are using, but | |
1222 | * that is currently not the case. */ | |
1223 | ||
09160832 | 1224 | writel(info->save_sel | info->sel_bit, info->sel_reg); |
03680b1e | 1225 | |
ac497c16 | 1226 | s3c2410_nand_clk_set_state(info, CLOCK_DISABLE); |
d1fef3c5 BD |
1227 | } |
1228 | ||
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | static int s3c24xx_nand_resume(struct platform_device *dev) | |
1233 | { | |
1234 | struct s3c2410_nand_info *info = platform_get_drvdata(dev); | |
09160832 | 1235 | unsigned long sel; |
d1fef3c5 BD |
1236 | |
1237 | if (info) { | |
ac497c16 | 1238 | s3c2410_nand_clk_set_state(info, CLOCK_ENABLE); |
30821fee | 1239 | s3c2410_nand_inithw(info); |
d1fef3c5 | 1240 | |
03680b1e BD |
1241 | /* Restore the state of the nFCE line. */ |
1242 | ||
09160832 BD |
1243 | sel = readl(info->sel_reg); |
1244 | sel &= ~info->sel_bit; | |
1245 | sel |= info->save_sel & info->sel_bit; | |
1246 | writel(sel, info->sel_reg); | |
03680b1e | 1247 | |
ac497c16 | 1248 | s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND); |
d1fef3c5 BD |
1249 | } |
1250 | ||
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | #else | |
1255 | #define s3c24xx_nand_suspend NULL | |
1256 | #define s3c24xx_nand_resume NULL | |
1257 | #endif | |
1258 | ||
a4f957f1 BD |
1259 | /* driver device registration */ |
1260 | ||
0abe75d2 | 1261 | static const struct platform_device_id s3c24xx_driver_ids[] = { |
ec0482e6 BD |
1262 | { |
1263 | .name = "s3c2410-nand", | |
1264 | .driver_data = TYPE_S3C2410, | |
1265 | }, { | |
1266 | .name = "s3c2440-nand", | |
1267 | .driver_data = TYPE_S3C2440, | |
1268 | }, { | |
1269 | .name = "s3c2412-nand", | |
1270 | .driver_data = TYPE_S3C2412, | |
9dbc0902 PK |
1271 | }, { |
1272 | .name = "s3c6400-nand", | |
1273 | .driver_data = TYPE_S3C2412, /* compatible with 2412 */ | |
3ae5eaec | 1274 | }, |
ec0482e6 | 1275 | { } |
1da177e4 LT |
1276 | }; |
1277 | ||
ec0482e6 | 1278 | MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); |
a4f957f1 | 1279 | |
ec0482e6 BD |
1280 | static struct platform_driver s3c24xx_nand_driver = { |
1281 | .probe = s3c24xx_nand_probe, | |
1282 | .remove = s3c24xx_nand_remove, | |
2c06a082 BD |
1283 | .suspend = s3c24xx_nand_suspend, |
1284 | .resume = s3c24xx_nand_resume, | |
ec0482e6 | 1285 | .id_table = s3c24xx_driver_ids, |
2c06a082 | 1286 | .driver = { |
ec0482e6 | 1287 | .name = "s3c24xx-nand", |
1c825ad1 | 1288 | .of_match_table = s3c24xx_nand_dt_ids, |
2c06a082 BD |
1289 | }, |
1290 | }; | |
1291 | ||
056fcab5 | 1292 | module_platform_driver(s3c24xx_nand_driver); |
1da177e4 LT |
1293 | |
1294 | MODULE_LICENSE("GPL"); | |
1295 | MODULE_AUTHOR("Ben Dooks <[email protected]>"); | |
a4f957f1 | 1296 | MODULE_DESCRIPTION("S3C24XX MTD NAND driver"); |