intersil7170.c revision 1.13 1 /* $NetBSD: intersil7170.c,v 1.13 2025/09/07 04:47:01 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 * Intersil 7170 time-of-day chip subroutines.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: intersil7170.c,v 1.13 2025/09/07 04:47:01 thorpej Exp $");
37
38 #include <sys/param.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/errno.h>
43
44 #include <sys/bus.h>
45 #include <dev/clock_subr.h>
46 #include <dev/ic/intersil7170reg.h>
47 #include <dev/ic/intersil7170var.h>
48
49 int intersil7170_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
50 int intersil7170_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
51
52 void
53 intersil7170_attach(struct intersil7170_softc *sc)
54 {
55 todr_chip_handle_t handle;
56
57 aprint_normal(": intersil7170");
58
59 handle = &sc->sc_handle;
60
61 handle->cookie = sc;
62 handle->todr_gettime_ymdhms = intersil7170_gettime_ymdhms;
63 handle->todr_settime_ymdhms = intersil7170_settime_ymdhms;
64
65 todr_attach(handle);
66 }
67
68 /*
69 * Set up the system's time, given a `reasonable' time value.
70 */
71 int
72 intersil7170_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
73 {
74 struct intersil7170_softc *sc = handle->cookie;
75 bus_space_tag_t bt = sc->sc_bst;
76 bus_space_handle_t bh = sc->sc_bsh;
77 uint8_t cmd;
78 int year;
79 int s;
80
81 /* No interrupts while we're fiddling with the chip */
82 s = splhigh();
83
84 /* Enable read (stop time) */
85 cmd = INTERSIL_COMMAND(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
86 bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
87
88 /* The order of reading out the clock elements is important */
89 (void)bus_space_read_1(bt, bh, INTERSIL_ICSEC); /* not used */
90 dt->dt_hour = bus_space_read_1(bt, bh, INTERSIL_IHOUR);
91 dt->dt_min = bus_space_read_1(bt, bh, INTERSIL_IMIN);
92 dt->dt_sec = bus_space_read_1(bt, bh, INTERSIL_ISEC);
93 dt->dt_mon = bus_space_read_1(bt, bh, INTERSIL_IMON);
94 dt->dt_day = bus_space_read_1(bt, bh, INTERSIL_IDAY);
95 year = bus_space_read_1(bt, bh, INTERSIL_IYEAR);
96 dt->dt_wday = bus_space_read_1(bt, bh, INTERSIL_IDOW);
97
98 /* Done writing (time wears on) */
99 cmd = INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
100 bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
101 splx(s);
102
103 year += sc->sc_year0;
104 if (year < POSIX_BASE_YEAR &&
105 (sc->sc_flag & INTERSIL7170_NO_CENT_ADJUST) == 0)
106 year += 100;
107
108 dt->dt_year = year;
109
110 return 0;
111 }
112
113 /*
114 * Reset the clock based on the current time.
115 */
116 int
117 intersil7170_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
118 {
119 struct intersil7170_softc *sc = handle->cookie;
120 bus_space_tag_t bt = sc->sc_bst;
121 bus_space_handle_t bh = sc->sc_bsh;
122 uint8_t cmd;
123 int year;
124 int s;
125
126 year = dt->dt_year - sc->sc_year0;
127 if (year > 99 && (sc->sc_flag & INTERSIL7170_NO_CENT_ADJUST) == 0)
128 year -= 100;
129
130 /* No interrupts while we're fiddling with the chip */
131 s = splhigh();
132
133 /* Enable write (stop time) */
134 cmd = INTERSIL_COMMAND(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
135 bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
136
137 /* The order of reading writing the clock elements is important */
138 bus_space_write_1(bt, bh, INTERSIL_ICSEC, 0);
139 bus_space_write_1(bt, bh, INTERSIL_IHOUR, dt->dt_hour);
140 bus_space_write_1(bt, bh, INTERSIL_IMIN, dt->dt_min);
141 bus_space_write_1(bt, bh, INTERSIL_ISEC, dt->dt_sec);
142 bus_space_write_1(bt, bh, INTERSIL_IMON, dt->dt_mon);
143 bus_space_write_1(bt, bh, INTERSIL_IDAY, dt->dt_day);
144 bus_space_write_1(bt, bh, INTERSIL_IYEAR, year);
145 bus_space_write_1(bt, bh, INTERSIL_IDOW, dt->dt_wday);
146
147 /* Done writing (time wears on) */
148 cmd = INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
149 bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
150 splx(s);
151
152 return 0;
153 }
154