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