Home | History | Annotate | Line # | Download | only in dev
eeprom.c revision 1.8
      1  1.8      gwr /*	$NetBSD: eeprom.c,v 1.8 1996/03/26 15:16:06 gwr Exp $	*/
      2  1.2      cgd 
      3  1.1      gwr /*
      4  1.1      gwr  * Copyright (c) 1994 Gordon W. Ross
      5  1.1      gwr  * All rights reserved.
      6  1.1      gwr  *
      7  1.1      gwr  * Redistribution and use in source and binary forms, with or without
      8  1.1      gwr  * modification, are permitted provided that the following conditions
      9  1.1      gwr  * are met:
     10  1.1      gwr  * 1. Redistributions of source code must retain the above copyright
     11  1.1      gwr  *    notice, this list of conditions and the following disclaimer.
     12  1.1      gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      gwr  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      gwr  *    documentation and/or other materials provided with the distribution.
     15  1.1      gwr  * 3. The name of the author may not be used to endorse or promote products
     16  1.1      gwr  *    derived from this software without specific prior written permission.
     17  1.1      gwr  *
     18  1.1      gwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1      gwr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1      gwr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1      gwr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1      gwr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1      gwr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1      gwr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1      gwr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1      gwr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1      gwr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1      gwr  */
     29  1.1      gwr 
     30  1.1      gwr /*
     31  1.1      gwr  * Access functions for the EEPROM (Electrically Eraseable PROM)
     32  1.1      gwr  * The main reason for the existence of this module is to
     33  1.1      gwr  * handle the painful task of updating the EEPROM contents.
     34  1.1      gwr  * After a write, it must not be touched for 10 milliseconds.
     35  1.1      gwr  * (See the Sun-3 Architecture Manual sec. 5.9)
     36  1.1      gwr  */
     37  1.1      gwr 
     38  1.1      gwr #include <sys/param.h>
     39  1.4      gwr #include <sys/systm.h>
     40  1.4      gwr #include <sys/device.h>
     41  1.1      gwr #include <sys/conf.h>
     42  1.1      gwr #include <sys/buf.h>
     43  1.1      gwr #include <sys/malloc.h>
     44  1.1      gwr 
     45  1.4      gwr #include <machine/autoconf.h>
     46  1.1      gwr #include <machine/obio.h>
     47  1.1      gwr #include <machine/eeprom.h>
     48  1.1      gwr 
     49  1.5      gwr #define HZ 100	/* XXX */
     50  1.5      gwr 
     51  1.5      gwr int ee_console;		/* for convenience of drivers */
     52  1.1      gwr 
     53  1.1      gwr static int ee_update(caddr_t buf, int off, int cnt);
     54  1.1      gwr 
     55  1.1      gwr static char *eeprom_va;
     56  1.1      gwr static int ee_busy, ee_want;
     57  1.1      gwr 
     58  1.8      gwr static int  eeprom_match __P((struct device *, void *vcf, void *args));
     59  1.8      gwr static void eeprom_attach __P((struct device *, struct device *, void *));
     60  1.4      gwr 
     61  1.7  thorpej struct cfattach eeprom_ca = {
     62  1.7  thorpej 	sizeof(struct device), eeprom_match, eeprom_attach
     63  1.7  thorpej };
     64  1.7  thorpej 
     65  1.7  thorpej struct cfdriver eeprom_cd = {
     66  1.7  thorpej 	NULL, "eeprom", DV_DULL
     67  1.7  thorpej };
     68  1.4      gwr 
     69  1.5      gwr /* Called very early by internal_configure. */
     70  1.1      gwr void eeprom_init()
     71  1.1      gwr {
     72  1.1      gwr 	eeprom_va = obio_find_mapping(OBIO_EEPROM, OBIO_EEPROM_SIZE);
     73  1.5      gwr 	ee_console = ((struct eeprom *)eeprom_va)->eeConsole;
     74  1.1      gwr }
     75  1.4      gwr 
     76  1.8      gwr static int
     77  1.8      gwr eeprom_match(parent, vcf, args)
     78  1.4      gwr     struct device *parent;
     79  1.4      gwr     void *vcf, *args;
     80  1.4      gwr {
     81  1.4      gwr     struct cfdata *cf = vcf;
     82  1.4      gwr 	struct confargs *ca = args;
     83  1.8      gwr 	int pa;
     84  1.4      gwr 
     85  1.4      gwr 	/* This driver only supports one unit. */
     86  1.4      gwr 	if (cf->cf_unit != 0)
     87  1.4      gwr 		return (0);
     88  1.8      gwr 
     89  1.8      gwr 	if ((pa = cf->cf_paddr) == -1) {
     90  1.8      gwr 		/* Use our default PA. */
     91  1.8      gwr 		pa = OBIO_EEPROM;
     92  1.8      gwr 	} else {
     93  1.8      gwr 		/* Validate the given PA. */
     94  1.8      gwr 		if (pa != OBIO_EEPROM)
     95  1.8      gwr 			return (0);
     96  1.8      gwr 	}
     97  1.8      gwr 	if (pa != ca->ca_paddr)
     98  1.8      gwr 		return (0);
     99  1.8      gwr 
    100  1.4      gwr 	if (eeprom_va == NULL)
    101  1.4      gwr 		return (0);
    102  1.8      gwr 
    103  1.4      gwr 	return (1);
    104  1.4      gwr }
    105  1.4      gwr 
    106  1.8      gwr static void
    107  1.8      gwr eeprom_attach(parent, self, args)
    108  1.4      gwr 	struct device *parent;
    109  1.4      gwr 	struct device *self;
    110  1.4      gwr 	void *args;
    111  1.4      gwr {
    112  1.4      gwr 	struct confargs *ca = args;
    113  1.4      gwr 
    114  1.4      gwr 	printf("\n");
    115  1.4      gwr }
    116  1.4      gwr 
    117  1.1      gwr 
    118  1.1      gwr static int ee_take()	/* Take the lock. */
    119  1.1      gwr {
    120  1.1      gwr 	int error = 0;
    121  1.1      gwr 	while (ee_busy) {
    122  1.1      gwr 		ee_want = 1;
    123  1.1      gwr 		error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
    124  1.1      gwr 		ee_want = 0;
    125  1.1      gwr 		if (error)	/* interrupted */
    126  1.1      gwr 			goto out;
    127  1.1      gwr 	}
    128  1.1      gwr 	ee_busy = 1;
    129  1.1      gwr  out:
    130  1.1      gwr 	return error;
    131  1.1      gwr }
    132  1.1      gwr 
    133  1.1      gwr static void ee_give()	/* Give the lock. */
    134  1.1      gwr {
    135  1.1      gwr 	ee_busy = 0;
    136  1.1      gwr 	if (ee_want) {
    137  1.1      gwr 		ee_want = 0;
    138  1.1      gwr 		wakeup(&ee_busy);
    139  1.1      gwr 	}
    140  1.1      gwr }
    141  1.1      gwr 
    142  1.1      gwr int eeprom_uio(struct uio *uio)
    143  1.1      gwr {
    144  1.1      gwr 	int error;
    145  1.1      gwr 	int off;	/* NOT off_t */
    146  1.1      gwr 	u_int cnt;
    147  1.1      gwr 	caddr_t va;
    148  1.1      gwr 	caddr_t buf = (caddr_t)0;
    149  1.1      gwr 
    150  1.1      gwr 	off = uio->uio_offset;
    151  1.1      gwr 	if (off >= OBIO_EEPROM_SIZE)
    152  1.1      gwr 		return (EFAULT);
    153  1.1      gwr 
    154  1.1      gwr 	cnt = uio->uio_resid;
    155  1.1      gwr 	if (cnt > (OBIO_EEPROM_SIZE - off))
    156  1.1      gwr 		cnt = (OBIO_EEPROM_SIZE - off);
    157  1.1      gwr 
    158  1.1      gwr 	if ((error = ee_take()) != 0)
    159  1.1      gwr 		return (error);
    160  1.1      gwr 
    161  1.1      gwr 	if (eeprom_va == NULL) {
    162  1.1      gwr 		error = ENXIO;
    163  1.1      gwr 		goto out;
    164  1.1      gwr 	}
    165  1.1      gwr 
    166  1.1      gwr 	va = eeprom_va;
    167  1.1      gwr 	if (uio->uio_rw != UIO_READ) {
    168  1.1      gwr 		/* Write requires a temporary buffer. */
    169  1.1      gwr 		buf = malloc(OBIO_EEPROM_SIZE, M_DEVBUF, M_WAITOK);
    170  1.1      gwr 		if (!buf) {
    171  1.1      gwr 			error = EAGAIN;
    172  1.1      gwr 			goto out;
    173  1.1      gwr 		}
    174  1.1      gwr 		va = buf;
    175  1.1      gwr 	}
    176  1.1      gwr 
    177  1.1      gwr 	if ((error = uiomove(va + off, (int)cnt, uio)) != 0)
    178  1.1      gwr 		goto out;
    179  1.1      gwr 
    180  1.1      gwr 	if (uio->uio_rw != UIO_READ)
    181  1.1      gwr 		error = ee_update(buf, off, cnt);
    182  1.1      gwr 
    183  1.1      gwr  out:
    184  1.1      gwr 	if (buf)
    185  1.1      gwr 		free(buf, M_DEVBUF);
    186  1.1      gwr 	ee_give();
    187  1.1      gwr 	return (error);
    188  1.1      gwr }
    189  1.1      gwr 
    190  1.1      gwr /*
    191  1.1      gwr  * Update the EEPROM from the passed buf.
    192  1.1      gwr  */
    193  1.1      gwr static int ee_update(char *buf, int off, int cnt)
    194  1.1      gwr {
    195  1.1      gwr 	volatile char *ep;
    196  1.1      gwr 	char *bp;
    197  1.1      gwr 
    198  1.1      gwr 	if (eeprom_va == NULL)
    199  1.1      gwr 		return (ENXIO);
    200  1.1      gwr 
    201  1.1      gwr 	ep = eeprom_va + off;
    202  1.1      gwr 	bp = buf + off;
    203  1.1      gwr 
    204  1.1      gwr 	while (cnt > 0) {
    205  1.1      gwr 		/*
    206  1.1      gwr 		 * DO NOT WRITE IT UNLESS WE HAVE TO because the
    207  1.1      gwr 		 * EEPROM has a limited number of write cycles.
    208  1.1      gwr 		 * After some number of writes it just fails!
    209  1.1      gwr 		 */
    210  1.1      gwr 		if (*ep != *bp) {
    211  1.1      gwr 			*ep  = *bp;
    212  1.1      gwr 			/*
    213  1.1      gwr 			 * We have written the EEPROM, so now we must
    214  1.1      gwr 			 * sleep for at least 10 milliseconds while
    215  1.1      gwr 			 * holding the lock to prevent all access to
    216  1.1      gwr 			 * the EEPROM while it recovers.
    217  1.1      gwr 			 */
    218  1.1      gwr 			(void)tsleep(eeprom_va, PZERO-1, "eeprom", HZ/50);
    219  1.1      gwr 		}
    220  1.1      gwr 		/* Make sure the write worked. */
    221  1.1      gwr 		if (*ep != *bp)
    222  1.1      gwr 			return (EIO);
    223  1.1      gwr 		ep++;
    224  1.1      gwr 		bp++;
    225  1.1      gwr 		cnt--;
    226  1.1      gwr 	}
    227  1.6      gwr 	return(0);
    228  1.1      gwr }
    229  1.1      gwr 
    230  1.1      gwr /*
    231  1.1      gwr  * Read a byte out of the EEPROM.  This is called from
    232  1.1      gwr  * things like the zs driver very early to find out
    233  1.1      gwr  * which device should be used as the console.
    234  1.1      gwr  */
    235  1.1      gwr int ee_get_byte(int off, int canwait)
    236  1.1      gwr {
    237  1.1      gwr 	int c = -1;
    238  1.1      gwr 	if ((off < 0) || (off >= OBIO_EEPROM_SIZE))
    239  1.1      gwr 		goto out;
    240  1.1      gwr 	if (eeprom_va == NULL)
    241  1.1      gwr 		goto out;
    242  1.1      gwr 
    243  1.1      gwr 	if (canwait) {
    244  1.1      gwr 		if (ee_take())
    245  1.1      gwr 			goto out;
    246  1.1      gwr 	} else {
    247  1.1      gwr 		if (ee_busy)
    248  1.1      gwr 			goto out;
    249  1.1      gwr 	}
    250  1.1      gwr 
    251  1.1      gwr 	c = eeprom_va[off] & 0xFF;
    252  1.1      gwr 
    253  1.1      gwr 	if (canwait)
    254  1.1      gwr 		ee_give();
    255  1.1      gwr  out:
    256  1.1      gwr 	return c;
    257  1.1      gwr }
    258