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