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