Home | History | Annotate | Line # | Download | only in dev
nvram.c revision 1.10
      1 /*	$NetBSD: nvram.c,v 1.10 2003/07/15 01:19:52 lukem 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/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.10 2003/07/15 01:19:52 lukem Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/conf.h>
     42 #include <sys/kernel.h>
     43 #include <sys/proc.h>
     44 #include <sys/systm.h>
     45 #include <sys/device.h>
     46 #include <sys/buf.h>
     47 #include <sys/uio.h>
     48 
     49 #include <machine/iomap.h>
     50 #include <machine/cpu.h>
     51 
     52 #include <atari/dev/clockreg.h>
     53 #include <atari/dev/nvramvar.h>
     54 
     55 #include "nvr.h"
     56 
     57 #define	MC_NVRAM_CSUM	(MC_NVRAM_START + MC_NVRAM_SIZE - 2)
     58 
     59 #if NNVR > 0
     60 static void	nvram_set_csum __P((u_char csum));
     61 static int	nvram_csum_valid __P((u_char csum));
     62 static u_char	nvram_csum __P((void));
     63 
     64 /*
     65  * Auto config stuff....
     66  */
     67 static void	nvr_attach __P((struct device *, struct device *, void *));
     68 static int	nvr_match __P((struct device *, struct cfdata *, void *));
     69 
     70 CFATTACH_DECL(nvr, sizeof(struct nvr_softc),
     71     nvr_match, nvr_attach, NULL, NULL);
     72 
     73 extern struct cfdriver nvr_cd;
     74 
     75 /*ARGSUSED*/
     76 static	int
     77 nvr_match(pdp, cfp, auxp)
     78 struct	device	*pdp;
     79 struct	cfdata	*cfp;
     80 void		*auxp;
     81 {
     82 	if (!strcmp((char *)auxp, "nvr"))
     83 		return (1);
     84 	return (0);
     85 }
     86 
     87 /*ARGSUSED*/
     88 static void
     89 nvr_attach(pdp, dp, auxp)
     90 struct	device *pdp, *dp;
     91 void	*auxp;
     92 {
     93 	struct nvr_softc	*nvr_soft;
     94 	int			nreg;
     95 
     96 	/*
     97 	 * Check the validity of the NVram contents
     98 	 */
     99 	if (!nvram_csum_valid(nvram_csum())) {
    100 		printf(": Invalid checksum - re-initialized");
    101 		for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
    102 			mc146818_write(RTC, nreg, 0);
    103 		nvram_set_csum(nvram_csum());
    104 	}
    105 	nvr_soft = nvr_cd.cd_devs[0];
    106 	nvr_soft->nvr_flags = NVR_CONFIGURED;
    107 	printf("\n");
    108 }
    109 /*
    110  * End of auto config stuff....
    111  */
    112 #endif /* NNVR > 0 */
    113 
    114 /*
    115  * Kernel internal interface
    116  */
    117 int
    118 nvr_get_byte(byteno)
    119 int	byteno;
    120 {
    121 #if NNVR > 0
    122 	struct nvr_softc	*nvr_soft;
    123 
    124 	nvr_soft = nvr_cd.cd_devs[0];
    125 	if (!(nvr_soft->nvr_flags & NVR_CONFIGURED))
    126 		return(NVR_INVALID);
    127 	return (mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff);
    128 #else
    129 	return(NVR_INVALID);
    130 #endif /* NNVR > 0 */
    131 }
    132 
    133 #if NNVR > 0
    134 
    135 int
    136 nvram_uio(uio)
    137 struct uio	*uio;
    138 {
    139 	int			i;
    140 	off_t			offset;
    141 	int			nleft;
    142 	u_char			buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1];
    143 	u_char			*p;
    144 	struct nvr_softc	*nvr_soft;
    145 
    146 	nvr_soft = nvr_cd.cd_devs[0];
    147 	if (!(nvr_soft->nvr_flags & NVR_CONFIGURED))
    148 		return ENXIO;
    149 
    150 #ifdef NV_DEBUG
    151 	printf("Request to transfer %d bytes offset: %d, %s nvram\n",
    152 				(long)uio->uio_resid, (long)uio->uio_offset,
    153 				(uio->uio_rw == UIO_READ) ? "from" : "to");
    154 #endif /* NV_DEBUG */
    155 
    156 	offset = uio->uio_offset + MC_NVRAM_START;
    157 	nleft  = uio->uio_resid;
    158 	if (offset + nleft >= MC_NVRAM_CSUM) {
    159 		if (offset == MC_NVRAM_CSUM)
    160 			return (0);
    161 		nleft = MC_NVRAM_CSUM - offset;
    162 		if (nleft <= 0)
    163 			return (EINVAL);
    164 	}
    165 #ifdef NV_DEBUG
    166 	printf("Translated: offset = %d, bytes: %d\n", (long)offset, nleft);
    167 #endif /* NV_DEBUG */
    168 
    169 	if (uio->uio_rw == UIO_READ) {
    170 		for (i = 0, p = buf; i < nleft; i++, p++)
    171 			*p =  mc146818_read(RTC, offset + i);
    172 	}
    173 	if ((i = uiomove(buf, nleft, uio)) != 0)
    174 		return (i);
    175 	if (uio->uio_rw == UIO_WRITE) {
    176 		for (i = 0, p = buf; i < nleft; i++, p++)
    177 			mc146818_write(RTC, offset + i, *p);
    178 		nvram_set_csum(nvram_csum());
    179 	}
    180 	return(0);
    181 }
    182 
    183 static u_char
    184 nvram_csum()
    185 {
    186 	u_char	csum;
    187 	int	nreg;
    188 
    189 	for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
    190 		csum += mc146818_read(RTC, nreg);
    191 	return(csum);
    192 }
    193 
    194 static int
    195 nvram_csum_valid(csum)
    196 u_char	csum;
    197 {
    198 	if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM))
    199 		|| (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1)))
    200 		return 0;
    201 	return 1;
    202 }
    203 
    204 static void
    205 nvram_set_csum(csum)
    206 u_char	csum;
    207 {
    208 	mc146818_write(RTC, MC_NVRAM_CSUM,    ~csum);
    209 	mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum);
    210 }
    211 #endif /* NNVR > 0 */
    212