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