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