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