Home | History | Annotate | Line # | Download | only in dev
aurtc.c revision 1.6
      1 /* $NetBSD: aurtc.c,v 1.6 2003/07/15 02:43:35 lukem 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/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: aurtc.c,v 1.6 2003/07/15 02:43:35 lukem Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 
     45 #include <dev/clock_subr.h>
     46 
     47 #include <evbmips/evbmips/clockvar.h>
     48 #include <mips/alchemy/include/aureg.h>
     49 #include <mips/alchemy/include/aubusvar.h>
     50 
     51 static int	aurtc_match(struct device *, struct cfdata *, void *);
     52 static void	aurtc_attach(struct device *, struct device *, void *);
     53 
     54 static void	aurtc_init(struct device *);
     55 static void	aurtc_get(struct device *, time_t, struct clocktime *);
     56 static void	aurtc_set(struct device *, struct clocktime *);
     57 
     58 CFATTACH_DECL(aurtc, sizeof (struct device),
     59     aurtc_match, aurtc_attach, NULL, NULL);
     60 
     61 const struct clockfns aurtc_clockfns = {
     62 	aurtc_init, aurtc_get, aurtc_set,
     63 };
     64 
     65 int
     66 aurtc_match(struct device *parent, struct cfdata *match, void *aux)
     67 {
     68 	struct aubus_attach_args *aa = aux;
     69 
     70 	if (strcmp(aa->aa_name, match->cf_name) == 0)
     71 		return (1);
     72 
     73 	return (0);
     74 }
     75 
     76 void
     77 aurtc_attach(struct device *parent, struct device *self, void *aux)
     78 {
     79 
     80 	printf(": Au1X00 programmable clock");	/* \n in clockattach */
     81 	clockattach(self, &aurtc_clockfns);
     82 }
     83 
     84 void
     85 aurtc_init(struct device *dev)
     86 {
     87 
     88 	/* We don't use the aurtc for the hardclock interrupt. */
     89 }
     90 
     91 /*
     92  * Note the fake "aurtc" on the Alchemy pb1000 uses a different year base.
     93  */
     94 #define	PB1000_YEAR_OFFSET	100
     95 
     96 /*
     97  * Get the time of day, based on the clock's value and/or the base value.
     98  */
     99 void
    100 aurtc_get(struct device *dev, time_t base, struct clocktime *ct)
    101 {
    102 	struct clock_ymdhms ymdhms;
    103 	time_t secs;
    104 
    105 	secs = *(u_int32_t *)MIPS_PHYS_TO_KSEG1(PC_BASE + PC_COUNTER_READ_0);
    106 
    107 	clock_secs_to_ymdhms(secs, &ymdhms);
    108 
    109 	ct->sec = ymdhms.dt_sec;
    110 	ct->min = ymdhms.dt_min;
    111 	ct->hour = ymdhms.dt_hour;
    112 	ct->dow = ymdhms.dt_wday;
    113 	ct->day = ymdhms.dt_day;
    114 	ct->mon = ymdhms.dt_mon;
    115 	ct->year = ymdhms.dt_year - PB1000_YEAR_OFFSET;
    116 }
    117 
    118 /*
    119  * Reset the TODR based on the time value.
    120  */
    121 void
    122 aurtc_set(struct device *dev, struct clocktime *ct)
    123 {
    124 	struct clock_ymdhms ymdhms;
    125 	time_t secs;
    126 
    127 	ymdhms.dt_sec = ct->sec;
    128 	ymdhms.dt_min = ct->min;
    129 	ymdhms.dt_hour = ct->hour;
    130 	ymdhms.dt_wday = ct->dow;
    131 	ymdhms.dt_day = ct->day;
    132 	ymdhms.dt_mon = ct->mon;
    133 	ymdhms.dt_year = ct->year + PB1000_YEAR_OFFSET;
    134 
    135 	secs = clock_ymdhms_to_secs(&ymdhms);
    136 
    137 	*(u_int32_t *)MIPS_PHYS_TO_KSEG1(PC_BASE + PC_COUNTER_READ_0) = secs;
    138 }
    139