summaryrefslogtreecommitdiff
path: root/sys/src/9/kw/flashkw.c
blob: e45bc29dec094e2a46cad7d6d2c4b2ea0afe30c9 (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
/*
 * sheevaplug nand flash driver
 *
 * for now separate from (inferno's) os/port/flashnand.c because the flash
 * seems newer, and has different commands, but that is nand-chip specific,
 * not sheevaplug-specific.  they should be merged in future.
 *
 * the sheevaplug has a hynix 4gbit flash chip: hy27uf084g2m.
 * 2048 byte pages, with 64 spare bytes each; erase block size is 128k.
 *
 * it has a "glueless" interface, at 0xf9000000.  that's the address
 * of the data register.  the command and address registers are those
 * or'ed with 1 and 2 respectively.
 *
 * linux uses this layout for the nand flash (from address 0 onwards):
 *	1mb for u-boot
 *	4mb for kernel
 *	507mb for file system
 *
 * this is not so relevant here except for ecc.  the first two areas
 * (u-boot and kernel) are expected to have 4-bit ecc per 512 bytes
 * (but calculated from last byte to first), bad erase blocks skipped.
 * the file system area has 1-bit ecc per 256 bytes.
 */

#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"
#include	"../port/error.h"

#include	"../port/flashif.h"
#include	"../port/nandecc.h"

enum {
	Debug		= 0,

	Nopage		= ~0ul,		/* cache is empty */

	/* vendors */
	Hynix		= 0xad,
	Samsung		= 0xec,

	/* chips */
	Hy27UF084G2M	= 0xdc,

	NandActCEBoot	= 1<<1,
};

typedef struct Nandreg Nandreg;
typedef struct Nandtab Nandtab;
typedef struct Cache Cache;

struct Nandreg {			/* hw registers */
	ulong	rdparms;
	ulong	wrparms;
	uchar	_pad0[0x70 - 0x20];
	ulong	ctl;
};

struct Nandtab {
	int	vid;
	int	did;
	vlong	size;
	char*	name;
};

struct Cache {
	Flash	*flif;
	ulong	pageno;
	ulong	pgsize;			/* r->pagesize */
	char	*page;			/* of pgsize bytes */
};

enum {
	/* commands */
	Readstatus	= 0x70,
	Readid		= 0x90,	/* needs 1 0-address write */
	Resetf		= 0xff,

	/*
	 * needs 5 address writes followed by Readstart,
	 * Readstartcache or Restartcopy.
	 */
	Read		= 0x00,
	Readstart	= 0x30,
	Readstartcache	= 0x31,
	Readstartcopy	= 0x35,
	/* after Readstartcache, to stop reading next pages */
	Readstopcache	= 0x34,

	/* needs 5 address writes, the data, and -start or -cache */
	Program		= 0x80,
	Programstart	= 0x10,
	Programcache	= 0x15,

	Copyback	= 0x85,	/* followed by Programstart */

	/* 3 address writes for block followed by Erasestart */
	Erase		= 0x60,
	Erasestart	= 0xd0,

	Randomread	= 0x85,
	Randomwrite	= 0x05,
	Randomwritestart= 0xe0,

	/* status bits */
	SFail		= 1<<0,
	SCachefail	= 1<<1,
	SIdle		= 1<<5,		/* doesn't seem to come on ever */
	SReady		= 1<<6,
	SNotprotected	= 1<<7,

	Srdymask	= SReady,	/* was SIdle|SReady */
};

Nandtab nandtab[] = {
	{Hynix,		Hy27UF084G2M,	512*MB,	"Hy27UF084G2M"},
	{Samsung,	0xdc,		512*MB,	"Samsung 2Gb"},
};

static Cache cache;

static void
nandcmd(Flash *f, uchar b)
{
	uchar *p = (uchar *)((ulong)f->addr|1);

	*p = b;
	coherence();
}

static void
nandaddr(Flash *f, uchar b)
{
	uchar *p = (uchar *)((ulong)f->addr|2);

	*p = b;
	coherence();
}

static uchar
nandread(Flash *f)
{
	return *(uchar *)f->addr;
}

static void
nandreadn(Flash *f, uchar *buf, long n)
{
	uchar *p = f->addr;

	while(n-- > 0)
		*buf++ = *p;
}

static void
nandwrite(Flash *f, uchar b)
{
	*(uchar *)f->addr = b;
	coherence();
}

static void
nandwriten(Flash *f, uchar *buf, long n)
{
	uchar *p = f->addr;

	while(n-- > 0)
		*p = *buf++;
	coherence();
}

static void
nandclaim(Flash*)
{
	Nandreg *nand = (Nandreg*)soc.nand;

	nand->ctl |= NandActCEBoot;
	coherence();
}

static void
nandunclaim(Flash*)
{
	Nandreg *nand = (Nandreg*)soc.nand;

	nand->ctl &= ~NandActCEBoot;
	coherence();
}


void	mmuidmap(uintptr phys, int mbs);

Nandtab *
findflash(Flash *f, uintptr pa, uchar *id4p)
{
	int i;
	ulong sts;
	uchar maker, device, id3, id4;
	Nandtab *chip;

	mmuidmap(pa, 16);
	f->addr = (void *)pa;

	/* make sure controller is idle */
	nandclaim(f);
	nandcmd(f, Resetf);
	nandunclaim(f);

	nandclaim(f);
	nandcmd(f, Readstatus);
	sts = nandread(f);
	nandunclaim(f);
	for (i = 10; i > 0 && !(sts & SReady); i--) {
		delay(50);
		nandclaim(f);
		nandcmd(f, Readstatus);
		sts = nandread(f);
		nandunclaim(f);
	}
	if(!(sts & SReady))
		return nil;

	nandclaim(f);
	nandcmd(f, Readid);
	nandaddr(f, 0);
	maker = nandread(f);
	device = nandread(f);
	id3 = nandread(f);
	USED(id3);
	id4 = nandread(f);
	nandunclaim(f);
	if (id4p)
		*id4p = id4;

	for(i = 0; i < nelem(nandtab); i++) {
		chip = &nandtab[i];
		if(chip->vid == maker && chip->did == device)
			return chip;
	}
	return nil;
}

int
flashat(Flash *f, uintptr pa)
{
	return findflash(f, pa, nil) != nil;
}

static int
idchip(Flash *f)
{
	uchar id4;
	Flashregion *r;
	Nandtab *chip;
	static int blocksizes[4] = { 64*1024, 128*1024, 256*1024, 0 };
	static int pagesizes[4] = { 1024, 2*1024, 0, 0 };
	static int spares[2] = { 8, 16 };		/* per 512 bytes */

	f->id = 0;
	f->devid = 0;
	f->width = 1;
	chip = findflash(f, (uintptr)f->addr, &id4);
	if (chip == nil)
		return -1;
	f->id = chip->vid;
	f->devid = chip->did;
	f->size = chip->size;
	f->width = 1;
	f->nr = 1;

	r = &f->regions[0];
	r->pagesize = pagesizes[id4 & MASK(2)];
	r->erasesize = blocksizes[(id4 >> 4) & MASK(2)];
	if (r->pagesize == 0 || r->erasesize == 0) {
		iprint("flashkw: bogus flash sizes\n");
		return -1;
	}
	r->n = f->size / r->erasesize;
	r->start = 0;
	r->end = f->size;
	assert(ispow2(r->pagesize));
	r->pageshift = log2(r->pagesize);
	assert(ispow2(r->erasesize));
	r->eraseshift = log2(r->erasesize);
	assert(r->eraseshift >= r->pageshift);
	if (cache.page == nil) {
		cache.pgsize = r->pagesize;
		cache.page = smalloc(r->pagesize);
	}

	r->spares = r->pagesize / 512 * spares[(id4 >> 2) & 1];
	print("#F0: kwnand: %s %,lud bytes pagesize %lud erasesize %,lud"
		" spares per page %lud\n", chip->name, f->size, r->pagesize,
		r->erasesize, r->spares);
	return 0;
}

static int
ctlrwait(Flash *f)
{
	int sts, cnt;

	nandclaim(f);
	for (;;) {
		nandcmd(f, Readstatus);
		for(cnt = 100; cnt > 0 && (nandread(f) & Srdymask) != Srdymask;
		    cnt--)
			microdelay(50);
		nandcmd(f, Readstatus);
		sts = nandread(f);
		if((sts & Srdymask) == Srdymask)
			break;
		print("flashkw: flash ctlr busy, sts %#ux: resetting\n", sts);
		nandcmd(f, Resetf);
	}
	nandunclaim(f);
	return 0;
}

static int
erasezone(Flash *f, Flashregion *r, ulong offset)
{
	int i;
	ulong page, block;
	uchar s;

	if (Debug) {
		print("flashkw: erasezone: offset %#lux, region nblocks %d,"
			" start %#lux, end %#lux\n", offset, r->n, r->start,
			r->end);
		print(" erasesize %lud, pagesize %lud\n",
			r->erasesize, r->pagesize);
	}
	assert(r->erasesize != 0);
	if(offset & (r->erasesize - 1)) {
		print("flashkw: erase offset %lud not block aligned\n", offset);
		return -1;
	}
	page = offset >> r->pageshift;
	block = page >> (r->eraseshift - r->pageshift);
	if (Debug)
		print("flashkw: erase: block %#lux\n", block);

	/* make sure controller is idle */
	if(ctlrwait(f) < 0) {
		print("flashkw: erase: flash busy\n");
		return -1;
	}

	/* start erasing */
	nandclaim(f);
	nandcmd(f, Erase);
	nandaddr(f, page>>0);
	nandaddr(f, page>>8);
	nandaddr(f, page>>16);
	nandcmd(f, Erasestart);

	/* invalidate cache on any erasure (slight overkill) */
	cache.pageno = Nopage;

	/* have to wait until flash is done.  typically ~2ms */
	delay(1);
	nandcmd(f, Readstatus);
	for(i = 0; i < 100; i++) {
		s = nandread(f);
		if(s & SReady) {
			nandunclaim(f);
			if(s & SFail) {
				print("flashkw: erase: failed, block %#lux\n",
					block);
				return -1;
			}
			return 0;
		}
		microdelay(50);
	}
	print("flashkw: erase timeout, block %#lux\n", block);
	nandunclaim(f);
	return -1;
}

static void
flcachepage(Flash *f, ulong page, uchar *buf)
{
	Flashregion *r = &f->regions[0];

	assert(cache.pgsize == r->pagesize);
	cache.flif = f;
	cache.pageno = page;
	/* permit i/o directly to or from the cache */
	if (buf != (uchar *)cache.page)
		memmove(cache.page, buf, cache.pgsize);
}

static int
write1page(Flash *f, ulong offset, void *buf)
{
	int i;
	ulong page, v;
	uchar s;
	uchar *eccp, *p;
	Flashregion *r = &f->regions[0];
	static uchar *oob;

	if (oob == nil)
		oob = smalloc(r->spares);

	page = offset >> r->pageshift;
	if (Debug)
		print("flashkw: write nand offset %#lux page %#lux\n",
			offset, page);

	if(offset & (r->pagesize - 1)) {
		print("flashkw: write offset %lud not page aligned\n", offset);
		return -1;
	}

	p = buf;
	memset(oob, 0xff, r->spares);
	assert(r->spares >= 24);
	eccp = oob + r->spares - 24;
	for(i = 0; i < r->pagesize / 256; i++) {
		v = nandecc(p);
		*eccp++ = v>>8;
		*eccp++ = v>>0;
		*eccp++ = v>>16;
		p += 256;
	}

	if(ctlrwait(f) < 0) {
		print("flashkw: write: nand not ready & idle\n");
		return -1;
	}

	/* write, only whole pages for now, no sub-pages */
	nandclaim(f);
	nandcmd(f, Program);
	nandaddr(f, 0);
	nandaddr(f, 0);
	nandaddr(f, page>>0);
	nandaddr(f, page>>8);
	nandaddr(f, page>>16);
	nandwriten(f, buf, r->pagesize);
	nandwriten(f, oob, r->spares);
	nandcmd(f, Programstart);

	microdelay(100);
	nandcmd(f, Readstatus);
	for(i = 0; i < 100; i++) {
		s = nandread(f);
		if(s & SReady) {
			nandunclaim(f);
			if(s & SFail) {
				print("flashkw: write failed, page %#lux\n",
					page);
				return -1;
			}
			return 0;
		}
		microdelay(10);
	}

	nandunclaim(f);
	flcachepage(f, page, buf);
	print("flashkw: write timeout for page %#lux\n", page);
	return -1;
}

static int
read1page(Flash *f, ulong offset, void *buf)
{
	int i;
	ulong addr, page, w;
	uchar *eccp, *p;
	Flashregion *r = &f->regions[0];
	static uchar *oob;

	if (oob == nil)
		oob = smalloc(r->spares);

	assert(r->pagesize != 0);
	addr = offset & (r->pagesize - 1);
	page = offset >> r->pageshift;
	if(addr != 0) {
		print("flashkw: read1page: read addr %#lux:"
			" must read aligned page\n", addr);
		return -1;
	}

	/* satisfy request from cache if possible */
	if (f == cache.flif && page == cache.pageno &&
	    r->pagesize == cache.pgsize) {
		memmove(buf, cache.page, r->pagesize);
		return 0;
	}

	if (Debug)
		print("flashkw: read offset %#lux addr %#lux page %#lux\n",
			offset, addr, page);

	nandclaim(f);
	nandcmd(f, Read);
	nandaddr(f, addr>>0);
	nandaddr(f, addr>>8);
	nandaddr(f, page>>0);
	nandaddr(f, page>>8);
	nandaddr(f, page>>16);
	nandcmd(f, Readstart);

	microdelay(50);

	nandreadn(f, buf, r->pagesize);
	nandreadn(f, oob, r->spares);

	nandunclaim(f);

	/* verify/correct data. last 8*3 bytes is ecc, per 256 bytes. */
	p = buf;
	assert(r->spares >= 24);
	eccp = oob + r->spares - 24;
	for(i = 0; i < r->pagesize / 256; i++) {
		w = eccp[0] << 8 | eccp[1] << 0 | eccp[2] << 16;
		eccp += 3;
		switch(nandecccorrect(p, nandecc(p), &w, 1)) {
		case NandEccErrorBad:
			print("(page %d)\n", i);
			return -1;
		case NandEccErrorOneBit:
		case NandEccErrorOneBitInEcc:
			print("(page %d)\n", i);
			/* fall through */
		case NandEccErrorGood:
			break;
		}
		p += 256;
	}

	flcachepage(f, page, buf);
	return 0;
}

/*
 * read a page at offset into cache, copy fragment from buf into it
 * at pagoff, and rewrite that page.
 */
static int
rewrite(Flash *f, ulong offset, ulong pagoff, void *buf, ulong size)
{
	if (read1page(f, offset, cache.page) < 0)
		return -1;
	memmove(&cache.page[pagoff], buf, size);
	return write1page(f, offset, cache.page);
}

/* there are no alignment constraints on offset, buf, nor n */
static int
write(Flash *f, ulong offset, void *buf, long n)
{
	uint un, frag, pagoff;
	ulong pgsize;
	uchar *p;
	Flashregion *r = &f->regions[0];

	if(n <= 0)
		panic("flashkw: write: non-positive count %ld", n);
	un = n;
	assert(r->pagesize != 0);
	pgsize = r->pagesize;

	/* if a partial first page exists, update the first page with it. */
	p = buf;
	pagoff = offset % pgsize;
	if (pagoff != 0) {
		frag = pgsize - pagoff;
		if (frag > un)		/* might not extend to end of page */
			frag = un;
		if (rewrite(f, offset - pagoff, pagoff, p, frag) < 0)
			return -1;
		offset += frag;
		p  += frag;
		un -= frag;
	}

	/* copy whole pages */
	while (un >= pgsize) {
		if (write1page(f, offset, p) < 0)
			return -1;
		offset += pgsize;
		p  += pgsize;
		un -= pgsize;
	}

	/* if a partial last page exists, update the last page with it. */
	if (un > 0)
		return rewrite(f, offset, 0, p, un);
	return 0;
}

/* there are no alignment constraints on offset, buf, nor n */
static int
read(Flash *f, ulong offset, void *buf, long n)
{
	uint un, frag, pagoff;
	ulong pgsize;
	uchar *p;
	Flashregion *r = &f->regions[0];

	if(n <= 0)
		panic("flashkw: read: non-positive count %ld", n);
	un = n;
	assert(r->pagesize != 0);
	pgsize = r->pagesize;

	/* if partial 1st page, read it into cache & copy fragment to buf */
	p = buf;
	pagoff = offset % pgsize;
	if (pagoff != 0) {
		frag = pgsize - pagoff;
		if (frag > un)		/* might not extend to end of page */
			frag = un;
		if (read1page(f, offset - pagoff, cache.page) < 0)
			return -1;
		offset += frag;
		memmove(p, &cache.page[pagoff], frag);
		p  += frag;
		un -= frag;
	}

	/* copy whole pages */
	while (un >= pgsize) {
		if (read1page(f, offset, p) < 0)
			return -1;
		offset += pgsize;
		p  += pgsize;
		un -= pgsize;
	}

	/* if partial last page, read into cache & copy initial fragment to buf */
	if (un > 0) {
		if (read1page(f, offset, cache.page) < 0)
			return -1;
		memmove(p, cache.page, un);
	}
	return 0;
}

static int
reset(Flash *f)
{
	if(f->data != nil)
		return 1;
	f->write = write;
 	f->read = read;
	f->eraseall = nil;
	f->erasezone = erasezone;
	f->suspend = nil;
	f->resume = nil;
	f->sort = "nand";
	cache.pageno = Nopage;
	return idchip(f);
}

void
flashkwlink(void)
{
	addflashcard("nand", reset);
}