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