aurtc.c revision 1.1 1 /* $NetBSD: aurtc.c,v 1.1 2002/07/29 15:39:14 simonb 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 struct cfattach aurtc_ca = {
56 sizeof (struct device), aurtc_match, aurtc_attach,
57 };
58
59 const struct clockfns aurtc_clockfns = {
60 aurtc_init, aurtc_get, aurtc_set,
61 };
62
63 int
64 aurtc_match(struct device *parent, struct cfdata *match, void *aux)
65 {
66 struct aubus_attach_args *aa = aux;
67
68 if (strcmp(aa->aa_name, match->cf_driver->cd_name) == 0)
69 return (1);
70
71 return (0);
72 }
73
74 void
75 aurtc_attach(struct device *parent, struct device *self, void *aux)
76 {
77
78 printf(": Au1X00 programmable clock"); /* \n in clockattach */
79 clockattach(self, &aurtc_clockfns);
80 }
81
82 void
83 aurtc_init(struct device *dev)
84 {
85
86 /* We don't use the aurtc for the hardclock interrupt. */
87 }
88
89 /*
90 * Note the fake "aurtc" on the Alchemy pb1000 uses a different year base.
91 */
92 #define PB1000_YEAR_OFFSET 100
93
94 /*
95 * Get the time of day, based on the clock's value and/or the base value.
96 */
97 void
98 aurtc_get(struct device *dev, time_t base, struct clocktime *ct)
99 {
100 struct clock_ymdhms ymdhms;
101 time_t secs;
102
103 secs = *(u_int32_t *)MIPS_PHYS_TO_KSEG1(PC_BASE + PC_COUNTER_READ_0);
104
105 clock_secs_to_ymdhms(secs, &ymdhms);
106
107 ct->sec = ymdhms.dt_sec;
108 ct->min = ymdhms.dt_min;
109 ct->hour = ymdhms.dt_hour;
110 ct->dow = ymdhms.dt_wday;
111 ct->day = ymdhms.dt_day;
112 ct->mon = ymdhms.dt_mon;
113 ct->year = ymdhms.dt_year - PB1000_YEAR_OFFSET;
114 }
115
116 /*
117 * Reset the TODR based on the time value.
118 */
119 void
120 aurtc_set(struct device *dev, struct clocktime *ct)
121 {
122 struct clock_ymdhms ymdhms;
123 time_t secs;
124
125 ymdhms.dt_sec = ct->sec;
126 ymdhms.dt_min = ct->min;
127 ymdhms.dt_hour = ct->hour;
128 ymdhms.dt_wday = ct->dow;
129 ymdhms.dt_day = ct->day;
130 ymdhms.dt_mon = ct->mon;
131 ymdhms.dt_year = ct->year + PB1000_YEAR_OFFSET;
132
133 secs = clock_ymdhms_to_secs(&ymdhms);
134
135 *(u_int32_t *)MIPS_PHYS_TO_KSEG1(PC_BASE + PC_COUNTER_READ_0) = secs;
136 }
137