summaryrefslogtreecommitdiff
path: root/os/port/flashcfi8.c
blob: 099267b68462983fcf0aaa4a21283675d6b73930 (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
#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"

/*
 * Common Flash Interface (1x8 and 2x8)
 */

/* interleaved flash has chips at even and odd word addresses */
#define	I(x)	(((x)<<24)|((x)<<16)|((x)<<8)|(x))

enum {
	ReadArray = I(0xFF),
	ReadQuery = I(0x98),
};

/* TO DO: flash amd */
#include "flashintel"

static int
cfiget1(Flash *f, ulong a)
{
	ulong v;

	v = flashget(f, a);
//iprint("%.8lux->%.4ux\n", a, v);
	return v & 0xFF;
}

static int
cfiget2(Flash *f, ulong i)
{
	return (cfiget1(f, i+1)<<8) | cfiget1(f, i);
}

static int
cfiquery(Flash *f)
{
	Flashregion *r;
	ulong addr;
	int i;

	flashput(f, 0x55, ReadQuery);
	if(!(cfiget1(f, 0x10) == 'Q' && cfiget1(f, 0x11) == 'R' && cfiget1(f, 0x12) == 'Y'))	/* TO DO: detect interleave */
		return 0;
	f->alg = cfiget2(f, 0x13);
	i = cfiget1(f, 0x27);
	if(i > 0 && i < 32)
		i = 1<<i;
	else
		i = 0;
	f->devsize = i;
	f->size = f->devsize;
	if(f->interleave)
		f->size *= 2;
	i = cfiget2(f, 0x2A);
	if(i > 0 && i < 32)
		i = 1<<i;
	else
		i = 0;
	f->maxwb = i;
	f->nr = cfiget1(f, 0x2C);
	if(f->nr != 0){
		addr = 0;
		for(i=0; i<f->nr; i++){
			r = &f->regions[i];
			r->n = cfiget2(f, 0x2D+4*i)+1;
			r->erasesize = cfiget2(f, 0x2D+2+4*i)*256;
			if(r->erasesize == 0)
				r->erasesize = 128;
			if(f->interleave)
				r->erasesize *= 2;	/* TO DO */
			r->start = addr;
			r->end = r->start + r->n*r->erasesize;
		}
		if(1){
			iprint("cfi: devsize=%lud maxwb=%d\n", f->devsize, f->maxwb);
			for(i=0; i<f->nr; i++){
				r = &f->regions[i];
				iprint("flash %d: %d %lud %8.8lux %8.8lux\n", i, r->n, r->erasesize, r->start, r->end);
			}
		}
	}else{
		f->nr = 1;
		f->regions[0] = (Flashregion){1, 0, f->devsize, f->devsize, 0};
	}
	return 1;
}

static int
reset(Flash *f)
{
	if(f->xip)
		return -1;	/* can't use this interface if executing from flash */
	if(f->width == 0)
		f->width = 1;
	if(!cfiquery(f) && f->bshift==0)
		f->bshift = 1;	/* try this */
	if(!cfiquery(f) || f->alg != 1 && f->alg != 3){
		if(f->alg == 2){
			print("amd algorithm\n");
			goto ok;
		}
		/* apparently not CFI: try to reset to read mode before return */
		flashput(f, 0x55, ClearStatus);
		flashput(f, 0x55, ReadArray);
		return -1;
	}
ok:
	switch(f->width){
	case 1:
		f->cmask = 0x00FF;
		break;
	case 2:
		f->cmask = 0xFFFF;
		break;
	case 4:
		f->cmask = 0xFFFFFFFF;
		break;
	}
	flashput(f, 0x55, ClearStatus);
	flashput(f, 0x55, ReadID);
	f->id = cfiget1(f, 0x00);
	f->devid = cfiget1(f, 0x01);
	flashput(f, 0x55, ClearStatus);
	flashput(f, 0x55, ReadArray);
	f->erasezone = nil;
	f->suspend = nil;
	f->resume = nil;
	return 0;
}

void
flashcfi8link(void)
{
	addflashcard("cfi8", reset);
}