mcclock_mace.c revision 1.2 1 /* $NetBSD: mcclock_mace.c,v 1.2 2004/01/19 00:30:17 sekiya Exp $ */
2
3 /*
4 * Copyright (c) 2001 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the NetBSD
17 * Foundation, Inc. and its contributors.
18 * 4. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1998 Mark Brinicombe.
37 * Copyright (c) 1998 Causality Limited.
38 * All rights reserved.
39 *
40 * Written by Mark Brinicombe, Causality Limited
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by Mark Brinicombe
53 * for the NetBSD Project.
54 * 4. The name of the company nor the name of the author may be used to
55 * endorse or promote products derived from this software without specific
56 * prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
59 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61 * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
64 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: mcclock_mace.c,v 1.2 2004/01/19 00:30:17 sekiya Exp $");
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76
77 #include <machine/cpu.h>
78 #include <machine/autoconf.h>
79 #include <machine/bus.h>
80 #include <machine/machtype.h>
81
82 #include <dev/clock_subr.h>
83 #include <dev/ic/ds1687reg.h>
84
85 #include <sgimips/mace/macevar.h>
86
87 #include <sgimips/sgimips/clockvar.h>
88
89 struct mcclock_mace_softc {
90 struct device sc_dev;
91
92 bus_space_tag_t sc_st;
93 bus_space_handle_t sc_sh;
94 };
95
96 static int mcclock_mace_match(struct device *, struct cfdata *, void *);
97 static void mcclock_mace_attach(struct device*, struct device *, void *);
98
99 static void mcclock_mace_init(struct device *);
100 static void mcclock_mace_get(struct device *, struct clock_ymdhms *);
101 static void mcclock_mace_set(struct device *, struct clock_ymdhms *);
102
103 unsigned int ds1687_read(void *arg, unsigned int addr);
104 void ds1687_write(void *arg, unsigned int addr, unsigned int data);
105
106 const struct clockfns mcclock_mace_clockfns = {
107 mcclock_mace_init, mcclock_mace_get, mcclock_mace_set
108 };
109
110 CFATTACH_DECL(mcclock_mace, sizeof(struct mcclock_mace_softc),
111 mcclock_mace_match, mcclock_mace_attach, NULL, NULL);
112
113 static int
114 mcclock_mace_match(struct device *parent, struct cfdata *match, void *aux)
115 {
116 if (mach_type == MACH_SGI_IP32)
117 return 1;
118 return 0;
119 }
120
121 void
122 mcclock_mace_attach(struct device *parent, struct device *self, void *aux)
123 {
124 struct mcclock_mace_softc *sc = (void *)self;
125 struct mace_attach_args *maa = aux;
126
127 sc->sc_st = maa->maa_st;
128 /* XXX should be bus_space_map() */
129 if (bus_space_subregion(maa->maa_st, maa->maa_sh,
130 maa->maa_offset, 0, &sc->sc_sh))
131 panic("mcclock_mace_attach: couldn't map");
132
133 /*
134 * We want a fixed format: 24-hour, BCD data, so just force the
135 * RTC to this mode; if there was junk there we'll be able to
136 * fix things up later when we discover the time read back is
137 * no good.
138 */
139 ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1);
140 ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS);
141
142 /* XXXrkb: init kickstart/wakeup stuff */
143
144 if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT))
145 printf(": lithium cell is dead, RTC unreliable");
146
147 printf("\n");
148 clockattach(&sc->sc_dev, &mcclock_mace_clockfns);
149 }
150
151 /*
152 * We need to use the following two functions from
153 * DS1687_PUT/GETTOD(). Hence the names.
154 */
155 unsigned int
156 ds1687_read(void *arg, unsigned int addr)
157 {
158 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
159
160 return (bus_space_read_1(sc->sc_st, sc->sc_sh, addr));
161 }
162
163 void
164 ds1687_write(void *arg, unsigned int addr, unsigned int data)
165 {
166 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
167
168 bus_space_write_1(sc->sc_st, sc->sc_sh, addr, data);
169 }
170
171 static void
172 mcclock_mace_init(struct device *dev)
173 {
174
175 return;
176 }
177
178 static void
179 mcclock_mace_get(struct device *dev, struct clock_ymdhms *dt)
180 {
181 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
182 ds1687_todregs regs;
183 int s;
184
185 s = splhigh();
186 DS1687_GETTOD(sc, ®s);
187 splx(s);
188
189 dt->dt_sec = FROMBCD(regs[DS1687_SOFT_SEC]);
190 dt->dt_min = FROMBCD(regs[DS1687_SOFT_MIN]);
191 dt->dt_hour = FROMBCD(regs[DS1687_SOFT_HOUR]);
192 dt->dt_wday = FROMBCD(regs[DS1687_SOFT_DOW]);
193 dt->dt_day = FROMBCD(regs[DS1687_SOFT_DOM]);
194 dt->dt_mon = FROMBCD(regs[DS1687_SOFT_MONTH]);
195 dt->dt_year = FROMBCD(regs[DS1687_SOFT_YEAR]) +
196 (100 * FROMBCD(regs[DS1687_SOFT_CENTURY]));
197 }
198
199 static void
200 mcclock_mace_set(struct device *dev, struct clock_ymdhms *dt)
201 {
202 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
203 ds1687_todregs regs;
204 int s;
205
206 memset(®s, 0, sizeof(regs));
207
208 regs[DS1687_SOFT_SEC] = TOBCD(dt->dt_sec);
209 regs[DS1687_SOFT_MIN] = TOBCD(dt->dt_min);
210 regs[DS1687_SOFT_HOUR] = TOBCD(dt->dt_hour);
211 regs[DS1687_SOFT_DOW] = TOBCD(dt->dt_wday);
212 regs[DS1687_SOFT_DOM] = TOBCD(dt->dt_day);
213 regs[DS1687_SOFT_MONTH] = TOBCD(dt->dt_mon);
214 regs[DS1687_SOFT_YEAR] = TOBCD(dt->dt_year % 100);
215 regs[DS1687_SOFT_CENTURY] = TOBCD(dt->dt_year / 100);
216 s = splhigh();
217 DS1687_PUTTOD(sc, ®s);
218 splx(s);
219 }
220