eeprom.c revision 1.2 1 /* $NetBSD: eeprom.c,v 1.2 1994/10/26 09:08:34 cgd 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/conf.h>
40 #include <sys/buf.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43
44 #include <machine/obio.h>
45 #include <machine/eeprom.h>
46
47 #define HZ 100
48
49 static int ee_update(caddr_t buf, int off, int cnt);
50
51 static char *eeprom_va;
52 static int ee_busy, ee_want;
53
54 void eeprom_init()
55 {
56 eeprom_va = obio_find_mapping(OBIO_EEPROM, OBIO_EEPROM_SIZE);
57 }
58
59 static int ee_take() /* Take the lock. */
60 {
61 int error = 0;
62 while (ee_busy) {
63 ee_want = 1;
64 error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
65 ee_want = 0;
66 if (error) /* interrupted */
67 goto out;
68 }
69 ee_busy = 1;
70 out:
71 return error;
72 }
73
74 static void ee_give() /* Give the lock. */
75 {
76 ee_busy = 0;
77 if (ee_want) {
78 ee_want = 0;
79 wakeup(&ee_busy);
80 }
81 }
82
83 int eeprom_uio(struct uio *uio)
84 {
85 int error;
86 int off; /* NOT off_t */
87 u_int cnt;
88 caddr_t va;
89 caddr_t buf = (caddr_t)0;
90
91 off = uio->uio_offset;
92 if (off >= OBIO_EEPROM_SIZE)
93 return (EFAULT);
94
95 cnt = uio->uio_resid;
96 if (cnt > (OBIO_EEPROM_SIZE - off))
97 cnt = (OBIO_EEPROM_SIZE - off);
98
99 if ((error = ee_take()) != 0)
100 return (error);
101
102 if (eeprom_va == NULL) {
103 error = ENXIO;
104 goto out;
105 }
106
107 va = eeprom_va;
108 if (uio->uio_rw != UIO_READ) {
109 /* Write requires a temporary buffer. */
110 buf = malloc(OBIO_EEPROM_SIZE, M_DEVBUF, M_WAITOK);
111 if (!buf) {
112 error = EAGAIN;
113 goto out;
114 }
115 va = buf;
116 }
117
118 if ((error = uiomove(va + off, (int)cnt, uio)) != 0)
119 goto out;
120
121 if (uio->uio_rw != UIO_READ)
122 error = ee_update(buf, off, cnt);
123
124 out:
125 if (buf)
126 free(buf, M_DEVBUF);
127 ee_give();
128 return (error);
129 }
130
131 /*
132 * Update the EEPROM from the passed buf.
133 */
134 static int ee_update(char *buf, int off, int cnt)
135 {
136 volatile char *ep;
137 char *bp;
138
139 if (eeprom_va == NULL)
140 return (ENXIO);
141
142 ep = eeprom_va + off;
143 bp = buf + off;
144
145 while (cnt > 0) {
146 /*
147 * DO NOT WRITE IT UNLESS WE HAVE TO because the
148 * EEPROM has a limited number of write cycles.
149 * After some number of writes it just fails!
150 */
151 if (*ep != *bp) {
152 *ep = *bp;
153 /*
154 * We have written the EEPROM, so now we must
155 * sleep for at least 10 milliseconds while
156 * holding the lock to prevent all access to
157 * the EEPROM while it recovers.
158 */
159 (void)tsleep(eeprom_va, PZERO-1, "eeprom", HZ/50);
160 }
161 /* Make sure the write worked. */
162 if (*ep != *bp)
163 return (EIO);
164 ep++;
165 bp++;
166 cnt--;
167 }
168 }
169
170 /*
171 * Read a byte out of the EEPROM. This is called from
172 * things like the zs driver very early to find out
173 * which device should be used as the console.
174 */
175 int ee_get_byte(int off, int canwait)
176 {
177 int c = -1;
178 if ((off < 0) || (off >= OBIO_EEPROM_SIZE))
179 goto out;
180 if (eeprom_va == NULL)
181 goto out;
182
183 if (canwait) {
184 if (ee_take())
185 goto out;
186 } else {
187 if (ee_busy)
188 goto out;
189 }
190
191 c = eeprom_va[off] & 0xFF;
192
193 if (canwait)
194 ee_give();
195 out:
196 return c;
197 }
198