summaryrefslogtreecommitdiffstats
path: root/sys/dev/isf/isf.c
blob: eaaad159753387d8e1fdc12524aa57f8560cc88a (plain)
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*-
 * Copyright (c) 2012 Robert N. M. Watson
 * Copyright (c) 2012 SRI International
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * 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 AUTHOR 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 AUTHOR 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>

#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/endian.h>
#include <sys/bio.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/taskqueue.h>

#include <machine/bus.h>
#include <machine/resource.h>

#include <geom/geom_disk.h>

#include <dev/isf/isf.h>

/* Read Mode */
#define	ISF_CMD_RA	0xFF	/* Read Array mode */
#define	ISF_CMD_RSR	0x70	/* Read Status Register mode */
#define	ISF_CMD_RDI	0x90	/* Read Device ID/Config Reg mode */
#define	ISF_CMD_RQ	0x98	/* Read Query mode */
#define	ISF_CMD_CSR	0x50	/* Clear Status Register */

/* Write Mode */
#define	ISF_CMD_WPS	0x40	/* Word Program Setup */
#define ISF_CMD_BPS	0xE8	/* Buffered Program Setup */
#define	ISF_CMD_BPC	0xD0	/* Buffered Program Confirm */

/* Erase Mode */
#define	ISF_CMD_BES	0x20	/* Block Erase Setup */
#define ISF_CMD_BEC	0xD0	/* Block Erase Confirm */

/* Block Locking/Unlocking */
#define	ISF_CMD_LBS	0x60	/* Lock Block Setup */
#define	ISF_CMD_LB	0x01	/* Lock Block */
#define	ISF_CMD_UB	0xD0	/* Unlock Block */

/*
 * Read Device Identifier registers.
 *
 * NOTE: ISF_RDIR_BLC is relative to the block base address.
 */
#define	ISF_REG_MC	0x00	/* Manufacture Code */
#define ISF_REG_ID	0x01	/* Device ID Code */
#define	ISF_REG_BLC	0x02	/* Block Lock Configuration */
#define ISF_REG_RCR	0x05	/* Read Configuration Register */

/*
 * Protection Registers
 */
#define ISF_REG_L0	0x80	/* Lock Register 0 */
#define ISF_REG_FPP	0x81	/* 64-bit Factory Protection Register */
#define ISF_REG_UPP	0x85	/* 64-bit User Protection Register */
#define ISF_REG_L1	0x89	/* Lock Register 1 */
#define ISF_REG_PP1	0x8A	/* 128-bit Protection Register 1 */
#define ISF_REG_PP2	0x92	/* 128-bit Protection Register 2 */
#define ISF_REG_PP3	0x9A	/* 128-bit Protection Register 3 */
#define ISF_REG_PP4	0xA2	/* 128-bit Protection Register 4 */
#define ISF_REG_PP5	0xAA	/* 128-bit Protection Register 5 */
#define ISF_REG_PP6	0xB2	/* 128-bit Protection Register 6 */
#define ISF_REG_PP7	0xBA	/* 128-bit Protection Register 7 */
#define ISF_REG_PP8	0xC2	/* 128-bit Protection Register 8 */
#define ISF_REG_PP9	0xCA	/* 128-bit Protection Register 9 */
#define ISF_REG_PP10	0xD2	/* 128-bit Protection Register 10 */
#define ISF_REG_PP11	0xDA	/* 128-bit Protection Register 11 */
#define ISF_REG_PP12	0xE2	/* 128-bit Protection Register 12 */
#define ISF_REG_PP13	0xEA	/* 128-bit Protection Register 13 */
#define ISF_REG_PP14	0xF2	/* 128-bit Protection Register 14 */
#define ISF_REG_PP15	0xFA	/* 128-bit Protection Register 15 */
#define ISF_REG_PP16	0x102	/* 128-bit Protection Register 16 */

#define	ISF_SR_BWS	(1 << 0)	/* BEFP Status */
#define	ISF_SR_BLS	(1 << 1)	/* Block-Locked Status */
#define ISF_SR_PSS	(1 << 2)	/* Program Suspend Status */
#define	ISF_SR_VPPS	(1 << 3)	/* Vpp Status */
#define	ISF_SR_PS	(1 << 4)	/* Program Status */
#define ISF_SR_ES	(1 << 5)	/* Erase Status */
#define ISF_SR_ESS	(1 << 6)	/* Erase Suspend Status */
#define ISF_SR_DWS	(1 << 7)	/* Device Write Status */
#define ISF_SR_FSC_MASK	(ISF_SR_VPPS | ISF_SR_PS | ISF_SR_BLS)

#define ISF_BUFFER_PROGRAM

MALLOC_DEFINE(M_ISF, "isf_data", "Intel StrateFlash driver");
static int	isf_debug = 0;

static struct isf_chips {
	uint16_t	 chip_id;
	size_t		 chip_size;
	const char	*chip_desc;
} chip_ids[] = {
	{ 0x8817, 0x0800000, "64-Mbit Top Parameter" },
	{ 0x881A, 0x0800000, "64-Mbit Bottom Parameter" },
	{ 0x8818, 0x1000000, "128-Mbit Top Parameter" },
	{ 0x881B, 0x1000000, "128-Mbit Bottom Parameter" },
	{ 0x8919, 0x2000000, "256-Mbit Top Parameter" },
	{ 0x891C, 0x2000000, "256-Mbit Bottom Parameter" },
	{ 0x8961, 0x2000000, "512-Mbit package (half)" },
	{ 0x0000, 0x0000000, NULL }
};

static void	isf_task(void *arg);

/* 
 * Device driver for the Intel StrataFlash NOR flash device.  This
 * implementation is known to work with 256Mb instances of the device, but may
 * also work with other 64/128/512Mb parts without much work.  Multiple
 * device instances should be used when multiple parts are in the same
 * physical package, due to variable block size support in the StrataFlash
 * part.
 */
devclass_t	isf_devclass;

static uint16_t
isf_read_reg(struct isf_softc *sc, uint16_t reg)
{

	if (isf_debug)
		device_printf(sc->isf_dev, "isf_read_reg(0x%02x)\n", reg);
	return (le16toh(bus_read_2(sc->isf_res, reg * 2)));
}

static uint64_t
isf_read_reg64(struct isf_softc *sc, uint16_t reg)
{
	uint64_t val;
	uint16_t *val16 = (uint16_t *)&val;

	if (isf_debug)
		device_printf(sc->isf_dev, "isf_read_reg64(0x%02x)\n", reg);
	val16[0] = bus_read_2(sc->isf_res, reg * 2);
	val16[1] = bus_read_2(sc->isf_res, (reg+1) * 2);
	val16[2] = bus_read_2(sc->isf_res, (reg+2) * 2);
	val16[3] = bus_read_2(sc->isf_res, (reg+3) * 2);

	return(le64toh(val));
}

static uint16_t
isf_read_off(struct isf_softc *sc, off_t off)
{

	KASSERT(off >= 0, ("%s: negative offset\n", __func__));
	KASSERT(off < sc->isf_disk->d_mediasize,
	    ("%s: offset out side address space 0x%08jx \n", __func__,
	    (intmax_t)off));

	if (isf_debug)
		device_printf(sc->isf_dev, "isf_read_off(0x%08jx)\n",
		    (intmax_t)off);
	return (le16toh(bus_read_2(sc->isf_res, off)));
}

static void
isf_write_cmd(struct isf_softc *sc, off_t off, uint16_t cmd)
{
	
	if (isf_debug)
		device_printf(sc->isf_dev, "isf_write_cmd(0x%08jx, 0x%02x)\n",
		    off, cmd);
	bus_write_2(sc->isf_res, off, htole16(cmd));
}

static uint16_t
isf_read_status(struct isf_softc *sc, off_t off)
{
	
	isf_write_cmd(sc, off/2, ISF_CMD_RSR);
	return isf_read_off(sc, off);
}

static void
isf_clear_status(struct isf_softc *sc)
{
	
	isf_write_cmd(sc, 0, ISF_CMD_CSR);
}

static int
isf_full_status_check(struct isf_softc *sc, off_t off)
{
	int		error = 0;
	uint16_t	status;
	
	status = isf_read_status(sc, off);
	if (status & ISF_SR_VPPS) {
		device_printf(sc->isf_dev, "Vpp Range Error\n");
		error = EIO;
	} else if (status & ISF_SR_PS) {
		device_printf(sc->isf_dev, "Program Error\n");
		error = EIO;
	} else if (status & ISF_SR_BLS) {
		device_printf(sc->isf_dev, "Device Protect Error\n");
		error = EIO;
	}
	isf_clear_status(sc);

	return(error);
}

static int
isf_full_erase_status_check(struct isf_softc *sc, off_t off)
{
	int		error = 0;
	uint16_t	status;
	
	status = isf_read_status(sc, off);
	if (status & ISF_SR_VPPS) {
		device_printf(sc->isf_dev, "Vpp Range Error\n");
		error = EIO;
	} else if (status & (ISF_SR_PS|ISF_SR_ES)) {
		device_printf(sc->isf_dev, "Command Sequence Error\n");
		error = EIO;
	} else if (status & ISF_SR_ES) {
		device_printf(sc->isf_dev, "Block Erase Error\n");
		error = EIO;
	} else if (status & ISF_SR_BLS) {
		device_printf(sc->isf_dev, "Block Locked Error\n");
		error = EIO;
	}
	isf_clear_status(sc);

	return(error);
}

static void
isf_unlock_block(struct isf_softc *sc, off_t off)
{

	isf_write_cmd(sc, off, ISF_CMD_LBS);
	isf_write_cmd(sc, off, ISF_CMD_UB);
	isf_write_cmd(sc, off, ISF_CMD_RA);
}

static void
isf_lock_block(struct isf_softc *sc, off_t off)
{

	isf_write_cmd(sc, off, ISF_CMD_LBS);
	isf_write_cmd(sc, off, ISF_CMD_LB);
	isf_write_cmd(sc, off, ISF_CMD_RA);
}

static void
isf_read(struct isf_softc *sc, off_t off, void *data, size_t len)
{

	KASSERT((uintptr_t)data % 2 == 0,
	    ("%s: unaligned data %p", __func__, data));
	KASSERT((len <= ISF_SECTORSIZE) && (len % 2 == 0),
	    ("%s: invalid length %ju", __func__, len));
	KASSERT(off % ISF_SECTORSIZE == 0,
	    ("%s: invalid offset %ju\n", __func__, off));

	/*
	 * It is not permitted to read blocks that are in the process of
	 * being erased, but we know they will be all 1's after the
	 * erase so just report that value if asked about a block that
	 * is being erased.
	 */
	if (sc->isf_bstate[off / ISF_ERASE_BLOCK] == BS_ERASING)
		memset(data, 0xFF, len);
	else
		bus_read_region_2(sc->isf_res, off, (uint16_t *)data, len / 2);
}

static int
isf_write(struct isf_softc *sc, off_t off, void *data, size_t len)
{
	int		 cycles, error = 0;
	uint16_t	*dp;
	uint16_t	 status;
	off_t		 coff;

	KASSERT((uintptr_t)data % 2 == 0,
	    ("%s: unaligned data %p", __func__, data));
	KASSERT((len <= ISF_SECTORSIZE) && (len % 2 == 0),
	    ("%s: invalid length %ju", __func__, len));
	KASSERT(off % ISF_SECTORSIZE == 0,
	    ("%s: invalid offset %ju\n", __func__, off));
	KASSERT(!sc->isf_erasing,
	    ("%s: trying to write while erasing\n", __func__));
	KASSERT(sc->isf_bstate[off / ISF_ERASE_BLOCK] != BS_ERASING,
	    ("%s: block being erased at %ju\n", __func__, off));

	isf_unlock_block(sc, off);

#ifdef ISF_BUFFER_PROGRAM
	for (dp = data, coff = off; dp - (uint16_t *)data < len / 2;
	    dp += 32, coff += 64) {
		isf_clear_status(sc);
		isf_write_cmd(sc, coff, ISF_CMD_BPS);
		cycles = 0xFFFF;
		while ( !(isf_read_off(sc, coff) & ISF_SR_DWS) ) {
			if (cycles-- == 0) {
				device_printf(sc->isf_dev, "timeout waiting"
				    " for write to start at 0x08%jx\n",
				    (intmax_t)coff);
				return (EIO);
			}
			isf_write_cmd(sc, coff, ISF_CMD_BPS);
		}

		/* When writing N blocks, send N-1 as the count */
		isf_write_cmd(sc, coff, 31);
		bus_write_region_2(sc->isf_res, coff, dp, 32);

		isf_write_cmd(sc, coff, ISF_CMD_BPC);

		status = isf_read_off(sc, coff);
		cycles = 0xFFFFF;
		while ( !(status & ISF_SR_DWS) ) {
			if (cycles-- == 0) {
				device_printf(sc->isf_dev, "timeout waiting"
				    " for write to complete at 0x08%jx\n",
				    (intmax_t)coff);
				error = EIO;
				break;
			}
			status = isf_read_off(sc, coff);
		}
		isf_full_status_check(sc, off);

		isf_write_cmd(sc, coff, ISF_CMD_RA);
	}
#else
	for (dp = data, coff = off; dp - (uint16_t *)data < len / 2;
	    dp++, coff += 2) {
		isf_write_cmd(sc, coff, ISF_CMD_WPS);
		bus_write_2(sc->isf_res, coff, *dp);
		status = isf_read_off(sc, coff);
		cycles=0xFFFFF;
		while ( !(status & ISF_SR_DWS) ) {
			if (cycles-- == 0) {
				device_printf(sc->isf_dev, "timeout waiting"
				    " for write to complete at 0x08%jx\n",
				    (intmax_t)coff);
				error = EIO;
				break;
			}
			status = isf_read_off(sc, coff);
		}

	}
	isf_full_status_check(sc, off);
	isf_write_cmd(sc, coff, ISF_CMD_RA);
#endif

	isf_lock_block(sc, off);

	return error;
}

static void
isf_erase_at(struct isf_softc *sc, off_t off)
{
	int		cycles;
	uint16_t	status;

	isf_unlock_block(sc, off);
	isf_clear_status(sc);

	isf_write_cmd(sc, off, ISF_CMD_BES);
	isf_write_cmd(sc, off, ISF_CMD_BEC);

	cycles=0xFFFFFF;
	status = isf_read_off(sc, off);
	while ( !(status & ISF_SR_DWS) ) {
#ifdef NOTYET
		ISF_SLEEP(sc, sc, hz);
#endif
		if (cycles-- == 0) {
			device_printf(sc->isf_dev,
			    "Giving up on erase\n");
			break;
		}
		status = isf_read_off(sc, off);
	}

	isf_full_erase_status_check(sc, off);

	isf_lock_block(sc, off);

	isf_write_cmd(sc, off, ISF_CMD_RA);
}

static void
isf_erase_range(struct isf_softc *sc, off_t blk_off, size_t size)
{
	off_t		off;
	off_t		ms = sc->isf_disk->d_mediasize;

	KASSERT(blk_off % ISF_ERASE_BLOCK == 0,
	    ("%s: invalid offset %ju\n", __func__, blk_off));

	ISF_LOCK_ASSERT(sc);

	for (off = blk_off; off < blk_off + size; off += ISF_ERASE_BLOCK) {
		sc->isf_bstate[off / ISF_ERASE_BLOCK] = BS_ERASING;

		/*
		 * The first or last 128K is four blocks depending which
		 * part this is.  For now, just assume both are and
		 * erase four times.
		 */
		if (off == 0 || ms - off == ISF_ERASE_BLOCK) {
			isf_erase_at(sc, off);
			isf_erase_at(sc, off + 0x08000);
			isf_erase_at(sc, off + 0x10000);
			isf_erase_at(sc, off + 0x18000);
		} else
			isf_erase_at(sc, off);

		sc->isf_bstate[off / ISF_ERASE_BLOCK] = BS_STEADY;
	}
}

/*
 * disk(9) methods.
 */
static int
isf_disk_ioctl(struct disk *disk, u_long cmd, void *data, int fflag,
    struct thread *td)
{
	int			error = 0;
	struct isf_softc	*sc = disk->d_drv1;
	struct isf_range	*ir;

	switch (cmd) {
	case ISF_ERASE:
		ir = data;
		if (ir->ir_off % ISF_ERASE_BLOCK != 0 ||
		    ir->ir_off >= disk->d_mediasize ||
		    ir->ir_size == 0 ||
		    ir->ir_size % ISF_ERASE_BLOCK != 0 ||
		    ir->ir_off + ir->ir_size > disk->d_mediasize) {
			error = EINVAL;
			break;
		}
		ISF_LOCK(sc);
		if (sc->isf_erasing) {
			ISF_UNLOCK(sc);
			error = EBUSY;
			break;
		}
		sc->isf_erasing = 1;
		isf_erase_range(sc, ir->ir_off, ir->ir_size);
		sc->isf_erasing = 0;
		ISF_UNLOCK(sc);
		break;
	default:
		error = EINVAL;
	}

	return (error);
}

static void
isf_disk_strategy(struct bio *bp)
{
	struct isf_softc *sc = bp->bio_disk->d_drv1;

	/*
	 * We advertise a block size and maximum I/O size up the stack; catch
	 * any attempts to not follow the rules.
	 */
	KASSERT(bp->bio_bcount == ISF_SECTORSIZE,
	    ("%s: I/O size not %d", __func__, ISF_SECTORSIZE));

	ISF_LOCK(sc);
	bioq_disksort(&sc->isf_bioq, bp);
	ISF_WAKEUP(sc);
	ISF_UNLOCK(sc);
}

static void
isf_task(void *arg)
{
	struct isf_softc	*sc = arg;
	struct bio		*bp;
	int			ss = sc->isf_disk->d_sectorsize;
	int			error, i;

	for (;;) {
		ISF_LOCK(sc);
		do {
			bp = bioq_first(&sc->isf_bioq);
			if (bp == NULL) {
				if (sc->isf_doomed)
					kproc_exit(0);
				else
					ISF_SLEEP(sc, sc, 0);
			}
		} while (bp == NULL);
		bioq_remove(&sc->isf_bioq, bp);

		error = 0;
		switch (bp->bio_cmd) {
		case BIO_READ:
			isf_read(sc, bp->bio_pblkno * ss, bp->bio_data,
			    bp->bio_bcount);
			break;

		case BIO_WRITE:
			/*
			 * In principle one could suspend the in-progress
			 * erase, process any pending writes to other
			 * blocks and then proceed, but that seems
			 * overly complex for the likely usage modes.
			 */
			if (sc->isf_erasing) {
				error = EBUSY;
				break;
			}

			/*
			 * Read in the block we want to write and check that
			 * we're only setting bits to 0.  If an erase would
			 * be required return an I/O error.
			 */
			isf_read(sc, bp->bio_pblkno * ss, sc->isf_rbuf,
			    bp->bio_bcount);
			for (i = 0; i < bp->bio_bcount / 2; i++)
				if ((sc->isf_rbuf[i] &
				    ((uint16_t *)bp->bio_data)[i]) !=
				    ((uint16_t *)bp->bio_data)[i]) {
					device_printf(sc->isf_dev, "write"
					    " requires erase at 0x%08jx\n",
					    bp->bio_pblkno * ss);
					error = EIO;
					break;
				}
			if (error != 0)
				break;

			error = isf_write(sc, bp->bio_pblkno * ss,
			    bp->bio_data, bp->bio_bcount);
			break;

		default:
			panic("%s: unsupported I/O operation %d", __func__,
			    bp->bio_cmd);
		}
		if (error == 0)
			biodone(bp);
		else
			biofinish(bp, NULL, error);
		ISF_UNLOCK(sc);
	}
}

static void
isf_dump_info(struct isf_softc *sc)
{
	int i;
	int32_t reg;
	
	isf_write_cmd(sc, 0, ISF_CMD_RDI);
	device_printf(sc->isf_dev, "manufacturer code: 0x%04x\n",
	    isf_read_reg(sc, ISF_REG_MC));
	device_printf(sc->isf_dev, "device id code: 0x%04x\n",
	    isf_read_reg(sc, ISF_REG_ID));
	device_printf(sc->isf_dev, "read config register: 0x%04x\n",
	    isf_read_reg(sc, ISF_REG_RCR));

	device_printf(sc->isf_dev, "lock register 0: 0x%04x\n",
	    isf_read_reg(sc, ISF_REG_L0));
	device_printf(sc->isf_dev, "lock register 1: 0x%04x\n",
	    isf_read_reg(sc, ISF_REG_L1));

	device_printf(sc->isf_dev, "factory PPR: 0x%016jx\n",
	    (uintmax_t)isf_read_reg64(sc, ISF_REG_FPP));
	device_printf(sc->isf_dev, "user PPR (64-bit): 0x%016jx\n",
	    (uintmax_t)isf_read_reg64(sc, ISF_REG_UPP));

	for (reg = ISF_REG_PP1, i = 1; reg <= ISF_REG_PP16; reg += 8, i++) {
		/* XXX: big-endian ordering of uint64_t's */
		device_printf(sc->isf_dev,
		    "user PPR [%02d]: 0x%016jx%016jx\n", i,
		    (uintmax_t)isf_read_reg64(sc, reg+4),
		    (uintmax_t)isf_read_reg64(sc, reg));
	}

	isf_write_cmd(sc, 0, ISF_CMD_RA);
}

static void
isf_disk_insert(struct isf_softc *sc, off_t mediasize)
{
	struct disk *disk;

	sc->isf_doomed = 0;
	sc->isf_erasing = 0;
	sc->isf_bstate = malloc(sizeof(*sc->isf_bstate) *
	    (mediasize / ISF_ERASE_BLOCK), M_ISF, M_ZERO | M_WAITOK);
	kproc_create(&isf_task, sc, &sc->isf_proc, 0, 0, "isf");

	disk = disk_alloc();
	disk->d_drv1 = sc;
	disk->d_name = "isf";
	disk->d_unit = sc->isf_unit;
	disk->d_strategy = isf_disk_strategy;
	disk->d_ioctl = isf_disk_ioctl;
	disk->d_sectorsize = ISF_SECTORSIZE;
	disk->d_mediasize = mediasize;
	disk->d_maxsize = ISF_SECTORSIZE;
	sc->isf_disk = disk;

	if (bootverbose)
		isf_dump_info(sc);

	disk_create(disk, DISK_VERSION);
	device_printf(sc->isf_dev, "%juM flash device\n",
	    (uintmax_t)disk->d_mediasize / (1024 * 1024));

}

static void
isf_disk_remove(struct isf_softc *sc)
{
	struct disk *disk;

	ISF_LOCK_ASSERT(sc);
	KASSERT(sc->isf_disk != NULL, ("%s: isf_disk NULL", __func__));

	sc->isf_doomed = 1;
	ISF_WAKEUP(sc);
	ISF_SLEEP(sc, sc->isf_proc, 0);

	/*
	 * XXXRW: Is it OK to call disk_destroy() under the mutex, or should
	 * we be deferring that to the calling context once it is released?
	 */
	disk = sc->isf_disk;
	disk_gone(disk);
	disk_destroy(disk);
	sc->isf_disk = NULL;
	free(sc->isf_bstate, M_ISF);
	device_printf(sc->isf_dev, "flash device removed\n");
}

int
isf_attach(struct isf_softc *sc)
{
	uint16_t		id;
	u_long			start, size;
	struct isf_chips	*cp = chip_ids;

	start = rman_get_start(sc->isf_res);
	if (start % 2 != 0) {
		device_printf(sc->isf_dev,
		    "Unsupported flash start alignment %lu\n",
		    start);
		return (ENXIO);
	}

	isf_write_cmd(sc, 0, ISF_CMD_RDI);
	id = isf_read_reg(sc, ISF_REG_ID);
	while (cp->chip_id != id)
		cp++;
	if (cp->chip_desc == NULL) {
		device_printf(sc->isf_dev,
		    "Unsupported device ID 0x%04x\n", id);
		return (ENXIO);
	}
	isf_write_cmd(sc, 0, ISF_CMD_RA);

	size = rman_get_size(sc->isf_res);
	if (size != cp->chip_size) {
		device_printf(sc->isf_dev,
		    "Unsupported flash size %lu\n", size);
		return (ENXIO);
	}

	bioq_init(&sc->isf_bioq);
	ISF_LOCK_INIT(sc);
	sc->isf_disk = NULL;
	isf_disk_insert(sc, size);
	return(0);
}

void
isf_detach(struct isf_softc *sc)
{

	/*
	 * Simulate a disk removal if one is present to deal with any pending
	 * or queued I/O.  This will occur as a result of a device driver
	 * detach -- the Intel StrataFlash has no notion of removal itself.
	 *
	 * XXXRW: Is the locking here right?
	 */
	ISF_LOCK(sc);
	isf_disk_remove(sc);
	bioq_flush(&sc->isf_bioq, NULL, ENXIO);
	KASSERT(bioq_first(&sc->isf_bioq) == NULL,
	    ("%s: non-empty bioq", __func__));
	ISF_UNLOCK(sc);
	ISF_LOCK_DESTROY(sc);
}
OpenPOWER on IntegriCloud