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