Home | History | Annotate | Line # | Download | only in mace
mcclock_mace.c revision 1.1
      1 /*	$NetBSD: mcclock_mace.c,v 1.1 2004/01/18 04:06:43 sekiya 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.1 2004/01/18 04:06:43 sekiya 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 	bus_space_tag_t		sc_st;
     93 	bus_space_handle_t	sc_sh;
     94 };
     95 
     96 static int	mcclock_mace_match(struct device *, struct cfdata *, void *);
     97 static void	mcclock_mace_attach(struct device*, struct device *, void *);
     98 
     99 static void	mcclock_mace_init(struct device *);
    100 static void	mcclock_mace_get(struct device *, struct clock_ymdhms *);
    101 static void	mcclock_mace_set(struct device *, struct clock_ymdhms *);
    102 
    103 unsigned int	ds1687_read(void *arg, unsigned int addr);
    104 void		ds1687_write(void *arg, unsigned int addr, unsigned int data);
    105 
    106 const struct clockfns mcclock_mace_clockfns = {
    107 	mcclock_mace_init, mcclock_mace_get, mcclock_mace_set
    108 };
    109 
    110 CFATTACH_DECL(mcclock_mace, sizeof(struct mcclock_mace_softc),
    111     mcclock_mace_match, mcclock_mace_attach, NULL, NULL);
    112 
    113 static int
    114 mcclock_mace_match(struct device *parent, struct cfdata *match, void *aux)
    115 {
    116 
    117 	return 1;
    118 }
    119 
    120 void
    121 mcclock_mace_attach(struct device *parent, struct device *self, void *aux)
    122 {
    123 	struct mcclock_mace_softc *sc = (void *)self;
    124 	struct mace_attach_args *maa = aux;
    125 
    126 	sc->sc_st = maa->maa_st;
    127 	/* XXX should be bus_space_map() */
    128 	if (bus_space_subregion(maa->maa_st, maa->maa_sh,
    129 	    maa->maa_offset, 0, &sc->sc_sh))
    130 		panic("mcclock_mace_attach: couldn't map");
    131 
    132 	/*
    133 	 * We want a fixed format: 24-hour, BCD data, so just force the
    134 	 * RTC to this mode; if there was junk there we'll be able to
    135 	 * fix things up later when we discover the time read back is
    136 	 * no good.
    137 	 */
    138 	ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1);
    139 	ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS);
    140 
    141 	/* XXXrkb: init kickstart/wakeup stuff */
    142 
    143 	if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT))
    144 		printf(": lithium cell is dead, RTC unreliable");
    145 
    146 	printf("\n");
    147 	clockattach(&sc->sc_dev, &mcclock_mace_clockfns);
    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 void
    171 mcclock_mace_init(struct device *dev)
    172 {
    173 
    174 	return;
    175 }
    176 
    177 static void
    178 mcclock_mace_get(struct device *dev, struct clock_ymdhms *dt)
    179 {
    180 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
    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 
    198 static void
    199 mcclock_mace_set(struct device *dev, struct clock_ymdhms *dt)
    200 {
    201 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
    202 	ds1687_todregs regs;
    203 	int s;
    204 
    205 	memset(&regs, 0, sizeof(regs));
    206 
    207 	regs[DS1687_SOFT_SEC] = TOBCD(dt->dt_sec);
    208 	regs[DS1687_SOFT_MIN] = TOBCD(dt->dt_min);
    209 	regs[DS1687_SOFT_HOUR] = TOBCD(dt->dt_hour);
    210 	regs[DS1687_SOFT_DOW] = TOBCD(dt->dt_wday);
    211 	regs[DS1687_SOFT_DOM] = TOBCD(dt->dt_day);
    212 	regs[DS1687_SOFT_MONTH] = TOBCD(dt->dt_mon);
    213 	regs[DS1687_SOFT_YEAR] = TOBCD(dt->dt_year % 100);
    214 	regs[DS1687_SOFT_CENTURY] = TOBCD(dt->dt_year / 100);
    215 	s = splhigh();
    216 	DS1687_PUTTOD(sc, &regs);
    217 	splx(s);
    218 }
    219