a34kbbc.c revision 1.24 1 1.24 thorpej /* $NetBSD: a34kbbc.c,v 1.24 2025/09/07 00:41:19 thorpej Exp $ */
2 1.1 is
3 1.1 is /*
4 1.21 rmind * Copyright (c) 1988 University of Utah.
5 1.1 is * Copyright (c) 1982, 1990 The Regents of the University of California.
6 1.1 is * All rights reserved.
7 1.1 is *
8 1.1 is * This code is derived from software contributed to Berkeley by
9 1.1 is * the Systems Programming Group of the University of Utah Computer
10 1.1 is * Science Department.
11 1.1 is *
12 1.1 is * Redistribution and use in source and binary forms, with or without
13 1.1 is * modification, are permitted provided that the following conditions
14 1.1 is * are met:
15 1.1 is * 1. Redistributions of source code must retain the above copyright
16 1.1 is * notice, this list of conditions and the following disclaimer.
17 1.1 is * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 is * notice, this list of conditions and the following disclaimer in the
19 1.1 is * documentation and/or other materials provided with the distribution.
20 1.13 agc * 3. Neither the name of the University nor the names of its contributors
21 1.13 agc * may be used to endorse or promote products derived from this software
22 1.13 agc * without specific prior written permission.
23 1.13 agc *
24 1.13 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.13 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.13 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.13 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.13 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.13 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.13 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.13 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.13 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.13 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.13 agc * SUCH DAMAGE.
35 1.13 agc *
36 1.13 agc * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 1.13 agc *
38 1.13 agc * @(#)clock.c 7.6 (Berkeley) 5/7/91
39 1.13 agc */
40 1.13 agc
41 1.10 aymeric #include <sys/cdefs.h>
42 1.24 thorpej __KERNEL_RCSID(0, "$NetBSD: a34kbbc.c,v 1.24 2025/09/07 00:41:19 thorpej Exp $");
43 1.1 is
44 1.1 is #include <sys/param.h>
45 1.1 is #include <sys/kernel.h>
46 1.1 is #include <sys/device.h>
47 1.1 is #include <sys/systm.h>
48 1.1 is #include <machine/psl.h>
49 1.1 is #include <machine/cpu.h>
50 1.1 is #include <amiga/amiga/device.h>
51 1.1 is #include <amiga/amiga/custom.h>
52 1.1 is #include <amiga/amiga/cia.h>
53 1.1 is #include <amiga/dev/rtc.h>
54 1.1 is #include <amiga/dev/zbusvar.h>
55 1.1 is
56 1.1 is #include <dev/clock_subr.h>
57 1.1 is
58 1.24 thorpej static int a34kbbc_match(device_t, cfdata_t, void *);
59 1.24 thorpej static void a34kbbc_attach(device_t, device_t, void *);
60 1.1 is
61 1.24 thorpej struct a34kbbc_softc {
62 1.24 thorpej device_t sc_dev;
63 1.24 thorpej struct todr_chip_handle sc_todr;
64 1.24 thorpej volatile struct rtclock3000 *sc_addr;
65 1.24 thorpej };
66 1.24 thorpej
67 1.24 thorpej CFATTACH_DECL_NEW(a34kbbc, sizeof(struct a34kbbc_softc),
68 1.12 thorpej a34kbbc_match, a34kbbc_attach, NULL, NULL);
69 1.1 is
70 1.24 thorpej static int a34kbbc_read(volatile struct rtclock3000 *,
71 1.24 thorpej struct clock_ymdhms *);
72 1.24 thorpej static int a34kugettod(todr_chip_handle_t, struct clock_ymdhms *);
73 1.24 thorpej static int a34kusettod(todr_chip_handle_t, struct clock_ymdhms *);
74 1.1 is
75 1.24 thorpej static int
76 1.23 chs a34kbbc_match(device_t parent, cfdata_t cf, void *aux)
77 1.1 is {
78 1.20 mhitch struct clock_ymdhms dt;
79 1.8 kleink static int a34kbbc_matched = 0;
80 1.8 kleink
81 1.23 chs if (!matchname("a34kbbc", aux))
82 1.1 is return(0);
83 1.1 is
84 1.16 is /* Allow only one instance. */
85 1.8 kleink if (a34kbbc_matched)
86 1.8 kleink return(0);
87 1.1 is
88 1.1 is if (!(is_a3000() || is_a4000()))
89 1.1 is return(0);
90 1.1 is
91 1.24 thorpej if (a34kbbc_read(ztwomap(0xdc0000), &dt) != 0)
92 1.1 is return(0);
93 1.1 is
94 1.8 kleink a34kbbc_matched = 1;
95 1.1 is return(1);
96 1.1 is }
97 1.1 is
98 1.1 is /*
99 1.1 is * Attach us to the rtc function pointers.
100 1.1 is */
101 1.24 thorpej static void
102 1.23 chs a34kbbc_attach(device_t parent, device_t self, void *aux)
103 1.1 is {
104 1.24 thorpej struct a34kbbc_softc *sc = device_private(self);
105 1.24 thorpej
106 1.1 is printf("\n");
107 1.1 is
108 1.24 thorpej sc->sc_dev = self;
109 1.24 thorpej sc->sc_addr = ztwomap(0xdc0000);
110 1.24 thorpej
111 1.24 thorpej sc->sc_todr.cookie = sc;
112 1.24 thorpej sc->sc_todr.todr_gettime_ymdhms = a34kugettod;
113 1.24 thorpej sc->sc_todr.todr_settime_ymdhms = a34kusettod;
114 1.24 thorpej todr_attach(&sc->sc_todr);
115 1.1 is }
116 1.1 is
117 1.24 thorpej static int
118 1.24 thorpej a34kbbc_read(volatile struct rtclock3000 *rt, struct clock_ymdhms *dt)
119 1.1 is {
120 1.1 is
121 1.1 is /* hold clock */
122 1.1 is rt->control1 = A3CONTROL1_HOLD_CLOCK;
123 1.1 is
124 1.1 is /* Copy the info. Careful about the order! */
125 1.19 gdamore dt->dt_sec = rt->second1 * 10 + rt->second2;
126 1.19 gdamore dt->dt_min = rt->minute1 * 10 + rt->minute2;
127 1.19 gdamore dt->dt_hour = rt->hour1 * 10 + rt->hour2;
128 1.19 gdamore dt->dt_wday = rt->weekday;
129 1.19 gdamore dt->dt_day = rt->day1 * 10 + rt->day2;
130 1.19 gdamore dt->dt_mon = rt->month1 * 10 + rt->month2;
131 1.19 gdamore dt->dt_year = rt->year1 * 10 + rt->year2;
132 1.1 is
133 1.19 gdamore dt->dt_year += CLOCK_BASE_YEAR;
134 1.5 is /* let it run again.. */
135 1.5 is rt->control1 = A3CONTROL1_FREE_CLOCK;
136 1.5 is
137 1.19 gdamore if (dt->dt_year < STARTOFTIME)
138 1.19 gdamore dt->dt_year += 100;
139 1.1 is
140 1.19 gdamore /*
141 1.19 gdamore * These checks are mostly redundant against those already in the
142 1.19 gdamore * generic todr, but apparently the attach code checks against the
143 1.19 gdamore * return value of this function, so we have to include a check here,
144 1.19 gdamore * too.
145 1.19 gdamore */
146 1.19 gdamore if ((dt->dt_hour > 23) ||
147 1.19 gdamore (dt->dt_wday > 6) ||
148 1.19 gdamore (dt->dt_day > 31) ||
149 1.19 gdamore (dt->dt_mon > 12) ||
150 1.19 gdamore /* (dt.dt_year < STARTOFTIME) || */ (dt->dt_year > 2036))
151 1.18 mhitch return (EINVAL);
152 1.1 is
153 1.17 mhitch return (0);
154 1.1 is }
155 1.1 is
156 1.24 thorpej static int
157 1.24 thorpej a34kugettod(todr_chip_handle_t h, struct clock_ymdhms *dt)
158 1.24 thorpej {
159 1.24 thorpej struct a34kbbc_softc *sc = h->cookie;
160 1.24 thorpej
161 1.24 thorpej return a34kbbc_read(sc->sc_addr, dt);
162 1.24 thorpej }
163 1.24 thorpej
164 1.24 thorpej static int
165 1.19 gdamore a34kusettod(todr_chip_handle_t h, struct clock_ymdhms *dt)
166 1.1 is {
167 1.24 thorpej struct a34kbbc_softc *sc = h->cookie;
168 1.24 thorpej volatile struct rtclock3000 *rt = sc->sc_addr;
169 1.1 is
170 1.1 is /*
171 1.1 is * there seem to be problems with the bitfield addressing
172 1.1 is * currently used..
173 1.1 is */
174 1.1 is
175 1.7 is rt->control1 = A3CONTROL1_HOLD_CLOCK; /* implies mode 0 */
176 1.19 gdamore rt->second1 = dt->dt_sec / 10;
177 1.19 gdamore rt->second2 = dt->dt_sec % 10;
178 1.19 gdamore rt->minute1 = dt->dt_min / 10;
179 1.19 gdamore rt->minute2 = dt->dt_min % 10;
180 1.19 gdamore rt->hour1 = dt->dt_hour / 10;
181 1.19 gdamore rt->hour2 = dt->dt_hour % 10;
182 1.19 gdamore rt->weekday = dt->dt_wday;
183 1.19 gdamore rt->day1 = dt->dt_day / 10;
184 1.19 gdamore rt->day2 = dt->dt_day % 10;
185 1.19 gdamore rt->month1 = dt->dt_mon / 10;
186 1.19 gdamore rt->month2 = dt->dt_mon % 10;
187 1.19 gdamore rt->year1 = (dt->dt_year / 10) % 10;
188 1.19 gdamore rt->year2 = dt->dt_year % 10;
189 1.6 is rt->control1 = A3CONTROL1_HOLD_CLOCK | 1; /* mode 1 registers */
190 1.19 gdamore rt->leapyear = dt->dt_year; /* XXX implicit % 4 */
191 1.7 is rt->control1 = A3CONTROL1_FREE_CLOCK; /* implies mode 1 */
192 1.1 is
193 1.17 mhitch return (0);
194 1.1 is }
195