eeprom.c revision 1.25 1 /* $NetBSD: eeprom.c,v 1.25 2003/07/15 03:36:14 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Access functions for the EEPROM (Electrically Eraseable PROM)
41 * The main reason for the existence of this module is to
42 * handle the painful task of updating the EEPROM contents.
43 * After a write, it must not be touched for 10 milliseconds.
44 * (See the Sun-3 Architecture Manual sec. 5.9)
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: eeprom.c,v 1.25 2003/07/15 03:36:14 lukem Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/conf.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/proc.h>
57 #include <sys/kernel.h>
58
59 #include <machine/autoconf.h>
60 #include <machine/idprom.h>
61 #include <machine/eeprom.h>
62
63 #ifndef EEPROM_SIZE
64 #define EEPROM_SIZE 0x800
65 #endif
66
67 struct eeprom *eeprom_copy; /* soft copy. */
68
69 static int ee_update(int off, int cnt);
70
71 static char *eeprom_va; /* mapping to actual device */
72 static int ee_size; /* size of usable part. */
73
74 static int ee_busy, ee_want; /* serialization */
75
76 static int eeprom_match __P((struct device *, struct cfdata *, void *));
77 static void eeprom_attach __P((struct device *, struct device *, void *));
78
79 CFATTACH_DECL(eeprom, sizeof(struct device),
80 eeprom_match, eeprom_attach, NULL, NULL);
81
82 static int
83 eeprom_match(parent, cf, args)
84 struct device *parent;
85 struct cfdata *cf;
86 void *args;
87 {
88 struct confargs *ca = args;
89
90 /* This driver only supports one instance. */
91 if (eeprom_va != NULL)
92 return (0);
93
94 if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
95 return (0);
96
97 return (1);
98 }
99
100 static void
101 eeprom_attach(parent, self, args)
102 struct device *parent;
103 struct device *self;
104 void *args;
105 {
106 struct confargs *ca = args;
107 char *src, *dst, *lim;
108
109 printf("\n");
110 #ifdef DIAGNOSTIC
111 if (sizeof(struct eeprom) != EEPROM_SIZE)
112 panic("eeprom struct wrong");
113 #endif
114
115 ee_size = EEPROM_SIZE;
116 eeprom_va = bus_mapin(ca->ca_bustype, ca->ca_paddr, ee_size);
117 if (!eeprom_va)
118 panic("eeprom_attach");
119
120 /* Keep a "soft" copy of the EEPROM to make access simpler. */
121 eeprom_copy = malloc(ee_size, M_DEVBUF, M_NOWAIT);
122 if (eeprom_copy == 0)
123 panic("eeprom_attach: malloc eeprom_copy");
124
125 /*
126 * On the 3/80, do not touch the last 40 bytes!
127 * Writes there are ignored, reads show zero.
128 * Reduce ee_size and clear the last part of the
129 * soft copy. Note: ee_update obeys ee_size.
130 */
131 if (cpu_machine_id == SUN3X_MACH_80)
132 ee_size -= 40;
133
134 /* Do only byte access in the EEPROM. */
135 src = eeprom_va;
136 dst = (char*) eeprom_copy;
137 lim = dst + ee_size;
138
139 do *dst++ = *src++;
140 while (dst < lim);
141
142 if (ee_size < EEPROM_SIZE) {
143 /* Clear out the last part. */
144 memset(dst, 0, (EEPROM_SIZE - ee_size));
145 }
146 }
147
148
149 /* Take the lock. */
150 static int
151 ee_take __P((void))
152 {
153 int error = 0;
154 while (ee_busy) {
155 ee_want = 1;
156 error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
157 ee_want = 0;
158 if (error) /* interrupted */
159 return error;
160 }
161 ee_busy = 1;
162 return error;
163 }
164
165 /* Give the lock. */
166 static void
167 ee_give __P((void))
168 {
169 ee_busy = 0;
170 if (ee_want) {
171 ee_want = 0;
172 wakeup(&ee_busy);
173 }
174 }
175
176 /*
177 * This is called by mem.c to handle /dev/eeprom
178 */
179 int
180 eeprom_uio(struct uio *uio)
181 {
182 int cnt, error;
183 int off; /* NOT off_t */
184 caddr_t va;
185
186 if (eeprom_copy == NULL)
187 return (ENXIO);
188
189 off = uio->uio_offset;
190 if ((off < 0) || (off > EEPROM_SIZE))
191 return (EFAULT);
192
193 cnt = min(uio->uio_resid, (EEPROM_SIZE - off));
194 if (cnt == 0)
195 return (0); /* EOF */
196
197 va = ((char*)eeprom_copy) + off;
198 error = uiomove(va, (int)cnt, uio);
199
200 /* If we wrote the rambuf, update the H/W. */
201 if (!error && (uio->uio_rw != UIO_READ)) {
202 error = ee_take();
203 if (!error)
204 error = ee_update(off, cnt);
205 ee_give();
206 }
207
208 return (error);
209 }
210
211 /*
212 * Update the EEPROM from the soft copy.
213 * Other than the attach function, this is
214 * the ONLY place we touch the EEPROM H/W.
215 */
216 static int
217 ee_update(int off, int cnt)
218 {
219 volatile char *ep;
220 char *bp;
221 int errcnt;
222
223 if (eeprom_va == NULL)
224 return (ENXIO);
225
226 /*
227 * This check is NOT redundant with the one in
228 * eeprom_uio above if we are on a 3/80 where
229 * ee_size < EEPROM_SIZE
230 */
231 if (cnt > (ee_size - off))
232 cnt = (ee_size - off);
233
234 bp = ((char*)eeprom_copy) + off;
235 ep = eeprom_va + off;
236 errcnt = 0;
237
238 while (cnt > 0) {
239 /*
240 * DO NOT WRITE IT UNLESS WE HAVE TO because the
241 * EEPROM has a limited number of write cycles.
242 * After some number of writes it just fails!
243 */
244 if (*ep != *bp) {
245 *ep = *bp;
246 /*
247 * We have written the EEPROM, so now we must
248 * sleep for at least 10 milliseconds while
249 * holding the lock to prevent all access to
250 * the EEPROM while it recovers.
251 */
252 (void)tsleep(eeprom_va, PZERO-1, "eeprom", hz/50);
253 }
254 /* Make sure the write worked. */
255 if (*ep != *bp)
256 errcnt++;
257 ep++;
258 bp++;
259 cnt--;
260 }
261 return (errcnt ? EIO : 0);
262 }
263