1 /* $NetBSD: eeprom.c,v 1.30 2008/04/28 20:23:37 martin 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.30 2008/04/28 20:23:37 martin 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/malloc.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 char *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(struct device *, struct cfdata *, void *); 70 static void eeprom_attach(struct device *, struct device *, void *); 71 72 CFATTACH_DECL(eeprom, sizeof(struct device), 73 eeprom_match, eeprom_attach, NULL, NULL); 74 75 static int 76 eeprom_match(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *args) 92 { 93 struct confargs *ca = args; 94 char *src, *dst, *lim; 95 96 printf("\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) 105 panic("eeprom_attach"); 106 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 == 0) 110 panic("eeprom_attach: malloc eeprom_copy"); 111 112 /* 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. 117 */ 118 if (cpu_machine_id == ID_SUN3X_80) 119 ee_size -= 40; 120 121 /* Do only byte access in the EEPROM. */ 122 src = eeprom_va; 123 dst = (char*) eeprom_copy; 124 lim = dst + ee_size; 125 126 do *dst++ = *src++; 127 while (dst < lim); 128 129 if (ee_size < EEPROM_SIZE) { 130 /* Clear out the last part. */ 131 memset(dst, 0, (EEPROM_SIZE - ee_size)); 132 } 133 } 134 135 136 /* Take the lock. */ 137 static int 138 ee_take(void) 139 { 140 int error = 0; 141 142 while (ee_busy) { 143 ee_want = 1; 144 error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0); 145 ee_want = 0; 146 if (error) /* interrupted */ 147 return error; 148 } 149 ee_busy = 1; 150 return error; 151 } 152 153 /* Give the lock. */ 154 static void 155 ee_give(void) 156 { 157 ee_busy = 0; 158 if (ee_want) { 159 ee_want = 0; 160 wakeup(&ee_busy); 161 } 162 } 163 164 /* 165 * This is called by mem.c to handle /dev/eeprom 166 */ 167 int 168 eeprom_uio(struct uio *uio) 169 { 170 int cnt, error; 171 int off; /* NOT off_t */ 172 void *va; 173 174 if (eeprom_copy == NULL) 175 return (ENXIO); 176 177 off = uio->uio_offset; 178 if ((off < 0) || (off > EEPROM_SIZE)) 179 return (EFAULT); 180 181 cnt = min(uio->uio_resid, (EEPROM_SIZE - off)); 182 if (cnt == 0) 183 return (0); /* EOF */ 184 185 va = ((char*)eeprom_copy) + off; 186 error = uiomove(va, (int)cnt, uio); 187 188 /* If we wrote the rambuf, update the H/W. */ 189 if (!error && (uio->uio_rw != UIO_READ)) { 190 error = ee_take(); 191 if (!error) 192 error = ee_update(off, cnt); 193 ee_give(); 194 } 195 196 return (error); 197 } 198 199 /* 200 * Update the EEPROM from the soft copy. 201 * Other than the attach function, this is 202 * the ONLY place we touch the EEPROM H/W. 203 */ 204 static int 205 ee_update(int off, int cnt) 206 { 207 volatile char *ep; 208 char *bp; 209 int errcnt; 210 211 if (eeprom_va == NULL) 212 return (ENXIO); 213 214 /* 215 * This check is NOT redundant with the one in 216 * eeprom_uio above if we are on a 3/80 where 217 * ee_size < EEPROM_SIZE 218 */ 219 if (cnt > (ee_size - off)) 220 cnt = (ee_size - off); 221 222 bp = ((char*)eeprom_copy) + off; 223 ep = eeprom_va + off; 224 errcnt = 0; 225 226 while (cnt > 0) { 227 /* 228 * DO NOT WRITE IT UNLESS WE HAVE TO because the 229 * EEPROM has a limited number of write cycles. 230 * After some number of writes it just fails! 231 */ 232 if (*ep != *bp) { 233 *ep = *bp; 234 /* 235 * We have written the EEPROM, so now we must 236 * sleep for at least 10 milliseconds while 237 * holding the lock to prevent all access to 238 * the EEPROM while it recovers. 239 */ 240 (void)tsleep(eeprom_va, PZERO-1, "eeprom", hz/50); 241 } 242 /* Make sure the write worked. */ 243 if (*ep != *bp) 244 errcnt++; 245 ep++; 246 bp++; 247 cnt--; 248 } 249 return (errcnt ? EIO : 0); 250 } 251