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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG |
| 25 | #define DEBUG |
| 26 | #endif |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/ioport.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/delay.h> |
| 36 | #include <linux/err.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 37 | #include <linux/slab.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 38 | #include <linux/clk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <linux/mtd/mtd.h> |
| 41 | #include <linux/mtd/nand.h> |
| 42 | #include <linux/mtd/nand_ecc.h> |
| 43 | #include <linux/mtd/partitions.h> |
| 44 | |
| 45 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Ben Dooks | b7a7018 | 2007-07-24 13:37:27 +0100 | [diff] [blame] | 47 | #include <asm/plat-s3c/regs-nand.h> |
| 48 | #include <asm/plat-s3c/nand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_MTD_NAND_S3C2410_HWECC |
| 51 | static int hardware_ecc = 1; |
| 52 | #else |
| 53 | static int hardware_ecc = 0; |
| 54 | #endif |
| 55 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP |
| 57 | static int clock_stop = 1; |
| 58 | #else |
| 59 | static const int clock_stop = 0; |
| 60 | #endif |
| 61 | |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* new oob placement block for use with hardware ecc generation |
| 64 | */ |
| 65 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 66 | static struct nand_ecclayout nand_hw_eccoob = { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 67 | .eccbytes = 3, |
| 68 | .eccpos = {0, 1, 2}, |
| 69 | .oobfree = {{8, 8}} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /* controller and mtd information */ |
| 73 | |
| 74 | struct s3c2410_nand_info; |
| 75 | |
| 76 | struct s3c2410_nand_mtd { |
| 77 | struct mtd_info mtd; |
| 78 | struct nand_chip chip; |
| 79 | struct s3c2410_nand_set *set; |
| 80 | struct s3c2410_nand_info *info; |
| 81 | int scan_res; |
| 82 | }; |
| 83 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 84 | enum s3c_cpu_type { |
| 85 | TYPE_S3C2410, |
| 86 | TYPE_S3C2412, |
| 87 | TYPE_S3C2440, |
| 88 | }; |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* overview of the s3c2410 nand state */ |
| 91 | |
| 92 | struct s3c2410_nand_info { |
| 93 | /* mtd info */ |
| 94 | struct nand_hw_control controller; |
| 95 | struct s3c2410_nand_mtd *mtds; |
| 96 | struct s3c2410_platform_nand *platform; |
| 97 | |
| 98 | /* device info */ |
| 99 | struct device *device; |
| 100 | struct resource *area; |
| 101 | struct clk *clk; |
Ben Dooks | fdf2fd5 | 2005-02-18 14:46:15 +0000 | [diff] [blame] | 102 | void __iomem *regs; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 103 | void __iomem *sel_reg; |
| 104 | int sel_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | int mtd_count; |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 106 | unsigned long save_sel; |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 107 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 108 | enum s3c_cpu_type cpu_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /* conversion functions */ |
| 112 | |
| 113 | static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd) |
| 114 | { |
| 115 | return container_of(mtd, struct s3c2410_nand_mtd, mtd); |
| 116 | } |
| 117 | |
| 118 | static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd) |
| 119 | { |
| 120 | return s3c2410_nand_mtd_toours(mtd)->info; |
| 121 | } |
| 122 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 123 | static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 125 | return platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 128 | static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 130 | return dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 133 | static inline int allow_clk_stop(struct s3c2410_nand_info *info) |
| 134 | { |
| 135 | return clock_stop; |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* timing calculations */ |
| 139 | |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 140 | #define NS_IN_KHZ 1000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 142 | 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] | 143 | { |
| 144 | int result; |
| 145 | |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 146 | result = (wanted * clk) / NS_IN_KHZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | result++; |
| 148 | |
| 149 | pr_debug("result %d from %ld, %d\n", result, clk, wanted); |
| 150 | |
| 151 | if (result > max) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 152 | printk("%d ns is too big for current clock rate %ld\n", wanted, clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | if (result < 1) |
| 157 | result = 1; |
| 158 | |
| 159 | return result; |
| 160 | } |
| 161 | |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 162 | #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | /* controller setup */ |
| 165 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 166 | static int s3c2410_nand_inithw(struct s3c2410_nand_info *info, |
| 167 | struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 169 | struct s3c2410_platform_nand *plat = to_nand_plat(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | unsigned long clkrate = clk_get_rate(info->clk); |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 171 | int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4; |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 172 | int tacls, twrph0, twrph1; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 173 | unsigned long cfg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | /* calculate the timing information for the controller */ |
| 176 | |
Ben Dooks | cfd320f | 2005-10-20 22:22:58 +0100 | [diff] [blame] | 177 | clkrate /= 1000; /* turn clock into kHz for ease of use */ |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (plat != NULL) { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 180 | tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max); |
| 181 | twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8); |
| 182 | twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } else { |
| 184 | /* default timings */ |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 185 | tacls = tacls_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | twrph0 = 8; |
| 187 | twrph1 = 8; |
| 188 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (tacls < 0 || twrph0 < 0 || twrph1 < 0) { |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 191 | dev_err(info->device, "cannot get suitable timings\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | } |
| 194 | |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 195 | dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n", |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 196 | tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 198 | switch (info->cpu_type) { |
| 199 | case TYPE_S3C2410: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 200 | cfg = S3C2410_NFCONF_EN; |
| 201 | cfg |= S3C2410_NFCONF_TACLS(tacls - 1); |
| 202 | cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1); |
| 203 | cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1); |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 204 | break; |
| 205 | |
| 206 | case TYPE_S3C2440: |
| 207 | case TYPE_S3C2412: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 208 | cfg = S3C2440_NFCONF_TACLS(tacls - 1); |
| 209 | cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1); |
| 210 | cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 211 | |
| 212 | /* enable the controller and de-assert nFCE */ |
| 213 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 214 | writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 217 | dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | writel(cfg, info->regs + S3C2410_NFCONF); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* select chip */ |
| 224 | |
| 225 | static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip) |
| 226 | { |
| 227 | struct s3c2410_nand_info *info; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 228 | struct s3c2410_nand_mtd *nmtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | struct nand_chip *this = mtd->priv; |
| 230 | unsigned long cur; |
| 231 | |
| 232 | nmtd = this->priv; |
| 233 | info = nmtd->info; |
| 234 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 235 | if (chip != -1 && allow_clk_stop(info)) |
| 236 | clk_enable(info->clk); |
| 237 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 238 | cur = readl(info->sel_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | if (chip == -1) { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 241 | cur |= info->sel_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } else { |
Ben Dooks | fb8d82a | 2005-07-06 21:05:10 +0100 | [diff] [blame] | 243 | if (nmtd->set != NULL && chip > nmtd->set->nr_chips) { |
Ben Dooks | 99974c6 | 2006-06-21 15:43:05 +0100 | [diff] [blame] | 244 | dev_err(info->device, "invalid chip %d\n", chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | |
| 248 | if (info->platform != NULL) { |
| 249 | if (info->platform->select_chip != NULL) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 250 | (info->platform->select_chip) (nmtd->set, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 253 | cur &= ~info->sel_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 256 | writel(cur, info->sel_reg); |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 257 | |
| 258 | if (chip == -1 && allow_clk_stop(info)) |
| 259 | clk_disable(info->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Ben Dooks | ad3b5fb | 2006-06-19 09:43:23 +0100 | [diff] [blame] | 262 | /* s3c2410_nand_hwcontrol |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 263 | * |
Ben Dooks | ad3b5fb | 2006-06-19 09:43:23 +0100 | [diff] [blame] | 264 | * Issue command and address cycles to the chip |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 265 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 267 | static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 268 | unsigned int ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
| 270 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
David Woodhouse | c9ac597 | 2006-11-30 08:17:38 +0000 | [diff] [blame] | 271 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 272 | if (cmd == NAND_CMD_NONE) |
| 273 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 275 | if (ctrl & NAND_CLE) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 276 | writeb(cmd, info->regs + S3C2410_NFCMD); |
| 277 | else |
| 278 | writeb(cmd, info->regs + S3C2410_NFADDR); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* command and control functions */ |
| 282 | |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 283 | static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
| 284 | unsigned int ctrl) |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 285 | { |
| 286 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 287 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 288 | if (cmd == NAND_CMD_NONE) |
| 289 | return; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 290 | |
David Woodhouse | f906887 | 2006-06-10 00:53:16 +0100 | [diff] [blame] | 291 | if (ctrl & NAND_CLE) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 292 | writeb(cmd, info->regs + S3C2440_NFCMD); |
| 293 | else |
| 294 | writeb(cmd, info->regs + S3C2440_NFADDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* s3c2410_nand_devready() |
| 298 | * |
| 299 | * returns 0 if the nand is busy, 1 if it is ready |
| 300 | */ |
| 301 | |
| 302 | static int s3c2410_nand_devready(struct mtd_info *mtd) |
| 303 | { |
| 304 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY; |
| 306 | } |
| 307 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 308 | static int s3c2440_nand_devready(struct mtd_info *mtd) |
| 309 | { |
| 310 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 311 | return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY; |
| 312 | } |
| 313 | |
| 314 | static int s3c2412_nand_devready(struct mtd_info *mtd) |
| 315 | { |
| 316 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 317 | return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY; |
| 318 | } |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* ECC handling functions */ |
| 321 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 322 | static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 323 | u_char *read_ecc, u_char *calc_ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 325 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 326 | unsigned int diff0, diff1, diff2; |
| 327 | unsigned int bit, byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 329 | 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] | 330 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 331 | diff0 = read_ecc[0] ^ calc_ecc[0]; |
| 332 | diff1 = read_ecc[1] ^ calc_ecc[1]; |
| 333 | diff2 = read_ecc[2] ^ calc_ecc[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 335 | pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n", |
| 336 | __func__, |
| 337 | read_ecc[0], read_ecc[1], read_ecc[2], |
| 338 | calc_ecc[0], calc_ecc[1], calc_ecc[2], |
| 339 | diff0, diff1, diff2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 341 | if (diff0 == 0 && diff1 == 0 && diff2 == 0) |
| 342 | return 0; /* ECC is ok */ |
| 343 | |
Ben Dooks | c45c6c6 | 2008-04-15 11:36:20 +0100 | [diff] [blame] | 344 | /* sometimes people do not think about using the ECC, so check |
| 345 | * to see if we have an 0xff,0xff,0xff read ECC and then ignore |
| 346 | * the error, on the assumption that this is an un-eccd page. |
| 347 | */ |
| 348 | if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff |
| 349 | && info->platform->ignore_unset_ecc) |
| 350 | return 0; |
| 351 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 352 | /* Can we correct this ECC (ie, one row and column change). |
| 353 | * Note, this is similar to the 256 error code on smartmedia */ |
| 354 | |
| 355 | if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 && |
| 356 | ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 && |
| 357 | ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) { |
| 358 | /* calculate the bit position of the error */ |
| 359 | |
Matt Reimer | d0bf379 | 2007-10-18 18:02:43 -0700 | [diff] [blame] | 360 | bit = ((diff2 >> 3) & 1) | |
| 361 | ((diff2 >> 4) & 2) | |
| 362 | ((diff2 >> 5) & 4); |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 363 | |
| 364 | /* calculate the byte position of the error */ |
| 365 | |
Matt Reimer | d0bf379 | 2007-10-18 18:02:43 -0700 | [diff] [blame] | 366 | byte = ((diff2 << 7) & 0x100) | |
| 367 | ((diff1 << 0) & 0x80) | |
| 368 | ((diff1 << 1) & 0x40) | |
| 369 | ((diff1 << 2) & 0x20) | |
| 370 | ((diff1 << 3) & 0x10) | |
| 371 | ((diff0 >> 4) & 0x08) | |
| 372 | ((diff0 >> 3) & 0x04) | |
| 373 | ((diff0 >> 2) & 0x02) | |
| 374 | ((diff0 >> 1) & 0x01); |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 375 | |
| 376 | dev_dbg(info->device, "correcting error bit %d, byte %d\n", |
| 377 | bit, byte); |
| 378 | |
| 379 | dat[byte] ^= (1 << bit); |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | /* if there is only one bit difference in the ECC, then |
| 384 | * one of only a row or column parity has changed, which |
| 385 | * means the error is most probably in the ECC itself */ |
| 386 | |
| 387 | diff0 |= (diff1 << 8); |
| 388 | diff0 |= (diff2 << 16); |
| 389 | |
| 390 | if ((diff0 & ~(1<<fls(diff0))) == 0) |
| 391 | return 1; |
| 392 | |
Matt Reimer | 4fac9f6 | 2007-10-18 18:02:44 -0700 | [diff] [blame] | 393 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 396 | /* ECC functions |
| 397 | * |
| 398 | * These allow the s3c2410 and s3c2440 to use the controller's ECC |
| 399 | * generator block to ECC the data as it passes through] |
| 400 | */ |
| 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 403 | { |
| 404 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 405 | unsigned long ctrl; |
| 406 | |
| 407 | ctrl = readl(info->regs + S3C2410_NFCONF); |
| 408 | ctrl |= S3C2410_NFCONF_INITECC; |
| 409 | writel(ctrl, info->regs + S3C2410_NFCONF); |
| 410 | } |
| 411 | |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 412 | static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 413 | { |
| 414 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 415 | unsigned long ctrl; |
| 416 | |
| 417 | ctrl = readl(info->regs + S3C2440_NFCONT); |
| 418 | writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT); |
| 419 | } |
| 420 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 421 | static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 422 | { |
| 423 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 424 | unsigned long ctrl; |
| 425 | |
| 426 | ctrl = readl(info->regs + S3C2440_NFCONT); |
| 427 | writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT); |
| 428 | } |
| 429 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 430 | static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 433 | |
| 434 | ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0); |
| 435 | ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1); |
| 436 | ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2); |
| 437 | |
Ben Dooks | a259324 | 2007-02-02 16:59:33 +0000 | [diff] [blame] | 438 | pr_debug("%s: returning ecc %02x%02x%02x\n", __func__, |
| 439 | ecc_code[0], ecc_code[1], ecc_code[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 444 | static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code) |
| 445 | { |
| 446 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 447 | unsigned long ecc = readl(info->regs + S3C2412_NFMECC0); |
| 448 | |
| 449 | ecc_code[0] = ecc; |
| 450 | ecc_code[1] = ecc >> 8; |
| 451 | ecc_code[2] = ecc >> 16; |
| 452 | |
| 453 | pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 458 | static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code) |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 459 | { |
| 460 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 461 | unsigned long ecc = readl(info->regs + S3C2440_NFMECC0); |
| 462 | |
| 463 | ecc_code[0] = ecc; |
| 464 | ecc_code[1] = ecc >> 8; |
| 465 | ecc_code[2] = ecc >> 16; |
| 466 | |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 467 | pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 472 | /* over-ride the standard functions for a little more speed. We can |
| 473 | * use read/write block to move the data buffers to/from the controller |
| 474 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 477 | { |
| 478 | struct nand_chip *this = mtd->priv; |
| 479 | readsb(this->IO_ADDR_R, buf, len); |
| 480 | } |
| 481 | |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 482 | static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 483 | { |
| 484 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 485 | readsl(info->regs + S3C2440_NFDATA, buf, len / 4); |
| 486 | } |
| 487 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 488 | static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | struct nand_chip *this = mtd->priv; |
| 491 | writesb(this->IO_ADDR_W, buf, len); |
| 492 | } |
| 493 | |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 494 | static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 495 | { |
| 496 | struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd); |
| 497 | writesl(info->regs + S3C2440_NFDATA, buf, len / 4); |
| 498 | } |
| 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | /* device management functions */ |
| 501 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 502 | static int s3c2410_nand_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 504 | struct s3c2410_nand_info *info = to_nand_info(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 506 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 508 | if (info == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return 0; |
| 510 | |
| 511 | /* first thing we need to do is release all our mtds |
| 512 | * and their partitions, then go through freeing the |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 513 | * resources used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (info->mtds != NULL) { |
| 517 | struct s3c2410_nand_mtd *ptr = info->mtds; |
| 518 | int mtdno; |
| 519 | |
| 520 | for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) { |
| 521 | pr_debug("releasing mtd %d (%p)\n", mtdno, ptr); |
| 522 | nand_release(&ptr->mtd); |
| 523 | } |
| 524 | |
| 525 | kfree(info->mtds); |
| 526 | } |
| 527 | |
| 528 | /* free the common resources */ |
| 529 | |
| 530 | if (info->clk != NULL && !IS_ERR(info->clk)) { |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 531 | if (!allow_clk_stop(info)) |
| 532 | clk_disable(info->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | clk_put(info->clk); |
| 534 | } |
| 535 | |
| 536 | if (info->regs != NULL) { |
| 537 | iounmap(info->regs); |
| 538 | info->regs = NULL; |
| 539 | } |
| 540 | |
| 541 | if (info->area != NULL) { |
| 542 | release_resource(info->area); |
| 543 | kfree(info->area); |
| 544 | info->area = NULL; |
| 545 | } |
| 546 | |
| 547 | kfree(info); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | #ifdef CONFIG_MTD_PARTITIONS |
| 553 | static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info, |
| 554 | struct s3c2410_nand_mtd *mtd, |
| 555 | struct s3c2410_nand_set *set) |
| 556 | { |
| 557 | if (set == NULL) |
| 558 | return add_mtd_device(&mtd->mtd); |
| 559 | |
| 560 | if (set->nr_partitions > 0 && set->partitions != NULL) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 561 | return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | return add_mtd_device(&mtd->mtd); |
| 565 | } |
| 566 | #else |
| 567 | static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info, |
| 568 | struct s3c2410_nand_mtd *mtd, |
| 569 | struct s3c2410_nand_set *set) |
| 570 | { |
| 571 | return add_mtd_device(&mtd->mtd); |
| 572 | } |
| 573 | #endif |
| 574 | |
| 575 | /* s3c2410_nand_init_chip |
| 576 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 577 | * init a single instance of an chip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | */ |
| 579 | |
| 580 | static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info, |
| 581 | struct s3c2410_nand_mtd *nmtd, |
| 582 | struct s3c2410_nand_set *set) |
| 583 | { |
| 584 | struct nand_chip *chip = &nmtd->chip; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 585 | void __iomem *regs = info->regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | chip->write_buf = s3c2410_nand_write_buf; |
| 588 | chip->read_buf = s3c2410_nand_read_buf; |
| 589 | chip->select_chip = s3c2410_nand_select_chip; |
| 590 | chip->chip_delay = 50; |
| 591 | chip->priv = nmtd; |
| 592 | chip->options = 0; |
| 593 | chip->controller = &info->controller; |
| 594 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 595 | switch (info->cpu_type) { |
| 596 | case TYPE_S3C2410: |
| 597 | chip->IO_ADDR_W = regs + S3C2410_NFDATA; |
| 598 | info->sel_reg = regs + S3C2410_NFCONF; |
| 599 | info->sel_bit = S3C2410_NFCONF_nFCE; |
| 600 | chip->cmd_ctrl = s3c2410_nand_hwcontrol; |
| 601 | chip->dev_ready = s3c2410_nand_devready; |
| 602 | break; |
| 603 | |
| 604 | case TYPE_S3C2440: |
| 605 | chip->IO_ADDR_W = regs + S3C2440_NFDATA; |
| 606 | info->sel_reg = regs + S3C2440_NFCONT; |
| 607 | info->sel_bit = S3C2440_NFCONT_nFCE; |
| 608 | chip->cmd_ctrl = s3c2440_nand_hwcontrol; |
| 609 | chip->dev_ready = s3c2440_nand_devready; |
Matt Reimer | b773bb2 | 2007-10-18 17:43:07 -0700 | [diff] [blame] | 610 | chip->read_buf = s3c2440_nand_read_buf; |
| 611 | chip->write_buf = s3c2440_nand_write_buf; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 612 | break; |
| 613 | |
| 614 | case TYPE_S3C2412: |
| 615 | chip->IO_ADDR_W = regs + S3C2440_NFDATA; |
| 616 | info->sel_reg = regs + S3C2440_NFCONT; |
| 617 | info->sel_bit = S3C2412_NFCONT_nFCE0; |
| 618 | chip->cmd_ctrl = s3c2440_nand_hwcontrol; |
| 619 | chip->dev_ready = s3c2412_nand_devready; |
| 620 | |
| 621 | if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT) |
| 622 | dev_info(info->device, "System booted from NAND\n"); |
| 623 | |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | chip->IO_ADDR_R = chip->IO_ADDR_W; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | nmtd->info = info; |
| 630 | nmtd->mtd.priv = chip; |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 631 | nmtd->mtd.owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | nmtd->set = set; |
| 633 | |
| 634 | if (hardware_ecc) { |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 635 | chip->ecc.calculate = s3c2410_nand_calculate_ecc; |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 636 | chip->ecc.correct = s3c2410_nand_correct_data; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 637 | chip->ecc.mode = NAND_ECC_HW; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 638 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 639 | switch (info->cpu_type) { |
| 640 | case TYPE_S3C2410: |
| 641 | chip->ecc.hwctl = s3c2410_nand_enable_hwecc; |
| 642 | chip->ecc.calculate = s3c2410_nand_calculate_ecc; |
| 643 | break; |
| 644 | |
| 645 | case TYPE_S3C2412: |
Matthieu CASTET | 4f65992 | 2007-02-13 12:30:38 +0100 | [diff] [blame] | 646 | chip->ecc.hwctl = s3c2412_nand_enable_hwecc; |
| 647 | chip->ecc.calculate = s3c2412_nand_calculate_ecc; |
| 648 | break; |
| 649 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 650 | case TYPE_S3C2440: |
| 651 | chip->ecc.hwctl = s3c2440_nand_enable_hwecc; |
| 652 | chip->ecc.calculate = s3c2440_nand_calculate_ecc; |
| 653 | break; |
| 654 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } else { |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 657 | chip->ecc.mode = NAND_ECC_SOFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
Ben Dooks | 1c21ab6 | 2008-04-15 11:36:21 +0100 | [diff] [blame] | 659 | |
| 660 | if (set->ecc_layout != NULL) |
| 661 | chip->ecc.layout = set->ecc_layout; |
Ben Dooks | 37e5ffa | 2008-04-15 11:36:22 +0100 | [diff] [blame] | 662 | |
| 663 | if (set->disable_ecc) |
| 664 | chip->ecc.mode = NAND_ECC_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 667 | /* s3c2410_nand_update_chip |
| 668 | * |
| 669 | * post-probe chip update, to change any items, such as the |
| 670 | * layout for large page nand |
| 671 | */ |
| 672 | |
| 673 | static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info, |
| 674 | struct s3c2410_nand_mtd *nmtd) |
| 675 | { |
| 676 | struct nand_chip *chip = &nmtd->chip; |
| 677 | |
Ben Dooks | 451d339 | 2008-05-20 17:32:14 +0100 | [diff] [blame] | 678 | dev_dbg(info->device, "chip %p => page shift %d\n", |
| 679 | chip, chip->page_shift); |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 680 | |
| 681 | if (hardware_ecc) { |
| 682 | /* change the behaviour depending on wether we are using |
| 683 | * the large or small page nand device */ |
| 684 | |
| 685 | if (chip->page_shift > 10) { |
| 686 | chip->ecc.size = 256; |
| 687 | chip->ecc.bytes = 3; |
| 688 | } else { |
| 689 | chip->ecc.size = 512; |
| 690 | chip->ecc.bytes = 3; |
| 691 | chip->ecc.layout = &nand_hw_eccoob; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | /* s3c2410_nand_probe |
| 697 | * |
| 698 | * called by device layer when it finds a device matching |
| 699 | * one our driver can handled. This code checks to see if |
| 700 | * it can allocate all necessary resources then calls the |
| 701 | * nand layer to look for devices |
| 702 | */ |
| 703 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 704 | static int s3c24xx_nand_probe(struct platform_device *pdev, |
| 705 | enum s3c_cpu_type cpu_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 707 | struct s3c2410_platform_nand *plat = to_nand_plat(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | struct s3c2410_nand_info *info; |
| 709 | struct s3c2410_nand_mtd *nmtd; |
| 710 | struct s3c2410_nand_set *sets; |
| 711 | struct resource *res; |
| 712 | int err = 0; |
| 713 | int size; |
| 714 | int nr_sets; |
| 715 | int setno; |
| 716 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 717 | pr_debug("s3c2410_nand_probe(%p)\n", pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 720 | if (info == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 721 | dev_err(&pdev->dev, "no memory for flash info\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | err = -ENOMEM; |
| 723 | goto exit_error; |
| 724 | } |
| 725 | |
| 726 | memzero(info, sizeof(*info)); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 727 | platform_set_drvdata(pdev, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | spin_lock_init(&info->controller.lock); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 730 | init_waitqueue_head(&info->controller.wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | /* get the clock source and enable it */ |
| 733 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 734 | info->clk = clk_get(&pdev->dev, "nand"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | if (IS_ERR(info->clk)) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 736 | dev_err(&pdev->dev, "failed to get clock\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | err = -ENOENT; |
| 738 | goto exit_error; |
| 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | clk_enable(info->clk); |
| 742 | |
| 743 | /* allocate and map the resource */ |
| 744 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 745 | /* currently we assume we have the one resource */ |
| 746 | res = pdev->resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | size = res->end - res->start + 1; |
| 748 | |
| 749 | info->area = request_mem_region(res->start, size, pdev->name); |
| 750 | |
| 751 | if (info->area == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 752 | dev_err(&pdev->dev, "cannot reserve register region\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | err = -ENOENT; |
| 754 | goto exit_error; |
| 755 | } |
| 756 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 757 | info->device = &pdev->dev; |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 758 | info->platform = plat; |
| 759 | info->regs = ioremap(res->start, size); |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 760 | info->cpu_type = cpu_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | if (info->regs == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 763 | dev_err(&pdev->dev, "cannot reserve register region\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | err = -EIO; |
| 765 | goto exit_error; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 768 | dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | /* initialise the hardware */ |
| 771 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 772 | err = s3c2410_nand_inithw(info, pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if (err != 0) |
| 774 | goto exit_error; |
| 775 | |
| 776 | sets = (plat != NULL) ? plat->sets : NULL; |
| 777 | nr_sets = (plat != NULL) ? plat->nr_sets : 1; |
| 778 | |
| 779 | info->mtd_count = nr_sets; |
| 780 | |
| 781 | /* allocate our information */ |
| 782 | |
| 783 | size = nr_sets * sizeof(*info->mtds); |
| 784 | info->mtds = kmalloc(size, GFP_KERNEL); |
| 785 | if (info->mtds == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 786 | dev_err(&pdev->dev, "failed to allocate mtd storage\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | err = -ENOMEM; |
| 788 | goto exit_error; |
| 789 | } |
| 790 | |
| 791 | memzero(info->mtds, size); |
| 792 | |
| 793 | /* initialise all possible chips */ |
| 794 | |
| 795 | nmtd = info->mtds; |
| 796 | |
| 797 | for (setno = 0; setno < nr_sets; setno++, nmtd++) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 798 | pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | s3c2410_nand_init_chip(info, nmtd, sets); |
| 801 | |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 802 | nmtd->scan_res = nand_scan_ident(&nmtd->mtd, |
| 803 | (sets) ? sets->nr_chips : 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
| 805 | if (nmtd->scan_res == 0) { |
Ben Dooks | 71d54f3 | 2008-04-15 11:36:19 +0100 | [diff] [blame] | 806 | s3c2410_nand_update_chip(info, nmtd); |
| 807 | nand_scan_tail(&nmtd->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | s3c2410_nand_add_partition(info, nmtd, sets); |
| 809 | } |
| 810 | |
| 811 | if (sets != NULL) |
| 812 | sets++; |
| 813 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 814 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 815 | if (allow_clk_stop(info)) { |
| 816 | dev_info(&pdev->dev, "clock idle support enabled\n"); |
| 817 | clk_disable(info->clk); |
| 818 | } |
| 819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | pr_debug("initialised ok\n"); |
| 821 | return 0; |
| 822 | |
| 823 | exit_error: |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 824 | s3c2410_nand_remove(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | if (err == 0) |
| 827 | err = -EINVAL; |
| 828 | return err; |
| 829 | } |
| 830 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 831 | /* PM Support */ |
| 832 | #ifdef CONFIG_PM |
| 833 | |
| 834 | static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm) |
| 835 | { |
| 836 | struct s3c2410_nand_info *info = platform_get_drvdata(dev); |
| 837 | |
| 838 | if (info) { |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 839 | info->save_sel = readl(info->sel_reg); |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 840 | |
| 841 | /* For the moment, we must ensure nFCE is high during |
| 842 | * the time we are suspended. This really should be |
| 843 | * handled by suspending the MTDs we are using, but |
| 844 | * that is currently not the case. */ |
| 845 | |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 846 | writel(info->save_sel | info->sel_bit, info->sel_reg); |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 847 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 848 | if (!allow_clk_stop(info)) |
| 849 | clk_disable(info->clk); |
| 850 | } |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int s3c24xx_nand_resume(struct platform_device *dev) |
| 856 | { |
| 857 | struct s3c2410_nand_info *info = platform_get_drvdata(dev); |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 858 | unsigned long sel; |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 859 | |
| 860 | if (info) { |
| 861 | clk_enable(info->clk); |
| 862 | s3c2410_nand_inithw(info, dev); |
| 863 | |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 864 | /* Restore the state of the nFCE line. */ |
| 865 | |
Ben Dooks | 0916083 | 2008-04-15 11:36:18 +0100 | [diff] [blame] | 866 | sel = readl(info->sel_reg); |
| 867 | sel &= ~info->sel_bit; |
| 868 | sel |= info->save_sel & info->sel_bit; |
| 869 | writel(sel, info->sel_reg); |
Ben Dooks | 03680b1 | 2007-11-19 23:28:07 +0000 | [diff] [blame] | 870 | |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 871 | if (allow_clk_stop(info)) |
| 872 | clk_disable(info->clk); |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | #else |
| 879 | #define s3c24xx_nand_suspend NULL |
| 880 | #define s3c24xx_nand_resume NULL |
| 881 | #endif |
| 882 | |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 883 | /* driver device registration */ |
| 884 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 885 | static int s3c2410_nand_probe(struct platform_device *dev) |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 886 | { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 887 | return s3c24xx_nand_probe(dev, TYPE_S3C2410); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 890 | static int s3c2440_nand_probe(struct platform_device *dev) |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 891 | { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 892 | return s3c24xx_nand_probe(dev, TYPE_S3C2440); |
| 893 | } |
| 894 | |
| 895 | static int s3c2412_nand_probe(struct platform_device *dev) |
| 896 | { |
| 897 | return s3c24xx_nand_probe(dev, TYPE_S3C2412); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 900 | static struct platform_driver s3c2410_nand_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | .probe = s3c2410_nand_probe, |
| 902 | .remove = s3c2410_nand_remove, |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 903 | .suspend = s3c24xx_nand_suspend, |
| 904 | .resume = s3c24xx_nand_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 905 | .driver = { |
| 906 | .name = "s3c2410-nand", |
| 907 | .owner = THIS_MODULE, |
| 908 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | }; |
| 910 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 911 | static struct platform_driver s3c2440_nand_driver = { |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 912 | .probe = s3c2440_nand_probe, |
| 913 | .remove = s3c2410_nand_remove, |
Ben Dooks | d1fef3c | 2006-06-19 09:29:38 +0100 | [diff] [blame] | 914 | .suspend = s3c24xx_nand_suspend, |
| 915 | .resume = s3c24xx_nand_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 916 | .driver = { |
| 917 | .name = "s3c2440-nand", |
| 918 | .owner = THIS_MODULE, |
| 919 | }, |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 920 | }; |
| 921 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 922 | static struct platform_driver s3c2412_nand_driver = { |
| 923 | .probe = s3c2412_nand_probe, |
| 924 | .remove = s3c2410_nand_remove, |
| 925 | .suspend = s3c24xx_nand_suspend, |
| 926 | .resume = s3c24xx_nand_resume, |
| 927 | .driver = { |
| 928 | .name = "s3c2412-nand", |
| 929 | .owner = THIS_MODULE, |
| 930 | }, |
| 931 | }; |
| 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | static int __init s3c2410_nand_init(void) |
| 934 | { |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 935 | printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n"); |
| 936 | |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 937 | platform_driver_register(&s3c2412_nand_driver); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 938 | platform_driver_register(&s3c2440_nand_driver); |
| 939 | return platform_driver_register(&s3c2410_nand_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static void __exit s3c2410_nand_exit(void) |
| 943 | { |
Ben Dooks | 2c06a08 | 2006-06-27 14:35:46 +0100 | [diff] [blame] | 944 | platform_driver_unregister(&s3c2412_nand_driver); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 945 | platform_driver_unregister(&s3c2440_nand_driver); |
| 946 | platform_driver_unregister(&s3c2410_nand_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | module_init(s3c2410_nand_init); |
| 950 | module_exit(s3c2410_nand_exit); |
| 951 | |
| 952 | MODULE_LICENSE("GPL"); |
| 953 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
Ben Dooks | a4f957f | 2005-06-20 12:48:25 +0100 | [diff] [blame] | 954 | MODULE_DESCRIPTION("S3C24XX MTD NAND driver"); |
Kay Sievers | 1ff1842 | 2008-04-18 13:44:27 -0700 | [diff] [blame] | 955 | MODULE_ALIAS("platform:s3c2410-nand"); |
| 956 | MODULE_ALIAS("platform:s3c2412-nand"); |
| 957 | MODULE_ALIAS("platform:s3c2440-nand"); |