Home | History | Annotate | Line # | Download | only in dev
nvram.c revision 1.8
      1 /*	$NetBSD: nvram.c,v 1.8 2002/09/27 20:31:00 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Nvram driver
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/conf.h>
     39 #include <sys/kernel.h>
     40 #include <sys/proc.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/buf.h>
     44 #include <sys/uio.h>
     45 
     46 #include <machine/iomap.h>
     47 #include <machine/cpu.h>
     48 
     49 #include <atari/dev/clockreg.h>
     50 #include <atari/dev/nvramvar.h>
     51 
     52 #include "nvr.h"
     53 
     54 #define	MC_NVRAM_CSUM	(MC_NVRAM_START + MC_NVRAM_SIZE - 2)
     55 
     56 #if NNVR > 0
     57 static void	nvram_set_csum __P((u_char csum));
     58 static int	nvram_csum_valid __P((u_char csum));
     59 static u_char	nvram_csum __P((void));
     60 
     61 /*
     62  * Auto config stuff....
     63  */
     64 static void	nvr_attach __P((struct device *, struct device *, void *));
     65 static int	nvr_match __P((struct device *, struct cfdata *, void *));
     66 
     67 const struct cfattach nvr_ca = {
     68 	sizeof(struct nvr_softc), nvr_match, nvr_attach
     69 };
     70 
     71 extern struct cfdriver nvr_cd;
     72 
     73 /*ARGSUSED*/
     74 static	int
     75 nvr_match(pdp, cfp, auxp)
     76 struct	device	*pdp;
     77 struct	cfdata	*cfp;
     78 void		*auxp;
     79 {
     80 	if (!strcmp((char *)auxp, "nvr"))
     81 		return (1);
     82 	return (0);
     83 }
     84 
     85 /*ARGSUSED*/
     86 static void
     87 nvr_attach(pdp, dp, auxp)
     88 struct	device *pdp, *dp;
     89 void	*auxp;
     90 {
     91 	struct nvr_softc	*nvr_soft;
     92 	int			nreg;
     93 
     94 	/*
     95 	 * Check the validity of the NVram contents
     96 	 */
     97 	if (!nvram_csum_valid(nvram_csum())) {
     98 		printf(": Invalid checksum - re-initialized");
     99 		for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
    100 			mc146818_write(RTC, nreg, 0);
    101 		nvram_set_csum(nvram_csum());
    102 	}
    103 	nvr_soft = nvr_cd.cd_devs[0];
    104 	nvr_soft->nvr_flags = NVR_CONFIGURED;
    105 	printf("\n");
    106 }
    107 /*
    108  * End of auto config stuff....
    109  */
    110 #endif /* NNVR > 0 */
    111 
    112 /*
    113  * Kernel internal interface
    114  */
    115 int
    116 nvr_get_byte(byteno)
    117 int	byteno;
    118 {
    119 #if NNVR > 0
    120 	struct nvr_softc	*nvr_soft;
    121 
    122 	nvr_soft = nvr_cd.cd_devs[0];
    123 	if (!(nvr_soft->nvr_flags & NVR_CONFIGURED))
    124 		return(NVR_INVALID);
    125 	return (mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff);
    126 #else
    127 	return(NVR_INVALID);
    128 #endif /* NNVR > 0 */
    129 }
    130 
    131 #if NNVR > 0
    132 
    133 int
    134 nvram_uio(uio)
    135 struct uio	*uio;
    136 {
    137 	int			i;
    138 	off_t			offset;
    139 	int			nleft;
    140 	u_char			buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1];
    141 	u_char			*p;
    142 	struct nvr_softc	*nvr_soft;
    143 
    144 	nvr_soft = nvr_cd.cd_devs[0];
    145 	if (!(nvr_soft->nvr_flags & NVR_CONFIGURED))
    146 		return ENXIO;
    147 
    148 #ifdef NV_DEBUG
    149 	printf("Request to transfer %d bytes offset: %d, %s nvram\n",
    150 				(long)uio->uio_resid, (long)uio->uio_offset,
    151 				(uio->uio_rw == UIO_READ) ? "from" : "to");
    152 #endif /* NV_DEBUG */
    153 
    154 	offset = uio->uio_offset + MC_NVRAM_START;
    155 	nleft  = uio->uio_resid;
    156 	if (offset + nleft >= MC_NVRAM_CSUM) {
    157 		if (offset == MC_NVRAM_CSUM)
    158 			return (0);
    159 		nleft = MC_NVRAM_CSUM - offset;
    160 		if (nleft <= 0)
    161 			return (EINVAL);
    162 	}
    163 #ifdef NV_DEBUG
    164 	printf("Translated: offset = %d, bytes: %d\n", (long)offset, nleft);
    165 #endif /* NV_DEBUG */
    166 
    167 	if (uio->uio_rw == UIO_READ) {
    168 		for (i = 0, p = buf; i < nleft; i++, p++)
    169 			*p =  mc146818_read(RTC, offset + i);
    170 	}
    171 	if ((i = uiomove(buf, nleft, uio)) != 0)
    172 		return (i);
    173 	if (uio->uio_rw == UIO_WRITE) {
    174 		for (i = 0, p = buf; i < nleft; i++, p++)
    175 			mc146818_write(RTC, offset + i, *p);
    176 		nvram_set_csum(nvram_csum());
    177 	}
    178 	return(0);
    179 }
    180 
    181 static u_char
    182 nvram_csum()
    183 {
    184 	u_char	csum;
    185 	int	nreg;
    186 
    187 	for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
    188 		csum += mc146818_read(RTC, nreg);
    189 	return(csum);
    190 }
    191 
    192 static int
    193 nvram_csum_valid(csum)
    194 u_char	csum;
    195 {
    196 	if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM))
    197 		|| (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1)))
    198 		return 0;
    199 	return 1;
    200 }
    201 
    202 static void
    203 nvram_set_csum(csum)
    204 u_char	csum;
    205 {
    206 	mc146818_write(RTC, MC_NVRAM_CSUM,    ~csum);
    207 	mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum);
    208 }
    209 #endif /* NNVR > 0 */
    210