mc146818.c revision 1.9 1 /* $NetBSD: mc146818.c,v 1.9 2006/09/06 06:26:54 simonb 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.9 2006/09/06 06:26:54 simonb 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 <machine/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(todr_chip_handle_t, volatile struct timeval *);
49 int mc146818_settime(todr_chip_handle_t, volatile struct timeval *);
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("mc146818_attach: invalid read/write functions");
60 #endif
61
62 printf(": mc146818 compatible time-of-day clock\n");
63
64 handle = &sc->sc_handle;
65 handle->cookie = sc;
66 handle->todr_gettime = mc146818_gettime;
67 handle->todr_settime = mc146818_settime;
68 handle->todr_setwen = NULL;
69 }
70
71 /*
72 * todr_gettime function:
73 * Get time of day and convert it to a struct timeval.
74 * Return 0 on success, an error number othersize.
75 */
76 int
77 mc146818_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
78 {
79 struct mc146818_softc *sc;
80 struct clock_ymdhms dt;
81 int s, timeout, cent, year;
82
83 sc = handle->cookie;
84
85 s = splclock(); /* XXX really needed? */
86
87 todr_wenable(handle, 1);
88
89 timeout = 1000000; /* XXX how long should we wait? */
90 for (;;) {
91 if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
92 break;
93 if (--timeout < 0) {
94 printf("mc146818_gettime: timeout\n");
95 return EBUSY;
96 }
97 }
98
99 #define FROMREG(x) ((sc->sc_flag & MC146818_BCD) ? FROMBCD(x) : (x))
100
101 dt.dt_sec = FROMREG((*sc->sc_mcread)(sc, MC_SEC));
102 dt.dt_min = FROMREG((*sc->sc_mcread)(sc, MC_MIN));
103 dt.dt_hour = FROMREG((*sc->sc_mcread)(sc, MC_HOUR));
104 dt.dt_wday = FROMREG((*sc->sc_mcread)(sc, MC_DOW));
105 dt.dt_day = FROMREG((*sc->sc_mcread)(sc, MC_DOM));
106 dt.dt_mon = FROMREG((*sc->sc_mcread)(sc, MC_MONTH));
107 year = FROMREG((*sc->sc_mcread)(sc, MC_YEAR));
108 if (sc->sc_getcent) {
109 cent = (*sc->sc_getcent)(sc);
110 year += cent * 100;
111 }
112
113 #undef FROMREG
114
115 year += sc->sc_year0;
116 if (year < POSIX_BASE_YEAR &&
117 (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
118 year += 100;
119 dt.dt_year = year;
120
121 todr_wenable(handle, 0);
122
123 splx(s);
124
125 /* simple sanity checks */
126 if (dt.dt_mon > 12 || dt.dt_day > 31 ||
127 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
128 return EIO;
129
130 tv->tv_sec = clock_ymdhms_to_secs(&dt);
131 tv->tv_usec = 0;
132 return 0;
133 }
134
135 /*
136 * todr_settime function:
137 * Set the time of day clock based on the value of the struct timeval arg.
138 * Return 0 on success, an error number othersize.
139 */
140 int
141 mc146818_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
142 {
143 struct mc146818_softc *sc;
144 struct clock_ymdhms dt;
145 int s, timeout, cent, year;
146
147 sc = handle->cookie;
148
149 /* Note: we ignore `tv_usec' */
150 clock_secs_to_ymdhms(tv->tv_sec, &dt);
151
152 s = splclock(); /* XXX really needed? */
153
154 todr_wenable(handle, 1);
155
156 timeout = 1000000; /* XXX how long should we wait? */
157 for (;;) {
158 if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
159 break;
160 if (--timeout < 0) {
161 printf("mc146818_settime: timeout\n");
162 return EBUSY;
163 }
164 }
165
166 #define TOREG(x) ((sc->sc_flag & MC146818_BCD) ? TOBCD(x) : (x))
167
168 (*sc->sc_mcwrite)(sc, MC_SEC, TOREG(dt.dt_sec));
169 (*sc->sc_mcwrite)(sc, MC_MIN, TOREG(dt.dt_min));
170 (*sc->sc_mcwrite)(sc, MC_HOUR, TOREG(dt.dt_hour));
171 (*sc->sc_mcwrite)(sc, MC_DOW, TOREG(dt.dt_wday));
172 (*sc->sc_mcwrite)(sc, MC_DOM, TOREG(dt.dt_day));
173 (*sc->sc_mcwrite)(sc, MC_MONTH, TOREG(dt.dt_mon));
174
175 year = dt.dt_year - sc->sc_year0;
176 if (sc->sc_setcent) {
177 cent = year / 100;
178 (*sc->sc_setcent)(sc, cent);
179 year -= cent * 100;
180 }
181 if (year > 99 &&
182 (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
183 year -= 100;
184 (*sc->sc_mcwrite)(sc, MC_YEAR, TOREG(year));
185
186 #undef TOREG
187
188 todr_wenable(handle, 0);
189
190 splx(s);
191
192 return 0;
193 }
194