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