summaryrefslogtreecommitdiff
path: root/sys/arch/sun3/dev/eeprom.c
blob: e0d096d044848e095b70ee1df937e18a4a8a5f69 (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
/*	$NetBSD: eeprom.c,v 1.6 1995/05/24 20:47:41 gwr Exp $	*/

/*
 * Copyright (c) 1994 Gordon W. Ross
 * 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. 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.
 */

/*
 * Access functions for the EEPROM (Electrically Eraseable PROM)
 * The main reason for the existence of this module is to
 * handle the painful task of updating the EEPROM contents.
 * After a write, it must not be touched for 10 milliseconds.
 * (See the Sun-3 Architecture Manual sec. 5.9)
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/buf.h>
#include <sys/malloc.h>

#include <machine/autoconf.h>
#include <machine/obio.h>
#include <machine/eeprom.h>

#define HZ 100	/* XXX */

int ee_console;		/* for convenience of drivers */

static int ee_update(caddr_t buf, int off, int cnt);

static char *eeprom_va;
static int ee_busy, ee_want;

int eeprom_match __P((struct device *, void *vcf, void *args));
void eeprom_attach __P((struct device *, struct device *, void *));

struct cfdriver eepromcd = {
	NULL, "eeprom", eeprom_match, eeprom_attach,
	DV_DULL, sizeof(struct device), 0 };

/* Called very early by internal_configure. */
void eeprom_init()
{
	eeprom_va = obio_find_mapping(OBIO_EEPROM, OBIO_EEPROM_SIZE);
	ee_console = ((struct eeprom *)eeprom_va)->eeConsole;
}

int eeprom_match(parent, vcf, args)
    struct device *parent;
    void *vcf, *args;
{
    struct cfdata *cf = vcf;
	struct confargs *ca = args;

	/* This driver only supports one unit. */
	if (cf->cf_unit != 0)
		return (0);
	if (eeprom_va == NULL)
		return (0);
	if (ca->ca_paddr == -1)
		ca->ca_paddr = OBIO_EEPROM;
	return (1);
}

void eeprom_attach(parent, self, args)
	struct device *parent;
	struct device *self;
	void *args;
{
	struct confargs *ca = args;

	printf("\n");
}


static int ee_take()	/* Take the lock. */
{
	int error = 0;
	while (ee_busy) {
		ee_want = 1;
		error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
		ee_want = 0;
		if (error)	/* interrupted */
			goto out;
	}
	ee_busy = 1;
 out:
	return error;
}

static void ee_give()	/* Give the lock. */
{
	ee_busy = 0;
	if (ee_want) {
		ee_want = 0;
		wakeup(&ee_busy);
	}
}

int eeprom_uio(struct uio *uio)
{
	int error;
	int off;	/* NOT off_t */
	u_int cnt;
	caddr_t va;
	caddr_t buf = (caddr_t)0;

	off = uio->uio_offset;
	if (off >= OBIO_EEPROM_SIZE)
		return (EFAULT);

	cnt = uio->uio_resid;
	if (cnt > (OBIO_EEPROM_SIZE - off))
		cnt = (OBIO_EEPROM_SIZE - off);

	if ((error = ee_take()) != 0)
		return (error);

	if (eeprom_va == NULL) {
		error = ENXIO;
		goto out;
	}

	va = eeprom_va;
	if (uio->uio_rw != UIO_READ) {
		/* Write requires a temporary buffer. */
		buf = malloc(OBIO_EEPROM_SIZE, M_DEVBUF, M_WAITOK);
		if (!buf) {
			error = EAGAIN;
			goto out;
		}
		va = buf;
	}

	if ((error = uiomove(va + off, (int)cnt, uio)) != 0)
		goto out;

	if (uio->uio_rw != UIO_READ)
		error = ee_update(buf, off, cnt);

 out:
	if (buf)
		free(buf, M_DEVBUF);
	ee_give();
	return (error);
}

/*
 * Update the EEPROM from the passed buf.
 */
static int ee_update(char *buf, int off, int cnt)
{
	volatile char *ep;
	char *bp;

	if (eeprom_va == NULL)
		return (ENXIO);

	ep = eeprom_va + off;
	bp = buf + off;

	while (cnt > 0) {
		/*
		 * DO NOT WRITE IT UNLESS WE HAVE TO because the
		 * EEPROM has a limited number of write cycles.
		 * After some number of writes it just fails!
		 */
		if (*ep != *bp) {
			*ep  = *bp;
			/*
			 * We have written the EEPROM, so now we must
			 * sleep for at least 10 milliseconds while
			 * holding the lock to prevent all access to
			 * the EEPROM while it recovers.
			 */
			(void)tsleep(eeprom_va, PZERO-1, "eeprom", HZ/50);
		}
		/* Make sure the write worked. */
		if (*ep != *bp)
			return (EIO);
		ep++;
		bp++;
		cnt--;
	}
	return(0);
}

/*
 * Read a byte out of the EEPROM.  This is called from
 * things like the zs driver very early to find out
 * which device should be used as the console.
 */
int ee_get_byte(int off, int canwait)
{
	int c = -1;
	if ((off < 0) || (off >= OBIO_EEPROM_SIZE))
		goto out;
	if (eeprom_va == NULL)
		goto out;

	if (canwait) {
		if (ee_take())
			goto out;
	} else {
		if (ee_busy)
			goto out;
	}

	c = eeprom_va[off] & 0xFF;

	if (canwait)
		ee_give();
 out:
	return c;
}