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