mc146818.c revision 1.13.8.1 1 /* $NetBSD: mc146818.c,v 1.13.8.1 2008/01/10 23:44:15 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 2003 Izumi Tsutsui. 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 author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * mc146818 and compatible time of day chip subroutines
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.13.8.1 2008/01/10 23:44:15 bouyer Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/errno.h>
40
41 #include <sys/bus.h>
42
43 #include <dev/clock_subr.h>
44
45 #include <dev/ic/mc146818reg.h>
46 #include <dev/ic/mc146818var.h>
47
48 int mc146818_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
49 int mc146818_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
50
51 void
52 mc146818_attach(struct mc146818_softc *sc)
53 {
54 todr_chip_handle_t handle;
55
56 #ifdef DIAGNOSTIC
57 if (sc->sc_mcread == NULL ||
58 sc->sc_mcwrite == NULL)
59 panic("%s: invalid read/write functions", __func__);
60 #endif
61
62 aprint_normal(": mc146818 compatible time-of-day clock");
63
64 handle = &sc->sc_handle;
65 handle->cookie = sc;
66 handle->todr_gettime = NULL;
67 handle->todr_settime = NULL;
68 handle->todr_gettime_ymdhms = mc146818_gettime_ymdhms;
69 handle->todr_settime_ymdhms = mc146818_settime_ymdhms;
70 handle->todr_setwen = NULL;
71
72 todr_attach(handle);
73 }
74
75 /*
76 * todr_gettime function:
77 * Get time of day and convert it to a struct timeval.
78 * Return 0 on success, an error number othersize.
79 */
80 int
81 mc146818_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
82 {
83 struct mc146818_softc *sc;
84 int s, timeout, cent, year;
85
86 sc = handle->cookie;
87
88 s = splclock(); /* XXX really needed? */
89
90 todr_wenable(handle, 1);
91
92 timeout = 1000000; /* XXX how long should we wait? */
93 for (;;) {
94 if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
95 break;
96 if (--timeout < 0) {
97 printf("%s: timeout\n", __func__);
98 return EBUSY;
99 }
100 }
101
102 #define FROMREG(x) ((sc->sc_flag & MC146818_BCD) ? FROMBCD(x) : (x))
103
104 dt->dt_sec = FROMREG((*sc->sc_mcread)(sc, MC_SEC));
105 dt->dt_min = FROMREG((*sc->sc_mcread)(sc, MC_MIN));
106 dt->dt_hour = FROMREG((*sc->sc_mcread)(sc, MC_HOUR));
107 dt->dt_wday = FROMREG((*sc->sc_mcread)(sc, MC_DOW));
108 dt->dt_day = FROMREG((*sc->sc_mcread)(sc, MC_DOM));
109 dt->dt_mon = FROMREG((*sc->sc_mcread)(sc, MC_MONTH));
110 year = FROMREG((*sc->sc_mcread)(sc, MC_YEAR));
111 if (sc->sc_getcent) {
112 cent = (*sc->sc_getcent)(sc);
113 year += cent * 100;
114 }
115
116 #undef FROMREG
117
118 year += sc->sc_year0;
119 if (year < POSIX_BASE_YEAR &&
120 (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
121 year += 100;
122 dt->dt_year = year;
123
124 todr_wenable(handle, 0);
125
126 splx(s);
127
128 return 0;
129 }
130
131 /*
132 * todr_settime function:
133 * Set the time of day clock based on the value of the struct timeval arg.
134 * Return 0 on success, an error number othersize.
135 */
136 int
137 mc146818_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
138 {
139 struct mc146818_softc *sc;
140 int s, timeout, cent, year;
141
142 sc = handle->cookie;
143
144 s = splclock(); /* XXX really needed? */
145
146 todr_wenable(handle, 1);
147
148 timeout = 1000000; /* XXX how long should we wait? */
149 for (;;) {
150 if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
151 break;
152 if (--timeout < 0) {
153 printf("%s: timeout\n", __func__);
154 return EBUSY;
155 }
156 }
157
158 #define TOREG(x) ((sc->sc_flag & MC146818_BCD) ? TOBCD(x) : (x))
159
160 (*sc->sc_mcwrite)(sc, MC_SEC, TOREG(dt->dt_sec));
161 (*sc->sc_mcwrite)(sc, MC_MIN, TOREG(dt->dt_min));
162 (*sc->sc_mcwrite)(sc, MC_HOUR, TOREG(dt->dt_hour));
163 (*sc->sc_mcwrite)(sc, MC_DOW, TOREG(dt->dt_wday));
164 (*sc->sc_mcwrite)(sc, MC_DOM, TOREG(dt->dt_day));
165 (*sc->sc_mcwrite)(sc, MC_MONTH, TOREG(dt->dt_mon));
166
167 year = dt->dt_year - sc->sc_year0;
168 if (sc->sc_setcent) {
169 cent = year / 100;
170 (*sc->sc_setcent)(sc, cent);
171 year -= cent * 100;
172 }
173 if (year > 99 &&
174 (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
175 year -= 100;
176 (*sc->sc_mcwrite)(sc, MC_YEAR, TOREG(year));
177
178 #undef TOREG
179
180 todr_wenable(handle, 0);
181
182 splx(s);
183
184 return 0;
185 }
186