mk48txx.c revision 1.29 1 /* $NetBSD: mk48txx.c,v 1.29 2025/09/07 21:45:16 thorpej 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 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.29 2025/09/07 21:45:16 thorpej Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/errno.h>
42
43 #include <sys/bus.h>
44 #include <dev/clock_subr.h>
45 #include <dev/ic/mk48txxreg.h>
46 #include <dev/ic/mk48txxvar.h>
47
48 int mk48txx_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
49 int mk48txx_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
50 uint8_t mk48txx_def_nvrd(struct mk48txx_softc *, int);
51 void mk48txx_def_nvwr(struct mk48txx_softc *, int, uint8_t);
52
53 const struct {
54 const char *name;
55 bus_size_t nvramsz;
56 bus_size_t clkoff;
57 int flags;
58 #define MK48TXX_EXT_REGISTERS 1 /* Has extended register set */
59 } mk48txx_models[] = {
60 { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
61 { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
62 { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
63 { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
64 { "ds1553", DS1553_CLKSZ, DS1553_CLKOFF, MK48TXX_EXT_REGISTERS },
65 };
66
67 void
68 mk48txx_attach(struct mk48txx_softc *sc)
69 {
70 todr_chip_handle_t handle;
71 int i;
72
73 aprint_normal(": %s", sc->sc_model);
74
75 i = __arraycount(mk48txx_models);
76 while (--i >= 0) {
77 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0)
78 break;
79 }
80 if (i < 0)
81 panic("%s: unsupported model", __func__);
82
83 sc->sc_nvramsz = mk48txx_models[i].nvramsz;
84 sc->sc_clkoffset = mk48txx_models[i].clkoff;
85
86 handle = &sc->sc_handle;
87 KASSERT(sc->sc_dev != NULL);
88 handle->todr_dev = sc->sc_dev;
89 handle->todr_gettime_ymdhms = mk48txx_gettime_ymdhms;
90 handle->todr_settime_ymdhms = mk48txx_settime_ymdhms;
91
92 if (sc->sc_nvrd == NULL)
93 sc->sc_nvrd = mk48txx_def_nvrd;
94 if (sc->sc_nvwr == NULL)
95 sc->sc_nvwr = mk48txx_def_nvwr;
96
97 todr_attach(handle);
98 }
99
100 /*
101 * Get time-of-day and convert to a `struct timeval'
102 * Return 0 on success; an error number otherwise.
103 */
104 int
105 mk48txx_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
106 {
107 struct mk48txx_softc *sc = device_private(handle->todr_dev);
108 bus_size_t clkoff;
109 int year;
110 uint8_t csr;
111
112 clkoff = sc->sc_clkoffset;
113
114 /* enable read (stop time) */
115 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
116 csr |= MK48TXX_CSR_READ;
117 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
118
119 dt->dt_sec = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_ISEC));
120 dt->dt_min = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMIN));
121 dt->dt_hour = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IHOUR));
122 dt->dt_day = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IDAY));
123 dt->dt_wday = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IWDAY));
124 dt->dt_mon = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMON));
125 year = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IYEAR));
126
127 if (sc->sc_flag & MK48TXX_HAVE_CENT_REG) {
128 year += 100*bcdtobin(csr & MK48TXX_CSR_CENT_MASK);
129 } else {
130 year += sc->sc_year0;
131 if (year < POSIX_BASE_YEAR &&
132 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
133 year += 100;
134 }
135
136 dt->dt_year = year;
137
138 /* time wears on */
139 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
140 csr &= ~MK48TXX_CSR_READ;
141 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
142
143 return 0;
144 }
145
146 /*
147 * Set the time-of-day clock based on the value of the `struct timeval' arg.
148 * Return 0 on success; an error number otherwise.
149 */
150 int
151 mk48txx_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
152 {
153 struct mk48txx_softc *sc = device_private(handle->todr_dev);
154 bus_size_t clkoff;
155 uint8_t csr;
156 int year;
157 int cent;
158
159 clkoff = sc->sc_clkoffset;
160
161 if ((sc->sc_flag & MK48TXX_HAVE_CENT_REG) == 0) {
162 cent = 0;
163 year = dt->dt_year - sc->sc_year0;
164 if (year > 99 &&
165 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
166 year -= 100;
167 } else {
168 cent = dt->dt_year / 100;
169 year = dt->dt_year % 100;
170 }
171
172 /* enable write */
173 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
174 csr |= MK48TXX_CSR_WRITE;
175 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
176
177 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ISEC, bintobcd(dt->dt_sec));
178 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMIN, bintobcd(dt->dt_min));
179 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IHOUR, bintobcd(dt->dt_hour));
180 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IWDAY, bintobcd(dt->dt_wday));
181 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IDAY, bintobcd(dt->dt_day));
182 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMON, bintobcd(dt->dt_mon));
183 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IYEAR, bintobcd(year));
184
185 /*
186 * If we have a century register and the century has changed
187 * update it.
188 */
189 if ((sc->sc_flag & MK48TXX_HAVE_CENT_REG)
190 && (csr & MK48TXX_CSR_CENT_MASK) != bintobcd(cent)) {
191 csr &= ~MK48TXX_CSR_CENT_MASK;
192 csr |= MK48TXX_CSR_CENT_MASK & bintobcd(cent);
193 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
194 }
195
196 /* load them up */
197 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
198 csr &= ~MK48TXX_CSR_WRITE;
199 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
200
201 return 0;
202 }
203
204 int
205 mk48txx_get_nvram_size(todr_chip_handle_t handle, bus_size_t *vp)
206 {
207 struct mk48txx_softc *sc = device_private(handle->todr_dev);
208
209 *vp = sc->sc_nvramsz;
210 return 0;
211 }
212
213 uint8_t
214 mk48txx_def_nvrd(struct mk48txx_softc *sc, int off)
215 {
216
217 return bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
218 }
219
220 void
221 mk48txx_def_nvwr(struct mk48txx_softc *sc, int off, uint8_t v)
222 {
223
224 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v);
225 }
226