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