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