Home | History | Annotate | Line # | Download | only in isa
      1 /*	$NetBSD: cmos.c,v 1.14 2023/12/20 15:00:07 thorpej 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  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     48  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     49  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     50  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     53  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     54  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     55  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
     57  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: cmos.c,v 1.14 2023/12/20 15:00:07 thorpej Exp $");
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/kernel.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/proc.h>
     69 #include <sys/device.h>
     70 #include <sys/conf.h>
     71 #include <sys/kauth.h>
     72 
     73 #include <sys/bus.h>
     74 #include <machine/intr.h>
     75 
     76 #include <dev/isa/isareg.h>
     77 #include <dev/isa/isavar.h>
     78 #include <dev/ic/mc146818reg.h>
     79 #include <i386/isa/nvram.h>
     80 
     81 #include "ioconf.h"
     82 
     83 #define	CMOS_SUM	32
     84 #define	CMOS_BIOSSPEC	34	/* start of BIOS-specific configuration data */
     85 
     86 #define	NVRAM_SUM	(MC_NVRAM_START + CMOS_SUM)
     87 #define	NVRAM_BIOSSPEC	(MC_NVRAM_START + CMOS_BIOSSPEC)
     88 
     89 #define	CMOS_SIZE	NVRAM_BIOSSPEC
     90 
     91 dev_type_open(cmos_open);
     92 dev_type_read(cmos_read);
     93 dev_type_write(cmos_write);
     94 
     95 static void cmos_sum(uint8_t *, int, int, int);
     96 
     97 const struct cdevsw cmos_cdevsw = {
     98 	.d_open = cmos_open,
     99 	.d_close = nullclose,
    100 	.d_read = cmos_read,
    101 	.d_write = cmos_write,
    102 	.d_ioctl = noioctl,
    103 	.d_stop = nostop,
    104 	.d_tty = notty,
    105 	.d_poll = nopoll,
    106 	.d_mmap = nommap,
    107 	.d_kqfilter = nokqfilter,
    108 	.d_discard = nodiscard,
    109 	.d_flag = D_OTHER | D_MPSAFE
    110 };
    111 
    112 static kmutex_t cmos_lock;
    113 static uint8_t cmos_buf[CMOS_SIZE];
    114 
    115 void
    116 cmosattach(int n)
    117 {
    118 
    119 	mutex_init(&cmos_lock, MUTEX_DEFAULT, IPL_NONE);
    120 }
    121 
    122 int
    123 cmos_open(dev_t dev, int flags, int ifmt, struct lwp *l)
    124 {
    125 
    126 	return kauth_authorize_machdep(l->l_cred,
    127 	    KAUTH_MACHDEP_NVRAM, NULL, NULL, NULL, NULL);
    128 }
    129 
    130 static void
    131 cmos_fetch(void)
    132 {
    133 	int i, s;
    134 	uint8_t *p;
    135 
    136 	p = cmos_buf;
    137 	s = splclock();
    138 	for (i = 0; i < CMOS_SIZE; i++)
    139 		*p++ = mc146818_read(NULL, i);
    140 	splx(s);
    141 }
    142 
    143 int
    144 cmos_read(dev_t dev, struct uio *uio, int ioflag)
    145 {
    146 	int error;
    147 
    148 	if (uio->uio_offset + uio->uio_resid > CMOS_SIZE)
    149 		return EINVAL;
    150 
    151 	mutex_enter(&cmos_lock);
    152 	cmos_fetch();
    153 	error = uiomove(cmos_buf + uio->uio_offset, uio->uio_resid, uio);
    154 	mutex_exit(&cmos_lock);
    155 
    156 	return error;
    157 }
    158 
    159 int
    160 cmos_write(dev_t dev, struct uio *uio, int ioflag)
    161 {
    162 	int error = 0, i, s;
    163 
    164 	if (uio->uio_offset + uio->uio_resid > CMOS_SIZE)
    165 		return EINVAL;
    166 
    167 	mutex_enter(&cmos_lock);
    168 	cmos_fetch();
    169 	error = uiomove(cmos_buf + uio->uio_offset, uio->uio_resid, uio);
    170 	if (error == 0) {
    171 		cmos_sum(cmos_buf, NVRAM_DISKETTE, NVRAM_SUM, NVRAM_SUM);
    172 		s = splclock();
    173 		for (i = NVRAM_DISKETTE; i < CMOS_SIZE; i++)
    174 			mc146818_write(NULL, i, cmos_buf[i]);
    175 		splx(s);
    176 	}
    177 	mutex_exit(&cmos_lock);
    178 
    179 	return error;
    180 }
    181 
    182 static void
    183 cmos_sum(uint8_t *p, int from, int to, int offset)
    184 {
    185 	int i;
    186 	uint16_t sum;
    187 
    188 #ifdef CMOS_DEBUG
    189 	printf("%s: from %d to %d and store %d\n", __func__, from, to, offset);
    190 #endif
    191 
    192 	sum = 0;
    193 	for (i = from; i < to; i++)
    194 		sum += p[i];
    195 	p[offset] = sum >> 8;
    196 	p[offset + 1] = sum & 0xff;
    197 }
    198