1 /* $NetBSD: eeprom.c,v 1.30 2008/04/28 20:23:37 martin Exp $ */
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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)
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: eeprom.c,v 1.30 2008/04/28 20:23:37 martin Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
48 #include <sys/malloc.h>
50 #include <sys/kernel.h>
52 #include <machine/autoconf.h>
53 #include <machine/idprom.h>
54 #include <machine/eeprom.h>
57 #define EEPROM_SIZE 0x800
60 struct eeprom
*eeprom_copy
; /* soft copy. */
62 static int ee_update(int off
, int cnt
);
64 static uint8_t *eeprom_va
; /* mapping to actual device */
65 static int ee_size
; /* size of usable part. */
67 static int ee_busy
, ee_want
; /* serialization */
69 static int eeprom_match(device_t
, cfdata_t
, void *);
70 static void eeprom_attach(device_t
, device_t
, void *);
72 CFATTACH_DECL_NEW(eeprom
, 0,
73 eeprom_match
, eeprom_attach
, NULL
, NULL
);
76 eeprom_match(device_t parent
, cfdata_t cf
, void *args
)
78 struct confargs
*ca
= args
;
80 /* This driver only supports one instance. */
81 if (eeprom_va
!= NULL
)
84 if (bus_peek(ca
->ca_bustype
, ca
->ca_paddr
, 1) == -1)
91 eeprom_attach(device_t parent
, device_t self
, void *args
)
93 struct confargs
*ca
= args
;
94 uint8_t *src
, *dst
, *lim
;
98 if (sizeof(struct eeprom
) != EEPROM_SIZE
)
99 panic("eeprom struct wrong");
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__
);
107 /* Keep a "soft" copy of the EEPROM to make access simpler. */
108 eeprom_copy
= malloc(ee_size
, M_DEVBUF
, M_NOWAIT
);
109 if (eeprom_copy
== NULL
)
110 panic("%s: malloc eeprom_copy", __func__
);
113 * On the 3/80, do not touch the last 40 bytes!
114 * Writes there are ignored, reads show zero.
115 * Reduce ee_size and clear the last part of the
116 * soft copy. Note: ee_update obeys ee_size.
118 if (cpu_machine_id
== ID_SUN3X_80
)
121 /* Do only byte access in the EEPROM. */
123 dst
= (uint8_t *)eeprom_copy
;
130 if (ee_size
< EEPROM_SIZE
) {
131 /* Clear out the last part. */
132 memset(dst
, 0, (EEPROM_SIZE
- ee_size
));
145 error
= tsleep(&ee_busy
, PZERO
| PCATCH
, "eeprom", 0);
147 if (error
) /* interrupted */
166 * This is called by mem.c to handle /dev/eeprom
169 eeprom_uio(struct uio
*uio
)
172 int off
; /* NOT off_t */
175 if (eeprom_copy
== NULL
)
178 off
= uio
->uio_offset
;
179 if ((off
< 0) || (off
> EEPROM_SIZE
))
182 cnt
= min(uio
->uio_resid
, (EEPROM_SIZE
- off
));
186 va
= ((uint8_t *)eeprom_copy
) + off
;
187 error
= uiomove(va
, (int)cnt
, uio
);
189 /* If we wrote the rambuf, update the H/W. */
190 if (error
== 0 && (uio
->uio_rw
!= UIO_READ
)) {
193 error
= ee_update(off
, cnt
);
201 * Update the EEPROM from the soft copy.
202 * Other than the attach function, this is
203 * the ONLY place we touch the EEPROM H/W.
206 ee_update(int off
, int cnt
)
208 volatile uint8_t *ep
;
212 if (eeprom_va
== NULL
)
216 * This check is NOT redundant with the one in
217 * eeprom_uio above if we are on a 3/80 where
218 * ee_size < EEPROM_SIZE
220 if (cnt
> (ee_size
- off
))
221 cnt
= (ee_size
- off
);
223 bp
= ((uint8_t *)eeprom_copy
) + off
;
224 ep
= eeprom_va
+ off
;
229 * DO NOT WRITE IT UNLESS WE HAVE TO because the
230 * EEPROM has a limited number of write cycles.
231 * After some number of writes it just fails!
236 * We have written the EEPROM, so now we must
237 * sleep for at least 10 milliseconds while
238 * holding the lock to prevent all access to
239 * the EEPROM while it recovers.
241 (void)tsleep(eeprom_va
, PZERO
- 1, "eeprom", hz
/ 50);
243 /* Make sure the write worked. */
250 return errcnt
? EIO
: 0;