eeprom.c revision 1.17 1 /* $NetBSD: eeprom.c,v 1.17 1998/01/12 20:32:19 thorpej 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/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/conf.h>
51 #include <sys/buf.h>
52 #include <sys/malloc.h>
53 #include <sys/proc.h>
54
55 #include <machine/autoconf.h>
56 #include <machine/obio.h>
57 #include <machine/eeprom.h>
58 #include <machine/machdep.h>
59
60 #define HZ 100 /* XXX */
61
62 #ifndef OBIO_EEPROM_SIZE
63 #define OBIO_EEPROM_SIZE sizeof(struct eeprom)
64 #endif
65
66 static int ee_update(int off, int cnt);
67
68 static char *eeprom_va;
69 static char *ee_rambuf;
70 static int ee_busy, ee_want;
71
72 static int eeprom_match __P((struct device *, struct cfdata *, void *));
73 static void eeprom_attach __P((struct device *, struct device *, void *));
74
75 struct cfattach eeprom_ca = {
76 sizeof(struct device), eeprom_match, eeprom_attach
77 };
78
79 static int
80 eeprom_match(parent, cf, args)
81 struct device *parent;
82 struct cfdata *cf;
83 void *args;
84 {
85 struct confargs *ca = args;
86
87 /* This driver only supports one unit. */
88 if (cf->cf_unit != 0)
89 return (0);
90
91 /* We use obio_mapin(), so require OBIO. */
92 if (ca->ca_bustype != BUS_OBIO)
93 return (0);
94
95 if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
96 return (0);
97
98 return (1);
99 }
100
101 static void
102 eeprom_attach(parent, self, args)
103 struct device *parent;
104 struct device *self;
105 void *args;
106 {
107 struct confargs *ca = args;
108 char *src, *dst, *lim;
109
110 printf("\n");
111
112 eeprom_va = obio_mapin(ca->ca_paddr, OBIO_EEPROM_SIZE);
113 if (!eeprom_va)
114 panic("eeprom_attach");
115
116 /* Keep a "soft" copy of the EEPROM to make access simpler. */
117 ee_rambuf = malloc(sizeof(struct eeprom), M_DEVBUF, M_NOWAIT);
118 if (ee_rambuf == 0)
119 panic("eeprom_attach: malloc ee_rambuf");
120
121 /* Do only byte access in the EEPROM. */
122 src = eeprom_va;
123 dst = ee_rambuf;
124 lim = ee_rambuf + sizeof(struct eeprom);
125 do *dst++ = *src++;
126 while (dst < lim);
127 }
128
129
130 /* Take the lock. */
131 static int
132 ee_take __P((void))
133 {
134 int error = 0;
135 while (ee_busy) {
136 ee_want = 1;
137 error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
138 ee_want = 0;
139 if (error) /* interrupted */
140 return error;
141 }
142 ee_busy = 1;
143 return error;
144 }
145
146 /* Give the lock. */
147 static void
148 ee_give __P((void))
149 {
150 ee_busy = 0;
151 if (ee_want) {
152 ee_want = 0;
153 wakeup(&ee_busy);
154 }
155 }
156
157 /*
158 * This is called by mem.c to handle /dev/eeprom
159 */
160 int
161 eeprom_uio(struct uio *uio)
162 {
163 int cnt, error;
164 int off; /* NOT off_t */
165 caddr_t va;
166
167 if (ee_rambuf == NULL)
168 return (ENXIO);
169
170 off = uio->uio_offset;
171 if ((off < 0) || (off > OBIO_EEPROM_SIZE))
172 return (EFAULT);
173
174 cnt = min(uio->uio_resid, (OBIO_EEPROM_SIZE - off));
175 if (cnt == 0)
176 return (0); /* EOF */
177
178 va = ee_rambuf + off;
179 error = uiomove(va, (int)cnt, uio);
180
181 /* If we wrote the rambuf, update the H/W. */
182 if (!error && (uio->uio_rw != UIO_READ)) {
183 error = ee_take();
184 if (!error)
185 error = ee_update(off, cnt);
186 ee_give();
187 }
188
189 return (error);
190 }
191
192 /*
193 * Update the EEPROM from the soft copy.
194 * Other than the attach function, this is
195 * the ONLY place we touch the EEPROM H/W.
196 */
197 static int
198 ee_update(int off, int cnt)
199 {
200 volatile char *ep;
201 char *bp;
202 int errcnt;
203
204 if (eeprom_va == NULL)
205 return (ENXIO);
206
207 bp = ee_rambuf + off;
208 ep = eeprom_va + off;
209 errcnt = 0;
210
211 while (cnt > 0) {
212 /*
213 * DO NOT WRITE IT UNLESS WE HAVE TO because the
214 * EEPROM has a limited number of write cycles.
215 * After some number of writes it just fails!
216 */
217 if (*ep != *bp) {
218 *ep = *bp;
219 /*
220 * We have written the EEPROM, so now we must
221 * sleep for at least 10 milliseconds while
222 * holding the lock to prevent all access to
223 * the EEPROM while it recovers.
224 */
225 (void)tsleep(eeprom_va, PZERO-1, "eeprom", HZ/50);
226 }
227 /* Make sure the write worked. */
228 if (*ep != *bp)
229 errcnt++;
230 ep++;
231 bp++;
232 cnt--;
233 }
234 return (errcnt ? EIO : 0);
235 }
236