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