Home | History | Annotate | Line # | Download | only in dev
rtc.c revision 1.2.2.2
      1  1.2.2.2  nathanw /*	$NetBSD: rtc.c,v 1.2.2.2 2002/01/08 00:24:36 nathanw Exp $	*/
      2  1.2.2.2  nathanw 
      3  1.2.2.2  nathanw /*
      4  1.2.2.2  nathanw  * Copyright (c) 1988 University of Utah.
      5  1.2.2.2  nathanw  * Copyright (c) 1982, 1990, 1993
      6  1.2.2.2  nathanw  *	The Regents of the University of California.  All rights reserved.
      7  1.2.2.2  nathanw  *
      8  1.2.2.2  nathanw  * This code is derived from software contributed to Berkeley by
      9  1.2.2.2  nathanw  * the Systems Programming Group of the University of Utah Computer
     10  1.2.2.2  nathanw  * Science Department.
     11  1.2.2.2  nathanw  *
     12  1.2.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     13  1.2.2.2  nathanw  * modification, are permitted provided that the following conditions
     14  1.2.2.2  nathanw  * are met:
     15  1.2.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     16  1.2.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     17  1.2.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.2.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     19  1.2.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     20  1.2.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     21  1.2.2.2  nathanw  *    must display the following acknowledgement:
     22  1.2.2.2  nathanw  *	This product includes software developed by the University of
     23  1.2.2.2  nathanw  *	California, Berkeley and its contributors.
     24  1.2.2.2  nathanw  * 4. Neither the name of the University nor the names of its contributors
     25  1.2.2.2  nathanw  *    may be used to endorse or promote products derived from this software
     26  1.2.2.2  nathanw  *    without specific prior written permission.
     27  1.2.2.2  nathanw  *
     28  1.2.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.2.2.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.2.2.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.2.2.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.2.2.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.2.2.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.2.2.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.2.2.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.2.2.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.2.2.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.2.2.2  nathanw  * SUCH DAMAGE.
     39  1.2.2.2  nathanw  *
     40  1.2.2.2  nathanw  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     41  1.2.2.2  nathanw  *
     42  1.2.2.2  nathanw  *	@(#)clock.c	8.2 (Berkeley) 1/12/94
     43  1.2.2.2  nathanw  */
     44  1.2.2.2  nathanw 
     45  1.2.2.2  nathanw /*
     46  1.2.2.2  nathanw  * attachment for HP300 real-time clock (RTC)
     47  1.2.2.2  nathanw  */
     48  1.2.2.2  nathanw 
     49  1.2.2.2  nathanw #include <sys/param.h>
     50  1.2.2.2  nathanw #include <sys/systm.h>
     51  1.2.2.2  nathanw #include <sys/device.h>
     52  1.2.2.2  nathanw #include <sys/malloc.h>
     53  1.2.2.2  nathanw 
     54  1.2.2.2  nathanw #include <hp300/dev/intiovar.h>
     55  1.2.2.2  nathanw #include <hp300/dev/rtcreg.h>
     56  1.2.2.2  nathanw 
     57  1.2.2.2  nathanw #include <dev/clock_subr.h>
     58  1.2.2.2  nathanw #include <hp300/hp300/clockvar.h>
     59  1.2.2.2  nathanw 
     60  1.2.2.2  nathanw struct rtc_softc {
     61  1.2.2.2  nathanw 	struct device sc_dev;
     62  1.2.2.2  nathanw 	bus_space_tag_t sc_bst;
     63  1.2.2.2  nathanw 	bus_space_handle_t sc_bsh;
     64  1.2.2.2  nathanw };
     65  1.2.2.2  nathanw 
     66  1.2.2.2  nathanw static int rtcmatch(struct device *, struct cfdata *, void *);
     67  1.2.2.2  nathanw static void rtcattach(struct device *, struct device *, void *aux);
     68  1.2.2.2  nathanw 
     69  1.2.2.2  nathanw struct cfattach rtc_ca = {
     70  1.2.2.2  nathanw 	sizeof (struct rtc_softc), rtcmatch, rtcattach,
     71  1.2.2.2  nathanw };
     72  1.2.2.2  nathanw 
     73  1.2.2.2  nathanw static int rtc_gettime(todr_chip_handle_t, struct timeval *);
     74  1.2.2.2  nathanw static int rtc_settime(todr_chip_handle_t, struct timeval *);
     75  1.2.2.2  nathanw static int rtc_getcal(todr_chip_handle_t, int *);
     76  1.2.2.2  nathanw static int rtc_setcal(todr_chip_handle_t, int);
     77  1.2.2.2  nathanw static u_int8_t	rtc_readreg(struct rtc_softc *, int);
     78  1.2.2.2  nathanw static u_int8_t rtc_writereg(struct rtc_softc *, int, u_int8_t);
     79  1.2.2.2  nathanw 
     80  1.2.2.2  nathanw 
     81  1.2.2.2  nathanw static int
     82  1.2.2.2  nathanw rtcmatch(parent, match, aux)
     83  1.2.2.2  nathanw 	struct device *parent;
     84  1.2.2.2  nathanw 	struct cfdata *match;
     85  1.2.2.2  nathanw 	void *aux;
     86  1.2.2.2  nathanw {
     87  1.2.2.2  nathanw 	struct intio_attach_args *ia = aux;
     88  1.2.2.2  nathanw 
     89  1.2.2.2  nathanw 	if (strcmp("rtc     ", ia->ia_modname) != 0)
     90  1.2.2.2  nathanw 		return (0);
     91  1.2.2.2  nathanw 
     92  1.2.2.2  nathanw 	return (1);
     93  1.2.2.2  nathanw }
     94  1.2.2.2  nathanw 
     95  1.2.2.2  nathanw static void
     96  1.2.2.2  nathanw rtcattach(parent, self, aux)
     97  1.2.2.2  nathanw 	struct device *parent, *self;
     98  1.2.2.2  nathanw 	void *aux;
     99  1.2.2.2  nathanw {
    100  1.2.2.2  nathanw 	struct intio_attach_args *ia = aux;
    101  1.2.2.2  nathanw 	struct rtc_softc *sc = (struct rtc_softc *)self;
    102  1.2.2.2  nathanw 	struct todr_chip_handle *todr_handle;
    103  1.2.2.2  nathanw 
    104  1.2.2.2  nathanw 	printf("\n");
    105  1.2.2.2  nathanw 
    106  1.2.2.2  nathanw 	if (bus_space_map(ia->ia_bst, ia->ia_iobase, INTIO_DEVSIZE, 0,
    107  1.2.2.2  nathanw 	    &sc->sc_bsh)) {
    108  1.2.2.2  nathanw 		printf("%s: can't map registers\n",
    109  1.2.2.2  nathanw 		    sc->sc_dev.dv_xname);
    110  1.2.2.2  nathanw 		return;
    111  1.2.2.2  nathanw 	}
    112  1.2.2.2  nathanw 
    113  1.2.2.2  nathanw 	todr_handle = malloc(sizeof(struct todr_chip_handle),
    114  1.2.2.2  nathanw 	    M_DEVBUF, M_NOWAIT);
    115  1.2.2.2  nathanw 
    116  1.2.2.2  nathanw 	todr_handle->cookie = sc;
    117  1.2.2.2  nathanw 	todr_handle->todr_gettime = rtc_gettime;
    118  1.2.2.2  nathanw 	todr_handle->todr_settime = rtc_settime;
    119  1.2.2.2  nathanw 	todr_handle->todr_getcal = rtc_getcal;
    120  1.2.2.2  nathanw 	todr_handle->todr_setcal = rtc_setcal;
    121  1.2.2.2  nathanw 	todr_handle->todr_setwen = NULL;
    122  1.2.2.2  nathanw 
    123  1.2.2.2  nathanw 	clockattach(todr_handle);
    124  1.2.2.2  nathanw }
    125  1.2.2.2  nathanw 
    126  1.2.2.2  nathanw static int
    127  1.2.2.2  nathanw rtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
    128  1.2.2.2  nathanw {
    129  1.2.2.2  nathanw 	struct rtc_softc *sc = (struct rtc_softc *)handle->cookie;
    130  1.2.2.2  nathanw 	int i, read_okay, year;
    131  1.2.2.2  nathanw 	struct clock_ymdhms dt;
    132  1.2.2.2  nathanw 	u_int8_t rtc_registers[NUM_RTC_REGS];
    133  1.2.2.2  nathanw 
    134  1.2.2.2  nathanw 	/* read rtc registers */
    135  1.2.2.2  nathanw 	read_okay = 0;
    136  1.2.2.2  nathanw 	while (!read_okay) {
    137  1.2.2.2  nathanw 		read_okay = 1;
    138  1.2.2.2  nathanw 		for (i = 0; i < NUM_RTC_REGS; i++)
    139  1.2.2.2  nathanw 			rtc_registers[i] = rtc_readreg(sc, i);
    140  1.2.2.2  nathanw 		for (i = 0; i < NUM_RTC_REGS; i++)
    141  1.2.2.2  nathanw 			if (rtc_registers[i] != rtc_readreg(sc, i))
    142  1.2.2.2  nathanw 				read_okay = 0;
    143  1.2.2.2  nathanw 	}
    144  1.2.2.2  nathanw 
    145  1.2.2.2  nathanw #define	rtc_to_decimal(a,b) 	(rtc_registers[a] * 10 + rtc_registers[b])
    146  1.2.2.2  nathanw 
    147  1.2.2.2  nathanw 	dt.dt_sec  = rtc_to_decimal(1, 0);
    148  1.2.2.2  nathanw 	dt.dt_min  = rtc_to_decimal(3, 2);
    149  1.2.2.2  nathanw 	dt.dt_hour = (rtc_registers[5] & RTC_REG5_HOUR) * 10 + rtc_registers[4];
    150  1.2.2.2  nathanw 	dt.dt_day  = rtc_to_decimal(8, 7);
    151  1.2.2.2  nathanw 	dt.dt_mon  = rtc_to_decimal(10, 9);
    152  1.2.2.2  nathanw 
    153  1.2.2.2  nathanw 	year = rtc_to_decimal(12, 11) + RTC_BASE_YEAR;
    154  1.2.2.2  nathanw 	if (year < POSIX_BASE_YEAR)
    155  1.2.2.2  nathanw 		year += 100;
    156  1.2.2.2  nathanw 	dt.dt_year = year;
    157  1.2.2.2  nathanw 
    158  1.2.2.2  nathanw #undef	rtc_to_decimal
    159  1.2.2.2  nathanw 
    160  1.2.2.2  nathanw 	/* simple sanity checks */
    161  1.2.2.2  nathanw 	if (dt.dt_mon > 12 || dt.dt_day > 31 ||
    162  1.2.2.2  nathanw 	    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
    163  1.2.2.2  nathanw 		return (1);
    164  1.2.2.2  nathanw 
    165  1.2.2.2  nathanw 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    166  1.2.2.2  nathanw 	tv->tv_usec = 0;
    167  1.2.2.2  nathanw 	return (0);
    168  1.2.2.2  nathanw }
    169  1.2.2.2  nathanw 
    170  1.2.2.2  nathanw static int
    171  1.2.2.2  nathanw rtc_settime(todr_chip_handle_t handle, struct timeval *tv)
    172  1.2.2.2  nathanw {
    173  1.2.2.2  nathanw 	struct rtc_softc *sc = (struct rtc_softc *)handle->cookie;
    174  1.2.2.2  nathanw 	int i, year;
    175  1.2.2.2  nathanw 	struct clock_ymdhms dt;
    176  1.2.2.2  nathanw 	u_int8_t rtc_registers[NUM_RTC_REGS];
    177  1.2.2.2  nathanw 
    178  1.2.2.2  nathanw 	/* Note: we ignore `tv_usec' */
    179  1.2.2.2  nathanw 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
    180  1.2.2.2  nathanw 
    181  1.2.2.2  nathanw 	year = dt.dt_year - RTC_BASE_YEAR;
    182  1.2.2.2  nathanw 	if (year > 99)
    183  1.2.2.2  nathanw 		year -= 100;
    184  1.2.2.2  nathanw 
    185  1.2.2.2  nathanw #define	decimal_to_rtc(a,b,n)		\
    186  1.2.2.2  nathanw 	rtc_registers[a] = (n) % 10;	\
    187  1.2.2.2  nathanw 	rtc_registers[b] = (n) / 10;
    188  1.2.2.2  nathanw 
    189  1.2.2.2  nathanw 	decimal_to_rtc(0, 1, dt.dt_sec);
    190  1.2.2.2  nathanw 	decimal_to_rtc(2, 3, dt.dt_min);
    191  1.2.2.2  nathanw 	decimal_to_rtc(7, 8, dt.dt_day);
    192  1.2.2.2  nathanw 	decimal_to_rtc(9, 10, dt.dt_mon);
    193  1.2.2.2  nathanw 	decimal_to_rtc(11, 12, year);
    194  1.2.2.2  nathanw 
    195  1.2.2.2  nathanw 	rtc_registers[4] = dt.dt_hour % 10;
    196  1.2.2.2  nathanw 	rtc_registers[5] = ((dt.dt_hour / 10) & RTC_REG5_HOUR) | RTC_REG5_24HR;
    197  1.2.2.2  nathanw 
    198  1.2.2.2  nathanw 	rtc_registers[6] = 0;
    199  1.2.2.2  nathanw 
    200  1.2.2.2  nathanw #undef	decimal_to_rtc
    201  1.2.2.2  nathanw 
    202  1.2.2.2  nathanw 	/* write rtc registers */
    203  1.2.2.2  nathanw 	rtc_writereg(sc, 15, 13);	/* reset prescalar */
    204  1.2.2.2  nathanw 	for (i = 0; i < NUM_RTC_REGS; i++)
    205  1.2.2.2  nathanw 		if (rtc_registers[i] !=
    206  1.2.2.2  nathanw 		    rtc_writereg(sc, i, rtc_registers[i]))
    207  1.2.2.2  nathanw 			return (1);
    208  1.2.2.2  nathanw 	return (0);
    209  1.2.2.2  nathanw }
    210  1.2.2.2  nathanw 
    211  1.2.2.2  nathanw static int
    212  1.2.2.2  nathanw rtc_getcal(todr_chip_handle_t handle, int *vp)
    213  1.2.2.2  nathanw {
    214  1.2.2.2  nathanw 	return (EOPNOTSUPP);
    215  1.2.2.2  nathanw }
    216  1.2.2.2  nathanw 
    217  1.2.2.2  nathanw static int
    218  1.2.2.2  nathanw rtc_setcal(todr_chip_handle_t handle, int v)
    219  1.2.2.2  nathanw {
    220  1.2.2.2  nathanw 	return (EOPNOTSUPP);
    221  1.2.2.2  nathanw }
    222  1.2.2.2  nathanw 
    223  1.2.2.2  nathanw static u_int8_t
    224  1.2.2.2  nathanw rtc_readreg(struct rtc_softc *sc, int reg)
    225  1.2.2.2  nathanw {
    226  1.2.2.2  nathanw 	bus_space_tag_t bst = sc->sc_bst;
    227  1.2.2.2  nathanw 	bus_space_handle_t bsh = sc->sc_bsh;
    228  1.2.2.2  nathanw 	u_int8_t data;
    229  1.2.2.2  nathanw 	int s = splvm();
    230  1.2.2.2  nathanw 
    231  1.2.2.2  nathanw 	data = reg;
    232  1.2.2.2  nathanw 	intio_device_writecmd(bst, bsh, RTC_SET_REG, &data, 1);
    233  1.2.2.2  nathanw 	intio_device_readcmd(bst, bsh, RTC_READ_REG, &data);
    234  1.2.2.2  nathanw 
    235  1.2.2.2  nathanw 	splx(s);
    236  1.2.2.2  nathanw 	return (data);
    237  1.2.2.2  nathanw }
    238  1.2.2.2  nathanw 
    239  1.2.2.2  nathanw static u_int8_t
    240  1.2.2.2  nathanw rtc_writereg(struct rtc_softc *sc, int reg, u_int8_t data)
    241  1.2.2.2  nathanw {
    242  1.2.2.2  nathanw 	bus_space_tag_t bst = sc->sc_bst;
    243  1.2.2.2  nathanw 	bus_space_handle_t bsh = sc->sc_bsh;
    244  1.2.2.2  nathanw 	u_int8_t tmp;
    245  1.2.2.2  nathanw 	int s = splvm();
    246  1.2.2.2  nathanw 
    247  1.2.2.2  nathanw 	tmp = (data << 4) | reg;
    248  1.2.2.2  nathanw 	intio_device_writecmd(bst, bsh, RTC_SET_REG, &tmp, 1);
    249  1.2.2.2  nathanw 	intio_device_writecmd(bst, bsh, RTC_WRITE_REG, NULL, 0);
    250  1.2.2.2  nathanw 	intio_device_readcmd(bst, bsh, RTC_READ_REG, &tmp);
    251  1.2.2.2  nathanw 
    252  1.2.2.2  nathanw 	splx(s);
    253  1.2.2.2  nathanw 	return (tmp);
    254  1.2.2.2  nathanw }
    255