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