mc146818.c revision 1.21 1 1.21 thorpej /* $NetBSD: mc146818.c,v 1.21 2024/03/06 02:31:44 thorpej Exp $ */
2 1.1 tsutsui
3 1.16 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 2003 Izumi Tsutsui. All rights reserved.
5 1.1 tsutsui *
6 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
7 1.1 tsutsui * modification, are permitted provided that the following conditions
8 1.1 tsutsui * are met:
9 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
10 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
11 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
13 1.1 tsutsui * documentation and/or other materials provided with the distribution.
14 1.1 tsutsui *
15 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 tsutsui */
26 1.1 tsutsui
27 1.1 tsutsui /*
28 1.1 tsutsui * mc146818 and compatible time of day chip subroutines
29 1.1 tsutsui */
30 1.1 tsutsui
31 1.1 tsutsui #include <sys/cdefs.h>
32 1.21 thorpej __KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.21 2024/03/06 02:31:44 thorpej Exp $");
33 1.1 tsutsui
34 1.1 tsutsui #include <sys/param.h>
35 1.1 tsutsui #include <sys/systm.h>
36 1.1 tsutsui #include <sys/device.h>
37 1.1 tsutsui #include <sys/errno.h>
38 1.1 tsutsui
39 1.13 ad #include <sys/bus.h>
40 1.1 tsutsui
41 1.1 tsutsui #include <dev/clock_subr.h>
42 1.1 tsutsui
43 1.1 tsutsui #include <dev/ic/mc146818reg.h>
44 1.1 tsutsui #include <dev/ic/mc146818var.h>
45 1.1 tsutsui
46 1.1 tsutsui void
47 1.5 tsutsui mc146818_attach(struct mc146818_softc *sc)
48 1.1 tsutsui {
49 1.1 tsutsui todr_chip_handle_t handle;
50 1.1 tsutsui
51 1.1 tsutsui #ifdef DIAGNOSTIC
52 1.1 tsutsui if (sc->sc_mcread == NULL ||
53 1.1 tsutsui sc->sc_mcwrite == NULL)
54 1.14 tsutsui panic("%s: invalid read/write functions", __func__);
55 1.1 tsutsui #endif
56 1.1 tsutsui
57 1.14 tsutsui aprint_normal(": mc146818 compatible time-of-day clock");
58 1.1 tsutsui
59 1.1 tsutsui handle = &sc->sc_handle;
60 1.1 tsutsui handle->cookie = sc;
61 1.21 thorpej KASSERT(handle->todr_gettime == NULL);
62 1.21 thorpej KASSERT(handle->todr_settime == NULL);
63 1.21 thorpej if (handle->todr_gettime_ymdhms == NULL) {
64 1.21 thorpej handle->todr_gettime_ymdhms = mc146818_gettime_ymdhms;
65 1.21 thorpej }
66 1.21 thorpej if (handle->todr_settime_ymdhms == NULL) {
67 1.21 thorpej handle->todr_settime_ymdhms = mc146818_settime_ymdhms;
68 1.21 thorpej }
69 1.15 tsutsui
70 1.15 tsutsui todr_attach(handle);
71 1.1 tsutsui }
72 1.1 tsutsui
73 1.1 tsutsui /*
74 1.1 tsutsui * todr_gettime function:
75 1.1 tsutsui * Get time of day and convert it to a struct timeval.
76 1.1 tsutsui * Return 0 on success, an error number othersize.
77 1.1 tsutsui */
78 1.1 tsutsui int
79 1.10 gdamore mc146818_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
80 1.1 tsutsui {
81 1.1 tsutsui struct mc146818_softc *sc;
82 1.1 tsutsui int s, timeout, cent, year;
83 1.1 tsutsui
84 1.1 tsutsui sc = handle->cookie;
85 1.1 tsutsui
86 1.1 tsutsui s = splclock(); /* XXX really needed? */
87 1.1 tsutsui
88 1.1 tsutsui timeout = 1000000; /* XXX how long should we wait? */
89 1.1 tsutsui for (;;) {
90 1.17 kenh if (((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP) == 0)
91 1.1 tsutsui break;
92 1.1 tsutsui if (--timeout < 0) {
93 1.18 tsutsui printf("%s: timeout\n", __func__);
94 1.1 tsutsui return EBUSY;
95 1.1 tsutsui }
96 1.1 tsutsui }
97 1.1 tsutsui
98 1.19 christos #define FROMREG(x) ((sc->sc_flag & MC146818_BCD) ? bcdtobin(x) : (x))
99 1.4 tsutsui
100 1.10 gdamore dt->dt_sec = FROMREG((*sc->sc_mcread)(sc, MC_SEC));
101 1.10 gdamore dt->dt_min = FROMREG((*sc->sc_mcread)(sc, MC_MIN));
102 1.10 gdamore dt->dt_hour = FROMREG((*sc->sc_mcread)(sc, MC_HOUR));
103 1.10 gdamore dt->dt_wday = FROMREG((*sc->sc_mcread)(sc, MC_DOW));
104 1.10 gdamore dt->dt_day = FROMREG((*sc->sc_mcread)(sc, MC_DOM));
105 1.10 gdamore dt->dt_mon = FROMREG((*sc->sc_mcread)(sc, MC_MONTH));
106 1.21 thorpej year = FROMREG((*sc->sc_mcread)(sc, MC_YEAR));
107 1.1 tsutsui if (sc->sc_getcent) {
108 1.1 tsutsui cent = (*sc->sc_getcent)(sc);
109 1.1 tsutsui year += cent * 100;
110 1.1 tsutsui }
111 1.1 tsutsui
112 1.4 tsutsui #undef FROMREG
113 1.4 tsutsui
114 1.1 tsutsui year += sc->sc_year0;
115 1.1 tsutsui if (year < POSIX_BASE_YEAR &&
116 1.3 tsutsui (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
117 1.1 tsutsui year += 100;
118 1.10 gdamore dt->dt_year = year;
119 1.1 tsutsui
120 1.1 tsutsui splx(s);
121 1.1 tsutsui
122 1.1 tsutsui return 0;
123 1.1 tsutsui }
124 1.1 tsutsui
125 1.1 tsutsui /*
126 1.1 tsutsui * todr_settime function:
127 1.1 tsutsui * Set the time of day clock based on the value of the struct timeval arg.
128 1.1 tsutsui * Return 0 on success, an error number othersize.
129 1.1 tsutsui */
130 1.1 tsutsui int
131 1.10 gdamore mc146818_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
132 1.1 tsutsui {
133 1.1 tsutsui struct mc146818_softc *sc;
134 1.17 kenh int s, cent, year;
135 1.1 tsutsui
136 1.1 tsutsui sc = handle->cookie;
137 1.1 tsutsui
138 1.1 tsutsui s = splclock(); /* XXX really needed? */
139 1.1 tsutsui
140 1.17 kenh /*
141 1.17 kenh * Disable RTC updates during clock updates
142 1.17 kenh */
143 1.17 kenh
144 1.17 kenh (*sc->sc_mcwrite)(sc, MC_REGB,
145 1.17 kenh (*sc->sc_mcread)(sc, MC_REGB) | MC_REGB_SET);
146 1.1 tsutsui
147 1.19 christos #define TOREG(x) ((sc->sc_flag & MC146818_BCD) ? bintobcd(x) : (x))
148 1.4 tsutsui
149 1.10 gdamore (*sc->sc_mcwrite)(sc, MC_SEC, TOREG(dt->dt_sec));
150 1.10 gdamore (*sc->sc_mcwrite)(sc, MC_MIN, TOREG(dt->dt_min));
151 1.10 gdamore (*sc->sc_mcwrite)(sc, MC_HOUR, TOREG(dt->dt_hour));
152 1.10 gdamore (*sc->sc_mcwrite)(sc, MC_DOW, TOREG(dt->dt_wday));
153 1.10 gdamore (*sc->sc_mcwrite)(sc, MC_DOM, TOREG(dt->dt_day));
154 1.10 gdamore (*sc->sc_mcwrite)(sc, MC_MONTH, TOREG(dt->dt_mon));
155 1.1 tsutsui
156 1.10 gdamore year = dt->dt_year - sc->sc_year0;
157 1.1 tsutsui if (sc->sc_setcent) {
158 1.1 tsutsui cent = year / 100;
159 1.1 tsutsui (*sc->sc_setcent)(sc, cent);
160 1.1 tsutsui year -= cent * 100;
161 1.1 tsutsui }
162 1.3 tsutsui if (year > 99 &&
163 1.3 tsutsui (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
164 1.1 tsutsui year -= 100;
165 1.4 tsutsui (*sc->sc_mcwrite)(sc, MC_YEAR, TOREG(year));
166 1.4 tsutsui
167 1.4 tsutsui #undef TOREG
168 1.1 tsutsui
169 1.17 kenh /*
170 1.17 kenh * Re-enable RTC updates
171 1.17 kenh */
172 1.17 kenh
173 1.17 kenh (*sc->sc_mcwrite)(sc, MC_REGB,
174 1.17 kenh (*sc->sc_mcread)(sc, MC_REGB) & ~MC_REGB_SET);
175 1.17 kenh
176 1.1 tsutsui splx(s);
177 1.1 tsutsui
178 1.1 tsutsui return 0;
179 1.1 tsutsui }
180