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