Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* linux/drivers/mtd/nand/s3c2410.c |
| 2 | * |
Ben Dooks | 7e74a50 | 2008-05-20 17:32:27 +0100 | [diff] [blame] | 3 | * Copyright © 2004-2008 Simtec Electronics |
| 4 | * http://armlinux.simtec.co.uk/ |
Ben Dooks | fdf2fd5 | 2005-02-18 14:46:15 +0000 | [diff] [blame] | 5 | * Ben Dooks <ben@simtec.co.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Ben Dooks | 7e74a50 | 2008-05-20 17:32:27 +0100 | [diff] [blame] | 7 | * Samsung S3C2410/S3C2440/S3C2412 NAND driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
Sachin Kamat | 92aeb5d | 2012-07-16 16:02:23 +0530 | [diff] [blame] | 24 | #define pr_fmt(fmt) "nand-s3c2410: " fmt |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG |
| 27 | #define DEBUG |
| 28 | #endif |
| 29 | |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/string.h> |
Sachin Kamat | d2a89be | 2012-07-16 16:02:24 +0530 | [diff] [blame] | 35 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/ioport.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 37 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/delay.h> |
| 39 | #include <linux/err.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 40 | #include <linux/slab.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 41 | #include <linux/clk.h> |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 42 | #include <linux/cpufreq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <linux/mtd/mtd.h> |
| 45 | #include <linux/mtd/nand.h> |
| 46 | #include <linux/mtd/nand_ecc.h> |
| 47 | #include <linux/mtd/partitions.h> |
| 48 | |
Arnd Bergmann | 436d42c | 2012-08-24 15:22:12 +0200 | [diff] [blame] | 49 | #include <linux/platform_data/mtd-nand-s3c2410.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Sachin Kamat | 02d0186 | 2014-01-10 11:24:13 +0530 | [diff] [blame^] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* new oob placement block for use with hardware ecc generation |
| 87 | */ |
| 88 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 89 | static struct nand_ecclayout nand_hw_eccoob = { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 90 | .eccbytes = 3, |
| 91 | .eccpos = {0, 1, 2}, |
| 92 | .oobfree = {{8, 8}} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* controller and mtd information */ |
| 96 | |
| 97 | struct s3c2410_nand_info; |
| 98 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 99 | /** |
| 100 | * struct s3c2410_nand_mtd - driver MTD structure |
| 101 | * @mtd: The MTD instance to pass to the MTD layer. |
| 102 | * @chip: The NAND chip information. |
| 103 | * @set: The platform information supplied for this set of NAND chips. |
| 104 | * @info: Link back to the hardware information. |
| 105 | * @scan_res: The result from calling nand_scan_ident(). |
| 106 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct s3c2410_nand_mtd { |
| 108 | struct mtd_info mtd; |
| 109 | struct nand_chip chip; |
| 110 | struct s3c2410_nand_set *set; |
| 111 | struct s3c2410_nand_info *info; |
| 112 | int scan_res; |
| 113 | }; |
| 114 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 115 | enum s3c_cpu_type { |
| 116 | TYPE_S3C2410, |
| 117 | TYPE_S3C2412, |
| 118 | TYPE_S3C2440, |
| 119 | }; |
| 120 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 121 | enum s3c_nand_clk_state { |
| 122 | CLOCK_DISABLE = 0, |
| 123 | CLOCK_ENABLE, |
| 124 | CLOCK_SUSPEND, |
| 125 | }; |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* overview of the s3c2410 nand state */ |
| 128 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 129 | /** |
| 130 | * struct s3c2410_nand_info - NAND controller state. |
| 131 | * @mtds: An array of MTD instances on this controoler. |
| 132 | * @platform: The platform data for this board. |
| 133 | * @device: The platform device we bound to. |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 134 | * @clk: The clock resource for this controller. |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 135 | * @regs: The area mapped for the hardware registers. |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 136 | * @sel_reg: Pointer to the register controlling the NAND selection. |
| 137 | * @sel_bit: The bit in @sel_reg to select the NAND chip. |
| 138 | * @mtd_count: The number of MTDs created from this controller. |
| 139 | * @save_sel: The contents of @sel_reg to be saved over suspend. |
| 140 | * @clk_rate: The clock rate from @clk. |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 141 | * @clk_state: The current clock state. |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 142 | * @cpu_type: The exact type of this controller. |
| 143 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | struct s3c2410_nand_info { |
| 145 | /* mtd info */ |
| 146 | struct nand_hw_control controller; |
| 147 | struct s3c2410_nand_mtd *mtds; |
| 148 | struct s3c2410_platform_nand *platform; |
| 149 | |
| 150 | /* device info */ |
| 151 | struct device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | struct clk *clk; |
Ben Dooks | fdf2fd5 | 2005-02-18 14:46:15 +0000 | [diff] [blame] | 153 | void __iomem *regs; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 154 | void __iomem *sel_reg; |
| 155 | int sel_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | int mtd_count; |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 157 | unsigned long save_sel; |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 158 | unsigned long clk_rate; |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 159 | enum s3c_nand_clk_state clk_state; |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 160 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 161 | enum s3c_cpu_type cpu_type; |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 162 | |
| 163 | #ifdef CONFIG_CPU_FREQ |
| 164 | struct notifier_block freq_transition; |
| 165 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /* conversion functions */ |
| 169 | |
| 170 | static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd) |
| 171 | { |
| 172 | return container_of(mtd, struct s3c2410_nand_mtd, mtd); |
| 173 | } |
| 174 | |
| 175 | static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd) |
| 176 | { |
| 177 | return s3c2410_nand_mtd_toours(mtd)->info; |
| 178 | } |
| 179 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 180 | static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 182 | return platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 185 | static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 187 | return dev_get_platdata(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 190 | static inline int allow_clk_suspend(struct s3c2410_nand_info *info) |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 191 | { |
Sachin Kamat | a68c5ec | 2012-07-16 16:02:25 +0530 | [diff] [blame] | 192 | #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP |
| 193 | return 1; |
| 194 | #else |
| 195 | return 0; |
| 196 | #endif |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 199 | /** |
| 200 | * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock. |
| 201 | * @info: The controller instance. |
| 202 | * @new_state: State to which clock should be set. |
| 203 | */ |
| 204 | static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info, |
| 205 | enum s3c_nand_clk_state new_state) |
| 206 | { |
| 207 | if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND) |
| 208 | return; |
| 209 | |
| 210 | if (info->clk_state == CLOCK_ENABLE) { |
| 211 | if (new_state != CLOCK_ENABLE) |
| 212 | clk_disable(info->clk); |
| 213 | } else { |
| 214 | if (new_state == CLOCK_ENABLE) |
| 215 | clk_enable(info->clk); |
| 216 | } |
| 217 | |
| 218 | info->clk_state = new_state; |
| 219 | } |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* timing calculations */ |
| 222 | |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 223 | #define NS_IN_KHZ 1000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 225 | /** |
| 226 | * s3c_nand_calc_rate - calculate timing data. |
| 227 | * @wanted: The cycle time in nanoseconds. |
| 228 | * @clk: The clock rate in kHz. |
| 229 | * @max: The maximum divider value. |
| 230 | * |
| 231 | * Calculate the timing value from the given parameters. |
| 232 | */ |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 233 | static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | int result; |
| 236 | |
Ben Dooks | 947391c | 2009-05-30 18:34:16 +0100 | [diff] [blame] | 237 | result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | pr_debug("result %d from %ld, %d\n", result, clk, wanted); |
| 240 | |
| 241 | if (result > max) { |
Sachin Kamat | 92aeb5d | 2012-07-16 16:02:23 +0530 | [diff] [blame] | 242 | pr_err("%d ns is too big for current clock rate %ld\n", |
| 243 | wanted, clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | if (result < 1) |
| 248 | result = 1; |
| 249 | |
| 250 | return result; |
| 251 | } |
| 252 | |
Sachin Kamat | 54cd020 | 2012-07-16 16:02:26 +0530 | [diff] [blame] | 253 | #define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | /* controller setup */ |
| 256 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 257 | /** |
| 258 | * s3c2410_nand_setrate - setup controller timing information. |
| 259 | * @info: The controller instance. |
| 260 | * |
| 261 | * Given the information supplied by the platform, calculate and set |
| 262 | * the necessary timing registers in the hardware to generate the |
| 263 | * necessary timing cycles to the hardware. |
| 264 | */ |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 265 | static int s3c2410_nand_setrate(struct s3c2410_nand_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 267 | struct s3c2410_platform_nand *plat = info->platform; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 268 | int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4; |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 269 | int tacls, twrph0, twrph1; |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 270 | unsigned long clkrate = clk_get_rate(info->clk); |
Nelson Castillo | 2612e52 | 2009-05-10 15:41:54 -0500 | [diff] [blame] | 271 | unsigned long uninitialized_var(set), cfg, uninitialized_var(mask); |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 272 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /* calculate the timing information for the controller */ |
| 275 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 276 | info->clk_rate = clkrate; |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 277 | clkrate /= 1000; /* turn clock into kHz for ease of use */ |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (plat != NULL) { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 280 | tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max); |
| 281 | twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8); |
| 282 | twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } else { |
| 284 | /* default timings */ |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 285 | tacls = tacls_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | twrph0 = 8; |
| 287 | twrph1 = 8; |
| 288 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (tacls < 0 || twrph0 < 0 || twrph1 < 0) { |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 291 | dev_err(info->device, "cannot get suitable timings\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return -EINVAL; |
| 293 | } |
| 294 | |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 295 | dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n", |
Sachin Kamat | 54cd020 | 2012-07-16 16:02:26 +0530 | [diff] [blame] | 296 | tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), |
| 297 | twrph1, to_ns(twrph1, clkrate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 299 | switch (info->cpu_type) { |
| 300 | case TYPE_S3C2410: |
| 301 | mask = (S3C2410_NFCONF_TACLS(3) | |
| 302 | S3C2410_NFCONF_TWRPH0(7) | |
| 303 | S3C2410_NFCONF_TWRPH1(7)); |
| 304 | set = S3C2410_NFCONF_EN; |
| 305 | set |= S3C2410_NFCONF_TACLS(tacls - 1); |
| 306 | set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1); |
| 307 | set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1); |
| 308 | break; |
| 309 | |
| 310 | case TYPE_S3C2440: |
| 311 | case TYPE_S3C2412: |
Peter Korsgaard | a755a38 | 2009-06-03 13:46:54 +0200 | [diff] [blame] | 312 | mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) | |
| 313 | S3C2440_NFCONF_TWRPH0(7) | |
| 314 | S3C2440_NFCONF_TWRPH1(7)); |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 315 | |
| 316 | set = S3C2440_NFCONF_TACLS(tacls - 1); |
| 317 | set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1); |
| 318 | set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1); |
| 319 | break; |
| 320 | |
| 321 | default: |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 322 | BUG(); |
| 323 | } |
| 324 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 325 | local_irq_save(flags); |
| 326 | |
| 327 | cfg = readl(info->regs + S3C2410_NFCONF); |
| 328 | cfg &= ~mask; |
| 329 | cfg |= set; |
| 330 | writel(cfg, info->regs + S3C2410_NFCONF); |
| 331 | |
| 332 | local_irq_restore(flags); |
| 333 | |
Andy Green | ae7304e | 2009-05-10 15:42:02 -0500 | [diff] [blame] | 334 | dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg); |
| 335 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 339 | /** |
| 340 | * s3c2410_nand_inithw - basic hardware initialisation |
| 341 | * @info: The hardware state. |
| 342 | * |
| 343 | * Do the basic initialisation of the hardware, using s3c2410_nand_setrate() |
| 344 | * to setup the hardware access speeds and set the controller to be enabled. |
| 345 | */ |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 346 | static int s3c2410_nand_inithw(struct s3c2410_nand_info *info) |
| 347 | { |
| 348 | int ret; |
| 349 | |
| 350 | ret = s3c2410_nand_setrate(info); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | |
Sachin Kamat | 54cd020 | 2012-07-16 16:02:26 +0530 | [diff] [blame] | 354 | switch (info->cpu_type) { |
| 355 | case TYPE_S3C2410: |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 356 | default: |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 357 | break; |
| 358 | |
Sachin Kamat | 54cd020 | 2012-07-16 16:02:26 +0530 | [diff] [blame] | 359 | case TYPE_S3C2440: |
| 360 | case TYPE_S3C2412: |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 361 | /* enable the controller and de-assert nFCE */ |
| 362 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 363 | writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 369 | /** |
| 370 | * s3c2410_nand_select_chip - select the given nand chip |
| 371 | * @mtd: The MTD instance for this chip. |
| 372 | * @chip: The chip number. |
| 373 | * |
| 374 | * This is called by the MTD layer to either select a given chip for the |
| 375 | * @mtd instance, or to indicate that the access has finished and the |
| 376 | * chip can be de-selected. |
| 377 | * |
| 378 | * The routine ensures that the nFCE line is correctly setup, and any |
| 379 | * platform specific selection code is called to route nFCE to the specific |
| 380 | * chip. |
| 381 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip) |
| 383 | { |
| 384 | struct s3c2410_nand_info *info; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 385 | struct s3c2410_nand_mtd *nmtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | struct nand_chip *this = mtd->priv; |
| 387 | unsigned long cur; |
| 388 | |
| 389 | nmtd = this->priv; |
| 390 | info = nmtd->info; |
| 391 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 392 | if (chip != -1) |
| 393 | s3c2410_nand_clk_set_state(info, CLOCK_ENABLE); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 394 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 395 | cur = readl(info->sel_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | if (chip == -1) { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 398 | cur |= info->sel_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } else { |
Ben Dooks | fb8d82a | 2005-07-06 21:05:10 +0100 | [diff] [blame] | 400 | if (nmtd->set != NULL && chip > nmtd->set->nr_chips) { |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 401 | dev_err(info->device, "invalid chip %d\n", chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
| 405 | if (info->platform != NULL) { |
| 406 | if (info->platform->select_chip != NULL) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 407 | (info->platform->select_chip) (nmtd->set, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 410 | cur &= ~info->sel_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 413 | writel(cur, info->sel_reg); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 414 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 415 | if (chip == -1) |
| 416 | s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Ben Dooks | ad3b5fb | 2006-06-19 09:43:23 +0100 | [diff] [blame] | 419 | /* s3c2410_nand_hwcontrol |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 420 | * |
Ben Dooks | ad3b5fb | 2006-06-19 09:43:23 +0100 | [diff] [blame] | 421 | * Issue command and address cycles to the chip |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 422 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 424 | static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 425 | unsigned int ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
David Woodhouse | c9ac597 | 2006-11-30 08:17:38 +0000 | [diff] [blame] | 428 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 429 | if (cmd == NAND_CMD_NONE) |
| 430 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 432 | if (ctrl & NAND_CLE) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 433 | writeb(cmd, info->regs + S3C2410_NFCMD); |
| 434 | else |
| 435 | writeb(cmd, info->regs + S3C2410_NFADDR); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* command and control functions */ |
| 439 | |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 440 | static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
| 441 | unsigned int ctrl) |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 442 | { |
| 443 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 444 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 445 | if (cmd == NAND_CMD_NONE) |
| 446 | return; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 447 | |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 448 | if (ctrl & NAND_CLE) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 449 | writeb(cmd, info->regs + S3C2440_NFCMD); |
| 450 | else |
| 451 | writeb(cmd, info->regs + S3C2440_NFADDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /* s3c2410_nand_devready() |
| 455 | * |
| 456 | * returns 0 if the nand is busy, 1 if it is ready |
| 457 | */ |
| 458 | |
| 459 | static int s3c2410_nand_devready(struct mtd_info *mtd) |
| 460 | { |
| 461 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY; |
| 463 | } |
| 464 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 465 | static int s3c2440_nand_devready(struct mtd_info *mtd) |
| 466 | { |
| 467 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 468 | return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY; |
| 469 | } |
| 470 | |
| 471 | static int s3c2412_nand_devready(struct mtd_info *mtd) |
| 472 | { |
| 473 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 474 | return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY; |
| 475 | } |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | /* ECC handling functions */ |
| 478 | |
Sachin Kamat | 19da415 | 2012-08-21 14:24:10 +0530 | [diff] [blame] | 479 | #ifdef CONFIG_MTD_NAND_S3C2410_HWECC |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 480 | static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 481 | u_char *read_ecc, u_char *calc_ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 483 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 484 | unsigned int diff0, diff1, diff2; |
| 485 | unsigned int bit, byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 487 | pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 489 | diff0 = read_ecc[0] ^ calc_ecc[0]; |
| 490 | diff1 = read_ecc[1] ^ calc_ecc[1]; |
| 491 | diff2 = read_ecc[2] ^ calc_ecc[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Andy Shevchenko | 13e8597 | 2012-08-02 16:06:47 +0300 | [diff] [blame] | 493 | pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n", |
| 494 | __func__, 3, read_ecc, 3, calc_ecc, |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 495 | diff0, diff1, diff2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 497 | if (diff0 == 0 && diff1 == 0 && diff2 == 0) |
| 498 | return 0; /* ECC is ok */ |
| 499 | |
Ben Dooks | c45c6c6 | 2008-04-15 11:36:20 +0100 | [diff] [blame] | 500 | /* sometimes people do not think about using the ECC, so check |
| 501 | * to see if we have an 0xff,0xff,0xff read ECC and then ignore |
| 502 | * the error, on the assumption that this is an un-eccd page. |
| 503 | */ |
| 504 | if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff |
| 505 | && info->platform->ignore_unset_ecc) |
| 506 | return 0; |
| 507 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 508 | /* Can we correct this ECC (ie, one row and column change). |
| 509 | * Note, this is similar to the 256 error code on smartmedia */ |
| 510 | |
| 511 | if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 && |
| 512 | ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 && |
| 513 | ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) { |
| 514 | /* calculate the bit position of the error */ |
| 515 | |
Matt Reimer | d0bf379 | 2007-10-18 18:02:43 -0700 | [diff] [blame] | 516 | bit = ((diff2 >> 3) & 1) | |
| 517 | ((diff2 >> 4) & 2) | |
| 518 | ((diff2 >> 5) & 4); |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 519 | |
| 520 | /* calculate the byte position of the error */ |
| 521 | |
Matt Reimer | d0bf379 | 2007-10-18 18:02:43 -0700 | [diff] [blame] | 522 | byte = ((diff2 << 7) & 0x100) | |
| 523 | ((diff1 << 0) & 0x80) | |
| 524 | ((diff1 << 1) & 0x40) | |
| 525 | ((diff1 << 2) & 0x20) | |
| 526 | ((diff1 << 3) & 0x10) | |
| 527 | ((diff0 >> 4) & 0x08) | |
| 528 | ((diff0 >> 3) & 0x04) | |
| 529 | ((diff0 >> 2) & 0x02) | |
| 530 | ((diff0 >> 1) & 0x01); |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 531 | |
| 532 | dev_dbg(info->device, "correcting error bit %d, byte %d\n", |
| 533 | bit, byte); |
| 534 | |
| 535 | dat[byte] ^= (1 << bit); |
| 536 | return 1; |
| 537 | } |
| 538 | |
| 539 | /* if there is only one bit difference in the ECC, then |
| 540 | * one of only a row or column parity has changed, which |
| 541 | * means the error is most probably in the ECC itself */ |
| 542 | |
| 543 | diff0 |= (diff1 << 8); |
| 544 | diff0 |= (diff2 << 16); |
| 545 | |
| 546 | if ((diff0 & ~(1<<fls(diff0))) == 0) |
| 547 | return 1; |
| 548 | |
Matt Reimer | 4fac9f6 | 2007-10-18 18:02:44 -0700 | [diff] [blame] | 549 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 552 | /* ECC functions |
| 553 | * |
| 554 | * These allow the s3c2410 and s3c2440 to use the controller's ECC |
| 555 | * generator block to ECC the data as it passes through] |
| 556 | */ |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 559 | { |
| 560 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 561 | unsigned long ctrl; |
| 562 | |
| 563 | ctrl = readl(info->regs + S3C2410_NFCONF); |
| 564 | ctrl |= S3C2410_NFCONF_INITECC; |
| 565 | writel(ctrl, info->regs + S3C2410_NFCONF); |
| 566 | } |
| 567 | |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 568 | static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 569 | { |
| 570 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 571 | unsigned long ctrl; |
| 572 | |
| 573 | ctrl = readl(info->regs + S3C2440_NFCONT); |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 574 | writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, |
| 575 | info->regs + S3C2440_NFCONT); |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 578 | static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 579 | { |
| 580 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 581 | unsigned long ctrl; |
| 582 | |
| 583 | ctrl = readl(info->regs + S3C2440_NFCONT); |
| 584 | writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT); |
| 585 | } |
| 586 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 587 | static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 588 | u_char *ecc_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
| 590 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 591 | |
| 592 | ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0); |
| 593 | ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1); |
| 594 | ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2); |
| 595 | |
Andy Shevchenko | 13e8597 | 2012-08-02 16:06:47 +0300 | [diff] [blame] | 596 | pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 601 | static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 602 | u_char *ecc_code) |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 603 | { |
| 604 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 605 | unsigned long ecc = readl(info->regs + S3C2412_NFMECC0); |
| 606 | |
| 607 | ecc_code[0] = ecc; |
| 608 | ecc_code[1] = ecc >> 8; |
| 609 | ecc_code[2] = ecc >> 16; |
| 610 | |
Andy Shevchenko | 13e8597 | 2012-08-02 16:06:47 +0300 | [diff] [blame] | 611 | pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code); |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 616 | static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 617 | u_char *ecc_code) |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 618 | { |
| 619 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 620 | unsigned long ecc = readl(info->regs + S3C2440_NFMECC0); |
| 621 | |
| 622 | ecc_code[0] = ecc; |
| 623 | ecc_code[1] = ecc >> 8; |
| 624 | ecc_code[2] = ecc >> 16; |
| 625 | |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 626 | pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 627 | |
| 628 | return 0; |
| 629 | } |
Sachin Kamat | 19da415 | 2012-08-21 14:24:10 +0530 | [diff] [blame] | 630 | #endif |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 631 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 632 | /* over-ride the standard functions for a little more speed. We can |
| 633 | * use read/write block to move the data buffers to/from the controller |
| 634 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 637 | { |
| 638 | struct nand_chip *this = mtd->priv; |
| 639 | readsb(this->IO_ADDR_R, buf, len); |
| 640 | } |
| 641 | |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 642 | static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 643 | { |
| 644 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
Ben Dooks | dea2aa6 | 2009-05-30 18:30:18 +0100 | [diff] [blame] | 645 | |
| 646 | readsl(info->regs + S3C2440_NFDATA, buf, len >> 2); |
| 647 | |
| 648 | /* cleanup if we've got less than a word to do */ |
| 649 | if (len & 3) { |
| 650 | buf += len & ~3; |
| 651 | |
| 652 | for (; len & 3; len--) |
| 653 | *buf++ = readb(info->regs + S3C2440_NFDATA); |
| 654 | } |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 657 | static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, |
| 658 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
| 660 | struct nand_chip *this = mtd->priv; |
| 661 | writesb(this->IO_ADDR_W, buf, len); |
| 662 | } |
| 663 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 664 | static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, |
| 665 | int len) |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 666 | { |
| 667 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
Ben Dooks | dea2aa6 | 2009-05-30 18:30:18 +0100 | [diff] [blame] | 668 | |
| 669 | writesl(info->regs + S3C2440_NFDATA, buf, len >> 2); |
| 670 | |
| 671 | /* cleanup any fractional write */ |
| 672 | if (len & 3) { |
| 673 | buf += len & ~3; |
| 674 | |
| 675 | for (; len & 3; len--, buf++) |
| 676 | writeb(*buf, info->regs + S3C2440_NFDATA); |
| 677 | } |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 680 | /* cpufreq driver support */ |
| 681 | |
| 682 | #ifdef CONFIG_CPU_FREQ |
| 683 | |
| 684 | static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb, |
| 685 | unsigned long val, void *data) |
| 686 | { |
| 687 | struct s3c2410_nand_info *info; |
| 688 | unsigned long newclk; |
| 689 | |
| 690 | info = container_of(nb, struct s3c2410_nand_info, freq_transition); |
| 691 | newclk = clk_get_rate(info->clk); |
| 692 | |
| 693 | if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) || |
| 694 | (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) { |
| 695 | s3c2410_nand_setrate(info); |
| 696 | } |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info) |
| 702 | { |
| 703 | info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition; |
| 704 | |
| 705 | return cpufreq_register_notifier(&info->freq_transition, |
| 706 | CPUFREQ_TRANSITION_NOTIFIER); |
| 707 | } |
| 708 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 709 | static inline void |
| 710 | s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info) |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 711 | { |
| 712 | cpufreq_unregister_notifier(&info->freq_transition, |
| 713 | CPUFREQ_TRANSITION_NOTIFIER); |
| 714 | } |
| 715 | |
| 716 | #else |
| 717 | static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info) |
| 718 | { |
| 719 | return 0; |
| 720 | } |
| 721 | |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 722 | static inline void |
| 723 | s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info) |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 724 | { |
| 725 | } |
| 726 | #endif |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* device management functions */ |
| 729 | |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 730 | static int s3c24xx_nand_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 732 | struct s3c2410_nand_info *info = to_nand_info(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 734 | if (info == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 737 | s3c2410_nand_cpufreq_deregister(info); |
| 738 | |
| 739 | /* Release all our mtds and their partitions, then go through |
| 740 | * freeing the resources used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | if (info->mtds != NULL) { |
| 744 | struct s3c2410_nand_mtd *ptr = info->mtds; |
| 745 | int mtdno; |
| 746 | |
| 747 | for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) { |
| 748 | pr_debug("releasing mtd %d (%p)\n", mtdno, ptr); |
| 749 | nand_release(&ptr->mtd); |
| 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* free the common resources */ |
| 754 | |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 755 | if (!IS_ERR(info->clk)) |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 756 | s3c2410_nand_clk_set_state(info, CLOCK_DISABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info, |
| 762 | struct s3c2410_nand_mtd *mtd, |
| 763 | struct s3c2410_nand_set *set) |
| 764 | { |
Sachin Kamat | ded4c55 | 2012-11-16 16:08:22 +0530 | [diff] [blame] | 765 | if (set) { |
Dmitry Eremin-Solenikov | 599501a | 2011-06-02 18:01:02 +0400 | [diff] [blame] | 766 | mtd->mtd.name = set->name; |
Andy Green | ed27f02 | 2009-05-10 15:42:09 -0500 | [diff] [blame] | 767 | |
Sachin Kamat | ded4c55 | 2012-11-16 16:08:22 +0530 | [diff] [blame] | 768 | return mtd_device_parse_register(&mtd->mtd, NULL, NULL, |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 769 | set->partitions, set->nr_partitions); |
Sachin Kamat | ded4c55 | 2012-11-16 16:08:22 +0530 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 775 | /** |
| 776 | * s3c2410_nand_init_chip - initialise a single instance of an chip |
| 777 | * @info: The base NAND controller the chip is on. |
| 778 | * @nmtd: The new controller MTD instance to fill in. |
| 779 | * @set: The information passed from the board specific platform data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | * |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 781 | * Initialise the given @nmtd from the information in @info and @set. This |
| 782 | * readies the structure for use with the MTD layer functions by ensuring |
| 783 | * all pointers are setup and the necessary control routines selected. |
| 784 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info, |
| 786 | struct s3c2410_nand_mtd *nmtd, |
| 787 | struct s3c2410_nand_set *set) |
| 788 | { |
| 789 | struct nand_chip *chip = &nmtd->chip; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 790 | void __iomem *regs = info->regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | chip->write_buf = s3c2410_nand_write_buf; |
| 793 | chip->read_buf = s3c2410_nand_read_buf; |
| 794 | chip->select_chip = s3c2410_nand_select_chip; |
| 795 | chip->chip_delay = 50; |
| 796 | chip->priv = nmtd; |
Ben Dooks | 74218fe | 2009-11-02 18:12:51 +0000 | [diff] [blame] | 797 | chip->options = set->options; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | chip->controller = &info->controller; |
| 799 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 800 | switch (info->cpu_type) { |
| 801 | case TYPE_S3C2410: |
| 802 | chip->IO_ADDR_W = regs + S3C2410_NFDATA; |
| 803 | info->sel_reg = regs + S3C2410_NFCONF; |
| 804 | info->sel_bit = S3C2410_NFCONF_nFCE; |
| 805 | chip->cmd_ctrl = s3c2410_nand_hwcontrol; |
| 806 | chip->dev_ready = s3c2410_nand_devready; |
| 807 | break; |
| 808 | |
| 809 | case TYPE_S3C2440: |
| 810 | chip->IO_ADDR_W = regs + S3C2440_NFDATA; |
| 811 | info->sel_reg = regs + S3C2440_NFCONT; |
| 812 | info->sel_bit = S3C2440_NFCONT_nFCE; |
| 813 | chip->cmd_ctrl = s3c2440_nand_hwcontrol; |
| 814 | chip->dev_ready = s3c2440_nand_devready; |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 815 | chip->read_buf = s3c2440_nand_read_buf; |
| 816 | chip->write_buf = s3c2440_nand_write_buf; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 817 | break; |
| 818 | |
| 819 | case TYPE_S3C2412: |
| 820 | chip->IO_ADDR_W = regs + S3C2440_NFDATA; |
| 821 | info->sel_reg = regs + S3C2440_NFCONT; |
| 822 | info->sel_bit = S3C2412_NFCONT_nFCE0; |
| 823 | chip->cmd_ctrl = s3c2440_nand_hwcontrol; |
| 824 | chip->dev_ready = s3c2412_nand_devready; |
| 825 | |
| 826 | if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT) |
| 827 | dev_info(info->device, "System booted from NAND\n"); |
| 828 | |
| 829 | break; |
Sachin Kamat | 54cd020 | 2012-07-16 16:02:26 +0530 | [diff] [blame] | 830 | } |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 831 | |
| 832 | chip->IO_ADDR_R = chip->IO_ADDR_W; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | nmtd->info = info; |
| 835 | nmtd->mtd.priv = chip; |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 836 | nmtd->mtd.owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | nmtd->set = set; |
| 838 | |
Sachin Kamat | a68c5ec | 2012-07-16 16:02:25 +0530 | [diff] [blame] | 839 | #ifdef CONFIG_MTD_NAND_S3C2410_HWECC |
| 840 | chip->ecc.calculate = s3c2410_nand_calculate_ecc; |
| 841 | chip->ecc.correct = s3c2410_nand_correct_data; |
| 842 | chip->ecc.mode = NAND_ECC_HW; |
| 843 | chip->ecc.strength = 1; |
| 844 | |
| 845 | switch (info->cpu_type) { |
| 846 | case TYPE_S3C2410: |
| 847 | chip->ecc.hwctl = s3c2410_nand_enable_hwecc; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 848 | chip->ecc.calculate = s3c2410_nand_calculate_ecc; |
Sachin Kamat | a68c5ec | 2012-07-16 16:02:25 +0530 | [diff] [blame] | 849 | break; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 850 | |
Sachin Kamat | a68c5ec | 2012-07-16 16:02:25 +0530 | [diff] [blame] | 851 | case TYPE_S3C2412: |
| 852 | chip->ecc.hwctl = s3c2412_nand_enable_hwecc; |
| 853 | chip->ecc.calculate = s3c2412_nand_calculate_ecc; |
| 854 | break; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 855 | |
Sachin Kamat | a68c5ec | 2012-07-16 16:02:25 +0530 | [diff] [blame] | 856 | case TYPE_S3C2440: |
| 857 | chip->ecc.hwctl = s3c2440_nand_enable_hwecc; |
| 858 | chip->ecc.calculate = s3c2440_nand_calculate_ecc; |
| 859 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
Sachin Kamat | a68c5ec | 2012-07-16 16:02:25 +0530 | [diff] [blame] | 861 | #else |
| 862 | chip->ecc.mode = NAND_ECC_SOFT; |
| 863 | #endif |
Ben Dooks | 1c21ab6 | 2008-04-15 11:36:21 +0100 | [diff] [blame] | 864 | |
| 865 | if (set->ecc_layout != NULL) |
| 866 | chip->ecc.layout = set->ecc_layout; |
Ben Dooks | 37e5ffa | 2008-04-15 11:36:22 +0100 | [diff] [blame] | 867 | |
| 868 | if (set->disable_ecc) |
| 869 | chip->ecc.mode = NAND_ECC_NONE; |
Andy Green | 8c3e843 | 2009-05-10 15:41:25 -0500 | [diff] [blame] | 870 | |
| 871 | switch (chip->ecc.mode) { |
| 872 | case NAND_ECC_NONE: |
| 873 | dev_info(info->device, "NAND ECC disabled\n"); |
| 874 | break; |
| 875 | case NAND_ECC_SOFT: |
| 876 | dev_info(info->device, "NAND soft ECC\n"); |
| 877 | break; |
| 878 | case NAND_ECC_HW: |
| 879 | dev_info(info->device, "NAND hardware ECC\n"); |
| 880 | break; |
| 881 | default: |
| 882 | dev_info(info->device, "NAND ECC UNKNOWN\n"); |
| 883 | break; |
| 884 | } |
Michel Pollet | 9db41f9 | 2009-05-13 16:54:14 +0100 | [diff] [blame] | 885 | |
| 886 | /* If you use u-boot BBT creation code, specifying this flag will |
| 887 | * let the kernel fish out the BBT from the NAND, and also skip the |
| 888 | * full NAND scan that can take 1/2s or so. Little things... */ |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 889 | if (set->flash_bbt) { |
Brian Norris | bb9ebd4e | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 890 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 891 | chip->options |= NAND_SKIP_BBTSCAN; |
| 892 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 895 | /** |
| 896 | * s3c2410_nand_update_chip - post probe update |
| 897 | * @info: The controller instance. |
| 898 | * @nmtd: The driver version of the MTD instance. |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 899 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 900 | * This routine is called after the chip probe has successfully completed |
Ben Dooks | 3db7215 | 2009-05-30 17:18:15 +0100 | [diff] [blame] | 901 | * and the relevant per-chip information updated. This call ensure that |
| 902 | * we update the internal state accordingly. |
| 903 | * |
| 904 | * The internal state is currently limited to the ECC state information. |
| 905 | */ |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 906 | static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info, |
| 907 | struct s3c2410_nand_mtd *nmtd) |
| 908 | { |
| 909 | struct nand_chip *chip = &nmtd->chip; |
| 910 | |
Ben Dooks | 451d339 | 2008-05-20 17:32:14 +0100 | [diff] [blame] | 911 | dev_dbg(info->device, "chip %p => page shift %d\n", |
| 912 | chip, chip->page_shift); |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 913 | |
Andy Green | 8c3e843 | 2009-05-10 15:41:25 -0500 | [diff] [blame] | 914 | if (chip->ecc.mode != NAND_ECC_HW) |
| 915 | return; |
| 916 | |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 917 | /* change the behaviour depending on whether we are using |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 918 | * the large or small page nand device */ |
| 919 | |
Andy Green | 8c3e843 | 2009-05-10 15:41:25 -0500 | [diff] [blame] | 920 | if (chip->page_shift > 10) { |
| 921 | chip->ecc.size = 256; |
| 922 | chip->ecc.bytes = 3; |
| 923 | } else { |
| 924 | chip->ecc.size = 512; |
| 925 | chip->ecc.bytes = 3; |
| 926 | chip->ecc.layout = &nand_hw_eccoob; |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 930 | /* s3c24xx_nand_probe |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | * |
| 932 | * called by device layer when it finds a device matching |
| 933 | * one our driver can handled. This code checks to see if |
| 934 | * it can allocate all necessary resources then calls the |
| 935 | * nand layer to look for devices |
| 936 | */ |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 937 | static int s3c24xx_nand_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 939 | struct s3c2410_platform_nand *plat = to_nand_plat(pdev); |
Sachin Kamat | 54cd020 | 2012-07-16 16:02:26 +0530 | [diff] [blame] | 940 | enum s3c_cpu_type cpu_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | struct s3c2410_nand_info *info; |
| 942 | struct s3c2410_nand_mtd *nmtd; |
| 943 | struct s3c2410_nand_set *sets; |
| 944 | struct resource *res; |
| 945 | int err = 0; |
| 946 | int size; |
| 947 | int nr_sets; |
| 948 | int setno; |
| 949 | |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 950 | cpu_type = platform_get_device_id(pdev)->driver_data; |
| 951 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 952 | pr_debug("s3c2410_nand_probe(%p)\n", pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 954 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (info == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | err = -ENOMEM; |
| 957 | goto exit_error; |
| 958 | } |
| 959 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 960 | platform_set_drvdata(pdev, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | spin_lock_init(&info->controller.lock); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 963 | init_waitqueue_head(&info->controller.wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | /* get the clock source and enable it */ |
| 966 | |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 967 | info->clk = devm_clk_get(&pdev->dev, "nand"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | if (IS_ERR(info->clk)) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 969 | dev_err(&pdev->dev, "failed to get clock\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | err = -ENOENT; |
| 971 | goto exit_error; |
| 972 | } |
| 973 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 974 | s3c2410_nand_clk_set_state(info, CLOCK_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
| 976 | /* allocate and map the resource */ |
| 977 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 978 | /* currently we assume we have the one resource */ |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 979 | res = pdev->resource; |
H Hartley Sweeten | fc161c4 | 2009-12-14 16:56:22 -0500 | [diff] [blame] | 980 | size = resource_size(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 982 | info->device = &pdev->dev; |
| 983 | info->platform = plat; |
| 984 | info->cpu_type = cpu_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 986 | info->regs = devm_ioremap_resource(&pdev->dev, res); |
| 987 | if (IS_ERR(info->regs)) { |
| 988 | err = PTR_ERR(info->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | goto exit_error; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 992 | dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | /* initialise the hardware */ |
| 995 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 996 | err = s3c2410_nand_inithw(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | if (err != 0) |
| 998 | goto exit_error; |
| 999 | |
| 1000 | sets = (plat != NULL) ? plat->sets : NULL; |
| 1001 | nr_sets = (plat != NULL) ? plat->nr_sets : 1; |
| 1002 | |
| 1003 | info->mtd_count = nr_sets; |
| 1004 | |
| 1005 | /* allocate our information */ |
| 1006 | |
| 1007 | size = nr_sets * sizeof(*info->mtds); |
Sachin Kamat | 6f32a3e | 2012-08-21 14:24:09 +0530 | [diff] [blame] | 1008 | info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | if (info->mtds == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | err = -ENOMEM; |
| 1011 | goto exit_error; |
| 1012 | } |
| 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | /* initialise all possible chips */ |
| 1015 | |
| 1016 | nmtd = info->mtds; |
| 1017 | |
| 1018 | for (setno = 0; setno < nr_sets; setno++, nmtd++) { |
Sachin Kamat | f938bc5 | 2012-08-21 10:21:15 +0530 | [diff] [blame] | 1019 | pr_debug("initialising set %d (%p, info %p)\n", |
| 1020 | setno, nmtd, info); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | s3c2410_nand_init_chip(info, nmtd, sets); |
| 1023 | |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 1024 | nmtd->scan_res = nand_scan_ident(&nmtd->mtd, |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 1025 | (sets) ? sets->nr_chips : 1, |
| 1026 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (nmtd->scan_res == 0) { |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 1029 | s3c2410_nand_update_chip(info, nmtd); |
| 1030 | nand_scan_tail(&nmtd->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | s3c2410_nand_add_partition(info, nmtd, sets); |
| 1032 | } |
| 1033 | |
| 1034 | if (sets != NULL) |
| 1035 | sets++; |
| 1036 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1037 | |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 1038 | err = s3c2410_nand_cpufreq_register(info); |
| 1039 | if (err < 0) { |
| 1040 | dev_err(&pdev->dev, "failed to init cpufreq support\n"); |
| 1041 | goto exit_error; |
| 1042 | } |
| 1043 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 1044 | if (allow_clk_suspend(info)) { |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1045 | dev_info(&pdev->dev, "clock idle support enabled\n"); |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 1046 | s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | pr_debug("initialised ok\n"); |
| 1050 | return 0; |
| 1051 | |
| 1052 | exit_error: |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1053 | s3c24xx_nand_remove(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
| 1055 | if (err == 0) |
| 1056 | err = -EINVAL; |
| 1057 | return err; |
| 1058 | } |
| 1059 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1060 | /* PM Support */ |
| 1061 | #ifdef CONFIG_PM |
| 1062 | |
| 1063 | static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm) |
| 1064 | { |
| 1065 | struct s3c2410_nand_info *info = platform_get_drvdata(dev); |
| 1066 | |
| 1067 | if (info) { |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 1068 | info->save_sel = readl(info->sel_reg); |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 1069 | |
| 1070 | /* For the moment, we must ensure nFCE is high during |
| 1071 | * the time we are suspended. This really should be |
| 1072 | * handled by suspending the MTDs we are using, but |
| 1073 | * that is currently not the case. */ |
| 1074 | |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 1075 | writel(info->save_sel | info->sel_bit, info->sel_reg); |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 1076 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 1077 | s3c2410_nand_clk_set_state(info, CLOCK_DISABLE); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | static int s3c24xx_nand_resume(struct platform_device *dev) |
| 1084 | { |
| 1085 | struct s3c2410_nand_info *info = platform_get_drvdata(dev); |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 1086 | unsigned long sel; |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1087 | |
| 1088 | if (info) { |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 1089 | s3c2410_nand_clk_set_state(info, CLOCK_ENABLE); |
Ben Dooks | 30821fe | 2008-07-15 11:58:31 +0100 | [diff] [blame] | 1090 | s3c2410_nand_inithw(info); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1091 | |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 1092 | /* Restore the state of the nFCE line. */ |
| 1093 | |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 1094 | sel = readl(info->sel_reg); |
| 1095 | sel &= ~info->sel_bit; |
| 1096 | sel |= info->save_sel & info->sel_bit; |
| 1097 | writel(sel, info->sel_reg); |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 1098 | |
Jiri Pinkava | ac497c1 | 2011-04-13 11:59:30 +0200 | [diff] [blame] | 1099 | s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | #else |
| 1106 | #define s3c24xx_nand_suspend NULL |
| 1107 | #define s3c24xx_nand_resume NULL |
| 1108 | #endif |
| 1109 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 1110 | /* driver device registration */ |
| 1111 | |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1112 | static struct platform_device_id s3c24xx_driver_ids[] = { |
| 1113 | { |
| 1114 | .name = "s3c2410-nand", |
| 1115 | .driver_data = TYPE_S3C2410, |
| 1116 | }, { |
| 1117 | .name = "s3c2440-nand", |
| 1118 | .driver_data = TYPE_S3C2440, |
| 1119 | }, { |
| 1120 | .name = "s3c2412-nand", |
| 1121 | .driver_data = TYPE_S3C2412, |
Peter Korsgaard | 9dbc090 | 2009-06-07 06:04:23 -0700 | [diff] [blame] | 1122 | }, { |
| 1123 | .name = "s3c6400-nand", |
| 1124 | .driver_data = TYPE_S3C2412, /* compatible with 2412 */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1125 | }, |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1126 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | }; |
| 1128 | |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1129 | MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 1130 | |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1131 | static struct platform_driver s3c24xx_nand_driver = { |
| 1132 | .probe = s3c24xx_nand_probe, |
| 1133 | .remove = s3c24xx_nand_remove, |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 1134 | .suspend = s3c24xx_nand_suspend, |
| 1135 | .resume = s3c24xx_nand_resume, |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1136 | .id_table = s3c24xx_driver_ids, |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 1137 | .driver = { |
Ben Dooks | ec0482e | 2009-05-30 16:55:29 +0100 | [diff] [blame] | 1138 | .name = "s3c24xx-nand", |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 1139 | .owner = THIS_MODULE, |
| 1140 | }, |
| 1141 | }; |
| 1142 | |
Sachin Kamat | 056fcab | 2012-07-16 16:02:22 +0530 | [diff] [blame] | 1143 | module_platform_driver(s3c24xx_nand_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | MODULE_LICENSE("GPL"); |
| 1146 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 1147 | MODULE_DESCRIPTION("S3C24XX MTD NAND driver"); |