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