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
|
/* $OpenBSD: flash.c,v 1.5 1996/04/28 11:11:50 deraadt Exp $ */
/*
* Copyright (c) 1995 Theo de Raadt
* All rights reserved.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed under OpenBSD by
* Theo de Raadt for Willowglen Singapore.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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/param.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/tty.h>
#include <sys/uio.h>
#include <sys/callout.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/syslog.h>
#include <sys/fcntl.h>
#include <sys/device.h>
#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <machine/mioctl.h>
#include "mc.h"
#if NMC > 0
#include <mvme68k/dev/mcreg.h>
#endif
#include <mvme68k/dev/flashreg.h>
struct flashsoftc {
struct device sc_dev;
u_char * sc_paddr;
volatile u_char * sc_vaddr;
u_char sc_manu;
u_char sc_ii;
int sc_len;
int sc_zonesize;
};
void flashattach __P((struct device *, struct device *, void *));
int flashmatch __P((struct device *, void *, void *));
struct cfattach flash_ca = {
sizeof(struct flashsoftc), flashmatch, flashattach
};
struct cfdriver flash_cd = {
NULL, "flash", DV_DULL, 0
};
int flashwritebyte __P((struct flashsoftc *sc, int addr, u_char val));
int flasherasezone __P((struct flashsoftc *sc, int addr));
struct flashii intel_flashii[] = {
{ "28F008SA", FLII_INTEL_28F008SA, 1024*1024, 64*1024 },
{ "28F008SA-L", FLII_INTEL_28F008SA_L, 1024*1024, 64*1024 },
{ NULL },
};
struct flashmanu {
char *name;
u_char manu;
struct flashii *flashii;
} flashmanu[] = {
{ "intel", FLMANU_INTEL, intel_flashii },
{ NULL }
};
int
flashmatch(parent, cf, args)
struct device *parent;
void *cf;
void *args;
{
struct confargs *ca = args;
#ifdef MVME147
if (cputyp == CPU_147)
return (0);
#endif
#ifdef MVME167
/*
* XXX: 166 has 4 byte-wide flash rams side-by-side, and
* isn't supported (yet).
*/
if (cputyp == CPU_166)
return (0);
if (cputyp == CPU_167)
return (0);
#endif
if (badpaddr(ca->ca_paddr, 1))
return (0);
/*
* XXX: need to determine if it is flash or rom
*/
return (1);
}
void
flashattach(parent, self, args)
struct device *parent, *self;
void *args;
{
struct flashsoftc *sc = (struct flashsoftc *)self;
struct confargs *ca = args;
int manu, ident;
sc->sc_paddr = ca->ca_paddr;
sc->sc_vaddr = mapiodev(sc->sc_paddr, NBPG);
switch (cputyp) {
#ifdef MVME162
case CPU_162:
mc_enableflashwrite(1);
break;
#endif
}
/* read manufacturer and product identifier from flash */
sc->sc_vaddr[0] = FLCMD_RESET;
sc->sc_vaddr[0] = FLCMD_READII;
sc->sc_manu = sc->sc_vaddr[0];
sc->sc_ii = sc->sc_vaddr[1];
sc->sc_vaddr[0] = FLCMD_RESET;
for (manu = 0; flashmanu[manu].name; manu++)
if (flashmanu[manu].manu == sc->sc_manu)
break;
if (flashmanu[manu].name == NULL) {
printf(": unknown manu 0x%02x ident %02x\n",
sc->sc_manu, sc->sc_ii);
return;
}
for (ident = 0; flashmanu[manu].flashii[ident].name; ident++)
if (flashmanu[manu].flashii[ident].ii == sc->sc_ii)
break;
if (flashmanu[manu].flashii[ident].name == NULL) {
printf(": unknown manu %s ident 0x%02x\n",
flashmanu[manu].name, sc->sc_ii);
return;
}
sc->sc_len = flashmanu[manu].flashii[ident].size;
sc->sc_zonesize = flashmanu[manu].flashii[ident].zonesize;
printf(": %s %s len %d", flashmanu[manu].name,
flashmanu[manu].flashii[ident].name, sc->sc_len);
sc->sc_vaddr[0] = FLCMD_CLEARSTAT;
sc->sc_vaddr[0] = FLCMD_RESET;
unmapiodev((void *)sc->sc_vaddr, NBPG);
sc->sc_vaddr = mapiodev(sc->sc_paddr, sc->sc_len);
if (sc->sc_vaddr == NULL) {
sc->sc_len = 0;
printf(" -- failed to map");
}
printf("\n");
}
u_char *
flashsavezone(sc, start)
struct flashsoftc *sc;
int start;
{
u_char *zone;
zone = (u_char *)malloc(sc->sc_zonesize, M_TEMP, M_WAITOK);
if (!zone)
return (NULL);
sc->sc_vaddr[0] = FLCMD_RESET;
bcopy((u_char *)&sc->sc_vaddr[start], zone, sc->sc_zonesize);
return (zone);
}
int
flashwritezone(sc, zone, start)
struct flashsoftc *sc;
u_char *zone;
int start;
{
u_char sr;
int i;
for (i = 0; i < sc->sc_zonesize; i++) {
if (zone[i] == 0xff)
continue;
sc->sc_vaddr[start + i] = FLCMD_WSETUP;
sc->sc_vaddr[start + i] = zone[i];
do {
sc->sc_vaddr[0] = FLCMD_READSTAT;
sr = sc->sc_vaddr[0];
} while (sr & FLSR_WSMS == 0);
if (sr & FLSR_BWS)
return (i); /* write failed on this byte! */
sc->sc_vaddr[0] = FLCMD_RESET;
}
free(zone, M_TEMP);
return (0);
}
int
flasherasezone(sc, addr)
struct flashsoftc *sc;
int addr;
{
u_char sr;
printf("erasing zone at %d\n", addr);
sc->sc_vaddr[addr] = FLCMD_ESETUP;
sc->sc_vaddr[addr] = FLCMD_ECONFIRM;
sc->sc_vaddr[0] = FLCMD_READSTAT;
sr = sc->sc_vaddr[0];
while ((sr & FLSR_WSMS) == 0) {
sc->sc_vaddr[0] = FLCMD_READSTAT;
sr = sc->sc_vaddr[0];
}
printf("sr=%2x\n", sr);
sc->sc_vaddr[0] = FLCMD_RESET;
if (sr & FLSR_ES)
return (-1);
return (0);
}
/*
* Should add some light retry code. If a write fails see if an
* erase helps the situation... eventually flash rams become
* useless but perhaps we can get just one more cycle out of it.
*/
int
flashwritebyte(sc, addr, val)
struct flashsoftc *sc;
int addr;
u_char val;
{
u_char sr;
sc->sc_vaddr[addr] = FLCMD_CLEARSTAT;
sr = sc->sc_vaddr[0];
sc->sc_vaddr[addr] = FLCMD_WSETUP;
sc->sc_vaddr[addr] = val;
delay(9);
do {
sr = sc->sc_vaddr[addr];
} while ((sr & FLSR_WSMS) == 0);
printf("write status %2x\n", sr);
sc->sc_vaddr[0] = FLCMD_RESET;
if (sr & FLSR_BWS)
return (-1); /* write failed! */
return (0);
}
/*ARGSUSED*/
int
flashopen(dev, flag, mode)
dev_t dev;
int flag, mode;
{
if (minor(dev) >= flash_cd.cd_ndevs ||
flash_cd.cd_devs[minor(dev)] == NULL)
return (ENODEV);
return (0);
}
/*ARGSUSED*/
int
flashclose(dev, flag, mode)
dev_t dev;
int flag, mode;
{
return (0);
}
/*ARGSUSED*/
int
flashioctl(dev, cmd, data, flag, p)
dev_t dev;
u_char *data;
int cmd, flag;
struct proc *p;
{
int unit = minor(dev);
struct flashsoftc *sc = (struct flashsoftc *) flash_cd.cd_devs[unit];
int error = 0;
switch (cmd) {
case MIOCGSIZ:
*(int *)data = sc->sc_len;
break;
default:
error = ENOTTY;
break;
}
return (error);
}
/*ARGSUSED*/
int
flashread(dev, uio, flags)
dev_t dev;
struct uio *uio;
int flags;
{
int unit = minor(dev);
struct flashsoftc *sc = (struct flashsoftc *) flash_cd.cd_devs[unit];
register vm_offset_t v;
register int c;
register struct iovec *iov;
int error = 0;
while (uio->uio_resid > 0 && error == 0) {
iov = uio->uio_iov;
if (iov->iov_len == 0) {
uio->uio_iov++;
uio->uio_iovcnt--;
if (uio->uio_iovcnt < 0)
panic("flashrw");
continue;
}
v = uio->uio_offset;
c = min(iov->iov_len, MAXPHYS);
if (v + c > sc->sc_len)
c = sc->sc_len - v; /* till end of FLASH */
if (c == 0)
return (0);
error = uiomove((u_char *)sc->sc_vaddr + v, c, uio);
}
return (error);
}
/*ARGSUSED*/
int
flashwrite(dev, uio, flags)
dev_t dev;
struct uio *uio;
int flags;
{
int unit = minor(dev);
struct flashsoftc *sc = (struct flashsoftc *) flash_cd.cd_devs[unit];
register vm_offset_t v;
register int c, i, r;
register struct iovec *iov;
int error = 0;
u_char *cmpbuf;
int neederase = 0, needwrite = 0;
int zonestart, zoneoff;
cmpbuf = (u_char *)malloc(sc->sc_zonesize, M_TEMP, M_WAITOK);
if (!cmpbuf)
return (ENOMEM);
while (uio->uio_resid > 0 && error == 0) {
iov = uio->uio_iov;
if (iov->iov_len == 0) {
uio->uio_iov++;
uio->uio_iovcnt--;
if (uio->uio_iovcnt < 0)
panic("flashrw");
continue;
}
/*
* constrain to be at most a zone in size, and
* aligned to be within that one zone only.
*/
v = uio->uio_offset;
zonestart = v & ~(sc->sc_zonesize - 1);
zoneoff = v & (sc->sc_zonesize - 1);
c = min(iov->iov_len, MAXPHYS);
if (v + c > sc->sc_len)
c = sc->sc_len - v; /* till end of FLASH */
if (c > sc->sc_zonesize - zoneoff)
c = sc->sc_zonesize - zoneoff; /* till end of zone */
if (c == 0)
return (0);
error = uiomove((u_char *)cmpbuf, c, uio);
/*
* compare to see if we are going to need a block erase
* operation.
*/
sc->sc_vaddr[0] = FLCMD_RESET;
for (i = 0; i < c; i++) {
u_char x = sc->sc_vaddr[v + i];
if (cmpbuf[i] & ~x)
neederase = 1;
if (cmpbuf[i] != x)
needwrite = 1;
}
if (needwrite && !neederase) {
/*
* we don't need to erase. all the bytes being
* written (thankfully) set bits.
*/
for (i = 0; i < c; i++) {
if (cmpbuf[i] == sc->sc_vaddr[v + i])
continue;
r = flashwritebyte(sc, v + i, cmpbuf[i]);
if (r == 0)
continue;
/*
* this doesn't make sense. we
* thought we didn't need to erase,
* but a write failed. let's try an
* erase operation..
*/
printf("%s: failed write at %d, trying erase\n",
sc->sc_dev.dv_xname, i);
goto tryerase;
}
} else if (neederase) {
u_char *mem;
tryerase:
mem = flashsavezone(sc, zonestart);
for (i = 0; i < c; i++)
mem[zoneoff + i] = cmpbuf[i];
flasherasezone(sc, zonestart);
r = flashwritezone(sc, mem, zonestart);
if (r) {
printf("%s: failed at offset %x\n",
sc->sc_dev.dv_xname, r);
free(mem, M_TEMP);
error = EIO;
}
}
}
free(cmpbuf, M_TEMP);
return (error);
}
int
flashmmap(dev, off, prot)
dev_t dev;
int off, prot;
{
int unit = minor(dev);
struct flashsoftc *sc = (struct flashsoftc *) flash_cd.cd_devs[unit];
/* allow access only in RAM */
if (off > sc->sc_len)
return (-1);
return (m68k_btop(sc->sc_paddr + off));
}
|