Home | History | Annotate | Line # | Download | only in mace
mcclock_mace.c revision 1.5
      1 /*	$NetBSD: mcclock_mace.c,v 1.5 2006/09/05 01:38:59 rumble Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *        This product includes software developed by the NetBSD
     17  *        Foundation, Inc. and its contributors.
     18  * 4. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
     23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1998 Mark Brinicombe.
     37  * Copyright (c) 1998 Causality Limited.
     38  * All rights reserved.
     39  *
     40  * Written by Mark Brinicombe, Causality Limited
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by Mark Brinicombe
     53  *	for the NetBSD Project.
     54  * 4. The name of the company nor the name of the author may be used to
     55  *    endorse or promote products derived from this software without specific
     56  *    prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
     59  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     60  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     61  * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     64  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: mcclock_mace.c,v 1.5 2006/09/05 01:38:59 rumble Exp $");
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 
     77 #include <machine/cpu.h>
     78 #include <machine/autoconf.h>
     79 #include <machine/bus.h>
     80 #include <machine/machtype.h>
     81 
     82 #include <dev/clock_subr.h>
     83 #include <dev/ic/ds1687reg.h>
     84 
     85 #include <sgimips/mace/macevar.h>
     86 
     87 #include <sgimips/sgimips/clockvar.h>
     88 
     89 struct mcclock_mace_softc {
     90 	struct device		sc_dev;
     91 
     92 	struct todr_chip_handle sc_todrch;
     93 
     94 	bus_space_tag_t		sc_st;
     95 	bus_space_handle_t	sc_sh;
     96 };
     97 
     98 static int	mcclock_mace_match(struct device *, struct cfdata *, void *);
     99 static void	mcclock_mace_attach(struct device*, struct device *, void *);
    100 
    101 static int	mcclock_mace_gettime(struct todr_chip_handle *,
    102     volatile struct timeval *);
    103 static int	mcclock_mace_settime(struct todr_chip_handle *,
    104     volatile struct timeval *);
    105 
    106 unsigned int	ds1687_read(void *arg, unsigned int addr);
    107 void		ds1687_write(void *arg, unsigned int addr, unsigned int data);
    108 
    109 CFATTACH_DECL(mcclock_mace, sizeof(struct mcclock_mace_softc),
    110     mcclock_mace_match, mcclock_mace_attach, NULL, NULL);
    111 
    112 static int
    113 mcclock_mace_match(struct device *parent, struct cfdata *match, void *aux)
    114 {
    115 	return 1;
    116 }
    117 
    118 void
    119 mcclock_mace_attach(struct device *parent, struct device *self, void *aux)
    120 {
    121 	struct mcclock_mace_softc *sc = (void *)self;
    122 	struct mace_attach_args *maa = aux;
    123 
    124 	sc->sc_st = maa->maa_st;
    125 	/* XXX should be bus_space_map() */
    126 	if (bus_space_subregion(maa->maa_st, maa->maa_sh,
    127 	    maa->maa_offset, 0, &sc->sc_sh))
    128 		panic("mcclock_mace_attach: couldn't map");
    129 
    130 	/*
    131 	 * We want a fixed format: 24-hour, BCD data, so just force the
    132 	 * RTC to this mode; if there was junk there we'll be able to
    133 	 * fix things up later when we discover the time read back is
    134 	 * no good.
    135 	 */
    136 	ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1);
    137 	ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS);
    138 
    139 	/* XXXrkb: init kickstart/wakeup stuff */
    140 
    141 	if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT))
    142 		printf(": lithium cell is dead, RTC unreliable");
    143 
    144 	printf("\n");
    145 
    146 	sc->sc_todrch.cookie = sc;
    147 	sc->sc_todrch.todr_gettime = mcclock_mace_gettime;
    148 	sc->sc_todrch.todr_settime = mcclock_mace_settime;
    149 	sc->sc_todrch.todr_setwen = NULL;
    150 
    151 	todr_attach(&sc->sc_todrch);
    152 }
    153 
    154 /*
    155  * We need to use the following two functions from
    156  * DS1687_PUT/GETTOD(). Hence the names.
    157  */
    158 unsigned int
    159 ds1687_read(void *arg, unsigned int addr)
    160 {
    161 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
    162 
    163 	return (bus_space_read_1(sc->sc_st, sc->sc_sh, addr));
    164 }
    165 
    166 void
    167 ds1687_write(void *arg, unsigned int addr, unsigned int data)
    168 {
    169 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
    170 
    171 	bus_space_write_1(sc->sc_st, sc->sc_sh, addr, data);
    172 }
    173 
    174 static int
    175 mcclock_mace_gettime(struct todr_chip_handle *todrch,
    176     volatile struct timeval *tv)
    177 {
    178 	struct mcclock_mace_softc *sc =
    179 	    (struct mcclock_mace_softc *)todrch->cookie;
    180 	struct clock_ymdhms dt;
    181 	ds1687_todregs regs;
    182 	int s;
    183 
    184 	s = splhigh();
    185 	DS1687_GETTOD(sc, &regs);
    186 	splx(s);
    187 
    188 	dt.dt_sec = FROMBCD(regs[DS1687_SOFT_SEC]);
    189 	dt.dt_min = FROMBCD(regs[DS1687_SOFT_MIN]);
    190 	dt.dt_hour = FROMBCD(regs[DS1687_SOFT_HOUR]);
    191 	dt.dt_wday = FROMBCD(regs[DS1687_SOFT_DOW]);
    192 	dt.dt_day = FROMBCD(regs[DS1687_SOFT_DOM]);
    193 	dt.dt_mon = FROMBCD(regs[DS1687_SOFT_MONTH]);
    194 	dt.dt_year = FROMBCD(regs[DS1687_SOFT_YEAR]) +
    195 	    (100 * FROMBCD(regs[DS1687_SOFT_CENTURY]));
    196 
    197 	/* simple sanity checks */
    198 	if (dt.dt_mon > 12 || dt.dt_day > 31 ||
    199 	    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
    200 		return (EIO);
    201 
    202 	tv->tv_sec = (long)clock_ymdhms_to_secs(&dt);
    203 	if (tv->tv_sec == -1)
    204 		return (ERANGE);
    205 	tv->tv_usec = 0;
    206 
    207 	return (0);
    208 }
    209 
    210 static int
    211 mcclock_mace_settime(struct todr_chip_handle *todrch,
    212     volatile struct timeval *tv)
    213 {
    214 	struct mcclock_mace_softc *sc =
    215 	    (struct mcclock_mace_softc *)todrch->cookie;
    216 	struct clock_ymdhms dt;
    217 	ds1687_todregs regs;
    218 	int s;
    219 
    220 	clock_secs_to_ymdhms((time_t)(tv->tv_sec + (tv->tv_usec > 500000)),&dt);
    221 
    222 	memset(&regs, 0, sizeof(regs));
    223 
    224 	regs[DS1687_SOFT_SEC] = TOBCD(dt.dt_sec);
    225 	regs[DS1687_SOFT_MIN] = TOBCD(dt.dt_min);
    226 	regs[DS1687_SOFT_HOUR] = TOBCD(dt.dt_hour);
    227 	regs[DS1687_SOFT_DOW] = TOBCD(dt.dt_wday);
    228 	regs[DS1687_SOFT_DOM] = TOBCD(dt.dt_day);
    229 	regs[DS1687_SOFT_MONTH] = TOBCD(dt.dt_mon);
    230 	regs[DS1687_SOFT_YEAR] = TOBCD(dt.dt_year % 100);
    231 	regs[DS1687_SOFT_CENTURY] = TOBCD(dt.dt_year / 100);
    232 	s = splhigh();
    233 	DS1687_PUTTOD(sc, &regs);
    234 	splx(s);
    235 
    236 	return (0);
    237 }
    238