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