summaryrefslogtreecommitdiff
path: root/os/port/flashintel
blob: f0093acf1c830c0cf8858febbe7871a098a45347 (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

enum {
	DQ7 = I(0x80),
	DQ6 = I(0x40),
	DQ5 = I(0x20),
	DQ4 = I(0x10),
	DQ3 = I(0x08),
	DQ2 = I(0x04),
	DQ1 = I(0x02),
	DQ0 = I(0x01),
};

/*
 * intel algorithm
 */

enum {
	ReadID = I(0x90),
	ClearStatus = I(0x50),
	ReadStatus = I(0x70),
	Program = I(0x40),
	BlockErase = I(0x20),
	Confirm = I(0xD0),
};

#define	DPRINT	if(0)print
#define	EPRINT	if(1)print

static char*
intelwait(Flash *f, void *p, ulong ticks)
{
	ulong csr, mask;

	ticks += m->ticks+1;
	mask = f->cmask;
	for(;;){
		if(f->width == 2)
			csr = *(ushort*)p;
		else
			csr = *(ulong*)p;
		if((csr & mask) == (DQ7 & mask))
			break;
		sched();
		if(m->ticks >= ticks)
			return "flash write timed out";
	}
	if(csr & (DQ5|DQ4|DQ3|DQ1)){
		EPRINT("flash: error: %8.8lux %8.8lux\n", p, csr);
		if(csr & DQ1)
			return "flash block locked";
		if(csr & DQ3)
			return "low flash programming voltage";
		return Eio;
	}
	return nil;
}

static int
intelerase(Flash *f, Flashregion *r, ulong addr)
{
	int s;
	char *e;

	DPRINT("flash: erase zone %8.8lux\n", addr);
	if(addr & (r->erasesize-1))
		return -1;	/* bad zone */
	if(f->width == 2){
		ushort *p = (ushort*)((ulong)f->addr + addr);
		s = splhi();
		*p = BlockErase & f->cmask;
		*p = Confirm & f->cmask;
		splx(s);
		e = intelwait(f, p, MS2TK(8*1000));
		*p = ClearStatus & f->cmask;
		*p = ReadArray & f->cmask;
	}else{
		ulong *p = (ulong*)((ulong)f->addr + addr);
		s = splhi();
		*p = BlockErase & f->cmask;
		*p = Confirm & f->cmask;
		splx(s);
		e = intelwait(f, p, MS2TK(8*1000));
		*p = ClearStatus & f->cmask;
		*p = ReadArray & f->cmask;
	}
	if(e != nil)
		error(e);
	return 0;
}

static int
intelwrite2(Flash *f, ulong offset, void *buf, long n)
{
	ushort *a, *v;
	ulong w;
	int s;
	char *e;

	if(((ulong)f->addr|offset|n)&(f->width-1))
		return -1;
	a = (ushort*)((ulong)f->addr + offset);
	n /= f->width;
	v = buf;
	if(waserror()){
		*a = ClearStatus & f->cmask;
		*a = ReadArray & f->cmask;
		nexterror();
	}
	for(; --n >= 0; v++, a++){
		w = *a;
		DPRINT("flash: write %p %#ulx -> %#lux\n", a, w, (ulong)*v);
		if(w == *v)
			continue;	/* already set */
		if(~w & *v)
			error("flash not erased");
		s = splhi();
		*a = Program & f->cmask;	/* program */
		*a = *v;
		splx(s);
		microdelay(8);
		e = intelwait(f, a, 5);
		*a = ClearStatus & f->cmask;
		*a = ReadArray & f->cmask;
		if(e != nil)
			error(e);
		w = *a;
		if(w != *v){
			EPRINT("flash: write %p %#8.8lux -> %#8.8lux failed\n", a, w, (ulong)*v);
			error(Eio);
		}
	}
	poperror();
	return 0;
}

static int
intelwrite4(Flash *f, ulong offset, void *buf, long n)
{
	ulong *a, *v;
	ulong w;
	int s;
	char *e;

	if(((ulong)f->addr|offset|n)&(f->width-1))
		return -1;
	a = (ulong*)((ulong)f->addr + offset);
	n /= f->width;
	v = buf;
	if(waserror()){
		*a = ClearStatus & f->cmask;
		*a = ReadArray & f->cmask;
		nexterror();
	}
	for(; --n >= 0; v++, a++){
		w = *a;
		DPRINT("flash: write %p %#ulx -> %#lux\n", a, w, (ulong)*v);
		if(w == *v)
			continue;	/* already set */
		if(~w & *v)
			error("flash not erased");
		s = splhi();
		*a = Program;	/* program */
		*a = *v;
		splx(s);
		microdelay(8);
		e = intelwait(f, a, 5);
		*a = ClearStatus & f->cmask;
		*a = ReadArray & f->cmask;
		if(e != nil)
			error(e);
		w = *a;
		if(w != *v){
			EPRINT("flash: write %p %#8.8lux -> %#8.8lux failed\n", a, w, *v);
			error(Eio);
		}
	}
	poperror();
	return 0;
}