Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*	$NetBSD: cfi_0002.c,v 1.8 2015/06/09 21:42:21 matt Exp $	*/
/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Cliff Neighbors.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "opt_flash.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cfi_0002.c,v 1.8 2015/06/09 21:42:21 matt Exp $"); 

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cdefs.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <sys/sched.h>
#include <sys/time.h>

#include <sys/bus.h>
        
#include <dev/nor/nor.h>
#include <dev/nor/cfi.h>
#include <dev/nor/cfi_0002.h>


static void cfi_0002_version_init(struct cfi * const);
static int  cfi_0002_read_page(device_t, flash_off_t, uint8_t *);
static int  cfi_0002_program_page(device_t, flash_off_t, const uint8_t *);
static int  cfi_0002_erase_block(device_t, flash_off_t);
static int  cfi_0002_erase_all(device_t);
static int  cfi_0002_busy(device_t, flash_off_t, u_long);
static int  cfi_0002_busy_wait(struct cfi * const, flash_off_t, u_long);
static int  cfi_0002_busy_poll(struct cfi * const, flash_off_t, u_long);
static int  cfi_0002_busy_yield(struct cfi * const, flash_off_t, u_long);
static int  cfi_0002_busy_dq7(struct cfi * const , flash_off_t);
#ifdef NOTYET
static int  cfi_0002_busy_reg(struct cfi * const, flash_off_t);
#endif

#ifdef NOR_VERBOSE
static const char *page_mode_str[] = {
	"(not supported)",
	"4 word page",
	"8 word page",
	"16 word page",
};

static const char *wp_mode_str[] = {
	"Flash device without WP Protect (No Boot)",
	"Eight 8 kB Sectors at TOP and Bottom with WP (Dual Boot)",
	"Bottom Boot Device with WP Protect (Bottom Boot)",
	"Top Boot Device with WP Protect (Top Boot)",
	"Uniform, Bottom WP Protect (Uniform Bottom Boot)",
	"Uniform, Top WP Protect (Uniform Top Boot)",
	"WP Protect for all sectors",
	"Uniform, Top or Bottom WP Protect",
};

static inline const char *
cfi_0002_page_mode_str(uint8_t mode)
{
	if (mode >= __arraycount(page_mode_str))
		panic("%s: mode %d out of range", __func__, mode);
	return page_mode_str[mode];
}

static inline const char *
cfi_0002_wp_mode_str(uint8_t mode)
{
	if (mode >= __arraycount(wp_mode_str))
		panic("%s: mode %d out of range", __func__, mode);
	return wp_mode_str[mode];
}
#endif

/*
 * cfi_0002_time_write_nbyte - maximum usec delay waiting for write buffer
 */
static inline u_long
cfi_0002_time_write_nbyte(struct cfi *cfi)
{
	u_int shft = cfi->cfi_qry_data.write_nbyte_time_typ;
	shft += cfi->cfi_qry_data.write_nbyte_time_max;
	u_long usec = 1UL << shft;
	return usec;
}

/*
 * cfi_0002_time_erase_blk - maximum usec delay waiting for erase block
 */
static inline u_long
cfi_0002_time_erase_blk(struct cfi *cfi)
{
	u_int shft = cfi->cfi_qry_data.erase_blk_time_typ;
	shft += cfi->cfi_qry_data.erase_blk_time_max;
	u_long usec = 1000UL << shft;
	return usec;
}

/*
 * cfi_0002_time_erase_all - maximum usec delay waiting for erase chip
 */
static inline u_long
cfi_0002_time_erase_all(struct cfi *cfi)
{
	u_int shft = cfi->cfi_qry_data.erase_chip_time_typ;
	shft += cfi->cfi_qry_data.erase_chip_time_max;
	u_long usec = 1000UL << shft;
	return usec;
}

/*
 * cfi_0002_time_dflt - maximum usec delay to use waiting for ready
 *
 * use the maximum delay for chip erase function
 * that should be the worst non-sick case
 */
static inline u_long
cfi_0002_time_dflt(struct cfi *cfi)
{
	return cfi_0002_time_erase_all(cfi);
}

void
cfi_0002_init(struct nor_softc * const sc, struct cfi * const cfi,
    struct nor_chip * const chip)
{
	CFI_0002_STATS_INIT(sc->sc_dev, cfi);

	cfi_0002_version_init(cfi);

	cfi->cfi_ops.cfi_reset = cfi_reset_std;
	cfi->cfi_yield_time = 500;		/* 500 usec */

	/* page size for buffered write */
	chip->nc_page_size =
		1 << cfi->cfi_qry_data.write_nbyte_size_max;

	/* these are unused */
	chip->nc_spare_size = 0;
	chip->nc_badmarker_offs = 0;

	/* establish command-set-specific interface ops */
	sc->sc_nor_if->read_page = cfi_0002_read_page;
	sc->sc_nor_if->program_page = cfi_0002_program_page;
	sc->sc_nor_if->erase_block = cfi_0002_erase_block;
	sc->sc_nor_if->erase_all = cfi_0002_erase_all;
	sc->sc_nor_if->busy = cfi_0002_busy;

}

/*
 * cfi_0002_version_init - command set version-specific initialization
 *
 * see "Programmer's Guide for the Spansion 65 nm GL-S MirrorBit EclipseTM
 * Flash Non-Volatile Memory Family Architecture" section 5.
 */
static void
cfi_0002_version_init(struct cfi * const cfi)
{
	const uint8_t major = cfi->cfi_qry_data.pri.cmd_0002.version_maj;
	const uint8_t minor = cfi->cfi_qry_data.pri.cmd_0002.version_min;

	if ((minor == '3') && (major == '1')) {
		/* cmdset version 1.3 */
		cfi->cfi_ops.cfi_busy = cfi_0002_busy_dq7;
#ifdef NOTYET
		cfi->cfi_ops.cfi_erase_sector = cfi_0002_erase_sector_q;
		cfi->cfi_ops.cfi_program_word = cfi_0002_program_word_ub;
	} else if ((minor >= '5') && (major == '1')) {
		/* cmdset version 1.5 or later */
		cfi->cfi_ops.cfi_busy = cfi_0002_busy_reg;
		cfi->cfi_ops.cfi_erase_sector = cfi_0002_erase_sector_1;
		cfi->cfi_ops.cfi_program_word = cfi_0002_program_word_no_ub;
#endif
	} else {
		/* XXX this is excessive */
		panic("%s: unknown cmdset version %c.%c\n",
			__func__, major, minor);
	}

}

void
cfi_0002_print(device_t self, struct cfi * const cfi)
{
#ifdef NOR_VERBOSE
	struct cmdset_0002_query_data *pri = &cfi->cfi_qry_data.pri.cmd_0002;

	aprint_normal_dev(self, "AMD/Fujitsu cmdset (0x0002) version=%c.%c\n",
		pri->version_maj, pri->version_min);
	aprint_normal_dev(self, "page mode type: %s\n",
		cfi_0002_page_mode_str(pri->page_mode_type));
	aprint_normal_dev(self, "wp protection: %s\n",
		cfi_0002_wp_mode_str(pri->wp_prot));
	aprint_normal_dev(self, "program suspend %ssupported\n",
		(pri->prog_susp == 0) ? "not " : "");
	aprint_normal_dev(self, "unlock bypass %ssupported\n",
		(pri->unlock_bypass == 0) ? "not " : "");
	aprint_normal_dev(self, "secure silicon sector size %#x\n",
		1 << pri->sss_size);
	aprint_normal_dev(self, "SW features %#x\n", pri->soft_feat);
	aprint_normal_dev(self, "page size %d\n", 1 << pri->page_size);
#endif
}

static int
cfi_0002_read_page(device_t self, flash_off_t offset, uint8_t *datap)
{
	struct nor_softc * const sc = device_private(self);
	KASSERT(sc != NULL);
	KASSERT(sc->sc_nor_if != NULL);
	struct cfi *cfi = (struct cfi * const)sc->sc_nor_if->private;
	KASSERT(cfi != NULL);
	struct nor_chip * const chip = &sc->sc_chip;
	KASSERT(chip != NULL);
	KASSERT(chip->nc_page_mask != 0);
	KASSERT((offset & ~chip->nc_page_mask) == 0);
	KASSERT (chip->nc_page_size != 0);
	KASSERT((chip->nc_page_size & ((1 << cfi->cfi_portwidth) - 1)) == 0);

	CFI_0002_STATS_INC(cfi, read_page);

	bus_size_t count = chip->nc_page_size >> cfi->cfi_portwidth;
							/* #words/page */

	int error = cfi_0002_busy_wait(cfi, offset, cfi_0002_time_dflt(cfi));
	if (error != 0)
		return error;

	switch(cfi->cfi_portwidth) {
	case 0:
		bus_space_read_region_1(cfi->cfi_bst, cfi->cfi_bsh, offset,
			(uint8_t *)datap, count);
		break;
	case 1:
		bus_space_read_region_2(cfi->cfi_bst, cfi->cfi_bsh, offset,
			(uint16_t *)datap, count);
		break;
	case 2:
		bus_space_read_region_4(cfi->cfi_bst, cfi->cfi_bsh, offset,
			(uint32_t *)datap, count);
		break;
	default:
		panic("%s: bad port width %d\n", __func__, cfi->cfi_portwidth);
	};

	return 0;
}

static int
cfi_0002_program_page(device_t self, flash_off_t offset, const uint8_t *datap)
{
	struct nor_softc * const sc = device_private(self);
	KASSERT(sc != NULL);
	KASSERT(sc->sc_nor_if != NULL);
	struct cfi *cfi = (struct cfi * const)sc->sc_nor_if->private;
	KASSERT(cfi != NULL);
	struct nor_chip * const chip = &sc->sc_chip;
	KASSERT(chip != NULL);
	KASSERT(chip->nc_page_mask != 0);
	KASSERT((offset & ~chip->nc_page_mask) == 0);
	KASSERT (chip->nc_page_size != 0);
	KASSERT((chip->nc_page_size & ((1 << cfi->cfi_portwidth) - 1)) == 0);

	CFI_0002_STATS_INC(cfi, program_page);

	bus_size_t count = chip->nc_page_size >> cfi->cfi_portwidth;
							/* #words/page */
	bus_size_t sa = offset << (3 - cfi->cfi_portwidth);
							/* sector addr */
	uint32_t wc = count - 1;			/* #words - 1 */

	int error = cfi_0002_busy_wait(cfi, offset, cfi_0002_time_dflt(cfi));
	if (error != 0)
		return ETIMEDOUT;

	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0xaa);
	cfi_cmd(cfi, cfi->cfi_unlock_addr2, 0x55);
	cfi_cmd(cfi, sa,                    0x25); /* Write To Buffer */
	cfi_cmd(cfi, sa,                    wc);

	switch(cfi->cfi_portwidth) {
	case 0:
		bus_space_write_region_1(cfi->cfi_bst, cfi->cfi_bsh, offset,
			(const uint8_t *)datap, count);
		break;
	case 1:
		bus_space_write_region_2(cfi->cfi_bst, cfi->cfi_bsh, offset,
			(const uint16_t *)datap, count);
		break;
	case 2:
		bus_space_write_region_4(cfi->cfi_bst, cfi->cfi_bsh, offset,
			(const uint32_t *)datap, count);
		break;
	default:
		panic("%s: bad port width %d\n", __func__, cfi->cfi_portwidth);
	};

	cfi_cmd(cfi, sa, 0x29);	/*  Write Buffer Program Confirm */

	error = cfi_0002_busy_wait(cfi, offset, cfi_0002_time_write_nbyte(cfi));

	return error;
}

static int
cfi_0002_erase_all(device_t self)
{
	struct nor_softc * const sc = device_private(self);
	KASSERT(sc != NULL);
	KASSERT(sc->sc_nor_if != NULL);
	struct cfi *cfi = (struct cfi * const)sc->sc_nor_if->private;
	KASSERT(cfi != NULL);

	CFI_0002_STATS_INC(cfi, erase_all);

	int error = cfi_0002_busy_wait(cfi, 0, cfi_0002_time_dflt(cfi));
	if (error != 0)
		return ETIMEDOUT;

	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0xaa);
	cfi_cmd(cfi, cfi->cfi_unlock_addr2, 0x55);
	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0x80); /* erase start */
	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0xaa);
	cfi_cmd(cfi, cfi->cfi_unlock_addr2, 0x55);
	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0x10); /* erase chip */

	error = cfi_0002_busy_wait(cfi, 0, cfi_0002_time_erase_all(cfi));

	return error;
}

static int
cfi_0002_erase_block(device_t self, flash_off_t offset)
{
	struct nor_softc * const sc = device_private(self);
	KASSERT(sc != NULL);
	KASSERT(sc->sc_nor_if != NULL);
	struct cfi *cfi = (struct cfi * const)sc->sc_nor_if->private;
	KASSERT(cfi != NULL);

	CFI_0002_STATS_INC(cfi, erase_block);

	bus_size_t sa = offset << (3 - cfi->cfi_portwidth);

	int error = cfi_0002_busy_wait(cfi, offset, cfi_0002_time_dflt(cfi));
	if (error != 0)
		return ETIMEDOUT;

	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0xaa);
	cfi_cmd(cfi, cfi->cfi_unlock_addr2, 0x55);
	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0x80); /* erase start */
	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0xaa);
	cfi_cmd(cfi, cfi->cfi_unlock_addr2, 0x55);
	cfi_cmd(cfi, sa,                    0x30); /* erase sector */

	error = cfi_0002_busy_wait(cfi, offset, cfi_0002_time_erase_blk(cfi));

	return error;
}

/*
 * cfi_0002_busy - nor_interface busy op
 */
static int
cfi_0002_busy(device_t self, flash_off_t offset, u_long usec)
{
	struct nor_softc *sc = device_private(self);
	KASSERT(sc != NULL);
	KASSERT(sc->sc_nor_if != NULL);
	struct cfi * const cfi = (struct cfi * const)sc->sc_nor_if->private;

	CFI_0002_STATS_INC(cfi, busy);

	return cfi_0002_busy_wait(cfi, offset, usec);
}

/*
 * cfi_0002_busy_wait - wait until device is not busy
 */
static int
cfi_0002_busy_wait(struct cfi * const cfi, flash_off_t offset, u_long usec)
{
	int error;

#ifdef CFI_0002_STATS
	struct timeval start;
	struct timeval now;
	struct timeval delta;

	if (usec > cfi->cfi_0002_stats.busy_usec_max)
		cfi->cfi_0002_stats.busy_usec_max = usec;
	if (usec < cfi->cfi_0002_stats.busy_usec_min)
		cfi->cfi_0002_stats.busy_usec_min = usec;
	microtime(&start);
#endif
	if (usec > cfi->cfi_yield_time) {
		error = cfi_0002_busy_yield(cfi, offset, usec);
#ifdef CFI_0002_STATS
		microtime(&now);
		cfi->cfi_0002_stats.busy_yield++;
		timersub(&now, &start, &delta);
		timeradd(&delta,
			&cfi->cfi_0002_stats.busy_yield_tv,
			&cfi->cfi_0002_stats.busy_yield_tv);
#endif
	} else {
		error = cfi_0002_busy_poll(cfi, offset, usec);
#ifdef CFI_0002_STATS
		microtime(&now);
		cfi->cfi_0002_stats.busy_poll++;
		timersub(&now, &start, &delta);
		timeradd(&delta,
			&cfi->cfi_0002_stats.busy_poll_tv,
			&cfi->cfi_0002_stats.busy_poll_tv);
#endif
	}
	return error;
}

/*
 * cfi_0002_busy_poll - poll until device is not busy
 */
static int
cfi_0002_busy_poll(struct cfi * const cfi, flash_off_t offset, u_long usec)
{
	u_long count = usec >> 3;
	if (count == 0)
		count = 1;	/* enforce minimum */
	do {
		if (! cfi->cfi_ops.cfi_busy(cfi, offset))
			return 0;	/* not busy */
		DELAY(8);
	} while (count-- != 0);

	return ETIMEDOUT;		/* busy */
}

/*
 * cfi_0002_busy_yield - yield until device is not busy
 */
static int
cfi_0002_busy_yield(struct cfi * const cfi, flash_off_t offset, u_long usec)
{
	struct timeval start;
	struct timeval delta;
	struct timeval limit;
	struct timeval now;

	microtime(&start);

	/* try optimism */
	if (! cfi->cfi_ops.cfi_busy(cfi, offset)) {
		CFI_0002_STATS_INC(cfi, busy_yield_hit);
		return 0;		/* not busy */
	}
	CFI_0002_STATS_INC(cfi, busy_yield_miss);

	delta.tv_sec = usec / 1000000;
	delta.tv_usec = usec % 1000000;
	timeradd(&start, &delta, &limit);
	do {
		yield();
		microtime(&now);
		if (! cfi->cfi_ops.cfi_busy(cfi, offset))
			return 0;	/* not busy */
	} while (timercmp(&now, &limit, <));

	CFI_0002_STATS_INC(cfi, busy_yield_timo);

	return ETIMEDOUT;		/* busy */
}

/*
 * cfi_0002_busy_dq7 - DQ7 "toggle" method to check busy
 *
 * Check busy during/after erase, program, protect operation.
 *
 * NOTE:
 *	Chip manufacturers (Spansion) plan to deprecate this method.
 */
static int
cfi_0002_busy_dq7(struct cfi * const cfi, flash_off_t offset)
{
	bus_space_tag_t bst = cfi->cfi_bst;
	bus_space_handle_t bsh = cfi->cfi_bsh;
	bool busy;

	switch(cfi->cfi_portwidth) {
	case 0: {
		uint8_t r0 = bus_space_read_1(bst, bsh, 0) & __BIT(7);
		uint8_t r1 = bus_space_read_1(bst, bsh, 0) & __BIT(7);
		busy = (r0 != r1);
		break;
	}
	case 1: {
		uint16_t r0 = bus_space_read_2(bst, bsh, 0);
		uint16_t r1 = bus_space_read_2(bst, bsh, 0);
		busy = (r0 != r1);
		break;
	}
	case 2: {
		uint32_t r0 = bus_space_read_4(bst, bsh, 0);
		uint32_t r1 = bus_space_read_4(bst, bsh, 0);
		busy = (r0 != r1);
		break;
	}
	default:
		busy = true;	/* appeas gcc */
		panic("%s: bad port width %d\n",
			__func__, cfi->cfi_portwidth);
	}
	return busy;
}

#ifdef NOTYET
/*
 * cfi_0002_busy_reg - read and evaluate Read Status Register
 *
 * NOTE:
 *	Read Status Register not present on all chips
 *	use "toggle" method when Read Status Register not available.
 */
static bool
cfi_0002_busy_reg(struct cfi * const cfi, flash_off_t offset)
{
	bus_space_tag_t bst = cfi->cfi_bst;
	bus_space_handle_t bsh = cfi->cfi_bsh;
	uint32_t r;

	cfi_cmd(cfi, cfi->cfi_unlock_addr1, 0x70); /* Status Register Read  */

	switch(cfi->cfi_portwidth) {
	case 0:
		r = bus_space_read_1(bst, bsh, 0);
		break;
	case 1:
		r = bus_space_read_2(bst, bsh, 0);
		break;
	case 2:
		r = bus_space_read_4(bst, bsh, 0);
		break;
	default:
		panic("%s: bad port width %d\n",
			__func__, cfi->cfi_portwidth);
	}

	return ((r & __BIT(7)) == 0):
}
#endif	/* NOTYET */

#ifdef CFI_0002_STATS
void
cfi_0002_stats_reset(struct cfi *cfi)
{
	memset(&cfi->cfi_0002_stats, 0, sizeof(struct cfi_0002_stats));
        cfi->cfi_0002_stats.busy_usec_min = ~0;
}

void
cfi_0002_stats_print(struct cfi *cfi)
{
	printf("read_page %lu\n", cfi->cfi_0002_stats.read_page);
	printf("program_page %lu\n", cfi->cfi_0002_stats.program_page);
	printf("erase_all %lu\n", cfi->cfi_0002_stats.erase_all);
	printf("erase_block %lu\n", cfi->cfi_0002_stats.erase_block);
	printf("busy %lu\n", cfi->cfi_0002_stats.busy);

	printf("write_nbyte_time_typ %d\n",
		 cfi->cfi_qry_data.write_nbyte_time_typ);
	printf("write_nbyte_time_max %d\n",
		 cfi->cfi_qry_data.write_nbyte_time_max);

	printf("erase_blk_time_typ %d\n",
		 cfi->cfi_qry_data.erase_blk_time_typ);
	printf("erase_blk_time_max %d\n",
		 cfi->cfi_qry_data.erase_blk_time_max);

	printf("erase_chip_time_typ %d\n",
		 cfi->cfi_qry_data.erase_chip_time_typ);
	printf("erase_chip_time_max %d\n",
		 cfi->cfi_qry_data.erase_chip_time_max);

	printf("time_write_nbyte %lu\n", cfi_0002_time_write_nbyte(cfi));
	printf("time_erase_blk %lu\n", cfi_0002_time_erase_blk(cfi));
	printf("time_erase_all %lu\n", cfi_0002_time_erase_all(cfi));

	printf("busy_usec_min %lu\n", cfi->cfi_0002_stats.busy_usec_min);
	printf("busy_usec_max %lu\n", cfi->cfi_0002_stats.busy_usec_max);

	printf("busy_poll_tv %lld.%d\n",
		cfi->cfi_0002_stats.busy_poll_tv.tv_sec,
		cfi->cfi_0002_stats.busy_poll_tv.tv_usec);
	printf("busy_yield_tv %lld.%d\n",
		cfi->cfi_0002_stats.busy_yield_tv.tv_sec,
		cfi->cfi_0002_stats.busy_yield_tv.tv_usec);
	printf("busy_poll %lu\n", cfi->cfi_0002_stats.busy_poll);
	printf("busy_yield %lu\n", cfi->cfi_0002_stats.busy_yield);
	printf("busy_yield_hit %lu\n", cfi->cfi_0002_stats.busy_yield_hit);
	printf("busy_yield_miss %lu\n", cfi->cfi_0002_stats.busy_yield_miss);
	printf("busy_yield_timo %lu\n", cfi->cfi_0002_stats.busy_yield_timo);
}
#endif	/* CFI_0002_STATS */