dpclock.c revision 1.8 1 /* $NetBSD: dpclock.c,v 1.8 2025/09/07 04:47:00 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001 Erik Reid
5 * Copyright (c) 2001 Rafal K. Boni
6 * Copyright (c) 2001 Christopher Sekiya
7 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * Portions of this code are derived from software contributed to The
11 * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
12 * Simulation Facility, NASA Ames Research Center.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41
42 #include <sys/bus.h>
43 #include <machine/autoconf.h>
44 #include <machine/sysconf.h>
45 #include <machine/machtype.h>
46
47 #include <dev/clock_subr.h>
48 #include <sgimips/dev/dp8573areg.h>
49
50 #include <sgimips/sgimips/clockvar.h>
51
52 struct dpclock_softc {
53 struct todr_chip_handle sc_todrch;
54
55 /* RTC registers */
56 bus_space_tag_t sc_rtct;
57 bus_space_handle_t sc_rtch;
58 int sc_offset;
59 };
60
61 static int dpclock_match(device_t, cfdata_t, void *);
62 static void dpclock_attach(device_t, device_t, void *);
63 static int dpclock_gettime_ymdhms(struct todr_chip_handle *,
64 struct clock_ymdhms *);
65 static int dpclock_settime_ymdhms(struct todr_chip_handle *,
66 struct clock_ymdhms *);
67
68 CFATTACH_DECL_NEW(dpclock, sizeof(struct dpclock_softc),
69 dpclock_match, dpclock_attach, NULL, NULL);
70
71 static int
72 dpclock_match(device_t parent, cfdata_t cf, void *aux)
73 {
74 struct mainbus_attach_args *ma = aux;
75
76 switch (mach_type) {
77 case MACH_SGI_IP6 | MACH_SGI_IP10:
78 if (ma->ma_addr == 0x1fbc0000)
79 return (1);
80 break;
81
82 case MACH_SGI_IP12:
83 case MACH_SGI_IP20:
84 if (ma->ma_addr == 0x1fb80e00)
85 return (1);
86 break;
87 }
88
89 return (0);
90 }
91
92 static void
93 writereg(struct dpclock_softc *sc, uint32_t reg, uint8_t val)
94 {
95 bus_space_write_1(sc->sc_rtct, sc->sc_rtch,
96 (reg << 2) + sc->sc_offset, val);
97 }
98
99 static uint8_t
100 readreg(struct dpclock_softc *sc, uint32_t reg)
101 {
102 return bus_space_read_1(sc->sc_rtct, sc->sc_rtch,
103 (reg << 2) + sc->sc_offset);
104 }
105
106 static void
107 dpclock_attach(device_t parent, device_t self, void *aux)
108 {
109 struct dpclock_softc *sc = device_private(self);
110 struct mainbus_attach_args *ma = aux;
111 int err;
112
113 printf("\n");
114
115 sc->sc_rtct = normal_memt;
116 /*
117 * All machines have one byte register per word. IP6/IP10 use
118 * the MSB, others the LSB.
119 */
120 if (mach_type == MACH_SGI_IP12 || mach_type == MACH_SGI_IP20)
121 sc->sc_offset = 3;
122 else
123 sc->sc_offset = 0;
124
125 if ((err = bus_space_map(sc->sc_rtct, ma->ma_addr, 0x1ffff,
126 BUS_SPACE_MAP_LINEAR, &sc->sc_rtch)) != 0) {
127 printf(": unable to map RTC registers, error = %d\n", err);
128 return;
129 }
130
131 sc->sc_todrch.cookie = sc;
132 sc->sc_todrch.todr_gettime_ymdhms = dpclock_gettime_ymdhms;
133 sc->sc_todrch.todr_settime_ymdhms = dpclock_settime_ymdhms;
134
135 todr_attach(&sc->sc_todrch);
136 }
137
138 /*
139 * Get the time of day, based on the clock's value and/or the base value.
140 */
141 static int
142 dpclock_gettime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
143 {
144 struct dpclock_softc *sc = (struct dpclock_softc *)todrch->cookie;
145 int s;
146 u_int8_t i, j;
147 u_int8_t regs[32];
148
149 s = splhigh();
150 i = readreg(sc, DP8573A_TIMESAVE_CTL);
151 j = i | DP8573A_TIMESAVE_CTL_EN;
152 writereg(sc, DP8573A_TIMESAVE_CTL, j);
153 writereg(sc, DP8573A_TIMESAVE_CTL, i);
154 splx(s);
155
156 for (i = 0; i < 32; i++)
157 regs[i] = readreg(sc, i);
158
159 dt->dt_sec = bcdtobin(regs[DP8573A_SAVE_SEC]);
160 dt->dt_min = bcdtobin(regs[DP8573A_SAVE_MIN]);
161
162 if (regs[DP8573A_RT_MODE] & DP8573A_RT_MODE_1224) {
163 dt->dt_hour = bcdtobin(regs[DP8573A_SAVE_HOUR] &
164 DP8573A_HOUR_12HR_MASK) +
165 ((regs[DP8573A_SAVE_HOUR] & DP8573A_RT_MODE_1224) ? 0 : 12);
166
167 /*
168 * In AM/PM mode, hour range is 01-12, so adding in 12 hours
169 * for PM gives us 01-24, whereas we want 00-23, so map hour
170 * 24 to hour 0.
171 */
172
173 if (dt->dt_hour == 24)
174 dt->dt_hour = 0;
175 } else {
176 dt->dt_hour = bcdtobin(regs[DP8573A_SAVE_HOUR] &
177 DP8573A_HOUR_24HR_MASK);
178 }
179
180 dt->dt_wday = bcdtobin(regs[DP8573A_DOW]); /* Not from time saved */
181 dt->dt_day = bcdtobin(regs[DP8573A_SAVE_DOM]);
182 dt->dt_mon = bcdtobin(regs[DP8573A_SAVE_MONTH]);
183 dt->dt_year = FROM_IRIX_YEAR(bcdtobin(regs[DP8573A_YEAR]));
184
185 return (0);
186 }
187
188 /*
189 * Reset the TODR based on the time value.
190 */
191 static int
192 dpclock_settime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
193 {
194 struct dpclock_softc *sc = (struct dpclock_softc *)todrch->cookie;
195 int s;
196 u_int8_t i, j;
197 u_int8_t regs[32];
198
199 s = splhigh();
200 i = readreg(sc, DP8573A_TIMESAVE_CTL);
201 j = i | DP8573A_TIMESAVE_CTL_EN;
202 writereg(sc, DP8573A_TIMESAVE_CTL, j);
203 writereg(sc, DP8573A_TIMESAVE_CTL, i);
204 splx(s);
205
206 for (i = 0; i < 32; i++)
207 regs[i] = readreg(sc, i);
208
209 regs[DP8573A_SUBSECOND] = 0;
210 regs[DP8573A_SECOND] = bintobcd(dt->dt_sec);
211 regs[DP8573A_MINUTE] = bintobcd(dt->dt_min);
212 regs[DP8573A_HOUR] = bintobcd(dt->dt_hour) & DP8573A_HOUR_24HR_MASK;
213 regs[DP8573A_DOW] = bintobcd(dt->dt_wday);
214 regs[DP8573A_DOM] = bintobcd(dt->dt_day);
215 regs[DP8573A_MONTH] = bintobcd(dt->dt_mon);
216 regs[DP8573A_YEAR] = bintobcd(TO_IRIX_YEAR(dt->dt_year));
217
218 s = splhigh();
219 i = readreg(sc, DP8573A_RT_MODE);
220 j = i & ~DP8573A_RT_MODE_CLKSS;
221 writereg(sc, DP8573A_RT_MODE, j);
222
223 for (i = 0; i < 10; i++)
224 writereg(sc, DP8573A_COUNTERS +i, regs[DP8573A_COUNTERS + i]);
225
226 writereg(sc, DP8573A_RT_MODE, i);
227 splx(s);
228
229 return (0);
230 }
231