1 /* $NetBSD: eeprom.c,v 1.31 2008/06/28 12:13:38 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.31 2008/06/28 12:13:38 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/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 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 = malloc(ee_size, M_DEVBUF, M_NOWAIT); 109 if (eeprom_copy == NULL) 110 panic("%s: malloc eeprom_copy", __func__); 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 = (uint8_t *)eeprom_copy; 124 lim = dst + ee_size; 125 126 do { 127 *dst++ = *src++; 128 } while (dst < lim); 129 130 if (ee_size < EEPROM_SIZE) { 131 /* Clear out the last part. */ 132 memset(dst, 0, (EEPROM_SIZE - ee_size)); 133 } 134 } 135 136 137 /* Take the lock. */ 138 static int 139 ee_take(void) 140 { 141 int error = 0; 142 143 while (ee_busy) { 144 ee_want = 1; 145 error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0); 146 ee_want = 0; 147 if (error) /* interrupted */ 148 return error; 149 } 150 ee_busy = 1; 151 return error; 152 } 153 154 /* Give the lock. */ 155 static void 156 ee_give(void) 157 { 158 ee_busy = 0; 159 if (ee_want) { 160 ee_want = 0; 161 wakeup(&ee_busy); 162 } 163 } 164 165 /* 166 * This is called by mem.c to handle /dev/eeprom 167 */ 168 int 169 eeprom_uio(struct uio *uio) 170 { 171 int cnt, error; 172 int off; /* NOT off_t */ 173 uint8_t *va; 174 175 if (eeprom_copy == NULL) 176 return ENXIO; 177 178 off = uio->uio_offset; 179 if ((off < 0) || (off > EEPROM_SIZE)) 180 return EFAULT; 181 182 cnt = min(uio->uio_resid, (EEPROM_SIZE - off)); 183 if (cnt == 0) 184 return 0; /* EOF */ 185 186 va = ((uint8_t *)eeprom_copy) + off; 187 error = uiomove(va, (int)cnt, uio); 188 189 /* If we wrote the rambuf, update the H/W. */ 190 if (error == 0 && (uio->uio_rw != UIO_READ)) { 191 error = ee_take(); 192 if (error == 0) 193 error = ee_update(off, cnt); 194 ee_give(); 195 } 196 197 return error; 198 } 199 200 /* 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. 204 */ 205 static int 206 ee_update(int off, int cnt) 207 { 208 volatile uint8_t *ep; 209 uint8_t *bp; 210 int errcnt; 211 212 if (eeprom_va == NULL) 213 return (ENXIO); 214 215 /* 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 219 */ 220 if (cnt > (ee_size - off)) 221 cnt = (ee_size - off); 222 223 bp = ((uint8_t *)eeprom_copy) + off; 224 ep = eeprom_va + off; 225 errcnt = 0; 226 227 while (cnt > 0) { 228 /* 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! 232 */ 233 if (*ep != *bp) { 234 *ep = *bp; 235 /* 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. 240 */ 241 (void)tsleep(eeprom_va, PZERO - 1, "eeprom", hz / 50); 242 } 243 /* Make sure the write worked. */ 244 if (*ep != *bp) 245 errcnt++; 246 ep++; 247 bp++; 248 cnt--; 249 } 250 return errcnt ? EIO : 0; 251 } 252