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