mk48txx.c revision 1.17.2.3 1 /* $NetBSD: mk48txx.c,v 1.17.2.3 2008/01/21 09:43:02 yamt 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.17.2.3 2008/01/21 09:43:02 yamt Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/errno.h>
49
50 #include <sys/bus.h>
51 #include <dev/clock_subr.h>
52 #include <dev/ic/mk48txxreg.h>
53 #include <dev/ic/mk48txxvar.h>
54
55 int mk48txx_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
56 int mk48txx_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
57 uint8_t mk48txx_def_nvrd(struct mk48txx_softc *, int);
58 void mk48txx_def_nvwr(struct mk48txx_softc *, int, uint8_t);
59
60 struct {
61 const char *name;
62 bus_size_t nvramsz;
63 bus_size_t clkoff;
64 int flags;
65 #define MK48TXX_EXT_REGISTERS 1 /* Has extended register set */
66 } 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 void
74 mk48txx_attach(struct mk48txx_softc *sc)
75 {
76 todr_chip_handle_t handle;
77 int i;
78
79 aprint_normal(": %s", sc->sc_model);
80
81 i = __arraycount(mk48txx_models);
82 while (--i >= 0) {
83 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0)
84 break;
85 }
86 if (i < 0)
87 panic("%s: unsupported model", __func__);
88
89 sc->sc_nvramsz = mk48txx_models[i].nvramsz;
90 sc->sc_clkoffset = mk48txx_models[i].clkoff;
91
92 handle = &sc->sc_handle;
93 handle->cookie = sc;
94 handle->todr_gettime = NULL;
95 handle->todr_settime = NULL;
96 handle->todr_gettime_ymdhms = mk48txx_gettime_ymdhms;
97 handle->todr_settime_ymdhms = mk48txx_settime_ymdhms;
98
99 if (sc->sc_nvrd == NULL)
100 sc->sc_nvrd = mk48txx_def_nvrd;
101 if (sc->sc_nvwr == NULL)
102 sc->sc_nvwr = mk48txx_def_nvwr;
103
104 todr_attach(handle);
105 }
106
107 /*
108 * Get time-of-day and convert to a `struct timeval'
109 * Return 0 on success; an error number otherwise.
110 */
111 int
112 mk48txx_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
113 {
114 struct mk48txx_softc *sc;
115 bus_size_t clkoff;
116 int year;
117 uint8_t csr;
118
119 sc = handle->cookie;
120 clkoff = sc->sc_clkoffset;
121
122 todr_wenable(handle, 1);
123
124 /* enable read (stop time) */
125 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
126 csr |= MK48TXX_CSR_READ;
127 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
128
129 dt->dt_sec = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_ISEC));
130 dt->dt_min = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMIN));
131 dt->dt_hour = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IHOUR));
132 dt->dt_day = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IDAY));
133 dt->dt_wday = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IWDAY));
134 dt->dt_mon = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMON));
135 year = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IYEAR));
136
137 year += sc->sc_year0;
138 if (year < POSIX_BASE_YEAR &&
139 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
140 year += 100;
141
142 dt->dt_year = year;
143
144 /* time wears on */
145 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
146 csr &= ~MK48TXX_CSR_READ;
147 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
148 todr_wenable(handle, 0);
149
150 return 0;
151 }
152
153 /*
154 * Set the time-of-day clock based on the value of the `struct timeval' arg.
155 * Return 0 on success; an error number otherwise.
156 */
157 int
158 mk48txx_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
159 {
160 struct mk48txx_softc *sc;
161 bus_size_t clkoff;
162 uint8_t csr;
163 int year;
164
165 sc = handle->cookie;
166 clkoff = sc->sc_clkoffset;
167
168 year = dt->dt_year - sc->sc_year0;
169 if (year > 99 &&
170 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
171 year -= 100;
172
173 todr_wenable(handle, 1);
174 /* enable write */
175 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
176 csr |= MK48TXX_CSR_WRITE;
177 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
178
179 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ISEC, TOBCD(dt->dt_sec));
180 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMIN, TOBCD(dt->dt_min));
181 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IHOUR, TOBCD(dt->dt_hour));
182 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IWDAY, TOBCD(dt->dt_wday));
183 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IDAY, TOBCD(dt->dt_day));
184 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMON, TOBCD(dt->dt_mon));
185 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IYEAR, TOBCD(year));
186
187 /* load them up */
188 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
189 csr &= ~MK48TXX_CSR_WRITE;
190 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
191 todr_wenable(handle, 0);
192 return 0;
193 }
194
195 int
196 mk48txx_get_nvram_size(todr_chip_handle_t handle, bus_size_t *vp)
197 {
198 struct mk48txx_softc *sc;
199
200 sc = handle->cookie;
201 *vp = sc->sc_nvramsz;
202 return 0;
203 }
204
205 uint8_t
206 mk48txx_def_nvrd(struct mk48txx_softc *sc, int off)
207 {
208
209 return bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
210 }
211
212 void
213 mk48txx_def_nvwr(struct mk48txx_softc *sc, int off, uint8_t v)
214 {
215
216 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v);
217 }
218