mk48txx.c revision 1.11 1 /* $NetBSD: mk48txx.c,v 1.11 2003/10/30 21:19:02 matt 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.11 2003/10/30 21:19:02 matt Exp $");
44
45 #include <sys/param.h>
46 #include <sys/malloc.h>
47 #include <sys/systm.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
54
55 struct mk48txx {
56 bus_space_tag_t mk_bt; /* bus tag & handle */
57 bus_space_handle_t mk_bh; /* */
58 bus_size_t mk_nvramsz; /* Size of NVRAM on the chip */
59 bus_size_t mk_clkoffset; /* Offset in NVRAM to clock bits */
60 u_int mk_year0; /* What year is represented on the system
61 by the chip's year counter at 0 */
62 mk48txx_nvrd_t mk_nvrd; /* NVRAM/RTC read function */
63 mk48txx_nvwr_t mk_nvwr; /* NVRAM/RTC write function */
64 };
65
66 int mk48txx_gettime(todr_chip_handle_t, struct timeval *);
67 int mk48txx_settime(todr_chip_handle_t, struct timeval *);
68 int mk48txx_getcal(todr_chip_handle_t, int *);
69 int mk48txx_setcal(todr_chip_handle_t, int);
70 u_int8_t mk48txx_def_nvrd(bus_space_tag_t, bus_space_handle_t, int);
71 void mk48txx_def_nvwr(bus_space_tag_t, bus_space_handle_t, int, u_int8_t);
72
73 int mk48txx_auto_century_adjust = 1;
74
75 struct {
76 const char *name;
77 bus_size_t nvramsz;
78 bus_size_t clkoff;
79 int flags;
80 #define MK48TXX_EXT_REGISTERS 1 /* Has extended register set */
81 } mk48txx_models[] = {
82 { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
83 { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
84 { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
85 { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
86 };
87
88 todr_chip_handle_t
89 mk48txx_attach(bt, bh, model, year0, nvread, nvwrite)
90 bus_space_tag_t bt;
91 bus_space_handle_t bh;
92 const char *model;
93 int year0;
94 mk48txx_nvrd_t nvread;
95 mk48txx_nvwr_t nvwrite;
96 {
97 todr_chip_handle_t handle;
98 struct mk48txx *mk;
99 bus_size_t nvramsz, clkoff;
100 int sz;
101 int i;
102
103 printf(": %s", model);
104
105 i = sizeof(mk48txx_models)/sizeof(mk48txx_models[0]);
106 while (--i >= 0) {
107 if (strcmp(model, mk48txx_models[i].name) == 0)
108 break;
109 }
110 if (i < 0) {
111 printf(": unsupported model");
112 return (NULL);
113 }
114 nvramsz = mk48txx_models[i].nvramsz;
115 clkoff = mk48txx_models[i].clkoff;
116
117 sz = ALIGN(sizeof(struct todr_chip_handle)) + sizeof(struct mk48txx);
118 handle = malloc(sz, M_DEVBUF, M_NOWAIT);
119 mk = (struct mk48txx *)((u_long)handle +
120 ALIGN(sizeof(struct todr_chip_handle)));
121 handle->cookie = mk;
122 handle->todr_gettime = mk48txx_gettime;
123 handle->todr_settime = mk48txx_settime;
124 handle->todr_getcal = mk48txx_getcal;
125 handle->todr_setcal = mk48txx_setcal;
126 handle->todr_setwen = NULL;
127 mk->mk_bt = bt;
128 mk->mk_bh = bh;
129 mk->mk_nvramsz = nvramsz;
130 mk->mk_clkoffset = clkoff;
131 mk->mk_year0 = year0;
132
133 if (nvread == NULL)
134 nvread = mk48txx_def_nvrd;
135 if (nvwrite == NULL)
136 nvwrite = mk48txx_def_nvwr;
137
138 mk->mk_nvrd = nvread;
139 mk->mk_nvwr = nvwrite;
140
141 return (handle);
142 }
143
144 /*
145 * Get time-of-day and convert to a `struct timeval'
146 * Return 0 on success; an error number otherwise.
147 */
148 int
149 mk48txx_gettime(handle, tv)
150 todr_chip_handle_t handle;
151 struct timeval *tv;
152 {
153 struct mk48txx *mk = handle->cookie;
154 bus_space_tag_t bt = mk->mk_bt;
155 bus_space_handle_t bh = mk->mk_bh;
156 bus_size_t clkoff = mk->mk_clkoffset;
157 struct clock_ymdhms dt;
158 int year;
159 u_int8_t csr;
160
161 todr_wenable(handle, 1);
162
163 /* enable read (stop time) */
164 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR);
165 csr |= MK48TXX_CSR_READ;
166 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr);
167
168 dt.dt_sec = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ISEC));
169 dt.dt_min = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IMIN));
170 dt.dt_hour = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IHOUR));
171 dt.dt_day = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IDAY));
172 dt.dt_wday = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IWDAY));
173 dt.dt_mon = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IMON));
174 year = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IYEAR));
175
176 year += mk->mk_year0;
177 if (year < POSIX_BASE_YEAR && mk48txx_auto_century_adjust != 0)
178 year += 100;
179
180 dt.dt_year = year;
181
182 /* time wears on */
183 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR);
184 csr &= ~MK48TXX_CSR_READ;
185 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr);
186 todr_wenable(handle, 0);
187
188 /* simple sanity checks */
189 if (dt.dt_mon > 12 || dt.dt_day > 31 ||
190 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
191 return (1);
192
193 tv->tv_sec = clock_ymdhms_to_secs(&dt);
194 tv->tv_usec = 0;
195 return (0);
196 }
197
198 /*
199 * Set the time-of-day clock based on the value of the `struct timeval' arg.
200 * Return 0 on success; an error number otherwise.
201 */
202 int
203 mk48txx_settime(handle, tv)
204 todr_chip_handle_t handle;
205 struct timeval *tv;
206 {
207 struct mk48txx *mk = handle->cookie;
208 bus_space_tag_t bt = mk->mk_bt;
209 bus_space_handle_t bh = mk->mk_bh;
210 bus_size_t clkoff = mk->mk_clkoffset;
211 struct clock_ymdhms dt;
212 u_int8_t csr;
213 int year;
214
215 /* Note: we ignore `tv_usec' */
216 clock_secs_to_ymdhms(tv->tv_sec, &dt);
217
218 year = dt.dt_year - mk->mk_year0;
219 if (year > 99 && mk48txx_auto_century_adjust != 0)
220 year -= 100;
221
222 todr_wenable(handle, 1);
223 /* enable write */
224 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR);
225 csr |= MK48TXX_CSR_WRITE;
226 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr);
227
228 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ISEC, TOBCD(dt.dt_sec));
229 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IMIN, TOBCD(dt.dt_min));
230 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IHOUR, TOBCD(dt.dt_hour));
231 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IWDAY, TOBCD(dt.dt_wday));
232 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IDAY, TOBCD(dt.dt_day));
233 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IMON, TOBCD(dt.dt_mon));
234 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IYEAR, TOBCD(year));
235
236 /* load them up */
237 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR);
238 csr &= ~MK48TXX_CSR_WRITE;
239 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr);
240 todr_wenable(handle, 0);
241 return (0);
242 }
243
244 int
245 mk48txx_getcal(handle, vp)
246 todr_chip_handle_t handle;
247 int *vp;
248 {
249 return (EOPNOTSUPP);
250 }
251
252 int
253 mk48txx_setcal(handle, v)
254 todr_chip_handle_t handle;
255 int v;
256 {
257 return (EOPNOTSUPP);
258 }
259
260 int
261 mk48txx_get_nvram_size(handle, vp)
262 todr_chip_handle_t handle;
263 bus_size_t *vp;
264 {
265 struct mk48txx *mk = handle->cookie;
266 *vp = mk->mk_nvramsz;
267 return (0);
268 }
269
270 u_int8_t
271 mk48txx_def_nvrd(bus_space_tag_t bt, bus_space_handle_t bh, int off)
272 {
273
274 return (bus_space_read_1(bt, bh, off));
275 }
276
277 void
278 mk48txx_def_nvwr(bus_space_tag_t bt, bus_space_handle_t bh, int off, u_int8_t v)
279 {
280
281 bus_space_write_1(bt, bh, off, v);
282 }
283