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