Home | History | Annotate | Line # | Download | only in dev
eeprom.c revision 1.16
      1  1.16       gwr /*	$NetBSD: eeprom.c,v 1.16 1997/04/28 21:51: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.14       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.14       gwr  * BE 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.1       gwr  */
     46   1.1       gwr 
     47   1.1       gwr #include <sys/param.h>
     48   1.4       gwr #include <sys/systm.h>
     49   1.4       gwr #include <sys/device.h>
     50   1.1       gwr #include <sys/conf.h>
     51   1.1       gwr #include <sys/buf.h>
     52   1.1       gwr #include <sys/malloc.h>
     53  1.13       gwr #include <sys/proc.h>
     54   1.1       gwr 
     55   1.4       gwr #include <machine/autoconf.h>
     56   1.1       gwr #include <machine/obio.h>
     57   1.1       gwr #include <machine/eeprom.h>
     58  1.15       gwr #include <machine/machdep.h>
     59   1.1       gwr 
     60   1.5       gwr #define HZ 100	/* XXX */
     61   1.5       gwr 
     62  1.15       gwr #ifndef OBIO_EEPROM_SIZE
     63  1.15       gwr #define OBIO_EEPROM_SIZE sizeof(struct eeprom)
     64  1.15       gwr #endif
     65   1.1       gwr 
     66  1.15       gwr static int ee_update(int off, int cnt);
     67   1.1       gwr 
     68   1.1       gwr static char *eeprom_va;
     69  1.15       gwr static char *ee_rambuf;
     70   1.1       gwr static int ee_busy, ee_want;
     71   1.1       gwr 
     72  1.13       gwr static int  eeprom_match __P((struct device *, struct cfdata *, void *));
     73   1.8       gwr static void eeprom_attach __P((struct device *, struct device *, void *));
     74   1.4       gwr 
     75   1.7   thorpej struct cfattach eeprom_ca = {
     76   1.7   thorpej 	sizeof(struct device), eeprom_match, eeprom_attach
     77   1.7   thorpej };
     78   1.7   thorpej 
     79   1.7   thorpej struct cfdriver eeprom_cd = {
     80   1.7   thorpej 	NULL, "eeprom", DV_DULL
     81   1.7   thorpej };
     82   1.4       gwr 
     83   1.8       gwr static int
     84  1.13       gwr eeprom_match(parent, cf, args)
     85   1.4       gwr     struct device *parent;
     86  1.13       gwr 	struct cfdata *cf;
     87  1.13       gwr     void *args;
     88   1.4       gwr {
     89   1.4       gwr 	struct confargs *ca = args;
     90   1.4       gwr 
     91   1.4       gwr 	/* This driver only supports one unit. */
     92   1.4       gwr 	if (cf->cf_unit != 0)
     93   1.4       gwr 		return (0);
     94   1.8       gwr 
     95  1.16       gwr 	/* We use obio_mapin(), so require OBIO. */
     96  1.16       gwr 	if (ca->ca_bustype != BUS_OBIO)
     97   1.8       gwr 		return (0);
     98   1.8       gwr 
     99  1.16       gwr 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
    100   1.4       gwr 		return (0);
    101   1.8       gwr 
    102   1.4       gwr 	return (1);
    103   1.4       gwr }
    104   1.4       gwr 
    105   1.8       gwr static void
    106   1.8       gwr eeprom_attach(parent, self, args)
    107   1.4       gwr 	struct device *parent;
    108   1.4       gwr 	struct device *self;
    109   1.4       gwr 	void *args;
    110   1.4       gwr {
    111  1.16       gwr 	struct confargs *ca = args;
    112  1.15       gwr 	char *src, *dst, *lim;
    113   1.4       gwr 
    114  1.10  christos 	printf("\n");
    115  1.16       gwr 
    116  1.16       gwr 	eeprom_va = obio_mapin(ca->ca_paddr, OBIO_EEPROM_SIZE);
    117  1.16       gwr 	if (!eeprom_va)
    118  1.16       gwr 		panic("eeprom_attach");
    119  1.15       gwr 
    120  1.15       gwr 	/* Keep a "soft" copy of the EEPROM to make access simpler. */
    121  1.15       gwr 	ee_rambuf = malloc(sizeof(struct eeprom), M_DEVBUF, M_NOWAIT);
    122  1.15       gwr 	if (ee_rambuf == 0)
    123  1.15       gwr 		panic("eeprom_attach: malloc ee_rambuf");
    124  1.15       gwr 
    125  1.15       gwr 	/* Do only byte access in the EEPROM. */
    126  1.15       gwr 	src = eeprom_va;
    127  1.15       gwr 	dst = ee_rambuf;
    128  1.15       gwr 	lim = ee_rambuf + sizeof(struct eeprom);
    129  1.15       gwr 	do *dst++ = *src++;
    130  1.15       gwr 	while (dst < lim);
    131   1.4       gwr }
    132   1.4       gwr 
    133   1.1       gwr 
    134  1.13       gwr /* Take the lock. */
    135  1.13       gwr static int
    136  1.13       gwr ee_take __P((void))
    137   1.1       gwr {
    138   1.1       gwr 	int error = 0;
    139   1.1       gwr 	while (ee_busy) {
    140   1.1       gwr 		ee_want = 1;
    141   1.1       gwr 		error = tsleep(&ee_busy, PZERO | PCATCH, "eeprom", 0);
    142   1.1       gwr 		ee_want = 0;
    143   1.1       gwr 		if (error)	/* interrupted */
    144  1.15       gwr 			return error;
    145   1.1       gwr 	}
    146   1.1       gwr 	ee_busy = 1;
    147   1.1       gwr 	return error;
    148   1.1       gwr }
    149   1.1       gwr 
    150  1.13       gwr /* Give the lock. */
    151  1.13       gwr static void
    152  1.13       gwr ee_give __P((void))
    153   1.1       gwr {
    154   1.1       gwr 	ee_busy = 0;
    155   1.1       gwr 	if (ee_want) {
    156   1.1       gwr 		ee_want = 0;
    157   1.1       gwr 		wakeup(&ee_busy);
    158   1.1       gwr 	}
    159   1.1       gwr }
    160   1.1       gwr 
    161  1.13       gwr /*
    162  1.15       gwr  * This is called by mem.c to handle /dev/eeprom
    163  1.13       gwr  */
    164  1.13       gwr int
    165  1.13       gwr eeprom_uio(struct uio *uio)
    166   1.1       gwr {
    167  1.15       gwr 	int cnt, error;
    168   1.1       gwr 	int off;	/* NOT off_t */
    169   1.1       gwr 	caddr_t va;
    170  1.15       gwr 
    171  1.15       gwr 	if (ee_rambuf == NULL)
    172  1.15       gwr 		return (ENXIO);
    173   1.1       gwr 
    174   1.1       gwr 	off = uio->uio_offset;
    175  1.15       gwr 	if ((off < 0) || (off > OBIO_EEPROM_SIZE))
    176   1.1       gwr 		return (EFAULT);
    177   1.1       gwr 
    178  1.15       gwr 	cnt = min(uio->uio_resid, (OBIO_EEPROM_SIZE - off));
    179  1.15       gwr 	if (cnt == 0)
    180  1.15       gwr 		return (0);	/* EOF */
    181  1.15       gwr 
    182  1.15       gwr 	va = ee_rambuf + off;
    183  1.15       gwr 	error = uiomove(va, (int)cnt, uio);
    184  1.15       gwr 
    185  1.15       gwr 	/* If we wrote the rambuf, update the H/W. */
    186  1.15       gwr 	if (!error && (uio->uio_rw != UIO_READ)) {
    187  1.15       gwr 		error = ee_take();
    188  1.15       gwr 		if (!error)
    189  1.15       gwr 			error = ee_update(off, cnt);
    190  1.15       gwr 		ee_give();
    191   1.1       gwr 	}
    192   1.1       gwr 
    193   1.1       gwr 	return (error);
    194   1.1       gwr }
    195   1.1       gwr 
    196   1.1       gwr /*
    197  1.15       gwr  * Update the EEPROM from the soft copy.
    198  1.15       gwr  * Other than the attach function, this is
    199  1.15       gwr  * the ONLY place we touch the EEPROM H/W.
    200   1.1       gwr  */
    201  1.13       gwr static int
    202  1.15       gwr ee_update(int off, int cnt)
    203   1.1       gwr {
    204   1.1       gwr 	volatile char *ep;
    205   1.1       gwr 	char *bp;
    206  1.15       gwr 	int errcnt;
    207   1.1       gwr 
    208   1.1       gwr 	if (eeprom_va == NULL)
    209   1.1       gwr 		return (ENXIO);
    210   1.1       gwr 
    211  1.15       gwr 	bp = ee_rambuf + off;
    212   1.1       gwr 	ep = eeprom_va + off;
    213  1.15       gwr 	errcnt = 0;
    214   1.1       gwr 
    215   1.1       gwr 	while (cnt > 0) {
    216   1.1       gwr 		/*
    217   1.1       gwr 		 * DO NOT WRITE IT UNLESS WE HAVE TO because the
    218   1.1       gwr 		 * EEPROM has a limited number of write cycles.
    219   1.1       gwr 		 * After some number of writes it just fails!
    220   1.1       gwr 		 */
    221   1.1       gwr 		if (*ep != *bp) {
    222   1.1       gwr 			*ep  = *bp;
    223   1.1       gwr 			/*
    224   1.1       gwr 			 * We have written the EEPROM, so now we must
    225   1.1       gwr 			 * sleep for at least 10 milliseconds while
    226   1.1       gwr 			 * holding the lock to prevent all access to
    227   1.1       gwr 			 * the EEPROM while it recovers.
    228   1.1       gwr 			 */
    229   1.1       gwr 			(void)tsleep(eeprom_va, PZERO-1, "eeprom", HZ/50);
    230   1.1       gwr 		}
    231   1.1       gwr 		/* Make sure the write worked. */
    232   1.1       gwr 		if (*ep != *bp)
    233  1.15       gwr 			errcnt++;
    234   1.1       gwr 		ep++;
    235   1.1       gwr 		bp++;
    236   1.1       gwr 		cnt--;
    237   1.1       gwr 	}
    238  1.15       gwr 	return (errcnt ? EIO : 0);
    239   1.1       gwr }
    240