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 | * |
Ben Gardiner | 34dd567 | 2011-06-14 16:35:06 -0400 | [diff] [blame] | 14 | * Copyright (C) 2008 Nokia Corporation: drop_ffs() function by |
| 15 | * Artem Bityutskiy <dedekind1@gmail.com> from mtd-utils |
| 16 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <common.h> |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 21 | #include <command.h> |
| 22 | #include <watchdog.h> |
| 23 | #include <malloc.h> |
Dirk Behme | 32d1f76 | 2007-08-02 17:42:08 +0200 | [diff] [blame] | 24 | #include <div64.h> |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 25 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 26 | #include <asm/errno.h> |
| 27 | #include <linux/mtd/mtd.h> |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 28 | #include <nand.h> |
| 29 | #include <jffs2/jffs2.h> |
| 30 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 31 | typedef struct erase_info erase_info_t; |
| 32 | typedef struct mtd_info mtd_info_t; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 33 | |
| 34 | /* support only for native endian JFFS2 */ |
| 35 | #define cpu_to_je16(x) (x) |
| 36 | #define cpu_to_je32(x) (x) |
| 37 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 38 | /** |
| 39 | * nand_erase_opts: - erase NAND flash with support for various options |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 40 | * (jffs2 formatting) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 41 | * |
| 42 | * @param meminfo NAND device to erase |
| 43 | * @param opts options, @see struct nand_erase_options |
| 44 | * @return 0 in case of success |
| 45 | * |
| 46 | * This code is ported from flash_eraseall.c from Linux mtd utils by |
| 47 | * Arcom Control System Ltd. |
| 48 | */ |
| 49 | int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts) |
| 50 | { |
| 51 | struct jffs2_unknown_node cleanmarker; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 52 | erase_info_t erase; |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 53 | unsigned long erase_length, erased_length; /* in blocks */ |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 54 | int result; |
| 55 | int percent_complete = -1; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 56 | const char *mtd_device = meminfo->name; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 57 | struct mtd_oob_ops oob_opts; |
| 58 | struct nand_chip *chip = meminfo->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 59 | |
Benoît Thébaudeau | ad5d6ee | 2012-11-05 10:16:15 +0000 | [diff] [blame] | 60 | if ((opts->offset & (meminfo->erasesize - 1)) != 0) { |
| 61 | printf("Attempt to erase non block-aligned data\n"); |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 62 | return -1; |
| 63 | } |
| 64 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 65 | memset(&erase, 0, sizeof(erase)); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 66 | memset(&oob_opts, 0, sizeof(oob_opts)); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 67 | |
| 68 | erase.mtd = meminfo; |
| 69 | erase.len = meminfo->erasesize; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 70 | erase.addr = opts->offset; |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 71 | erase_length = lldiv(opts->length + meminfo->erasesize - 1, |
| 72 | meminfo->erasesize); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 73 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 74 | cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 75 | cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 76 | cleanmarker.totlen = cpu_to_je32(8); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 77 | |
| 78 | /* scrub option allows to erase badblock. To prevent internal |
| 79 | * check from erase() method, set block check method to dummy |
| 80 | * and disable bad block table while erasing. |
| 81 | */ |
| 82 | if (opts->scrub) { |
Marek Vasut | 971d9a1 | 2011-09-12 06:04:06 +0200 | [diff] [blame] | 83 | erase.scrub = opts->scrub; |
| 84 | /* |
| 85 | * We don't need the bad block table anymore... |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 86 | * after scrub, there are no bad blocks left! |
| 87 | */ |
Marek Vasut | 971d9a1 | 2011-09-12 06:04:06 +0200 | [diff] [blame] | 88 | if (chip->bbt) { |
| 89 | kfree(chip->bbt); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 90 | } |
Marek Vasut | 971d9a1 | 2011-09-12 06:04:06 +0200 | [diff] [blame] | 91 | chip->bbt = NULL; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 94 | for (erased_length = 0; |
| 95 | erased_length < erase_length; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 96 | erase.addr += meminfo->erasesize) { |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 97 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 98 | WATCHDOG_RESET(); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 99 | |
Heiko Schocher | 0ae98b4 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 100 | if (opts->lim && (erase.addr >= (opts->offset + opts->lim))) { |
| 101 | puts("Size of erase exceeds limit\n"); |
| 102 | return -EFBIG; |
| 103 | } |
Masahiro Yamada | 7580d58 | 2013-07-12 10:53:37 +0900 | [diff] [blame] | 104 | if (!opts->scrub) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 105 | int ret = mtd_block_isbad(meminfo, erase.addr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 106 | if (ret > 0) { |
| 107 | if (!opts->quiet) |
| 108 | printf("\rSkipping bad block at " |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 109 | "0x%08llx " |
Wolfgang Denk | d5cf1a4 | 2006-10-12 11:43:47 +0200 | [diff] [blame] | 110 | " \n", |
| 111 | erase.addr); |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 112 | |
| 113 | if (!opts->spread) |
| 114 | erased_length++; |
| 115 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 116 | continue; |
| 117 | |
| 118 | } else if (ret < 0) { |
| 119 | printf("\n%s: MTD get bad block failed: %d\n", |
| 120 | mtd_device, |
| 121 | ret); |
| 122 | return -1; |
| 123 | } |
| 124 | } |
| 125 | |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 126 | erased_length++; |
| 127 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 128 | result = mtd_erase(meminfo, &erase); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 129 | if (result != 0) { |
| 130 | printf("\n%s: MTD Erase failure: %d\n", |
| 131 | mtd_device, result); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | /* format for JFFS2 ? */ |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 136 | if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 137 | struct mtd_oob_ops ops; |
| 138 | ops.ooblen = 8; |
| 139 | ops.datbuf = NULL; |
| 140 | ops.oobbuf = (uint8_t *)&cleanmarker; |
| 141 | ops.ooboffs = 0; |
| 142 | ops.mode = MTD_OPS_AUTO_OOB; |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 143 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 144 | result = mtd_write_oob(meminfo, |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame^] | 145 | erase.addr, |
| 146 | &ops); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 147 | if (result != 0) { |
| 148 | printf("\n%s: MTD writeoob failure: %d\n", |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 149 | mtd_device, result); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 150 | continue; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | if (!opts->quiet) { |
Scott Wood | 1b5cd51 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 155 | unsigned long long n = erased_length * 100ULL; |
Matthias Fuchs | 82714b9 | 2007-09-11 17:04:00 +0200 | [diff] [blame] | 156 | int percent; |
| 157 | |
| 158 | do_div(n, erase_length); |
| 159 | percent = (int)n; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 160 | |
| 161 | /* output progress message only at whole percent |
| 162 | * steps to reduce the number of messages printed |
| 163 | * on (slow) serial consoles |
| 164 | */ |
| 165 | if (percent != percent_complete) { |
| 166 | percent_complete = percent; |
| 167 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 168 | printf("\rErasing at 0x%llx -- %3d%% complete.", |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 169 | erase.addr, percent); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 170 | |
| 171 | if (opts->jffs2 && result == 0) |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 172 | printf(" Cleanmarker written at 0x%llx.", |
Scott Wood | d50ad35 | 2008-10-29 14:20:26 -0500 | [diff] [blame] | 173 | erase.addr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | if (!opts->quiet) |
| 178 | printf("\n"); |
| 179 | |
Marek Vasut | 971d9a1 | 2011-09-12 06:04:06 +0200 | [diff] [blame] | 180 | if (opts->scrub) |
| 181 | chip->scan_bbt(meminfo); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 186 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
| 187 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 188 | /****************************************************************************** |
| 189 | * Support for locking / unlocking operations of some NAND devices |
| 190 | *****************************************************************************/ |
| 191 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 192 | /** |
| 193 | * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT |
| 194 | * state |
| 195 | * |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 196 | * @param mtd nand mtd instance |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 197 | * @param tight bring device in lock tight mode |
| 198 | * |
| 199 | * @return 0 on success, -1 in case of error |
| 200 | * |
| 201 | * The lock / lock-tight command only applies to the whole chip. To get some |
| 202 | * parts of the chip lock and others unlocked use the following sequence: |
| 203 | * |
| 204 | * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin) |
| 205 | * - Call nand_unlock() once for each consecutive area to be unlocked |
| 206 | * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1) |
| 207 | * |
| 208 | * If the device is in lock-tight state software can't change the |
| 209 | * current active lock/unlock state of all pages. nand_lock() / nand_unlock() |
| 210 | * calls will fail. It is only posible to leave lock-tight state by |
| 211 | * an hardware signal (low pulse on _WP pin) or by power down. |
| 212 | */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 213 | int nand_lock(struct mtd_info *mtd, int tight) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 214 | { |
| 215 | int ret = 0; |
| 216 | int status; |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 217 | struct nand_chip *chip = mtd->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 218 | |
| 219 | /* select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 220 | chip->select_chip(mtd, 0); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 221 | |
Joe Hershberger | 8b177d4 | 2013-02-08 09:27:19 +0000 | [diff] [blame] | 222 | /* check the Lock Tight Status */ |
| 223 | chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, 0); |
| 224 | if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) { |
| 225 | printf("nand_lock: Device is locked tight!\n"); |
| 226 | ret = -1; |
| 227 | goto out; |
| 228 | } |
| 229 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 230 | chip->cmdfunc(mtd, |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 231 | (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK), |
| 232 | -1, -1); |
| 233 | |
| 234 | /* call wait ready function */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 235 | status = chip->waitfunc(mtd, chip); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 236 | |
| 237 | /* see if device thinks it succeeded */ |
| 238 | if (status & 0x01) { |
| 239 | ret = -1; |
| 240 | } |
| 241 | |
Joe Hershberger | 8b177d4 | 2013-02-08 09:27:19 +0000 | [diff] [blame] | 242 | out: |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 243 | /* de-select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 244 | chip->select_chip(mtd, -1); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * nand_get_lock_status: - query current lock state from one page of NAND |
| 250 | * flash |
| 251 | * |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 252 | * @param mtd nand mtd instance |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 253 | * @param offset page address to query (must be page-aligned!) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 254 | * |
| 255 | * @return -1 in case of error |
| 256 | * >0 lock status: |
| 257 | * bitfield with the following combinations: |
| 258 | * NAND_LOCK_STATUS_TIGHT: page in tight state |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 259 | * NAND_LOCK_STATUS_UNLOCK: page unlocked |
| 260 | * |
| 261 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 2511ba0 | 2009-05-16 14:27:40 +0200 | [diff] [blame] | 262 | int nand_get_lock_status(struct mtd_info *mtd, loff_t offset) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 263 | { |
| 264 | int ret = 0; |
| 265 | int chipnr; |
| 266 | int page; |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 267 | struct nand_chip *chip = mtd->priv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 268 | |
| 269 | /* select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 270 | chipnr = (int)(offset >> chip->chip_shift); |
| 271 | chip->select_chip(mtd, chipnr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 272 | |
| 273 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 274 | if ((offset & (mtd->writesize - 1)) != 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 275 | printf("nand_get_lock_status: " |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 276 | "Start address must be beginning of " |
| 277 | "nand page!\n"); |
| 278 | ret = -1; |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | /* check the Lock Status */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 283 | page = (int)(offset >> chip->page_shift); |
| 284 | chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 285 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 286 | ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 287 | | NAND_LOCK_STATUS_UNLOCK); |
| 288 | |
| 289 | out: |
| 290 | /* de-select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 291 | chip->select_chip(mtd, -1); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * nand_unlock: - Unlock area of NAND pages |
| 297 | * only one consecutive area can be unlocked at one time! |
| 298 | * |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 299 | * @param mtd nand mtd instance |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 300 | * @param start start byte address |
| 301 | * @param length number of bytes to unlock (must be a multiple of |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 302 | * page size nand->writesize) |
Joe Hershberger | cccf595 | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 303 | * @param allexcept if set, unlock everything not selected |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 304 | * |
| 305 | * @return 0 on success, -1 in case of error |
| 306 | */ |
Joe Hershberger | dfa8ba4 | 2012-08-22 16:49:43 -0500 | [diff] [blame] | 307 | int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length, |
| 308 | int allexcept) |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 309 | { |
| 310 | int ret = 0; |
| 311 | int chipnr; |
| 312 | int status; |
| 313 | int page; |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 314 | struct nand_chip *chip = mtd->priv; |
Joe Hershberger | cccf595 | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 315 | |
Joe Hershberger | dfa8ba4 | 2012-08-22 16:49:43 -0500 | [diff] [blame] | 316 | debug("nand_unlock%s: start: %08llx, length: %d!\n", |
Joe Hershberger | cccf595 | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 317 | allexcept ? " (allexcept)" : "", start, length); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 318 | |
| 319 | /* select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 320 | chipnr = (int)(start >> chip->chip_shift); |
| 321 | chip->select_chip(mtd, chipnr); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 322 | |
| 323 | /* check the WP bit */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 324 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 325 | if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 326 | printf("nand_unlock: Device is write protected!\n"); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 327 | ret = -1; |
| 328 | goto out; |
| 329 | } |
| 330 | |
Joe Hershberger | 8b177d4 | 2013-02-08 09:27:19 +0000 | [diff] [blame] | 331 | /* check the Lock Tight Status */ |
| 332 | page = (int)(start >> chip->page_shift); |
| 333 | chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask); |
| 334 | if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) { |
| 335 | printf("nand_unlock: Device is locked tight!\n"); |
| 336 | ret = -1; |
| 337 | goto out; |
| 338 | } |
| 339 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 340 | if ((start & (mtd->erasesize - 1)) != 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 341 | printf("nand_unlock: Start address must be beginning of " |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 342 | "nand block!\n"); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 343 | ret = -1; |
| 344 | goto out; |
| 345 | } |
| 346 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 347 | if (length == 0 || (length & (mtd->erasesize - 1)) != 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 348 | printf("nand_unlock: Length must be a multiple of nand block " |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 349 | "size %08x!\n", mtd->erasesize); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 350 | ret = -1; |
| 351 | goto out; |
| 352 | } |
| 353 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 354 | /* |
| 355 | * Set length so that the last address is set to the |
| 356 | * starting address of the last block |
| 357 | */ |
| 358 | length -= mtd->erasesize; |
| 359 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 360 | /* submit address of first page to unlock */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 361 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 362 | |
| 363 | /* submit ADDRESS of LAST page to unlock */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 364 | page += (int)(length >> chip->page_shift); |
Joe Hershberger | cccf595 | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * Page addresses for unlocking are supposed to be block-aligned. |
| 368 | * At least some NAND chips use the low bit to indicate that the |
| 369 | * page range should be inverted. |
| 370 | */ |
| 371 | if (allexcept) |
| 372 | page |= 1; |
| 373 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 374 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 375 | |
| 376 | /* call wait ready function */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 377 | status = chip->waitfunc(mtd, chip); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 378 | /* see if device thinks it succeeded */ |
| 379 | if (status & 0x01) { |
| 380 | /* there was an error */ |
| 381 | ret = -1; |
| 382 | goto out; |
| 383 | } |
| 384 | |
| 385 | out: |
| 386 | /* de-select the NAND device */ |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 387 | chip->select_chip(mtd, -1); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 388 | return ret; |
| 389 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 390 | #endif |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 391 | |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 392 | /** |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 393 | * check_skip_len |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 394 | * |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 395 | * Check if there are any bad blocks, and whether length including bad |
| 396 | * blocks fits into device |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 397 | * |
| 398 | * @param nand NAND device |
| 399 | * @param offset offset in flash |
| 400 | * @param length image length |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 401 | * @param used length of flash needed for the requested length |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 402 | * @return 0 if the image fits and there are no bad blocks |
| 403 | * 1 if the image fits, but there are bad blocks |
| 404 | * -1 if the image does not fit |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 405 | */ |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 406 | static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length, |
| 407 | size_t *used) |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 408 | { |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 409 | size_t len_excl_bad = 0; |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 410 | int ret = 0; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 411 | |
| 412 | while (len_excl_bad < length) { |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 413 | size_t block_len, block_off; |
| 414 | loff_t block_start; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 415 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 416 | if (offset >= nand->size) |
| 417 | return -1; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 418 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 419 | block_start = offset & ~(loff_t)(nand->erasesize - 1); |
| 420 | block_off = offset & (nand->erasesize - 1); |
| 421 | block_len = nand->erasesize - block_off; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 422 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 423 | if (!nand_block_isbad(nand, block_start)) |
| 424 | len_excl_bad += block_len; |
| 425 | else |
| 426 | ret = 1; |
| 427 | |
| 428 | offset += block_len; |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 429 | *used += block_len; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 432 | /* If the length is not a multiple of block_len, adjust. */ |
| 433 | if (len_excl_bad > length) |
| 434 | *used -= (len_excl_bad - length); |
| 435 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 436 | return ret; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 437 | } |
Ben Gardiner | 34dd567 | 2011-06-14 16:35:06 -0400 | [diff] [blame] | 438 | |
| 439 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
| 440 | static size_t drop_ffs(const nand_info_t *nand, const u_char *buf, |
| 441 | const size_t *len) |
| 442 | { |
htbegin | 610e855 | 2013-03-01 23:00:34 +0000 | [diff] [blame] | 443 | size_t l = *len; |
| 444 | ssize_t i; |
Ben Gardiner | 34dd567 | 2011-06-14 16:35:06 -0400 | [diff] [blame] | 445 | |
| 446 | for (i = l - 1; i >= 0; i--) |
| 447 | if (buf[i] != 0xFF) |
| 448 | break; |
| 449 | |
| 450 | /* The resulting length must be aligned to the minimum flash I/O size */ |
| 451 | l = i + 1; |
| 452 | l = (l + nand->writesize - 1) / nand->writesize; |
| 453 | l *= nand->writesize; |
| 454 | |
| 455 | /* |
| 456 | * since the input length may be unaligned, prevent access past the end |
| 457 | * of the buffer |
| 458 | */ |
| 459 | return min(l, *len); |
| 460 | } |
| 461 | #endif |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 462 | |
| 463 | /** |
| 464 | * nand_write_skip_bad: |
| 465 | * |
| 466 | * Write image to NAND flash. |
| 467 | * Blocks that are marked bad are skipped and the is written to the next |
| 468 | * block instead as long as the image is short enough to fit even after |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 469 | * skipping the bad blocks. Due to bad blocks we may not be able to |
| 470 | * perform the requested write. In the case where the write would |
| 471 | * extend beyond the end of the NAND device, both length and actual (if |
| 472 | * not NULL) are set to 0. In the case where the write would extend |
| 473 | * beyond the limit we are passed, length is set to 0 and actual is set |
| 474 | * to the required length. |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 475 | * |
| 476 | * @param nand NAND device |
| 477 | * @param offset offset in flash |
| 478 | * @param length buffer length |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 479 | * @param actual set to size required to write length worth of |
| 480 | * buffer or 0 on error, if not NULL |
| 481 | * @param lim maximum size that actual may be in order to not |
| 482 | * exceed the buffer |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 483 | * @param buffer buffer to read from |
Ben Gardiner | 1caafbb | 2011-05-24 10:18:35 -0400 | [diff] [blame] | 484 | * @param flags flags modifying the behaviour of the write to NAND |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 485 | * @return 0 in case of success |
| 486 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 2511ba0 | 2009-05-16 14:27:40 +0200 | [diff] [blame] | 487 | int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length, |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 488 | size_t *actual, loff_t lim, u_char *buffer, int flags) |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 489 | { |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 490 | int rval = 0, blocksize; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 491 | size_t left_to_write = *length; |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 492 | size_t used_for_write = 0; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 493 | u_char *p_buffer = buffer; |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 494 | int need_skip; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 495 | |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 496 | if (actual) |
| 497 | *actual = 0; |
| 498 | |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 499 | #ifdef CONFIG_CMD_NAND_YAFFS |
Ben Gardiner | 1caafbb | 2011-05-24 10:18:35 -0400 | [diff] [blame] | 500 | if (flags & WITH_YAFFS_OOB) { |
Ben Gardiner | 871dea4 | 2011-06-14 16:35:05 -0400 | [diff] [blame] | 501 | if (flags & ~WITH_YAFFS_OOB) |
| 502 | return -EINVAL; |
| 503 | |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 504 | int pages; |
| 505 | pages = nand->erasesize / nand->writesize; |
| 506 | blocksize = (pages * nand->oobsize) + nand->erasesize; |
| 507 | if (*length % (nand->writesize + nand->oobsize)) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 508 | printf("Attempt to write incomplete page" |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 509 | " in yaffs mode\n"); |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | } else |
| 513 | #endif |
| 514 | { |
| 515 | blocksize = nand->erasesize; |
| 516 | } |
| 517 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 518 | /* |
| 519 | * nand_write() handles unaligned, partial page writes. |
| 520 | * |
| 521 | * We allow length to be unaligned, for convenience in |
| 522 | * using the $filesize variable. |
| 523 | * |
| 524 | * However, starting at an unaligned offset makes the |
| 525 | * semantics of bad block skipping ambiguous (really, |
| 526 | * you should only start a block skipping access at a |
| 527 | * partition boundary). So don't try to handle that. |
| 528 | */ |
| 529 | if ((offset & (nand->writesize - 1)) != 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 530 | printf("Attempt to write non page-aligned data\n"); |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 531 | *length = 0; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 532 | return -EINVAL; |
| 533 | } |
| 534 | |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 535 | need_skip = check_skip_len(nand, offset, *length, &used_for_write); |
| 536 | |
| 537 | if (actual) |
| 538 | *actual = used_for_write; |
| 539 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 540 | if (need_skip < 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 541 | printf("Attempt to write outside the flash area\n"); |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 542 | *length = 0; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 543 | return -EINVAL; |
| 544 | } |
| 545 | |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 546 | if (used_for_write > lim) { |
| 547 | puts("Size of write exceeds partition or device limit\n"); |
| 548 | *length = 0; |
| 549 | return -EFBIG; |
| 550 | } |
| 551 | |
Ben Gardiner | 34dd567 | 2011-06-14 16:35:06 -0400 | [diff] [blame] | 552 | if (!need_skip && !(flags & WITH_DROP_FFS)) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 553 | rval = nand_write(nand, offset, length, buffer); |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 554 | if (rval == 0) |
| 555 | return 0; |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 556 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 557 | *length = 0; |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 558 | printf("NAND write to offset %llx failed %d\n", |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 559 | offset, rval); |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 560 | return rval; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | while (left_to_write > 0) { |
| 564 | size_t block_offset = offset & (nand->erasesize - 1); |
Ben Gardiner | 34dd567 | 2011-06-14 16:35:06 -0400 | [diff] [blame] | 565 | size_t write_size, truncated_write_size; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 566 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 567 | WATCHDOG_RESET(); |
Giulio Benetti | 749bd66 | 2009-07-31 17:30:34 -0500 | [diff] [blame] | 568 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 569 | if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) { |
| 570 | printf("Skip bad block 0x%08llx\n", |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 571 | offset & ~(nand->erasesize - 1)); |
| 572 | offset += nand->erasesize - block_offset; |
| 573 | continue; |
| 574 | } |
| 575 | |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 576 | if (left_to_write < (blocksize - block_offset)) |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 577 | write_size = left_to_write; |
| 578 | else |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 579 | write_size = blocksize - block_offset; |
| 580 | |
| 581 | #ifdef CONFIG_CMD_NAND_YAFFS |
Ben Gardiner | 1caafbb | 2011-05-24 10:18:35 -0400 | [diff] [blame] | 582 | if (flags & WITH_YAFFS_OOB) { |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 583 | int page, pages; |
| 584 | size_t pagesize = nand->writesize; |
| 585 | size_t pagesize_oob = pagesize + nand->oobsize; |
| 586 | struct mtd_oob_ops ops; |
| 587 | |
| 588 | ops.len = pagesize; |
| 589 | ops.ooblen = nand->oobsize; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 590 | ops.mode = MTD_OPS_AUTO_OOB; |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 591 | ops.ooboffs = 0; |
| 592 | |
| 593 | pages = write_size / pagesize_oob; |
| 594 | for (page = 0; page < pages; page++) { |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 595 | WATCHDOG_RESET(); |
| 596 | |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 597 | ops.datbuf = p_buffer; |
| 598 | ops.oobbuf = ops.datbuf + pagesize; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 599 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 600 | rval = mtd_write_oob(nand, offset, &ops); |
Liu, Wentao | de95b32 | 2012-01-17 19:55:02 +0000 | [diff] [blame] | 601 | if (rval != 0) |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | offset += pagesize; |
| 605 | p_buffer += pagesize_oob; |
| 606 | } |
| 607 | } |
| 608 | else |
| 609 | #endif |
| 610 | { |
Ben Gardiner | 34dd567 | 2011-06-14 16:35:06 -0400 | [diff] [blame] | 611 | truncated_write_size = write_size; |
| 612 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
| 613 | if (flags & WITH_DROP_FFS) |
| 614 | truncated_write_size = drop_ffs(nand, p_buffer, |
| 615 | &write_size); |
| 616 | #endif |
| 617 | |
| 618 | rval = nand_write(nand, offset, &truncated_write_size, |
| 619 | p_buffer); |
Lei Wen | 4b5deaa | 2011-01-06 11:11:58 +0800 | [diff] [blame] | 620 | offset += write_size; |
| 621 | p_buffer += write_size; |
| 622 | } |
| 623 | |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 624 | if (rval != 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 625 | printf("NAND write to offset %llx failed %d\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 626 | offset, rval); |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 627 | *length -= left_to_write; |
| 628 | return rval; |
| 629 | } |
| 630 | |
| 631 | left_to_write -= write_size; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * nand_read_skip_bad: |
| 639 | * |
| 640 | * Read image from NAND flash. |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 641 | * Blocks that are marked bad are skipped and the next block is read |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 642 | * instead as long as the image is short enough to fit even after |
| 643 | * skipping the bad blocks. Due to bad blocks we may not be able to |
| 644 | * perform the requested read. In the case where the read would extend |
| 645 | * beyond the end of the NAND device, both length and actual (if not |
| 646 | * NULL) are set to 0. In the case where the read would extend beyond |
| 647 | * the limit we are passed, length is set to 0 and actual is set to the |
| 648 | * required length. |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 649 | * |
| 650 | * @param nand NAND device |
| 651 | * @param offset offset in flash |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 652 | * @param length buffer length, on return holds number of read bytes |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 653 | * @param actual set to size required to read length worth of buffer or 0 |
| 654 | * on error, if not NULL |
| 655 | * @param lim maximum size that actual may be in order to not exceed the |
| 656 | * buffer |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 657 | * @param buffer buffer to write to |
| 658 | * @return 0 in case of success |
| 659 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 2511ba0 | 2009-05-16 14:27:40 +0200 | [diff] [blame] | 660 | int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length, |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 661 | size_t *actual, loff_t lim, u_char *buffer) |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 662 | { |
| 663 | int rval; |
| 664 | size_t left_to_read = *length; |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 665 | size_t used_for_read = 0; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 666 | u_char *p_buffer = buffer; |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 667 | int need_skip; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 668 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 669 | if ((offset & (nand->writesize - 1)) != 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 670 | printf("Attempt to read non page-aligned data\n"); |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 671 | *length = 0; |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 672 | if (actual) |
| 673 | *actual = 0; |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 674 | return -EINVAL; |
| 675 | } |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 676 | |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 677 | need_skip = check_skip_len(nand, offset, *length, &used_for_read); |
| 678 | |
| 679 | if (actual) |
| 680 | *actual = used_for_read; |
| 681 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 682 | if (need_skip < 0) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 683 | printf("Attempt to read outside the flash area\n"); |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 684 | *length = 0; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 685 | return -EINVAL; |
| 686 | } |
| 687 | |
Tom Rini | 32d9618 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 688 | if (used_for_read > lim) { |
| 689 | puts("Size of read exceeds partition or device limit\n"); |
| 690 | *length = 0; |
| 691 | return -EFBIG; |
| 692 | } |
| 693 | |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 694 | if (!need_skip) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 695 | rval = nand_read(nand, offset, length, buffer); |
Valeriy Glushkov | f2aead8 | 2009-07-14 13:51:10 +0300 | [diff] [blame] | 696 | if (!rval || rval == -EUCLEAN) |
| 697 | return 0; |
Scott Wood | fe3b5e1 | 2010-07-30 16:11:41 -0500 | [diff] [blame] | 698 | |
| 699 | *length = 0; |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 700 | printf("NAND read from offset %llx failed %d\n", |
Valeriy Glushkov | f2aead8 | 2009-07-14 13:51:10 +0300 | [diff] [blame] | 701 | offset, rval); |
Scott Wood | 90e0a6b | 2008-11-25 10:47:02 -0600 | [diff] [blame] | 702 | return rval; |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | while (left_to_read > 0) { |
| 706 | size_t block_offset = offset & (nand->erasesize - 1); |
| 707 | size_t read_length; |
| 708 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 709 | WATCHDOG_RESET(); |
Giulio Benetti | 749bd66 | 2009-07-31 17:30:34 -0500 | [diff] [blame] | 710 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 711 | if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) { |
| 712 | printf("Skipping bad block 0x%08llx\n", |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 713 | offset & ~(nand->erasesize - 1)); |
| 714 | offset += nand->erasesize - block_offset; |
| 715 | continue; |
| 716 | } |
| 717 | |
| 718 | if (left_to_read < (nand->erasesize - block_offset)) |
| 719 | read_length = left_to_read; |
| 720 | else |
| 721 | read_length = nand->erasesize - block_offset; |
| 722 | |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 723 | rval = nand_read(nand, offset, &read_length, p_buffer); |
Valeriy Glushkov | f2aead8 | 2009-07-14 13:51:10 +0300 | [diff] [blame] | 724 | if (rval && rval != -EUCLEAN) { |
Benoît Thébaudeau | e26708e | 2012-11-05 10:15:46 +0000 | [diff] [blame] | 725 | printf("NAND read from offset %llx failed %d\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 726 | offset, rval); |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 727 | *length -= left_to_read; |
| 728 | return rval; |
| 729 | } |
| 730 | |
| 731 | left_to_read -= read_length; |
| 732 | offset += read_length; |
| 733 | p_buffer += read_length; |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | } |
Benoît Thébaudeau | 5661f70 | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 738 | |
| 739 | #ifdef CONFIG_CMD_NAND_TORTURE |
| 740 | |
| 741 | /** |
| 742 | * check_pattern: |
| 743 | * |
| 744 | * Check if buffer contains only a certain byte pattern. |
| 745 | * |
| 746 | * @param buf buffer to check |
| 747 | * @param patt the pattern to check |
| 748 | * @param size buffer size in bytes |
| 749 | * @return 1 if there are only patt bytes in buf |
| 750 | * 0 if something else was found |
| 751 | */ |
| 752 | static int check_pattern(const u_char *buf, u_char patt, int size) |
| 753 | { |
| 754 | int i; |
| 755 | |
| 756 | for (i = 0; i < size; i++) |
| 757 | if (buf[i] != patt) |
| 758 | return 0; |
| 759 | return 1; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * nand_torture: |
| 764 | * |
| 765 | * Torture a block of NAND flash. |
| 766 | * This is useful to determine if a block that caused a write error is still |
| 767 | * good or should be marked as bad. |
| 768 | * |
| 769 | * @param nand NAND device |
| 770 | * @param offset offset in flash |
| 771 | * @return 0 if the block is still good |
| 772 | */ |
| 773 | int nand_torture(nand_info_t *nand, loff_t offset) |
| 774 | { |
| 775 | u_char patterns[] = {0xa5, 0x5a, 0x00}; |
| 776 | struct erase_info instr = { |
| 777 | .mtd = nand, |
| 778 | .addr = offset, |
| 779 | .len = nand->erasesize, |
| 780 | }; |
| 781 | size_t retlen; |
| 782 | int err, ret = -1, i, patt_count; |
| 783 | u_char *buf; |
| 784 | |
| 785 | if ((offset & (nand->erasesize - 1)) != 0) { |
| 786 | puts("Attempt to torture a block at a non block-aligned offset\n"); |
| 787 | return -EINVAL; |
| 788 | } |
| 789 | |
| 790 | if (offset + nand->erasesize > nand->size) { |
| 791 | puts("Attempt to torture a block outside the flash area\n"); |
| 792 | return -EINVAL; |
| 793 | } |
| 794 | |
| 795 | patt_count = ARRAY_SIZE(patterns); |
| 796 | |
| 797 | buf = malloc(nand->erasesize); |
| 798 | if (buf == NULL) { |
| 799 | puts("Out of memory for erase block buffer\n"); |
| 800 | return -ENOMEM; |
| 801 | } |
| 802 | |
| 803 | for (i = 0; i < patt_count; i++) { |
| 804 | err = nand->erase(nand, &instr); |
| 805 | if (err) { |
| 806 | printf("%s: erase() failed for block at 0x%llx: %d\n", |
| 807 | nand->name, instr.addr, err); |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | /* Make sure the block contains only 0xff bytes */ |
| 812 | err = nand->read(nand, offset, nand->erasesize, &retlen, buf); |
| 813 | if ((err && err != -EUCLEAN) || retlen != nand->erasesize) { |
| 814 | printf("%s: read() failed for block at 0x%llx: %d\n", |
| 815 | nand->name, instr.addr, err); |
| 816 | goto out; |
| 817 | } |
| 818 | |
| 819 | err = check_pattern(buf, 0xff, nand->erasesize); |
| 820 | if (!err) { |
| 821 | printf("Erased block at 0x%llx, but a non-0xff byte was found\n", |
| 822 | offset); |
| 823 | ret = -EIO; |
| 824 | goto out; |
| 825 | } |
| 826 | |
| 827 | /* Write a pattern and check it */ |
| 828 | memset(buf, patterns[i], nand->erasesize); |
| 829 | err = nand->write(nand, offset, nand->erasesize, &retlen, buf); |
| 830 | if (err || retlen != nand->erasesize) { |
| 831 | printf("%s: write() failed for block at 0x%llx: %d\n", |
| 832 | nand->name, instr.addr, err); |
| 833 | goto out; |
| 834 | } |
| 835 | |
| 836 | err = nand->read(nand, offset, nand->erasesize, &retlen, buf); |
| 837 | if ((err && err != -EUCLEAN) || retlen != nand->erasesize) { |
| 838 | printf("%s: read() failed for block at 0x%llx: %d\n", |
| 839 | nand->name, instr.addr, err); |
| 840 | goto out; |
| 841 | } |
| 842 | |
| 843 | err = check_pattern(buf, patterns[i], nand->erasesize); |
| 844 | if (!err) { |
| 845 | printf("Pattern 0x%.2x checking failed for block at " |
| 846 | "0x%llx\n", patterns[i], offset); |
| 847 | ret = -EIO; |
| 848 | goto out; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | ret = 0; |
| 853 | |
| 854 | out: |
| 855 | free(buf); |
| 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | #endif |