xref: /netbsd-src/sys/arch/sun3/dev/eeprom.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: eeprom.c,v 1.28 2005/12/11 12:19:20 christos 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.28 2005/12/11 12:19:20 christos 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(struct device *, struct cfdata *, void *);
77 static void eeprom_attach(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(struct device *parent, struct cfdata *cf, void *args)
84 {
85 	struct confargs *ca = args;
86 
87 	/* This driver only supports one instance. */
88 	if (eeprom_va != NULL)
89 		return (0);
90 
91 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
92 		return (0);
93 
94 	return (1);
95 }
96 
97 static void
98 eeprom_attach(struct device *parent, struct device *self, void *args)
99 {
100 	struct confargs *ca = args;
101 	char *src, *dst, *lim;
102 
103 	printf("\n");
104 #ifdef	DIAGNOSTIC
105 	if (sizeof(struct eeprom) != EEPROM_SIZE)
106 		panic("eeprom struct wrong");
107 #endif
108 
109 	ee_size = EEPROM_SIZE;
110 	eeprom_va = bus_mapin(ca->ca_bustype, ca->ca_paddr, ee_size);
111 	if (!eeprom_va)
112 		panic("eeprom_attach");
113 
114 	/* Keep a "soft" copy of the EEPROM to make access simpler. */
115 	eeprom_copy = malloc(ee_size, M_DEVBUF, M_NOWAIT);
116 	if (eeprom_copy == 0)
117 		panic("eeprom_attach: malloc eeprom_copy");
118 
119 	/*
120 	 * On the 3/80, do not touch the last 40 bytes!
121 	 * Writes there are ignored, reads show zero.
122 	 * Reduce ee_size and clear the last part of the
123 	 * soft copy.  Note: ee_update obeys ee_size.
124 	 */
125 	if (cpu_machine_id == ID_SUN3X_80)
126 		ee_size -= 40;
127 
128 	/* Do only byte access in the EEPROM. */
129 	src = eeprom_va;
130 	dst = (char*) eeprom_copy;
131 	lim = dst + ee_size;
132 
133 	do *dst++ = *src++;
134 	while (dst < lim);
135 
136 	if (ee_size < EEPROM_SIZE) {
137 		/* Clear out the last part. */
138 		memset(dst, 0, (EEPROM_SIZE - ee_size));
139 	}
140 }
141 
142 
143 /* Take the lock. */
144 static int
145 ee_take(void)
146 {
147 	int error = 0;
148 
149 	while (ee_busy) {
150 		ee_want = 1;
151 		error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
152 		ee_want = 0;
153 		if (error)	/* interrupted */
154 			return error;
155 	}
156 	ee_busy = 1;
157 	return error;
158 }
159 
160 /* Give the lock. */
161 static void
162 ee_give(void)
163 {
164 	ee_busy = 0;
165 	if (ee_want) {
166 		ee_want = 0;
167 		wakeup(&ee_busy);
168 	}
169 }
170 
171 /*
172  * This is called by mem.c to handle /dev/eeprom
173  */
174 int
175 eeprom_uio(struct uio *uio)
176 {
177 	int cnt, error;
178 	int off;	/* NOT off_t */
179 	caddr_t va;
180 
181 	if (eeprom_copy == NULL)
182 		return (ENXIO);
183 
184 	off = uio->uio_offset;
185 	if ((off < 0) || (off > EEPROM_SIZE))
186 		return (EFAULT);
187 
188 	cnt = min(uio->uio_resid, (EEPROM_SIZE - off));
189 	if (cnt == 0)
190 		return (0);	/* EOF */
191 
192 	va = ((char*)eeprom_copy) + off;
193 	error = uiomove(va, (int)cnt, uio);
194 
195 	/* If we wrote the rambuf, update the H/W. */
196 	if (!error && (uio->uio_rw != UIO_READ)) {
197 		error = ee_take();
198 		if (!error)
199 			error = ee_update(off, cnt);
200 		ee_give();
201 	}
202 
203 	return (error);
204 }
205 
206 /*
207  * Update the EEPROM from the soft copy.
208  * Other than the attach function, this is
209  * the ONLY place we touch the EEPROM H/W.
210  */
211 static int
212 ee_update(int off, int cnt)
213 {
214 	volatile char *ep;
215 	char *bp;
216 	int errcnt;
217 
218 	if (eeprom_va == NULL)
219 		return (ENXIO);
220 
221 	/*
222 	 * This check is NOT redundant with the one in
223 	 * eeprom_uio above if we are on a 3/80 where
224 	 * ee_size < EEPROM_SIZE
225 	 */
226 	if (cnt > (ee_size - off))
227 		cnt = (ee_size - off);
228 
229 	bp = ((char*)eeprom_copy) + off;
230 	ep = eeprom_va + off;
231 	errcnt = 0;
232 
233 	while (cnt > 0) {
234 		/*
235 		 * DO NOT WRITE IT UNLESS WE HAVE TO because the
236 		 * EEPROM has a limited number of write cycles.
237 		 * After some number of writes it just fails!
238 		 */
239 		if (*ep != *bp) {
240 			*ep  = *bp;
241 			/*
242 			 * We have written the EEPROM, so now we must
243 			 * sleep for at least 10 milliseconds while
244 			 * holding the lock to prevent all access to
245 			 * the EEPROM while it recovers.
246 			 */
247 			(void)tsleep(eeprom_va, PZERO-1, "eeprom", hz/50);
248 		}
249 		/* Make sure the write worked. */
250 		if (*ep != *bp)
251 			errcnt++;
252 		ep++;
253 		bp++;
254 		cnt--;
255 	}
256 	return (errcnt ? EIO : 0);
257 }
258