]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/mk48txx/mk48txx.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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, MK48T37 and MK48T59 time-of-day chip
37  * subroutines
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/clock.h>
44 #include <sys/eventhandler.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/rman.h>
48 #include <sys/watchdog.h>
49
50 #include <machine/bus.h>
51
52 #include <dev/mk48txx/mk48txxreg.h>
53 #include <dev/mk48txx/mk48txxvar.h>
54
55 #include "clock_if.h"
56
57 static uint8_t  mk48txx_def_nvrd(device_t dev, int off);
58 static void     mk48txx_def_nvwr(device_t dev, int off, uint8_t v);
59 static void     mk48txx_watchdog(void *arg, u_int cmd, int *error);
60
61 static const struct {
62         const char *name;
63         bus_size_t nvramsz;
64         bus_size_t clkoff;
65         u_int flags;
66 #define MK48TXX_EXT_REGISTERS   1       /* Has extended register set. */
67 } const mk48txx_models[] = {
68         { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
69         { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
70         { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
71         { "mk48t37", MK48T37_CLKSZ, MK48T37_CLKOFF, MK48TXX_EXT_REGISTERS },
72         { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
73 };
74
75 int
76 mk48txx_attach(device_t dev)
77 {
78         struct mk48txx_softc *sc;
79         int i;
80         uint8_t wday;
81
82         sc = device_get_softc(dev);
83
84         if (mtx_initialized(&sc->sc_mtx) == 0) {
85                 device_printf(dev, "%s: mutex not initialized\n", __func__);
86                 return (ENXIO);
87         }
88
89         device_printf(dev, "model %s", sc->sc_model);
90         i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
91         while (--i >= 0) {
92                 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0) {
93                         break;
94                 }
95         }
96         if (i < 0) {
97                 device_printf(dev, " (unsupported)\n");
98                 return (ENXIO);
99         }
100         printf("\n");
101         sc->sc_nvramsz = mk48txx_models[i].nvramsz;
102         sc->sc_clkoffset = mk48txx_models[i].clkoff;
103
104         if (sc->sc_nvrd == NULL)
105                 sc->sc_nvrd = mk48txx_def_nvrd;
106         if (sc->sc_nvwr == NULL)
107                 sc->sc_nvwr = mk48txx_def_nvwr;
108
109         if (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS) {
110                 mtx_lock(&sc->sc_mtx);
111                 if ((*sc->sc_nvrd)(dev, sc->sc_clkoffset + MK48TXX_FLAGS) &
112                     MK48TXX_FLAGS_BL) {
113                         mtx_unlock(&sc->sc_mtx);
114                         device_printf(dev, "%s: battery low\n", __func__);
115                         return (ENXIO);
116                 }
117                 mtx_unlock(&sc->sc_mtx);
118         }
119
120         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
121                 /*
122                  * Use MK48TXX_WDAY_CB instead of manually adjusting the
123                  * century.
124                  */
125                 if (!(mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
126                         device_printf(dev, "%s: no century bit\n", __func__);
127                         return (ENXIO);
128                 } else {
129                         mtx_lock(&sc->sc_mtx);
130                         wday = (*sc->sc_nvrd)
131                             (dev, sc->sc_clkoffset + MK48TXX_IWDAY);
132                         wday |= MK48TXX_WDAY_CEB;
133                         (*sc->sc_nvwr)
134                             (dev, sc->sc_clkoffset + MK48TXX_IWDAY, wday);
135                         mtx_unlock(&sc->sc_mtx);
136                 }
137         }
138
139         clock_register(dev, 1000000);   /* 1 second resolution */
140
141         if ((sc->sc_flag & MK48TXX_WDOG_REGISTER) &&
142             (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
143                 sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
144                     mk48txx_watchdog, dev, 0);
145                 device_printf(dev,
146                     "watchdog registered, timeout interval max. 128 sec\n");
147         }
148
149         return (0);
150 }
151
152 /*
153  * Get time-of-day and convert to a `struct timespec'
154  * Return 0 on success; an error number otherwise.
155  */
156 int
157 mk48txx_gettime(device_t dev, struct timespec *ts)
158 {
159         struct mk48txx_softc *sc;
160         bus_size_t clkoff;
161         struct clocktime ct;
162         int year;
163         uint8_t csr;
164
165         sc = device_get_softc(dev);
166         clkoff = sc->sc_clkoffset;
167
168         mtx_lock(&sc->sc_mtx);
169         /* enable read (stop time) */
170         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
171         csr |= MK48TXX_CSR_READ;
172         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
173
174 #define FROMREG(reg, mask)      ((*sc->sc_nvrd)(dev, clkoff + (reg)) & (mask))
175
176         ct.nsec = 0;
177         ct.sec = FROMBCD(FROMREG(MK48TXX_ISEC, MK48TXX_SEC_MASK));
178         ct.min = FROMBCD(FROMREG(MK48TXX_IMIN, MK48TXX_MIN_MASK));
179         ct.hour = FROMBCD(FROMREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK));
180         ct.day = FROMBCD(FROMREG(MK48TXX_IDAY, MK48TXX_DAY_MASK));
181 #if 0
182         /* Map dow from 1 - 7 to 0 - 6; FROMBCD() isn't necessary here. */
183         ct.dow = FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK) - 1;
184 #else
185         /*
186          * Set dow = -1 because some drivers (for example the NetBSD and
187          * OpenBSD mk48txx(4)) don't set it correctly.
188          */
189         ct.dow = -1;
190 #endif
191         ct.mon = FROMBCD(FROMREG(MK48TXX_IMON, MK48TXX_MON_MASK));
192         year = FROMBCD(FROMREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK));
193         year += sc->sc_year0;
194         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST)
195                 year += (FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB) >>
196                     MK48TXX_WDAY_CB_SHIFT) * 100;
197         else if (year < POSIX_BASE_YEAR)
198                 year += 100;
199
200 #undef FROMREG
201
202         ct.year = year;
203
204         /* time wears on */
205         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
206         csr &= ~MK48TXX_CSR_READ;
207         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
208         mtx_unlock(&sc->sc_mtx);
209
210         return (clock_ct_to_ts(&ct, ts));
211 }
212
213 /*
214  * Set the time-of-day clock based on the value of the `struct timespec' arg.
215  * Return 0 on success; an error number otherwise.
216  */
217 int
218 mk48txx_settime(device_t dev, struct timespec *ts)
219 {
220         struct mk48txx_softc *sc;
221         bus_size_t clkoff;
222         struct clocktime ct;
223         uint8_t csr;
224         int cent, year;
225
226         sc = device_get_softc(dev);
227         clkoff = sc->sc_clkoffset;
228
229         /* Accuracy is only one second. */
230         if (ts->tv_nsec >= 500000000)
231                 ts->tv_sec++;
232         ts->tv_nsec = 0;
233         clock_ts_to_ct(ts, &ct);
234
235         mtx_lock(&sc->sc_mtx);
236         /* enable write */
237         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
238         csr |= MK48TXX_CSR_WRITE;
239         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
240
241 #define TOREG(reg, mask, val)                                           \
242         ((*sc->sc_nvwr)(dev, clkoff + (reg),                            \
243         ((*sc->sc_nvrd)(dev, clkoff + (reg)) & ~(mask)) |               \
244         ((val) & (mask))))
245
246         TOREG(MK48TXX_ISEC, MK48TXX_SEC_MASK, TOBCD(ct.sec));
247         TOREG(MK48TXX_IMIN, MK48TXX_MIN_MASK, TOBCD(ct.min));
248         TOREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK, TOBCD(ct.hour));
249         /* Map dow from 0 - 6 to 1 - 7; TOBCD() isn't necessary here. */
250         TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK, ct.dow + 1);
251         TOREG(MK48TXX_IDAY, MK48TXX_DAY_MASK, TOBCD(ct.day));
252         TOREG(MK48TXX_IMON, MK48TXX_MON_MASK, TOBCD(ct.mon));
253
254         year = ct.year - sc->sc_year0;
255         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
256                 cent = year / 100;
257                 TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB,
258                     cent << MK48TXX_WDAY_CB_SHIFT);
259                 year -= cent * 100;
260         } else if (year > 99)
261                 year -= 100;
262         TOREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK, TOBCD(year));
263
264 #undef TOREG
265
266         /* load them up */
267         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
268         csr &= ~MK48TXX_CSR_WRITE;
269         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
270         mtx_unlock(&sc->sc_mtx);
271         return (0);
272 }
273
274 static uint8_t
275 mk48txx_def_nvrd(device_t dev, int off)
276 {
277         struct mk48txx_softc *sc;
278
279         sc = device_get_softc(dev);
280         return (bus_read_1(sc->sc_res, off));
281 }
282
283 static void
284 mk48txx_def_nvwr(device_t dev, int off, uint8_t v)
285 {
286         struct mk48txx_softc *sc;
287
288         sc = device_get_softc(dev);
289         bus_write_1(sc->sc_res, off, v);
290 }
291
292 static void
293 mk48txx_watchdog(void *arg, u_int cmd, int *error)
294 {
295         device_t dev;
296         struct mk48txx_softc *sc;
297         uint8_t t, wdog;
298
299         dev = arg;
300         sc = device_get_softc(dev);
301
302         t = cmd & WD_INTERVAL;
303         if (t >= 26 && t <= 37) {
304                 wdog = 0;
305                 if (t <= WD_TO_2SEC) {
306                         wdog |= MK48TXX_WDOG_RB_1_16;
307                         t -= 26;
308                 } else if (t <= WD_TO_8SEC) {
309                         wdog |= MK48TXX_WDOG_RB_1_4;
310                         t -= WD_TO_250MS;
311                 } else if (t <= WD_TO_32SEC) {
312                         wdog |= MK48TXX_WDOG_RB_1;
313                         t -= WD_TO_1SEC;
314                 } else {
315                         wdog |= MK48TXX_WDOG_RB_4;
316                         t -= WD_TO_4SEC;
317                 }
318                 wdog |= (min(1 << t,
319                     MK48TXX_WDOG_BMB_MASK >> MK48TXX_WDOG_BMB_SHIFT)) <<
320                     MK48TXX_WDOG_BMB_SHIFT;
321                 if (sc->sc_flag & MK48TXX_WDOG_ENABLE_WDS)
322                         wdog |= MK48TXX_WDOG_WDS;
323                 *error = 0;
324         } else {
325                 wdog = 0;
326         }
327         mtx_lock(&sc->sc_mtx);
328         (*sc->sc_nvwr)(dev, sc->sc_clkoffset + MK48TXX_WDOG, wdog);
329         mtx_unlock(&sc->sc_mtx);
330 }