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