Home | History | Annotate | Line # | Download | only in dec
mcclock.c revision 1.4
      1 /*	$NetBSD: mcclock.c,v 1.4 1996/10/13 02:59:41 christos 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 <sys/param.h>
     31 #include <sys/kernel.h>
     32 #include <sys/systm.h>
     33 #include <sys/device.h>
     34 
     35 #include <alpha/alpha/clockvar.h>
     36 #include <alpha/alpha/mcclockvar.h>
     37 #include <dev/ic/mc146818reg.h>
     38 
     39 struct cfdriver mcclock_cd = {
     40 	NULL, "mcclock", DV_DULL,
     41 };
     42 
     43 void	mcclock_init __P((struct device *));
     44 void	mcclock_get __P((struct device *, time_t, struct clocktime *));
     45 void	mcclock_set __P((struct device *, struct clocktime *));
     46 
     47 const struct clockfns mcclock_clockfns = {
     48 	mcclock_init, mcclock_get, mcclock_set,
     49 };
     50 
     51 #define	mc146818_write(dev, reg, datum)					\
     52 	    (*(dev)->sc_busfns->mc_bf_write)(dev, reg, datum)
     53 #define	mc146818_read(dev, reg)						\
     54 	    (*(dev)->sc_busfns->mc_bf_read)(dev, reg)
     55 
     56 void
     57 mcclock_attach(sc, busfns)
     58 	struct mcclock_softc *sc;
     59 	const struct mcclock_busfns *busfns;
     60 {
     61 
     62 	printf(": mc146818 or compatible");
     63 
     64 	sc->sc_busfns = busfns;
     65 
     66 	/* Turn interrupts off, just in case. */
     67 	mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
     68 
     69 	clockattach(&sc->sc_dev, &mcclock_clockfns);
     70 }
     71 
     72 void
     73 mcclock_init(dev)
     74 	struct device *dev;
     75 {
     76 	struct mcclock_softc *sc = (struct mcclock_softc *)dev;
     77 
     78 	mc146818_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_1024_Hz);
     79 	mc146818_write(sc, MC_REGB,
     80 	    MC_REGB_PIE | MC_REGB_BINARY | MC_REGB_24HR);
     81 }
     82 
     83 /*
     84  * Get the time of day, based on the clock's value and/or the base value.
     85  */
     86 void
     87 mcclock_get(dev, base, ct)
     88 	struct device *dev;
     89 	time_t base;
     90 	struct clocktime *ct;
     91 {
     92 	struct mcclock_softc *sc = (struct mcclock_softc *)dev;
     93 	mc_todregs regs;
     94 	int s;
     95 
     96 	s = splclock();
     97 	MC146818_GETTOD(sc, &regs)
     98 	splx(s);
     99 
    100 	ct->sec = regs[MC_SEC];
    101 	ct->min = regs[MC_MIN];
    102 	ct->hour = regs[MC_HOUR];
    103 	ct->dow = regs[MC_DOW];
    104 	ct->day = regs[MC_DOM];
    105 	ct->mon = regs[MC_MONTH];
    106 	ct->year = regs[MC_YEAR];
    107 }
    108 
    109 /*
    110  * Reset the TODR based on the time value.
    111  */
    112 void
    113 mcclock_set(dev, ct)
    114 	struct device *dev;
    115 	struct clocktime *ct;
    116 {
    117 	struct mcclock_softc *sc = (struct mcclock_softc *)dev;
    118 	mc_todregs regs;
    119 	int s;
    120 
    121 	s = splclock();
    122 	MC146818_GETTOD(sc, &regs);
    123 	splx(s);
    124 
    125 	regs[MC_SEC] = ct->sec;
    126 	regs[MC_MIN] = ct->min;
    127 	regs[MC_HOUR] = ct->hour;
    128 	regs[MC_DOW] = ct->dow;
    129 	regs[MC_DOM] = ct->day;
    130 	regs[MC_MONTH] = ct->mon;
    131 	regs[MC_YEAR] = ct->year;
    132 
    133 	s = splclock();
    134 	MC146818_PUTTOD(sc, &regs);
    135 	splx(s);
    136 }
    137