1 /* $NetBSD: timekeeper.c,v 1.15 2014/11/20 16:34:25 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
33
34 __KERNEL_RCSID(0, "$NetBSD: timekeeper.c,v 1.15 2014/11/20 16:34:25 christos Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/kernel.h>
40
41 #include <machine/cpu.h>
42
43 #include <dev/clock_subr.h>
44 #include <luna68k/dev/timekeeper.h>
45 #include <machine/autoconf.h>
46
47 #include "ioconf.h"
48
49 #define YEAR0 1970 /* year offset */
50
51 struct timekeeper_softc {
52 device_t sc_dev;
53 void *sc_clock, *sc_nvram;
54 int sc_nvramsize;
55 uint8_t sc_image[2040];
56 struct todr_chip_handle sc_todr;
57 };
58
59 static int clock_match(device_t, cfdata_t, void *);
60 static void clock_attach(device_t, device_t, void *);
61 static void dsclock_init(struct timekeeper_softc *);
62
63 CFATTACH_DECL_NEW(clock, sizeof (struct timekeeper_softc),
64 clock_match, clock_attach, NULL, NULL);
65
66 static int mkclock_get(todr_chip_handle_t, struct clock_ymdhms *);
67 static int mkclock_set(todr_chip_handle_t, struct clock_ymdhms *);
68 static int dsclock_get(todr_chip_handle_t, struct clock_ymdhms *);
69 static int dsclock_set(todr_chip_handle_t, struct clock_ymdhms *);
70
71 static int
clock_match(device_t parent,cfdata_t cf,void * aux)72 clock_match(device_t parent, cfdata_t cf, void *aux)
73 {
74 struct mainbus_attach_args *ma = aux;
75
76 if (strcmp(ma->ma_name, clock_cd.cd_name))
77 return 0;
78 return 1;
79 }
80
81 static void
clock_attach(device_t parent,device_t self,void * aux)82 clock_attach(device_t parent, device_t self, void *aux)
83 {
84 struct timekeeper_softc *sc = device_private(self);
85 struct mainbus_attach_args *ma = aux;
86
87 sc->sc_dev = self;
88
89 switch (machtype) {
90 default:
91 case LUNA_I: /* Mostek MK48T02 */
92 sc->sc_clock = (void *)(ma->ma_addr + 2040);
93 sc->sc_nvram = (void *)ma->ma_addr;
94 sc->sc_nvramsize = 2040;
95 sc->sc_todr.todr_gettime_ymdhms = mkclock_get;
96 sc->sc_todr.todr_settime_ymdhms = mkclock_set;
97 sc->sc_todr.cookie = sc;
98 aprint_normal(": mk48t02\n");
99 break;
100 case LUNA_II: /* Dallas DS1287A */
101 sc->sc_clock = (void *)ma->ma_addr;
102 sc->sc_nvram = (void *)(ma->ma_addr + MC_NREGS);
103 sc->sc_nvramsize = 50;
104 sc->sc_todr.todr_gettime_ymdhms = dsclock_get;
105 sc->sc_todr.todr_settime_ymdhms = dsclock_set;
106 sc->sc_todr.cookie = sc;
107 dsclock_init(sc);
108 aprint_normal(": ds1287a\n");
109 break;
110 }
111 todr_attach(&sc->sc_todr);
112 memcpy(sc->sc_image, sc->sc_nvram, sc->sc_nvramsize);
113 }
114
115 /*
116 * Get the time of day, based on the clock's value and/or the base value.
117 */
118 static int
mkclock_get(todr_chip_handle_t tch,struct clock_ymdhms * dt)119 mkclock_get(todr_chip_handle_t tch, struct clock_ymdhms *dt)
120 {
121 struct timekeeper_softc *sc = (void *)tch->cookie;
122 volatile uint8_t *chiptime = (void *)sc->sc_clock;
123 int s;
124
125 s = splclock();
126 chiptime[MK_CSR] |= MK_CSR_READ; /* enable read (stop time) */
127 dt->dt_sec = bcdtobin(chiptime[MK_SEC]);
128 dt->dt_min = bcdtobin(chiptime[MK_MIN]);
129 dt->dt_hour = bcdtobin(chiptime[MK_HOUR]);
130 dt->dt_wday = bcdtobin(chiptime[MK_DOW]);
131 dt->dt_day = bcdtobin(chiptime[MK_DOM]);
132 dt->dt_mon = bcdtobin(chiptime[MK_MONTH]);
133 dt->dt_year = bcdtobin(chiptime[MK_YEAR]) + YEAR0;
134 chiptime[MK_CSR] &= ~MK_CSR_READ; /* time wears on */
135 splx(s);
136 return 0;
137 }
138
139 /*
140 * Reset the TODR based on the time value.
141 */
142 static int
mkclock_set(todr_chip_handle_t tch,struct clock_ymdhms * dt)143 mkclock_set(todr_chip_handle_t tch, struct clock_ymdhms *dt)
144 {
145 struct timekeeper_softc *sc = (void *)tch->cookie;
146 volatile uint8_t *chiptime = (void *)sc->sc_clock;
147 volatile uint8_t *stamp = (uint8_t *)sc->sc_nvram + 0x10;
148 int s;
149
150 s = splclock();
151 chiptime[MK_CSR] |= MK_CSR_WRITE; /* enable write */
152 chiptime[MK_SEC] = bintobcd(dt->dt_sec);
153 chiptime[MK_MIN] = bintobcd(dt->dt_min);
154 chiptime[MK_HOUR] = bintobcd(dt->dt_hour);
155 chiptime[MK_DOW] = bintobcd(dt->dt_wday);
156 chiptime[MK_DOM] = bintobcd(dt->dt_day);
157 chiptime[MK_MONTH] = bintobcd(dt->dt_mon);
158 chiptime[MK_YEAR] = bintobcd(dt->dt_year - YEAR0);
159 chiptime[MK_CSR] &= ~MK_CSR_WRITE; /* load them up */
160 splx(s);
161
162 stamp[0] = 'R'; stamp[1] = 'T'; stamp[2] = 'C'; stamp[3] = '\0';
163 return 0;
164 }
165
166 static void
dsclock_init(struct timekeeper_softc * sc)167 dsclock_init(struct timekeeper_softc *sc)
168 {
169 volatile uint8_t *chiptime = (void *)sc->sc_clock;
170
171 /*
172 * It looks the firmware ROM doesn't initialize DS1287 at all
173 * even after the chip is replaced, so explicitly initialize
174 * control registers here.
175 */
176 chiptime = (void *)sc->sc_clock;
177
178 /* No DSE, 24HR, BINARY */
179 chiptime[MC_REGB] =
180 (chiptime[MC_REGB] & ~MC_REGB_DSE) |
181 (MC_REGB_24HR | MC_REGB_BINARY);
182
183 /* make sure to start integrated clock OSC */
184 chiptime[MC_REGA] =
185 (chiptime[MC_REGA] & ~MC_REGA_DVMASK) | MC_BASE_32_KHz;
186 }
187
188 /*
189 * Get the time of day, based on the clock's value and/or the base value.
190 */
191 static int
dsclock_get(todr_chip_handle_t tch,struct clock_ymdhms * dt)192 dsclock_get(todr_chip_handle_t tch, struct clock_ymdhms *dt)
193 {
194 struct timekeeper_softc *sc = (void *)tch->cookie;
195 volatile uint8_t *chiptime = (void *)sc->sc_clock;
196 int s;
197
198 s = splclock();
199 /* update in progress; spin loop */
200 while (chiptime[MC_REGA] & MC_REGA_UIP)
201 continue;
202 dt->dt_sec = chiptime[MC_SEC];
203 dt->dt_min = chiptime[MC_MIN];
204 dt->dt_hour = chiptime[MC_HOUR];
205 dt->dt_wday = chiptime[MC_DOW];
206 dt->dt_day = chiptime[MC_DOM];
207 dt->dt_mon = chiptime[MC_MONTH];
208 dt->dt_year = chiptime[MC_YEAR] + YEAR0;
209 splx(s);
210 return 0;
211 }
212
213 /*
214 * Reset the TODR based on the time value.
215 */
216 static int
dsclock_set(todr_chip_handle_t tch,struct clock_ymdhms * dt)217 dsclock_set(todr_chip_handle_t tch, struct clock_ymdhms *dt)
218 {
219 struct timekeeper_softc *sc = (void *)tch->cookie;
220 volatile uint8_t *chiptime = (void *)sc->sc_clock;
221 int s;
222
223 s = splclock();
224 chiptime[MC_REGB] |= MC_REGB_SET; /* enable write */
225 chiptime[MC_SEC] = dt->dt_sec;
226 chiptime[MC_MIN] = dt->dt_min;
227 chiptime[MC_HOUR] = dt->dt_hour;
228 chiptime[MC_DOW] = dt->dt_wday;
229 chiptime[MC_DOM] = dt->dt_day;
230 chiptime[MC_MONTH] = dt->dt_mon;
231 chiptime[MC_YEAR] = dt->dt_year - YEAR0;
232 chiptime[MC_REGB] &= ~MC_REGB_SET; /* load them up */
233 splx(s);
234 return 0;
235 }
236