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