Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 1 | /* |
Marcel Ziswiler | aea6856 | 2007-12-30 03:30:46 +0100 | [diff] [blame] | 2 | * drivers/mtd/nand/nand_util.c |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006 by Weiss-Electronic GmbH. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * @author: Guido Classen <clagix@gmail.com> |
| 8 | * @descr: NAND Flash support |
| 9 | * @references: borrowed heavily from Linux mtd-utils code: |
| 10 | * flash_eraseall.c by Arcom Control System Ltd |
| 11 | * nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com) |
| 12 | * and Thomas Gleixner (tglx@linutronix.de) |
| 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License version |
| 19 | * 2 as published by the Free Software Foundation. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 29 | * MA 02111-1307 USA |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 34 | #include <command.h> |
| 35 | #include <watchdog.h> |
| 36 | #include <malloc.h> |
Dirk Behme | 32d1f76 | 2007-08-02 17:42:08 +0200 | [diff] [blame] | 37 | #include <div64.h> |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 38 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 39 | |
| 40 | #include <asm/errno.h> |
| 41 | #include <linux/mtd/mtd.h> |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 42 | #include <nand.h> |
| 43 | #include <jffs2/jffs2.h> |
| 44 | |
| 45 | typedef struct erase_info erase_info_t; |
| 46 | typedef struct mtd_info mtd_info_t; |
| 47 | |
| 48 | /* support only for native endian JFFS2 */ |
| 49 | #define cpu_to_je16(x) (x) |
| 50 | #define cpu_to_je32(x) (x) |
| 51 | |
| 52 | /*****************************************************************************/ |
| 53 | static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * nand_erase_opts: - erase NAND flash with support for various options |
| 60 | * (jffs2 formating) |
| 61 | * |
| 62 | * @param meminfo NAND device to erase |
| 63 | * @param opts options, @see struct nand_erase_options |
| 64 | * @return 0 in case of success |
| 65 | * |
| 66 | * This code is ported from flash_eraseall.c from Linux mtd utils by |
| 67 | * Arcom Control System Ltd. |
| 68 | */ |
| 69 | int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts) |
| 70 | { |
| 71 | struct jffs2_unknown_node cleanmarker; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 72 | erase_info_t erase; |
| 73 | ulong erase_length; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 74 | int bbtest = 1; |
| 75 | int result; |
| 76 | int percent_complete = -1; |
| 77 | int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL; |
| 78 | const char *mtd_device = meminfo->name; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 79 | struct mtd_oob_ops oob_opts; |
| 80 | struct nand_chip *chip = meminfo->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 81 | |
| 82 | memset(&erase, 0, sizeof(erase)); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 83 | memset(&oob_opts, 0, sizeof(oob_opts)); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 84 | |
| 85 | erase.mtd = meminfo; |
| 86 | erase.len = meminfo->erasesize; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 87 | erase.addr = opts->offset; |
| 88 | erase_length = opts->length; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 89 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 90 | cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); |
| 91 | cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); |
| 92 | cleanmarker.totlen = cpu_to_je32(8); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 93 | |
| 94 | /* scrub option allows to erase badblock. To prevent internal |
| 95 | * check from erase() method, set block check method to dummy |
| 96 | * and disable bad block table while erasing. |
| 97 | */ |
| 98 | if (opts->scrub) { |
| 99 | struct nand_chip *priv_nand = meminfo->priv; |
| 100 | |
| 101 | nand_block_bad_old = priv_nand->block_bad; |
| 102 | priv_nand->block_bad = nand_block_bad_scrub; |
| 103 | /* we don't need the bad block table anymore... |
| 104 | * after scrub, there are no bad blocks left! |
| 105 | */ |
| 106 | if (priv_nand->bbt) { |
| 107 | kfree(priv_nand->bbt); |
| 108 | } |
| 109 | priv_nand->bbt = NULL; |
| 110 | } |
| 111 | |
Dirk Behme | 6f94ed0 | 2008-01-16 14:26:59 +0100 | [diff] [blame] | 112 | if (erase_length < meminfo->erasesize) { |
Stefan Roese | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 113 | printf("Warning: Erase size 0x%08lx smaller than one " \ |
Dirk Behme | 6f94ed0 | 2008-01-16 14:26:59 +0100 | [diff] [blame] | 114 | "erase block 0x%08x\n",erase_length, meminfo->erasesize); |
| 115 | printf(" Erasing 0x%08x instead\n", meminfo->erasesize); |
| 116 | erase_length = meminfo->erasesize; |
| 117 | } |
| 118 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 119 | for (; |
| 120 | erase.addr < opts->offset + erase_length; |
| 121 | erase.addr += meminfo->erasesize) { |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 122 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 123 | WATCHDOG_RESET (); |
| 124 | |
| 125 | if (!opts->scrub && bbtest) { |
| 126 | int ret = meminfo->block_isbad(meminfo, erase.addr); |
| 127 | if (ret > 0) { |
| 128 | if (!opts->quiet) |
| 129 | printf("\rSkipping bad block at " |
Wolfgang Denk | d5cf1a4 | 2006-10-12 11:43:47 +0200 | [diff] [blame] | 130 | "0x%08x " |
| 131 | " \n", |
| 132 | erase.addr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 133 | continue; |
| 134 | |
| 135 | } else if (ret < 0) { |
| 136 | printf("\n%s: MTD get bad block failed: %d\n", |
| 137 | mtd_device, |
| 138 | ret); |
| 139 | return -1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | result = meminfo->erase(meminfo, &erase); |
| 144 | if (result != 0) { |
| 145 | printf("\n%s: MTD Erase failure: %d\n", |
| 146 | mtd_device, result); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | /* format for JFFS2 ? */ |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 151 | if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) { |
| 152 | chip->ops.ooblen = 8; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 153 | chip->ops.datbuf = NULL; |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 154 | chip->ops.oobbuf = (uint8_t *)&cleanmarker; |
| 155 | chip->ops.ooboffs = 0; |
| 156 | chip->ops.mode = MTD_OOB_AUTO; |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 157 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 158 | result = meminfo->write_oob(meminfo, |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 159 | erase.addr, |
| 160 | &chip->ops); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 161 | if (result != 0) { |
| 162 | printf("\n%s: MTD writeoob failure: %d\n", |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 163 | mtd_device, result); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 164 | continue; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!opts->quiet) { |
Wolfgang Denk | 3688293 | 2007-08-13 21:57:53 +0200 | [diff] [blame] | 169 | unsigned long long n =(unsigned long long) |
Matthias Fuchs | 82714b9 | 2007-09-11 17:04:00 +0200 | [diff] [blame] | 170 | (erase.addr + meminfo->erasesize - opts->offset) |
| 171 | * 100; |
| 172 | int percent; |
| 173 | |
| 174 | do_div(n, erase_length); |
| 175 | percent = (int)n; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 176 | |
| 177 | /* output progress message only at whole percent |
| 178 | * steps to reduce the number of messages printed |
| 179 | * on (slow) serial consoles |
| 180 | */ |
| 181 | if (percent != percent_complete) { |
| 182 | percent_complete = percent; |
| 183 | |
| 184 | printf("\rErasing at 0x%x -- %3d%% complete.", |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 185 | erase.addr, percent); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 186 | |
| 187 | if (opts->jffs2 && result == 0) |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 188 | printf(" Cleanmarker written at 0x%x.", |
| 189 | erase.addr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | if (!opts->quiet) |
| 194 | printf("\n"); |
| 195 | |
| 196 | if (nand_block_bad_old) { |
| 197 | struct nand_chip *priv_nand = meminfo->priv; |
| 198 | |
| 199 | priv_nand->block_bad = nand_block_bad_old; |
| 200 | priv_nand->scan_bbt(meminfo); |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 206 | /* XXX U-BOOT XXX */ |
| 207 | #if 0 |
| 208 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 209 | #define MAX_PAGE_SIZE 2048 |
| 210 | #define MAX_OOB_SIZE 64 |
| 211 | |
| 212 | /* |
| 213 | * buffer array used for writing data |
| 214 | */ |
| 215 | static unsigned char data_buf[MAX_PAGE_SIZE]; |
| 216 | static unsigned char oob_buf[MAX_OOB_SIZE]; |
| 217 | |
| 218 | /* OOB layouts to pass into the kernel as default */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 219 | static struct nand_ecclayout none_ecclayout = { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 220 | .useecc = MTD_NANDECC_OFF, |
| 221 | }; |
| 222 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 223 | static struct nand_ecclayout jffs2_ecclayout = { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 224 | .useecc = MTD_NANDECC_PLACE, |
| 225 | .eccbytes = 6, |
| 226 | .eccpos = { 0, 1, 2, 3, 6, 7 } |
| 227 | }; |
| 228 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 229 | static struct nand_ecclayout yaffs_ecclayout = { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 230 | .useecc = MTD_NANDECC_PLACE, |
| 231 | .eccbytes = 6, |
| 232 | .eccpos = { 8, 9, 10, 13, 14, 15} |
| 233 | }; |
| 234 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 235 | static struct nand_ecclayout autoplace_ecclayout = { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 236 | .useecc = MTD_NANDECC_AUTOPLACE |
| 237 | }; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 238 | #endif |
| 239 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 240 | /* XXX U-BOOT XXX */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 241 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
| 242 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 243 | /****************************************************************************** |
| 244 | * Support for locking / unlocking operations of some NAND devices |
| 245 | *****************************************************************************/ |
| 246 | |
| 247 | #define NAND_CMD_LOCK 0x2a |
| 248 | #define NAND_CMD_LOCK_TIGHT 0x2c |
| 249 | #define NAND_CMD_UNLOCK1 0x23 |
| 250 | #define NAND_CMD_UNLOCK2 0x24 |
| 251 | #define NAND_CMD_LOCK_STATUS 0x7a |
| 252 | |
| 253 | /** |
| 254 | * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT |
| 255 | * state |
| 256 | * |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 257 | * @param mtd nand mtd instance |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 258 | * @param tight bring device in lock tight mode |
| 259 | * |
| 260 | * @return 0 on success, -1 in case of error |
| 261 | * |
| 262 | * The lock / lock-tight command only applies to the whole chip. To get some |
| 263 | * parts of the chip lock and others unlocked use the following sequence: |
| 264 | * |
| 265 | * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin) |
| 266 | * - Call nand_unlock() once for each consecutive area to be unlocked |
| 267 | * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1) |
| 268 | * |
| 269 | * If the device is in lock-tight state software can't change the |
| 270 | * current active lock/unlock state of all pages. nand_lock() / nand_unlock() |
| 271 | * calls will fail. It is only posible to leave lock-tight state by |
| 272 | * an hardware signal (low pulse on _WP pin) or by power down. |
| 273 | */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 274 | int nand_lock(struct mtd_info *mtd, int tight) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 275 | { |
| 276 | int ret = 0; |
| 277 | int status; |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 278 | struct nand_chip *chip = mtd->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 279 | |
| 280 | /* select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 281 | chip->select_chip(mtd, 0); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 282 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 283 | chip->cmdfunc(mtd, |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 284 | (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK), |
| 285 | -1, -1); |
| 286 | |
| 287 | /* call wait ready function */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 288 | status = chip->waitfunc(mtd, chip); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 289 | |
| 290 | /* see if device thinks it succeeded */ |
| 291 | if (status & 0x01) { |
| 292 | ret = -1; |
| 293 | } |
| 294 | |
| 295 | /* de-select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 296 | chip->select_chip(mtd, -1); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * nand_get_lock_status: - query current lock state from one page of NAND |
| 302 | * flash |
| 303 | * |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 304 | * @param mtd nand mtd instance |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 305 | * @param offset page address to query (muss be page aligned!) |
| 306 | * |
| 307 | * @return -1 in case of error |
| 308 | * >0 lock status: |
| 309 | * bitfield with the following combinations: |
| 310 | * NAND_LOCK_STATUS_TIGHT: page in tight state |
| 311 | * NAND_LOCK_STATUS_LOCK: page locked |
| 312 | * NAND_LOCK_STATUS_UNLOCK: page unlocked |
| 313 | * |
| 314 | */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 315 | int nand_get_lock_status(struct mtd_info *mtd, ulong offset) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 316 | { |
| 317 | int ret = 0; |
| 318 | int chipnr; |
| 319 | int page; |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 320 | struct nand_chip *chip = mtd->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 321 | |
| 322 | /* select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 323 | chipnr = (int)(offset >> chip->chip_shift); |
| 324 | chip->select_chip(mtd, chipnr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 325 | |
| 326 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 327 | if ((offset & (mtd->writesize - 1)) != 0) { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 328 | printf ("nand_get_lock_status: " |
| 329 | "Start address must be beginning of " |
| 330 | "nand page!\n"); |
| 331 | ret = -1; |
| 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | /* check the Lock Status */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 336 | page = (int)(offset >> chip->page_shift); |
| 337 | chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 338 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 339 | ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 340 | | NAND_LOCK_STATUS_LOCK |
| 341 | | NAND_LOCK_STATUS_UNLOCK); |
| 342 | |
| 343 | out: |
| 344 | /* de-select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 345 | chip->select_chip(mtd, -1); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * nand_unlock: - Unlock area of NAND pages |
| 351 | * only one consecutive area can be unlocked at one time! |
| 352 | * |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 353 | * @param mtd nand mtd instance |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 354 | * @param start start byte address |
| 355 | * @param length number of bytes to unlock (must be a multiple of |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 356 | * page size nand->writesize) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 357 | * |
| 358 | * @return 0 on success, -1 in case of error |
| 359 | */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 360 | int nand_unlock(struct mtd_info *mtd, ulong start, ulong length) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 361 | { |
| 362 | int ret = 0; |
| 363 | int chipnr; |
| 364 | int status; |
| 365 | int page; |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 366 | struct nand_chip *chip = mtd->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 367 | printf ("nand_unlock: start: %08x, length: %d!\n", |
| 368 | (int)start, (int)length); |
| 369 | |
| 370 | /* select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 371 | chipnr = (int)(start >> chip->chip_shift); |
| 372 | chip->select_chip(mtd, chipnr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 373 | |
| 374 | /* check the WP bit */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 375 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 376 | if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 377 | printf ("nand_unlock: Device is write protected!\n"); |
| 378 | ret = -1; |
| 379 | goto out; |
| 380 | } |
| 381 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 382 | if ((start & (mtd->erasesize - 1)) != 0) { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 383 | printf ("nand_unlock: Start address must be beginning of " |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 384 | "nand block!\n"); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 385 | ret = -1; |
| 386 | goto out; |
| 387 | } |
| 388 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 389 | if (length == 0 || (length & (mtd->erasesize - 1)) != 0) { |
| 390 | printf ("nand_unlock: Length must be a multiple of nand block " |
| 391 | "size %08x!\n", mtd->erasesize); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 392 | ret = -1; |
| 393 | goto out; |
| 394 | } |
| 395 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 396 | /* |
| 397 | * Set length so that the last address is set to the |
| 398 | * starting address of the last block |
| 399 | */ |
| 400 | length -= mtd->erasesize; |
| 401 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 402 | /* submit address of first page to unlock */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 403 | page = (int)(start >> chip->page_shift); |
| 404 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 405 | |
| 406 | /* submit ADDRESS of LAST page to unlock */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 407 | page += (int)(length >> chip->page_shift); |
| 408 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 409 | |
| 410 | /* call wait ready function */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 411 | status = chip->waitfunc(mtd, chip); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 412 | /* see if device thinks it succeeded */ |
| 413 | if (status & 0x01) { |
| 414 | /* there was an error */ |
| 415 | ret = -1; |
| 416 | goto out; |
| 417 | } |
| 418 | |
| 419 | out: |
| 420 | /* de-select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 421 | chip->select_chip(mtd, -1); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 422 | return ret; |
| 423 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 424 | #endif |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 425 | |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 426 | /** |
| 427 | * get_len_incl_bad |
| 428 | * |
| 429 | * Check if length including bad blocks fits into device. |
| 430 | * |
| 431 | * @param nand NAND device |
| 432 | * @param offset offset in flash |
| 433 | * @param length image length |
| 434 | * @return image length including bad blocks |
| 435 | */ |
| 436 | static size_t get_len_incl_bad (nand_info_t *nand, size_t offset, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 437 | const size_t length) |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 438 | { |
| 439 | size_t len_incl_bad = 0; |
| 440 | size_t len_excl_bad = 0; |
| 441 | size_t block_len; |
| 442 | |
| 443 | while (len_excl_bad < length) { |
| 444 | block_len = nand->erasesize - (offset & (nand->erasesize - 1)); |
| 445 | |
| 446 | if (!nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) |
| 447 | len_excl_bad += block_len; |
| 448 | |
| 449 | len_incl_bad += block_len; |
| 450 | offset += block_len; |
| 451 | |
| 452 | if ((offset + len_incl_bad) >= nand->size) |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | return len_incl_bad; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * nand_write_skip_bad: |
| 461 | * |
| 462 | * Write image to NAND flash. |
| 463 | * Blocks that are marked bad are skipped and the is written to the next |
| 464 | * block instead as long as the image is short enough to fit even after |
| 465 | * skipping the bad blocks. |
| 466 | * |
| 467 | * @param nand NAND device |
| 468 | * @param offset offset in flash |
| 469 | * @param length buffer length |
| 470 | * @param buf buffer to read from |
| 471 | * @return 0 in case of success |
| 472 | */ |
| 473 | int nand_write_skip_bad(nand_info_t *nand, size_t offset, size_t *length, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 474 | u_char *buffer) |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 475 | { |
| 476 | int rval; |
| 477 | size_t left_to_write = *length; |
| 478 | size_t len_incl_bad; |
| 479 | u_char *p_buffer = buffer; |
| 480 | |
| 481 | /* Reject writes, which are not page aligned */ |
| 482 | if ((offset & (nand->writesize - 1)) != 0 || |
| 483 | (*length & (nand->writesize - 1)) != 0) { |
| 484 | printf ("Attempt to write non page aligned data\n"); |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
| 488 | len_incl_bad = get_len_incl_bad (nand, offset, *length); |
| 489 | |
| 490 | if ((offset + len_incl_bad) >= nand->size) { |
| 491 | printf ("Attempt to write outside the flash area\n"); |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | if (len_incl_bad == *length) { |
| 496 | rval = nand_write (nand, offset, length, buffer); |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 497 | if (rval != 0) |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 498 | printf ("NAND write to offset %zx failed %d\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 499 | offset, rval); |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 500 | |
| 501 | return rval; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | while (left_to_write > 0) { |
| 505 | size_t block_offset = offset & (nand->erasesize - 1); |
| 506 | size_t write_size; |
| 507 | |
| 508 | if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) { |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 509 | printf ("Skip bad block 0x%08zx\n", |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 510 | offset & ~(nand->erasesize - 1)); |
| 511 | offset += nand->erasesize - block_offset; |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | if (left_to_write < (nand->erasesize - block_offset)) |
| 516 | write_size = left_to_write; |
| 517 | else |
| 518 | write_size = nand->erasesize - block_offset; |
| 519 | |
| 520 | rval = nand_write (nand, offset, &write_size, p_buffer); |
| 521 | if (rval != 0) { |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 522 | printf ("NAND write to offset %zx failed %d\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 523 | offset, rval); |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 524 | *length -= left_to_write; |
| 525 | return rval; |
| 526 | } |
| 527 | |
| 528 | left_to_write -= write_size; |
| 529 | offset += write_size; |
| 530 | p_buffer += write_size; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * nand_read_skip_bad: |
| 538 | * |
| 539 | * Read image from NAND flash. |
| 540 | * Blocks that are marked bad are skipped and the next block is readen |
| 541 | * instead as long as the image is short enough to fit even after skipping the |
| 542 | * bad blocks. |
| 543 | * |
| 544 | * @param nand NAND device |
| 545 | * @param offset offset in flash |
| 546 | * @param length buffer length, on return holds remaining bytes to read |
| 547 | * @param buffer buffer to write to |
| 548 | * @return 0 in case of success |
| 549 | */ |
| 550 | int nand_read_skip_bad(nand_info_t *nand, size_t offset, size_t *length, |
| 551 | u_char *buffer) |
| 552 | { |
| 553 | int rval; |
| 554 | size_t left_to_read = *length; |
| 555 | size_t len_incl_bad; |
| 556 | u_char *p_buffer = buffer; |
| 557 | |
| 558 | len_incl_bad = get_len_incl_bad (nand, offset, *length); |
| 559 | |
| 560 | if ((offset + len_incl_bad) >= nand->size) { |
| 561 | printf ("Attempt to read outside the flash area\n"); |
| 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | if (len_incl_bad == *length) { |
| 566 | rval = nand_read (nand, offset, length, buffer); |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 567 | if (rval != 0) |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 568 | printf ("NAND read from offset %zx failed %d\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 569 | offset, rval); |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 570 | |
| 571 | return rval; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | while (left_to_read > 0) { |
| 575 | size_t block_offset = offset & (nand->erasesize - 1); |
| 576 | size_t read_length; |
| 577 | |
| 578 | if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) { |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 579 | printf ("Skipping bad block 0x%08zx\n", |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 580 | offset & ~(nand->erasesize - 1)); |
| 581 | offset += nand->erasesize - block_offset; |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | if (left_to_read < (nand->erasesize - block_offset)) |
| 586 | read_length = left_to_read; |
| 587 | else |
| 588 | read_length = nand->erasesize - block_offset; |
| 589 | |
| 590 | rval = nand_read (nand, offset, &read_length, p_buffer); |
| 591 | if (rval != 0) { |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 592 | printf ("NAND read from offset %zx failed %d\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 593 | offset, rval); |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 594 | *length -= left_to_read; |
| 595 | return rval; |
| 596 | } |
| 597 | |
| 598 | left_to_read -= read_length; |
| 599 | offset += read_length; |
| 600 | p_buffer += read_length; |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |