Home | History | Annotate | Line # | Download | only in dev
aurtc.c revision 1.4
      1 /* $NetBSD: aurtc.c,v 1.4 2002/10/01 02:56:45 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 
     42 #include <dev/clock_subr.h>
     43 
     44 #include <evbmips/evbmips/clockvar.h>
     45 #include <mips/alchemy/include/aureg.h>
     46 #include <mips/alchemy/include/aubusvar.h>
     47 
     48 static int	aurtc_match(struct device *, struct cfdata *, void *);
     49 static void	aurtc_attach(struct device *, struct device *, void *);
     50 
     51 static void	aurtc_init(struct device *);
     52 static void	aurtc_get(struct device *, time_t, struct clocktime *);
     53 static void	aurtc_set(struct device *, struct clocktime *);
     54 
     55 CFATTACH_DECL(aurtc, sizeof (struct device),
     56     aurtc_match, aurtc_attach, NULL, NULL)
     57 
     58 const struct clockfns aurtc_clockfns = {
     59 	aurtc_init, aurtc_get, aurtc_set,
     60 };
     61 
     62 int
     63 aurtc_match(struct device *parent, struct cfdata *match, void *aux)
     64 {
     65 	struct aubus_attach_args *aa = aux;
     66 
     67 	if (strcmp(aa->aa_name, match->cf_name) == 0)
     68 		return (1);
     69 
     70 	return (0);
     71 }
     72 
     73 void
     74 aurtc_attach(struct device *parent, struct device *self, void *aux)
     75 {
     76 
     77 	printf(": Au1X00 programmable clock");	/* \n in clockattach */
     78 	clockattach(self, &aurtc_clockfns);
     79 }
     80 
     81 void
     82 aurtc_init(struct device *dev)
     83 {
     84 
     85 	/* We don't use the aurtc for the hardclock interrupt. */
     86 }
     87 
     88 /*
     89  * Note the fake "aurtc" on the Alchemy pb1000 uses a different year base.
     90  */
     91 #define	PB1000_YEAR_OFFSET	100
     92 
     93 /*
     94  * Get the time of day, based on the clock's value and/or the base value.
     95  */
     96 void
     97 aurtc_get(struct device *dev, time_t base, struct clocktime *ct)
     98 {
     99 	struct clock_ymdhms ymdhms;
    100 	time_t secs;
    101 
    102 	secs = *(u_int32_t *)MIPS_PHYS_TO_KSEG1(PC_BASE + PC_COUNTER_READ_0);
    103 
    104 	clock_secs_to_ymdhms(secs, &ymdhms);
    105 
    106 	ct->sec = ymdhms.dt_sec;
    107 	ct->min = ymdhms.dt_min;
    108 	ct->hour = ymdhms.dt_hour;
    109 	ct->dow = ymdhms.dt_wday;
    110 	ct->day = ymdhms.dt_day;
    111 	ct->mon = ymdhms.dt_mon;
    112 	ct->year = ymdhms.dt_year - PB1000_YEAR_OFFSET;
    113 }
    114 
    115 /*
    116  * Reset the TODR based on the time value.
    117  */
    118 void
    119 aurtc_set(struct device *dev, struct clocktime *ct)
    120 {
    121 	struct clock_ymdhms ymdhms;
    122 	time_t secs;
    123 
    124 	ymdhms.dt_sec = ct->sec;
    125 	ymdhms.dt_min = ct->min;
    126 	ymdhms.dt_hour = ct->hour;
    127 	ymdhms.dt_wday = ct->dow;
    128 	ymdhms.dt_day = ct->day;
    129 	ymdhms.dt_mon = ct->mon;
    130 	ymdhms.dt_year = ct->year + PB1000_YEAR_OFFSET;
    131 
    132 	secs = clock_ymdhms_to_secs(&ymdhms);
    133 
    134 	*(u_int32_t *)MIPS_PHYS_TO_KSEG1(PC_BASE + PC_COUNTER_READ_0) = secs;
    135 }
    136