]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/mk48txx/mk48txx.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / mk48txx / mk48txx.c
1 /*-
2  * Copyright (c) 2000 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Paul Kranenburg.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  *      $NetBSD: mk48txx.c,v 1.25 2008/04/28 20:23:50 martin Exp $
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*
36  * Mostek MK48T02, MK48T08, MK48T18, MK48T59 time-of-day chip subroutines
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/clock.h>
43 #include <sys/eventhandler.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/rman.h>
47 #include <sys/watchdog.h>
48
49 #include <machine/bus.h>
50
51 #include <dev/mk48txx/mk48txxreg.h>
52 #include <dev/mk48txx/mk48txxvar.h>
53
54 #include "clock_if.h"
55
56 static uint8_t  mk48txx_def_nvrd(device_t dev, int off);
57 static void     mk48txx_def_nvwr(device_t dev, int off, uint8_t v);
58 static void     mk48txx_watchdog(void *arg, u_int cmd, int *error);
59
60 static const struct {
61         const char *name;
62         bus_size_t nvramsz;
63         bus_size_t clkoff;
64         u_int flags;
65 #define MK48TXX_EXT_REGISTERS   1       /* Has extended register set. */
66 } const mk48txx_models[] = {
67         { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
68         { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
69         { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
70         { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
71 };
72
73 int
74 mk48txx_attach(device_t dev)
75 {
76         struct mk48txx_softc *sc;
77         int i;
78         uint8_t wday;
79
80         sc = device_get_softc(dev);
81
82         if (mtx_initialized(&sc->sc_mtx) == 0) {
83                 device_printf(dev, "%s: mutex not initialized\n", __func__);
84                 return (ENXIO);
85         }
86
87         device_printf(dev, "model %s", sc->sc_model);
88         i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
89         while (--i >= 0) {
90                 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0) {
91                         break;
92                 }
93         }
94         if (i < 0) {
95                 device_printf(dev, " (unsupported)\n");
96                 return (ENXIO);
97         }
98         printf("\n");
99         sc->sc_nvramsz = mk48txx_models[i].nvramsz;
100         sc->sc_clkoffset = mk48txx_models[i].clkoff;
101
102         if (sc->sc_nvrd == NULL)
103                 sc->sc_nvrd = mk48txx_def_nvrd;
104         if (sc->sc_nvwr == NULL)
105                 sc->sc_nvwr = mk48txx_def_nvwr;
106
107         if (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS) {
108                 mtx_lock(&sc->sc_mtx);
109                 if ((*sc->sc_nvrd)(dev, sc->sc_clkoffset + MK48TXX_FLAGS) &
110                     MK48TXX_FLAGS_BL) {
111                         mtx_unlock(&sc->sc_mtx);
112                         device_printf(dev, "%s: battery low\n", __func__);
113                         return (ENXIO);
114                 }
115                 mtx_unlock(&sc->sc_mtx);
116         }
117
118         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
119                 /*
120                  * Use MK48TXX_WDAY_CB instead of manually adjusting the
121                  * century.
122                  */
123                 if (!(mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
124                         device_printf(dev, "%s: no century bit\n", __func__);
125                         return (ENXIO);
126                 } else {
127                         mtx_lock(&sc->sc_mtx);
128                         wday = (*sc->sc_nvrd)
129                             (dev, sc->sc_clkoffset + MK48TXX_IWDAY);
130                         wday |= MK48TXX_WDAY_CEB;
131                         (*sc->sc_nvwr)
132                             (dev, sc->sc_clkoffset + MK48TXX_IWDAY, wday);
133                         mtx_unlock(&sc->sc_mtx);
134                 }
135         }
136
137         clock_register(dev, 1000000);   /* 1 second resolution */
138
139         if ((sc->sc_flag & MK48TXX_WDOG_REGISTER) &&
140             (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
141                 sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
142                     mk48txx_watchdog, dev, 0);
143                 device_printf(dev,
144                     "watchdog registered, timeout interval max. 128 sec\n");
145         }
146
147         return (0);
148 }
149
150 /*
151  * Get time-of-day and convert to a `struct timespec'
152  * Return 0 on success; an error number otherwise.
153  */
154 int
155 mk48txx_gettime(device_t dev, struct timespec *ts)
156 {
157         struct mk48txx_softc *sc;
158         bus_size_t clkoff;
159         struct clocktime ct;
160         int year;
161         uint8_t csr;
162
163         sc = device_get_softc(dev);
164         clkoff = sc->sc_clkoffset;
165
166         mtx_lock(&sc->sc_mtx);
167         /* enable read (stop time) */
168         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
169         csr |= MK48TXX_CSR_READ;
170         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
171
172 #define FROMREG(reg, mask)      ((*sc->sc_nvrd)(dev, clkoff + (reg)) & (mask))
173
174         ct.nsec = 0;
175         ct.sec = FROMBCD(FROMREG(MK48TXX_ISEC, MK48TXX_SEC_MASK));
176         ct.min = FROMBCD(FROMREG(MK48TXX_IMIN, MK48TXX_MIN_MASK));
177         ct.hour = FROMBCD(FROMREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK));
178         ct.day = FROMBCD(FROMREG(MK48TXX_IDAY, MK48TXX_DAY_MASK));
179 #if 0
180         /* Map dow from 1 - 7 to 0 - 6; FROMBCD() isn't necessary here. */
181         ct.dow = FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK) - 1;
182 #else
183         /*
184          * Set dow = -1 because some drivers (for example the NetBSD and
185          * OpenBSD mk48txx(4)) don't set it correctly.
186          */
187         ct.dow = -1;
188 #endif
189         ct.mon = FROMBCD(FROMREG(MK48TXX_IMON, MK48TXX_MON_MASK));
190         year = FROMBCD(FROMREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK));
191         year += sc->sc_year0;
192         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST)
193                 year += (FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB) >>
194                     MK48TXX_WDAY_CB_SHIFT) * 100;
195         else if (year < POSIX_BASE_YEAR)
196                 year += 100;
197
198 #undef FROMREG
199
200         ct.year = year;
201
202         /* time wears on */
203         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
204         csr &= ~MK48TXX_CSR_READ;
205         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
206         mtx_unlock(&sc->sc_mtx);
207
208         return (clock_ct_to_ts(&ct, ts));
209 }
210
211 /*
212  * Set the time-of-day clock based on the value of the `struct timespec' arg.
213  * Return 0 on success; an error number otherwise.
214  */
215 int
216 mk48txx_settime(device_t dev, struct timespec *ts)
217 {
218         struct mk48txx_softc *sc;
219         bus_size_t clkoff;
220         struct clocktime ct;
221         uint8_t csr;
222         int cent, year;
223
224         sc = device_get_softc(dev);
225         clkoff = sc->sc_clkoffset;
226
227         /* Accuracy is only one second. */
228         if (ts->tv_nsec >= 500000000)
229                 ts->tv_sec++;
230         ts->tv_nsec = 0;
231         clock_ts_to_ct(ts, &ct);
232
233         mtx_lock(&sc->sc_mtx);
234         /* enable write */
235         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
236         csr |= MK48TXX_CSR_WRITE;
237         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
238
239 #define TOREG(reg, mask, val)                                           \
240         ((*sc->sc_nvwr)(dev, clkoff + (reg),                            \
241         ((*sc->sc_nvrd)(dev, clkoff + (reg)) & ~(mask)) |               \
242         ((val) & (mask))))
243
244         TOREG(MK48TXX_ISEC, MK48TXX_SEC_MASK, TOBCD(ct.sec));
245         TOREG(MK48TXX_IMIN, MK48TXX_MIN_MASK, TOBCD(ct.min));
246         TOREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK, TOBCD(ct.hour));
247         /* Map dow from 0 - 6 to 1 - 7; TOBCD() isn't necessary here. */
248         TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK, ct.dow + 1);
249         TOREG(MK48TXX_IDAY, MK48TXX_DAY_MASK, TOBCD(ct.day));
250         TOREG(MK48TXX_IMON, MK48TXX_MON_MASK, TOBCD(ct.mon));
251
252         year = ct.year - sc->sc_year0;
253         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
254                 cent = year / 100;
255                 TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB,
256                     cent << MK48TXX_WDAY_CB_SHIFT);
257                 year -= cent * 100;
258         } else if (year > 99)
259                 year -= 100;
260         TOREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK, TOBCD(year));
261
262 #undef TOREG
263
264         /* load them up */
265         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
266         csr &= ~MK48TXX_CSR_WRITE;
267         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
268         mtx_unlock(&sc->sc_mtx);
269         return (0);
270 }
271
272 static uint8_t
273 mk48txx_def_nvrd(device_t dev, int off)
274 {
275         struct mk48txx_softc *sc;
276
277         sc = device_get_softc(dev);
278         return (bus_read_1(sc->sc_res, off));
279 }
280
281 static void
282 mk48txx_def_nvwr(device_t dev, int off, uint8_t v)
283 {
284         struct mk48txx_softc *sc;
285
286         sc = device_get_softc(dev);
287         bus_write_1(sc->sc_res, off, v);
288 }
289
290 static void
291 mk48txx_watchdog(void *arg, u_int cmd, int *error)
292 {
293         device_t dev;
294         struct mk48txx_softc *sc;
295         uint8_t t, wdog;
296
297         dev = arg;
298         sc = device_get_softc(dev);
299
300         t = cmd & WD_INTERVAL;
301         if (t >= 26 && t <= 37) {
302                 wdog = 0;
303                 if (t <= WD_TO_2SEC) {
304                         wdog |= MK48TXX_WDOG_RB_1_16;
305                         t -= 26;
306                 } else if (t <= WD_TO_8SEC) {
307                         wdog |= MK48TXX_WDOG_RB_1_4;
308                         t -= WD_TO_250MS;
309                 } else if (t <= WD_TO_32SEC) {
310                         wdog |= MK48TXX_WDOG_RB_1;
311                         t -= WD_TO_1SEC;
312                 } else {
313                         wdog |= MK48TXX_WDOG_RB_4;
314                         t -= WD_TO_4SEC;
315                 }
316                 wdog |= (min(1 << t,
317                     MK48TXX_WDOG_BMB_MASK >> MK48TXX_WDOG_BMB_SHIFT)) <<
318                     MK48TXX_WDOG_BMB_SHIFT;
319                 if (sc->sc_flag & MK48TXX_WDOG_ENABLE_WDS)
320                         wdog |= MK48TXX_WDOG_WDS;
321                 *error = 0;
322         } else {
323                 wdog = 0;
324         }
325         mtx_lock(&sc->sc_mtx);
326         (*sc->sc_nvwr)(dev, sc->sc_clkoffset + MK48TXX_WDOG, wdog);
327         mtx_unlock(&sc->sc_mtx);
328 }