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