eeprom.c revision 1.7 1 /* $NetBSD: eeprom.c,v 1.7 1996/03/17 02:03:47 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994 Gordon W. Ross
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Access functions for the EEPROM (Electrically Eraseable PROM)
32 * The main reason for the existence of this module is to
33 * handle the painful task of updating the EEPROM contents.
34 * After a write, it must not be touched for 10 milliseconds.
35 * (See the Sun-3 Architecture Manual sec. 5.9)
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/conf.h>
42 #include <sys/buf.h>
43 #include <sys/malloc.h>
44
45 #include <machine/autoconf.h>
46 #include <machine/obio.h>
47 #include <machine/eeprom.h>
48
49 #define HZ 100 /* XXX */
50
51 int ee_console; /* for convenience of drivers */
52
53 static int ee_update(caddr_t buf, int off, int cnt);
54
55 static char *eeprom_va;
56 static int ee_busy, ee_want;
57
58 int eeprom_match __P((struct device *, void *vcf, void *args));
59 void eeprom_attach __P((struct device *, struct device *, void *));
60
61 struct cfattach eeprom_ca = {
62 sizeof(struct device), eeprom_match, eeprom_attach
63 };
64
65 struct cfdriver eeprom_cd = {
66 NULL, "eeprom", DV_DULL
67 };
68
69 /* Called very early by internal_configure. */
70 void eeprom_init()
71 {
72 eeprom_va = obio_find_mapping(OBIO_EEPROM, OBIO_EEPROM_SIZE);
73 ee_console = ((struct eeprom *)eeprom_va)->eeConsole;
74 }
75
76 int eeprom_match(parent, vcf, args)
77 struct device *parent;
78 void *vcf, *args;
79 {
80 struct cfdata *cf = vcf;
81 struct confargs *ca = args;
82
83 /* This driver only supports one unit. */
84 if (cf->cf_unit != 0)
85 return (0);
86 if (eeprom_va == NULL)
87 return (0);
88 if (ca->ca_paddr == -1)
89 ca->ca_paddr = OBIO_EEPROM;
90 return (1);
91 }
92
93 void eeprom_attach(parent, self, args)
94 struct device *parent;
95 struct device *self;
96 void *args;
97 {
98 struct confargs *ca = args;
99
100 printf("\n");
101 }
102
103
104 static int ee_take() /* Take the lock. */
105 {
106 int error = 0;
107 while (ee_busy) {
108 ee_want = 1;
109 error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
110 ee_want = 0;
111 if (error) /* interrupted */
112 goto out;
113 }
114 ee_busy = 1;
115 out:
116 return error;
117 }
118
119 static void ee_give() /* Give the lock. */
120 {
121 ee_busy = 0;
122 if (ee_want) {
123 ee_want = 0;
124 wakeup(&ee_busy);
125 }
126 }
127
128 int eeprom_uio(struct uio *uio)
129 {
130 int error;
131 int off; /* NOT off_t */
132 u_int cnt;
133 caddr_t va;
134 caddr_t buf = (caddr_t)0;
135
136 off = uio->uio_offset;
137 if (off >= OBIO_EEPROM_SIZE)
138 return (EFAULT);
139
140 cnt = uio->uio_resid;
141 if (cnt > (OBIO_EEPROM_SIZE - off))
142 cnt = (OBIO_EEPROM_SIZE - off);
143
144 if ((error = ee_take()) != 0)
145 return (error);
146
147 if (eeprom_va == NULL) {
148 error = ENXIO;
149 goto out;
150 }
151
152 va = eeprom_va;
153 if (uio->uio_rw != UIO_READ) {
154 /* Write requires a temporary buffer. */
155 buf = malloc(OBIO_EEPROM_SIZE, M_DEVBUF, M_WAITOK);
156 if (!buf) {
157 error = EAGAIN;
158 goto out;
159 }
160 va = buf;
161 }
162
163 if ((error = uiomove(va + off, (int)cnt, uio)) != 0)
164 goto out;
165
166 if (uio->uio_rw != UIO_READ)
167 error = ee_update(buf, off, cnt);
168
169 out:
170 if (buf)
171 free(buf, M_DEVBUF);
172 ee_give();
173 return (error);
174 }
175
176 /*
177 * Update the EEPROM from the passed buf.
178 */
179 static int ee_update(char *buf, int off, int cnt)
180 {
181 volatile char *ep;
182 char *bp;
183
184 if (eeprom_va == NULL)
185 return (ENXIO);
186
187 ep = eeprom_va + off;
188 bp = buf + off;
189
190 while (cnt > 0) {
191 /*
192 * DO NOT WRITE IT UNLESS WE HAVE TO because the
193 * EEPROM has a limited number of write cycles.
194 * After some number of writes it just fails!
195 */
196 if (*ep != *bp) {
197 *ep = *bp;
198 /*
199 * We have written the EEPROM, so now we must
200 * sleep for at least 10 milliseconds while
201 * holding the lock to prevent all access to
202 * the EEPROM while it recovers.
203 */
204 (void)tsleep(eeprom_va, PZERO-1, "eeprom", HZ/50);
205 }
206 /* Make sure the write worked. */
207 if (*ep != *bp)
208 return (EIO);
209 ep++;
210 bp++;
211 cnt--;
212 }
213 return(0);
214 }
215
216 /*
217 * Read a byte out of the EEPROM. This is called from
218 * things like the zs driver very early to find out
219 * which device should be used as the console.
220 */
221 int ee_get_byte(int off, int canwait)
222 {
223 int c = -1;
224 if ((off < 0) || (off >= OBIO_EEPROM_SIZE))
225 goto out;
226 if (eeprom_va == NULL)
227 goto out;
228
229 if (canwait) {
230 if (ee_take())
231 goto out;
232 } else {
233 if (ee_busy)
234 goto out;
235 }
236
237 c = eeprom_va[off] & 0xFF;
238
239 if (canwait)
240 ee_give();
241 out:
242 return c;
243 }
244