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