Home | History | Annotate | Line # | Download | only in mace
mcclock_mace.c revision 1.7
      1 /*	$NetBSD: mcclock_mace.c,v 1.7 2007/09/26 05:50:02 macallan 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.7 2007/09/26 05:50:02 macallan 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 struct mcclock_mace_softc *mace0 = NULL;
     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 int	mcclock_mace_gettime(struct todr_chip_handle *,
    100     volatile struct timeval *);
    101 static int	mcclock_mace_settime(struct todr_chip_handle *,
    102     volatile struct timeval *);
    103 
    104 unsigned int	ds1687_read(void *arg, unsigned int addr);
    105 void		ds1687_write(void *arg, unsigned int addr, unsigned int data);
    106 
    107 void		mcclock_poweroff(void);
    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 	return 1;
    117 }
    118 
    119 void
    120 mcclock_mace_attach(struct device *parent, struct device *self, void *aux)
    121 {
    122 	struct mcclock_mace_softc *sc = (void *)self;
    123 	struct mace_attach_args *maa = aux;
    124 
    125 	sc->sc_st = maa->maa_st;
    126 	/* XXX should be bus_space_map() */
    127 	if (bus_space_subregion(maa->maa_st, maa->maa_sh,
    128 	    maa->maa_offset, 0, &sc->sc_sh))
    129 		panic("mcclock_mace_attach: couldn't map");
    130 
    131 	/*
    132 	 * We want a fixed format: 24-hour, BCD data, so just force the
    133 	 * RTC to this mode; if there was junk there we'll be able to
    134 	 * fix things up later when we discover the time read back is
    135 	 * no good.
    136 	 */
    137 	ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1);
    138 	ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS);
    139 
    140 	/* XXXrkb: init kickstart/wakeup stuff */
    141 
    142 	if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT))
    143 		printf(": lithium cell is dead, RTC unreliable");
    144 
    145 	printf("\n");
    146 
    147 	sc->sc_todrch.cookie = sc;
    148 	sc->sc_todrch.todr_gettime = mcclock_mace_gettime;
    149 	sc->sc_todrch.todr_settime = mcclock_mace_settime;
    150 	sc->sc_todrch.todr_setwen = NULL;
    151 
    152 	todr_attach(&sc->sc_todrch);
    153 	mace0 = sc;
    154 }
    155 
    156 /*
    157  * We need to use the following two functions from
    158  * DS1687_PUT/GETTOD(). Hence the names.
    159  */
    160 unsigned int
    161 ds1687_read(void *arg, unsigned int addr)
    162 {
    163 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
    164 
    165 	return (bus_space_read_1(sc->sc_st, sc->sc_sh, addr));
    166 }
    167 
    168 void
    169 ds1687_write(void *arg, unsigned int addr, unsigned int data)
    170 {
    171 	struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
    172 
    173 	bus_space_write_1(sc->sc_st, sc->sc_sh, addr, data);
    174 }
    175 
    176 static int
    177 mcclock_mace_gettime(struct todr_chip_handle *todrch,
    178     volatile struct timeval *tv)
    179 {
    180 	struct mcclock_mace_softc *sc =
    181 	    (struct mcclock_mace_softc *)todrch->cookie;
    182 	struct clock_ymdhms dt;
    183 	ds1687_todregs regs;
    184 	int s;
    185 
    186 	s = splhigh();
    187 	DS1687_GETTOD(sc, &regs);
    188 	splx(s);
    189 
    190 	dt.dt_sec = FROMBCD(regs[DS1687_SOFT_SEC]);
    191 	dt.dt_min = FROMBCD(regs[DS1687_SOFT_MIN]);
    192 	dt.dt_hour = FROMBCD(regs[DS1687_SOFT_HOUR]);
    193 	dt.dt_wday = FROMBCD(regs[DS1687_SOFT_DOW]);
    194 	dt.dt_day = FROMBCD(regs[DS1687_SOFT_DOM]);
    195 	dt.dt_mon = FROMBCD(regs[DS1687_SOFT_MONTH]);
    196 	dt.dt_year = FROMBCD(regs[DS1687_SOFT_YEAR]) +
    197 	    (100 * FROMBCD(regs[DS1687_SOFT_CENTURY]));
    198 
    199 	/* simple sanity checks */
    200 	if (dt.dt_mon > 12 || dt.dt_day > 31 ||
    201 	    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
    202 		return (EIO);
    203 
    204 	tv->tv_sec = (long)clock_ymdhms_to_secs(&dt);
    205 	if (tv->tv_sec == -1)
    206 		return (ERANGE);
    207 	tv->tv_usec = 0;
    208 
    209 	return (0);
    210 }
    211 
    212 static int
    213 mcclock_mace_settime(struct todr_chip_handle *todrch,
    214     volatile struct timeval *tv)
    215 {
    216 	struct mcclock_mace_softc *sc =
    217 	    (struct mcclock_mace_softc *)todrch->cookie;
    218 	struct clock_ymdhms dt;
    219 	ds1687_todregs regs;
    220 	int s;
    221 
    222 	clock_secs_to_ymdhms((time_t)(tv->tv_sec + (tv->tv_usec > 500000)),&dt);
    223 
    224 	memset(&regs, 0, sizeof(regs));
    225 
    226 	regs[DS1687_SOFT_SEC] = TOBCD(dt.dt_sec);
    227 	regs[DS1687_SOFT_MIN] = TOBCD(dt.dt_min);
    228 	regs[DS1687_SOFT_HOUR] = TOBCD(dt.dt_hour);
    229 	regs[DS1687_SOFT_DOW] = TOBCD(dt.dt_wday);
    230 	regs[DS1687_SOFT_DOM] = TOBCD(dt.dt_day);
    231 	regs[DS1687_SOFT_MONTH] = TOBCD(dt.dt_mon);
    232 	regs[DS1687_SOFT_YEAR] = TOBCD(dt.dt_year % 100);
    233 	regs[DS1687_SOFT_CENTURY] = TOBCD(dt.dt_year / 100);
    234 	s = splhigh();
    235 	DS1687_PUTTOD(sc, &regs);
    236 	splx(s);
    237 
    238 	return (0);
    239 }
    240 
    241 void
    242 mcclock_poweroff()
    243 {
    244 	int s;
    245 	uint8_t a, xctl_a, xctl_b;
    246 
    247 	if (mace0 == NULL)
    248 		return;
    249 
    250 	s = splhigh();
    251 	a = ds1687_read(mace0, DS1687_CONTROLA);
    252 	a &= ~DS1687_DV2;
    253 	a |= DS1687_DV1;
    254 	ds1687_write(mace0, DS1687_CONTROLA, a | DS1687_BANK1);
    255 	wbflush();
    256 
    257 	xctl_b = ds1687_read(mace0, DS1687_BANK1_XCTRL4B);
    258 	xctl_b |= DS1687_X4B_ABE | DS1687_X4B_KIE;
    259 	ds1687_write(mace0, DS1687_BANK1_XCTRL4B, xctl_b);
    260 
    261 	xctl_a = ds1687_read(mace0, DS1687_BANK1_XCTRL4A);
    262 	xctl_a &= ~(DS1687_X4A_RCF | DS1687_X4A_WAF | DS1687_X4A_KF);
    263 	ds1687_write(mace0, DS1687_BANK1_XCTRL4A, xctl_a);
    264 	wbflush();
    265 
    266 	/* and down we go */
    267 	ds1687_write(mace0, DS1687_BANK1_XCTRL4A, xctl_a | DS1687_X4A_PAB);
    268 	ds1687_write(mace0, DS1687_CONTROLA, a);
    269 	wbflush();
    270 	while(1);
    271 }
    272