Home | History | Annotate | Line # | Download | only in dec
mcclock.c revision 1.6
      1 /* $NetBSD: mcclock.c,v 1.6 1997/04/07 00:19:15 cgd Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <machine/options.h>		/* Pull in config options headers */
     31 
     32 #include <sys/param.h>
     33 #include <sys/kernel.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 
     37 #include <alpha/alpha/clockvar.h>
     38 #include <alpha/alpha/mcclockvar.h>
     39 #include <dev/ic/mc146818reg.h>
     40 
     41 struct cfdriver mcclock_cd = {
     42 	NULL, "mcclock", DV_DULL,
     43 };
     44 
     45 void	mcclock_init __P((struct device *));
     46 void	mcclock_get __P((struct device *, time_t, struct clocktime *));
     47 void	mcclock_set __P((struct device *, struct clocktime *));
     48 
     49 const struct clockfns mcclock_clockfns = {
     50 	mcclock_init, mcclock_get, mcclock_set,
     51 };
     52 
     53 #define	mc146818_write(dev, reg, datum)					\
     54 	    (*(dev)->sc_busfns->mc_bf_write)(dev, reg, datum)
     55 #define	mc146818_read(dev, reg)						\
     56 	    (*(dev)->sc_busfns->mc_bf_read)(dev, reg)
     57 
     58 void
     59 mcclock_attach(sc, busfns)
     60 	struct mcclock_softc *sc;
     61 	const struct mcclock_busfns *busfns;
     62 {
     63 
     64 	printf(": mc146818 or compatible");
     65 
     66 	sc->sc_busfns = busfns;
     67 
     68 	/* Turn interrupts off, just in case. */
     69 	mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
     70 
     71 	clockattach(&sc->sc_dev, &mcclock_clockfns);
     72 }
     73 
     74 void
     75 mcclock_init(dev)
     76 	struct device *dev;
     77 {
     78 	struct mcclock_softc *sc = (struct mcclock_softc *)dev;
     79 
     80 	mc146818_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_1024_Hz);
     81 	mc146818_write(sc, MC_REGB,
     82 	    MC_REGB_PIE | MC_REGB_SQWE | MC_REGB_BINARY | MC_REGB_24HR);
     83 }
     84 
     85 /*
     86  * Get the time of day, based on the clock's value and/or the base value.
     87  */
     88 void
     89 mcclock_get(dev, base, ct)
     90 	struct device *dev;
     91 	time_t base;
     92 	struct clocktime *ct;
     93 {
     94 	struct mcclock_softc *sc = (struct mcclock_softc *)dev;
     95 	mc_todregs regs;
     96 	int s;
     97 
     98 	s = splclock();
     99 	MC146818_GETTOD(sc, &regs)
    100 	splx(s);
    101 
    102 	ct->sec = regs[MC_SEC];
    103 	ct->min = regs[MC_MIN];
    104 	ct->hour = regs[MC_HOUR];
    105 	ct->dow = regs[MC_DOW];
    106 	ct->day = regs[MC_DOM];
    107 	ct->mon = regs[MC_MONTH];
    108 	ct->year = regs[MC_YEAR];
    109 }
    110 
    111 /*
    112  * Reset the TODR based on the time value.
    113  */
    114 void
    115 mcclock_set(dev, ct)
    116 	struct device *dev;
    117 	struct clocktime *ct;
    118 {
    119 	struct mcclock_softc *sc = (struct mcclock_softc *)dev;
    120 	mc_todregs regs;
    121 	int s;
    122 
    123 	s = splclock();
    124 	MC146818_GETTOD(sc, &regs);
    125 	splx(s);
    126 
    127 	regs[MC_SEC] = ct->sec;
    128 	regs[MC_MIN] = ct->min;
    129 	regs[MC_HOUR] = ct->hour;
    130 	regs[MC_DOW] = ct->dow;
    131 	regs[MC_DOM] = ct->day;
    132 	regs[MC_MONTH] = ct->mon;
    133 	regs[MC_YEAR] = ct->year;
    134 
    135 	s = splclock();
    136 	MC146818_PUTTOD(sc, &regs);
    137 	splx(s);
    138 }
    139