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