FreeBSD/Linux Kernel Cross Reference
sys/dev/ic/mk48txx.c
1 /* $NetBSD: mk48txx.c,v 1.14 2004/03/01 23:46:44 kleink Exp $ */
2 /*-
3 * Copyright (c) 2000 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Paul Kranenburg.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.14 2004/03/01 23:46:44 kleink Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/errno.h>
49
50 #include <machine/bus.h>
51 #include <dev/clock_subr.h>
52 #include <dev/ic/mk48txxreg.h>
53 #include <dev/ic/mk48txxvar.h>
54
55 int mk48txx_gettime(todr_chip_handle_t, struct timeval *);
56 int mk48txx_settime(todr_chip_handle_t, struct timeval *);
57 int mk48txx_getcal(todr_chip_handle_t, int *);
58 int mk48txx_setcal(todr_chip_handle_t, int);
59 u_int8_t mk48txx_def_nvrd(struct mk48txx_softc *, int);
60 void mk48txx_def_nvwr(struct mk48txx_softc *, int, u_int8_t);
61
62 struct {
63 const char *name;
64 bus_size_t nvramsz;
65 bus_size_t clkoff;
66 int flags;
67 #define MK48TXX_EXT_REGISTERS 1 /* Has extended register set */
68 } mk48txx_models[] = {
69 { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
70 { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
71 { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
72 { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
73 };
74
75 void
76 mk48txx_attach(sc)
77 struct mk48txx_softc *sc;
78 {
79 todr_chip_handle_t handle;
80 int i;
81
82 printf(": %s", sc->sc_model);
83
84 i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
85 while (--i >= 0) {
86 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0)
87 break;
88 }
89 if (i < 0)
90 panic("mk48txx_attach: unsupported model");
91
92 sc->sc_nvramsz = mk48txx_models[i].nvramsz;
93 sc->sc_clkoffset = mk48txx_models[i].clkoff;
94
95 handle = &sc->sc_handle;
96 handle->cookie = sc;
97 handle->todr_gettime = mk48txx_gettime;
98 handle->todr_settime = mk48txx_settime;
99 handle->todr_getcal = mk48txx_getcal;
100 handle->todr_setcal = mk48txx_setcal;
101 handle->todr_setwen = NULL;
102
103 if (sc->sc_nvrd == NULL)
104 sc->sc_nvrd = mk48txx_def_nvrd;
105 if (sc->sc_nvwr == NULL)
106 sc->sc_nvwr = mk48txx_def_nvwr;
107 }
108
109 /*
110 * Get time-of-day and convert to a `struct timeval'
111 * Return 0 on success; an error number otherwise.
112 */
113 int
114 mk48txx_gettime(handle, tv)
115 todr_chip_handle_t handle;
116 struct timeval *tv;
117 {
118 struct mk48txx_softc *sc;
119 bus_size_t clkoff;
120 struct clock_ymdhms dt;
121 int year;
122 u_int8_t csr;
123
124 sc = handle->cookie;
125 clkoff = sc->sc_clkoffset;
126
127 todr_wenable(handle, 1);
128
129 /* enable read (stop time) */
130 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
131 csr |= MK48TXX_CSR_READ;
132 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
133
134 dt.dt_sec = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_ISEC));
135 dt.dt_min = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMIN));
136 dt.dt_hour = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IHOUR));
137 dt.dt_day = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IDAY));
138 dt.dt_wday = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IWDAY));
139 dt.dt_mon = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMON));
140 year = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IYEAR));
141
142 year += sc->sc_year0;
143 if (year < POSIX_BASE_YEAR &&
144 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
145 year += 100;
146
147 dt.dt_year = year;
148
149 /* time wears on */
150 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
151 csr &= ~MK48TXX_CSR_READ;
152 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
153 todr_wenable(handle, 0);
154
155 /* simple sanity checks */
156 if (dt.dt_mon > 12 || dt.dt_day > 31 ||
157 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
158 return EIO;
159
160 tv->tv_sec = clock_ymdhms_to_secs(&dt);
161 tv->tv_usec = 0;
162 return 0;
163 }
164
165 /*
166 * Set the time-of-day clock based on the value of the `struct timeval' arg.
167 * Return 0 on success; an error number otherwise.
168 */
169 int
170 mk48txx_settime(handle, tv)
171 todr_chip_handle_t handle;
172 struct timeval *tv;
173 {
174 struct mk48txx_softc *sc;
175 bus_size_t clkoff;
176 struct clock_ymdhms dt;
177 u_int8_t csr;
178 int year;
179
180 sc = handle->cookie;
181 clkoff = sc->sc_clkoffset;
182
183 /* Note: we ignore `tv_usec' */
184 clock_secs_to_ymdhms(tv->tv_sec, &dt);
185
186 year = dt.dt_year - sc->sc_year0;
187 if (year > 99 &&
188 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
189 year -= 100;
190
191 todr_wenable(handle, 1);
192 /* enable write */
193 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
194 csr |= MK48TXX_CSR_WRITE;
195 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
196
197 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ISEC, TOBCD(dt.dt_sec));
198 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMIN, TOBCD(dt.dt_min));
199 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IHOUR, TOBCD(dt.dt_hour));
200 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IWDAY, TOBCD(dt.dt_wday));
201 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IDAY, TOBCD(dt.dt_day));
202 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMON, TOBCD(dt.dt_mon));
203 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IYEAR, TOBCD(year));
204
205 /* load them up */
206 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
207 csr &= ~MK48TXX_CSR_WRITE;
208 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
209 todr_wenable(handle, 0);
210 return 0;
211 }
212
213 int
214 mk48txx_getcal(handle, vp)
215 todr_chip_handle_t handle;
216 int *vp;
217 {
218
219 return EOPNOTSUPP;
220 }
221
222 int
223 mk48txx_setcal(handle, v)
224 todr_chip_handle_t handle;
225 int v;
226 {
227
228 return EOPNOTSUPP;
229 }
230
231 int
232 mk48txx_get_nvram_size(handle, vp)
233 todr_chip_handle_t handle;
234 bus_size_t *vp;
235 {
236 struct mk48txx_softc *sc;
237
238 sc = handle->cookie;
239 *vp = sc->sc_nvramsz;
240 return 0;
241 }
242
243 u_int8_t
244 mk48txx_def_nvrd(struct mk48txx_softc *sc, int off)
245 {
246
247 return bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
248 }
249
250 void
251 mk48txx_def_nvwr(struct mk48txx_softc *sc, int off, u_int8_t v)
252 {
253
254 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v);
255 }
Cache object: 38e502d26775707d1b74e250f4c6b523
|