Home | History | Annotate | Line # | Download | only in dev
rtc.c revision 1.20
      1 /*	$NetBSD: rtc.c,v 1.20 2020/01/02 22:32:20 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 Valeriy E. Ushakov
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * `rtc' is a DS1287A (== MC146818A) time-of-day clock at EBus.
     32  * In Krups it's not used to store idprom so this driver doesn't
     33  * support it.  Don't know about other ms-IIep systems.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.20 2020/01/02 22:32:20 thorpej Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <sys/systm.h>
     44 #ifdef GPROF
     45 #include <sys/gmon.h>
     46 #endif
     47 
     48 #include <sys/bus.h>
     49 #include <machine/autoconf.h>
     50 
     51 #include <dev/clock_subr.h>
     52 #include <dev/ic/mc146818reg.h>
     53 
     54 #include <dev/ebus/ebusreg.h>
     55 #include <dev/ebus/ebusvar.h>
     56 
     57 struct rtc_ebus_softc {
     58 	bus_space_tag_t		sc_bt;	/* parent bus tag */
     59 	bus_space_handle_t	sc_bh;	/* handle for registers */
     60 	struct todr_chip_handle	sc_todr;/* TODR handle */
     61 };
     62 
     63 static int	rtcmatch_ebus(device_t, cfdata_t, void *);
     64 static void	rtcattach_ebus(device_t, device_t, void *);
     65 
     66 CFATTACH_DECL_NEW(rtc_ebus, sizeof(struct rtc_ebus_softc),
     67     rtcmatch_ebus, rtcattach_ebus, NULL, NULL);
     68 
     69 /* todr(9) methods */
     70 static int rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     71 static int rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     72 
     73 int rtc_auto_century_adjust = 1; /* XXX: do we ever want not to? */
     74 
     75 /*
     76  * MD read/write functions declared in mc146818reg.h
     77  */
     78 #define	RTC_ADDR	0
     79 #define	RTC_DATA	1
     80 
     81 u_int
     82 mc146818_read(void *cookie, u_int reg)
     83 {
     84 	struct rtc_ebus_softc *sc = cookie;
     85 
     86 	bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
     87 	return (bus_space_read_1(sc->sc_bt, sc->sc_bh, RTC_DATA));
     88 }
     89 
     90 void
     91 mc146818_write(void *cookie, u_int reg, u_int datum)
     92 {
     93 	struct rtc_ebus_softc *sc = cookie;
     94 
     95 	bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg);
     96 	bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_DATA, datum);
     97 }
     98 
     99 
    100 static int
    101 rtcmatch_ebus(device_t parent, cfdata_t cf, void *aux)
    102 {
    103 	struct ebus_attach_args *ea = aux;
    104 
    105 	return (strcmp(cf->cf_name, ea->ea_name) == 0);
    106 }
    107 
    108 static void
    109 rtcattach_ebus(device_t parent, device_t self, void *aux)
    110 {
    111 	struct rtc_ebus_softc *sc = device_private(self);
    112 	struct ebus_attach_args *ea = aux;
    113 	todr_chip_handle_t handle;
    114 
    115 	sc->sc_bt = ea->ea_bustag;
    116 	if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    117 			  ea->ea_reg[0].size, 0, &sc->sc_bh) != 0)
    118 	{
    119 		printf(": unable to map registers\n");
    120 		return;
    121 	}
    122 
    123 	/* XXX: no "model" property in Krups */
    124 	printf(": time-of-day clock\n");
    125 
    126 	/*
    127 	 * Turn interrupts off (clear MC_REGB_?IE bits), just in case
    128 	 * (although they shouldn't be wired to an interrupt
    129 	 * controller on sparcs).
    130 	 */
    131 	mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
    132 
    133 	/* setup our todr_handle */
    134 	handle = &sc->sc_todr;
    135 	handle->cookie = sc;
    136 	handle->bus_cookie = NULL; /* unused */
    137 	handle->todr_gettime = NULL;
    138 	handle->todr_settime = NULL;
    139 	handle->todr_gettime_ymdhms = rtc_gettime_ymdhms;
    140 	handle->todr_settime_ymdhms = rtc_settime_ymdhms;
    141 	handle->todr_setwen = NULL; /* not necessary, no idprom to protect */
    142 
    143 	todr_attach(handle);
    144 }
    145 
    146 
    147 static int
    148 rtc_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    149 {
    150 	struct rtc_ebus_softc *sc = handle->cookie;
    151 	u_int year;
    152 
    153 	/* update in progress; spin loop */
    154 	while (mc146818_read(sc, MC_REGA) & MC_REGA_UIP)
    155 		continue;
    156 
    157 	/* stop updates (XXX: do we need that???) */
    158 	mc146818_write(sc, MC_REGB,
    159 		       (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
    160 
    161 	/* read time */
    162 	dt->dt_sec  = mc146818_read(sc, MC_SEC);
    163 	dt->dt_min  = mc146818_read(sc, MC_MIN);
    164 	dt->dt_hour = mc146818_read(sc, MC_HOUR);
    165 	dt->dt_day  = mc146818_read(sc, MC_DOM);
    166 	dt->dt_mon  = mc146818_read(sc, MC_MONTH);
    167 	year        = mc146818_read(sc, MC_YEAR);
    168 
    169 	/* reenable updates */
    170 	mc146818_write(sc, MC_REGB,
    171 		       (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
    172 
    173 	/* year in the century 0..99: adjust to AD */
    174 	year += 1900;
    175 	if (year < POSIX_BASE_YEAR && rtc_auto_century_adjust != 0)
    176 		year += 100;
    177 	dt->dt_year = year;
    178 
    179 	return (0);
    180 }
    181 
    182 static int
    183 rtc_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    184 {
    185 	struct rtc_ebus_softc *sc = handle->cookie;
    186 	u_int year;
    187 
    188 	year = dt->dt_year - 1900;
    189 	if (year >= 100 && rtc_auto_century_adjust != 0)
    190 		year -= 100;
    191 
    192 	/* stop updates */
    193 	mc146818_write(sc, MC_REGB,
    194 		       (mc146818_read(sc, MC_REGB) | MC_REGB_SET));
    195 
    196 	mc146818_write(sc, MC_SEC,   dt->dt_sec);
    197 	mc146818_write(sc, MC_MIN,   dt->dt_min);
    198 	mc146818_write(sc, MC_HOUR,  dt->dt_hour);
    199 	mc146818_write(sc, MC_DOW,   dt->dt_wday + 1);
    200 	mc146818_write(sc, MC_DOM,   dt->dt_day);
    201 	mc146818_write(sc, MC_MONTH, dt->dt_mon);
    202 	mc146818_write(sc, MC_YEAR,  year);
    203 
    204 	/* reenable updates */
    205 	mc146818_write(sc, MC_REGB,
    206 		       (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET));
    207 	return (0);
    208 }
    209