Home | History | Annotate | Line # | Download | only in isa
cmos.c revision 1.4
      1 /*	$NetBSD: cmos.c,v 1.4 2008/06/28 14:56:22 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2003 JONE System Co., Inc.
      5  * All right reserved.
      6  *
      7  * Copyright (C) 1999, 2000, 2001, 2002 JEPRO Co., Ltd.
      8  * All right reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  *
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  */
     33 /*
     34  * Copyright (c) 2007 David Young.  All right reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or
     37  * without modification, are permitted provided that the following
     38  * conditions are met:
     39  *
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above
     43  *    copyright notice, this list of conditions and the following
     44  *    disclaimer in the documentation and/or other materials provided
     45  *    with the distribution.
     46  * 3. The name of David Young may not be used to endorse or promote
     47  *    products derived from this software without specific prior
     48  *    written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     51  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     56  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     57  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
     60  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: cmos.c,v 1.4 2008/06/28 14:56:22 ad Exp $");
     66 
     67 #include <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/kernel.h>
     70 #include <sys/malloc.h>
     71 #include <sys/ioctl.h>
     72 #include <sys/proc.h>
     73 #include <sys/device.h>
     74 #include <sys/conf.h>
     75 #include <sys/kauth.h>
     76 
     77 #include <machine/bus.h>
     78 #include <machine/intr.h>
     79 
     80 #include <dev/isa/isareg.h>
     81 #include <dev/isa/isavar.h>
     82 #include <dev/ic/mc146818reg.h>
     83 #include <i386/isa/nvram.h>
     84 
     85 #define	CMOS_SUM	32
     86 #define	CMOS_BIOSSPEC	34	/* start of BIOS-specific configuration data */
     87 
     88 #define	NVRAM_SUM	(MC_NVRAM_START + CMOS_SUM)
     89 #define	NVRAM_BIOSSPEC	(MC_NVRAM_START + CMOS_BIOSSPEC)
     90 
     91 #define	CMOS_SIZE	NVRAM_BIOSSPEC
     92 
     93 struct cmos_softc {			/* driver status information */
     94 	int	sc_open;
     95 	uint8_t	sc_buf[CMOS_SIZE];
     96 } cmos_softc;
     97 
     98 #ifdef CMOS_DEBUG
     99 int cmos_debug = 0;
    100 #endif
    101 
    102 void cmosattach(int);
    103 dev_type_open(cmos_open);
    104 dev_type_close(cmos_close);
    105 dev_type_read(cmos_read);
    106 dev_type_write(cmos_write);
    107 
    108 static void cmos_sum(uint8_t *, int, int, int);
    109 #ifdef CMOS_DEBUG
    110 static void cmos_dump(uint8_t *);
    111 #endif
    112 
    113 const struct cdevsw cmos_cdevsw = {
    114 	.d_open = cmos_open, .d_close = cmos_close, .d_read = cmos_read,
    115 	.d_write = cmos_write, .d_ioctl = noioctl,
    116 	.d_stop = nostop, .d_tty = notty, .d_poll = nopoll, .d_mmap = nommap,
    117 	.d_kqfilter = nokqfilter,
    118 };
    119 
    120 void
    121 cmosattach(int n)
    122 {
    123 
    124 }
    125 
    126 int
    127 cmos_open(dev_t dev, int flags, int ifmt, struct lwp *l)
    128 {
    129 	struct cmos_softc *sc = &cmos_softc;
    130 	struct proc *p = l->l_proc;
    131 	int error;
    132 
    133 	error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    134 	    &p->p_acflag);
    135 
    136 	if (error != 0)
    137 		return error;
    138 
    139 	sc->sc_open = 1;
    140 #ifdef CMOS_DEBUG
    141 	printf("cmos: opened\n");
    142 #endif
    143 	return 0;
    144 }
    145 
    146 int
    147 cmos_close(dev_t dev, int flags, int ifmt, struct lwp *l)
    148 {
    149 	struct cmos_softc *sc = &cmos_softc;
    150 
    151 	sc->sc_open = 0;
    152 #ifdef CMOS_DEBUG
    153 	printf("cmos: closed\n");
    154 #endif
    155 	return 0;
    156 }
    157 
    158 static void
    159 cmos_fetch(struct cmos_softc *sc)
    160 {
    161 	int i, s;
    162 	uint8_t *p;
    163 
    164 	p = sc->sc_buf;
    165 
    166 	s = splclock();
    167 	for (i = 0; i < CMOS_SIZE; i++)
    168 		*p++ = mc146818_read(sc, i);
    169 	splx(s);
    170 }
    171 
    172 int
    173 cmos_read(dev_t dev, struct uio *uio, int ioflag)
    174 {
    175 	struct cmos_softc *sc = &cmos_softc;
    176 
    177 	if (uio->uio_resid > CMOS_SIZE)
    178 		return EINVAL;
    179 
    180 #ifdef CMOS_DEBUG
    181 	printf("cmos: try to read to %d\n", CMOS_SIZE);
    182 #endif
    183 	cmos_fetch(sc);
    184 
    185 	return uiomove(sc->sc_buf, CMOS_SIZE, uio);
    186 }
    187 
    188 int
    189 cmos_write(dev_t dev, struct uio *uio, int ioflag)
    190 {
    191 	struct cmos_softc *sc = &cmos_softc;
    192 	int error = 0, i, s;
    193 
    194 	cmos_fetch(sc);
    195 
    196 	error = uiomove(sc->sc_buf, CMOS_SIZE, uio);
    197 #ifdef CMOS_DEBUG
    198 	printf("write %d\n", l);
    199 	if (cmos_debug)
    200 		cmos_dump(sc->sc_buf);
    201 #endif
    202 	if (error)
    203 		return error;
    204 
    205 	cmos_sum(sc->sc_buf, NVRAM_DISKETTE, NVRAM_SUM, NVRAM_SUM);
    206 
    207 #ifdef CMOS_DEBUG
    208 	if (cmos_debug)
    209 		cmos_dump(sc->sc_buf);
    210 #endif
    211 	s = splclock();
    212 	for (i = NVRAM_DISKETTE; i < CMOS_SIZE; i++)
    213 		mc146818_write(sc, i, sc->sc_buf[i]);
    214 	splx(s);
    215 
    216 	return error;
    217 }
    218 
    219 static void
    220 cmos_sum(uint8_t *p, int from, int to, int offset)
    221 {
    222 	int i;
    223 	uint16_t sum;
    224 
    225 #ifdef CMOS_DEBUG
    226 	printf("%s: from %d to %d and store %d\n", __func__, from, to, offset);
    227 #endif
    228 
    229 	sum = 0;
    230 	for (i = from; i < to; i++)
    231 		sum += p[i];
    232 	p[offset] = sum >> 8;
    233 	p[offset + 1] = sum & 0xff;
    234 }
    235 
    236 #ifdef CMOS_DEBUG
    237 static void
    238 cmos_dump(uint8_t *p)
    239 {
    240 	int i;
    241 
    242 	for (i = 0; i < CMOS_SIZE; i++) {
    243 		if (i % 16 == 0)
    244 			printf("%02x:", i);
    245 		printf(" %02x", p[i]);
    246 		if (i % 16 == 15)
    247 			printf("\n", buf);
    248 	}
    249 	printf("\n");
    250 }
    251 #endif
    252