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