Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: nvram.c,v 1.23 2023/01/06 10:28:28 tsutsui 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Nvram driver
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.23 2023/01/06 10:28:28 tsutsui Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/conf.h>
     37 #include <sys/kernel.h>
     38 #include <sys/proc.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/buf.h>
     42 #include <sys/uio.h>
     43 
     44 #include <machine/iomap.h>
     45 #include <machine/cpu.h>
     46 
     47 #include <atari/dev/clockreg.h>
     48 #include <atari/dev/nvramvar.h>
     49 
     50 #include "ioconf.h"
     51 
     52 #include "nvr.h"
     53 
     54 #ifdef NVRAM_DEBUG
     55 #define DPRINTF(a) printf a
     56 #else
     57 #define DPRINTF(a)
     58 #endif
     59 
     60 #define	MC_NVRAM_CSUM	(MC_NVRAM_START + MC_NVRAM_SIZE - 2)
     61 
     62 #if NNVR > 0
     63 static void	nvram_set_csum(u_char csum);
     64 static int	nvram_csum_valid(u_char csum);
     65 static u_char	nvram_csum(void);
     66 
     67 /*
     68  * Auto config stuff....
     69  */
     70 static void	nvr_attach(device_t, device_t, void *);
     71 static int	nvr_match(device_t, cfdata_t, void *);
     72 
     73 CFATTACH_DECL_NEW(nvr, sizeof(struct nvr_softc),
     74     nvr_match, nvr_attach, NULL, NULL);
     75 
     76 /*ARGSUSED*/
     77 static int
     78 nvr_match(device_t parent, cfdata_t cf, void *aux)
     79 {
     80 
     81 	if (!strcmp((char *)aux, "nvr"))
     82 		return 1;
     83 	return 0;
     84 }
     85 
     86 /*ARGSUSED*/
     87 static void
     88 nvr_attach(device_t parent, device_t self, void *aux)
     89 {
     90 	struct nvr_softc *sc;
     91 	int nreg;
     92 
     93 	/*
     94 	 * Check the validity of the NVram contents
     95 	 */
     96 	/* XXX: Milan's firmware seems to use different check method */
     97 	if ((machineid & ATARI_MILAN) == 0) {
     98 		if (!nvram_csum_valid(nvram_csum())) {
     99 			aprint_error(": Invalid checksum - re-initialized");
    100 			for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM;
    101 			    nreg++)
    102 				mc146818_write(RTC, nreg, 0);
    103 			nvram_set_csum(nvram_csum());
    104 		}
    105 	}
    106 	sc = device_private(self);
    107 	sc->sc_dev = self;
    108 	sc->sc_flags = NVR_CONFIGURED;
    109 	aprint_normal("\n");
    110 }
    111 /*
    112  * End of auto config stuff....
    113  */
    114 #endif /* NNVR > 0 */
    115 
    116 /*
    117  * Kernel internal interface
    118  */
    119 int
    120 nvr_get_byte(int byteno)
    121 {
    122 #if NNVR > 0
    123 	struct nvr_softc *sc;
    124 
    125 	sc = device_lookup_private(&nvr_cd, 0);
    126 	if ((sc->sc_flags & NVR_CONFIGURED) == 0)
    127 		return NVR_INVALID;
    128 	return mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff;
    129 #else
    130 	return NVR_INVALID;
    131 #endif /* NNVR > 0 */
    132 }
    133 
    134 #if NNVR > 0
    135 
    136 int
    137 nvram_uio(struct uio *uio)
    138 {
    139 	int i;
    140 	off_t offset;
    141 	int nleft;
    142 	uint8_t buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1];
    143 	uint8_t *p;
    144 	struct nvr_softc *sc;
    145 
    146 	sc = device_lookup_private(&nvr_cd,0);
    147 	if ((sc->sc_flags & NVR_CONFIGURED) == 0)
    148 		return ENXIO;
    149 
    150 	DPRINTF(("Request to transfer %d bytes offset: %d, %s nvram\n",
    151 	    (long)uio->uio_resid, (long)uio->uio_offset,
    152 	    (uio->uio_rw == UIO_READ) ? "from" : "to"));
    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 	DPRINTF(("Translated: offset = %d, bytes: %d\n", (long)offset, nleft));
    164 
    165 	if (uio->uio_rw == UIO_READ) {
    166 		for (i = 0, p = buf; i < nleft; i++, p++)
    167 			*p = mc146818_read(RTC, offset + i);
    168 	}
    169 	if ((i = uiomove(buf, nleft, uio)) != 0)
    170 		return i;
    171 	if (uio->uio_rw == UIO_WRITE) {
    172 		for (i = 0, p = buf; i < nleft; i++, p++)
    173 			mc146818_write(RTC, offset + i, *p);
    174 		nvram_set_csum(nvram_csum());
    175 	}
    176 	return 0;
    177 }
    178 
    179 static u_char
    180 nvram_csum(void)
    181 {
    182 	uint8_t csum;
    183 	int nreg;
    184 
    185 	for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
    186 		csum += mc146818_read(RTC, nreg);
    187 	return csum;
    188 }
    189 
    190 static int
    191 nvram_csum_valid(u_char csum)
    192 {
    193 
    194 	if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM))
    195 		|| (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1)))
    196 		return 0;
    197 	return 1;
    198 }
    199 
    200 static void
    201 nvram_set_csum(u_char csum)
    202 {
    203 
    204 	mc146818_write(RTC, MC_NVRAM_CSUM,    ~csum);
    205 	mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum);
    206 }
    207 #endif /* NNVR > 0 */
    208