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