intersil7170.c revision 1.1.6.1 1 1.1.6.1 thorpej /* $NetBSD: intersil7170.c,v 1.1.6.1 2002/01/10 19:54:36 thorpej Exp $ */
2 1.1 pk /*-
3 1.1 pk * Copyright (c) 2000 The NetBSD Foundation, Inc.
4 1.1 pk * All rights reserved.
5 1.1 pk *
6 1.1 pk * This code is derived from software contributed to The NetBSD Foundation
7 1.1 pk * by Paul Kranenburg.
8 1.1 pk *
9 1.1 pk * Redistribution and use in source and binary forms, with or without
10 1.1 pk * modification, are permitted provided that the following conditions
11 1.1 pk * are met:
12 1.1 pk * 1. Redistributions of source code must retain the above copyright
13 1.1 pk * notice, this list of conditions and the following disclaimer.
14 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 pk * notice, this list of conditions and the following disclaimer in the
16 1.1 pk * documentation and/or other materials provided with the distribution.
17 1.1 pk * 3. All advertising materials mentioning features or use of this software
18 1.1 pk * must display the following acknowledgement:
19 1.1 pk * This product includes software developed by the NetBSD
20 1.1 pk * Foundation, Inc. and its contributors.
21 1.1 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 pk * contributors may be used to endorse or promote products derived
23 1.1 pk * from this software without specific prior written permission.
24 1.1 pk *
25 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
36 1.1 pk */
37 1.1 pk
38 1.1 pk /*
39 1.1 pk * Intersil 7170 time-of-day chip subroutines.
40 1.1 pk */
41 1.1.6.1 thorpej
42 1.1.6.1 thorpej #include <sys/cdefs.h>
43 1.1.6.1 thorpej __KERNEL_RCSID(0, "$NetBSD: intersil7170.c,v 1.1.6.1 2002/01/10 19:54:36 thorpej Exp $");
44 1.1 pk
45 1.1 pk #include <sys/param.h>
46 1.1 pk #include <sys/malloc.h>
47 1.1 pk #include <sys/systm.h>
48 1.1 pk #include <sys/errno.h>
49 1.1 pk
50 1.1 pk #include <machine/bus.h>
51 1.1 pk #include <dev/clock_subr.h>
52 1.1 pk #include <dev/ic/intersil7170.h>
53 1.1 pk
54 1.1 pk #define intersil_command(run, interrupt) \
55 1.1 pk (run | interrupt | INTERSIL_CMD_FREQ_32K | INTERSIL_CMD_24HR_MODE | \
56 1.1 pk INTERSIL_CMD_NORMAL_MODE)
57 1.1 pk
58 1.1 pk struct intersil7170_softc {
59 1.1 pk bus_space_tag_t sil_bt;
60 1.1 pk bus_space_handle_t sil_bh;
61 1.1 pk int sil_year0;
62 1.1 pk };
63 1.1 pk
64 1.1 pk int intersil7170_gettime(todr_chip_handle_t, struct timeval *);
65 1.1 pk int intersil7170_settime(todr_chip_handle_t, struct timeval *);
66 1.1 pk int intersil7170_getcal(todr_chip_handle_t, int *);
67 1.1 pk int intersil7170_setcal(todr_chip_handle_t, int);
68 1.1 pk
69 1.1 pk int intersil7170_auto_century_adjust = 1;
70 1.1 pk
71 1.1 pk todr_chip_handle_t
72 1.1 pk intersil7170_attach(bt, bh, year0)
73 1.1 pk bus_space_tag_t bt;
74 1.1 pk bus_space_handle_t bh;
75 1.1 pk int year0;
76 1.1 pk {
77 1.1 pk todr_chip_handle_t handle;
78 1.1 pk struct intersil7170_softc *sil;
79 1.1 pk int sz;
80 1.1 pk
81 1.1 pk printf(": intersil7170");
82 1.1 pk
83 1.1 pk sz = ALIGN(sizeof(struct todr_chip_handle)) + sizeof(struct intersil7170);
84 1.1 pk handle = malloc(sz, M_DEVBUF, M_NOWAIT);
85 1.1 pk sil = (struct intersil7170_softc *)((u_long)handle +
86 1.1 pk ALIGN(sizeof(struct todr_chip_handle)));
87 1.1 pk handle->cookie = sil;
88 1.1 pk handle->todr_gettime = intersil7170_gettime;
89 1.1 pk handle->todr_settime = intersil7170_settime;
90 1.1 pk handle->todr_getcal = intersil7170_getcal;
91 1.1 pk handle->todr_setcal = intersil7170_setcal;
92 1.1 pk sil->sil_bt = bt;
93 1.1 pk sil->sil_bh = bh;
94 1.1 pk sil->sil_year0 = year0;
95 1.1 pk
96 1.1 pk return (handle);
97 1.1 pk }
98 1.1 pk
99 1.1 pk /*
100 1.1 pk * Set up the system's time, given a `reasonable' time value.
101 1.1 pk */
102 1.1 pk int
103 1.1 pk intersil7170_gettime(handle, tv)
104 1.1 pk todr_chip_handle_t handle;
105 1.1 pk struct timeval *tv;
106 1.1 pk {
107 1.1 pk struct intersil7170_softc *sil = handle->cookie;
108 1.1 pk bus_space_tag_t bt = sil->sil_bt;
109 1.1 pk bus_space_handle_t bh = sil->sil_bh;
110 1.1 pk struct clock_ymdhms dt;
111 1.1 pk u_int8_t cmd;
112 1.1 pk int year;
113 1.1 pk int s;
114 1.1 pk
115 1.1 pk /* No interrupts while we're fiddling with the chip */
116 1.1 pk s = splhigh();
117 1.1 pk
118 1.1 pk /* Enable read (stop time) */
119 1.1 pk cmd = intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
120 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
121 1.1 pk
122 1.1 pk /* The order of reading out the clock elements is important */
123 1.1 pk bus_space_read_1(bt, bh, INTERSIL_ICSEC); /* not used */
124 1.1 pk dt.dt_hour = bus_space_read_1(bt, bh, INTERSIL_IHOUR);
125 1.1 pk dt.dt_min = bus_space_read_1(bt, bh, INTERSIL_IMIN);
126 1.1 pk dt.dt_sec = bus_space_read_1(bt, bh, INTERSIL_ISEC);
127 1.1 pk dt.dt_mon = bus_space_read_1(bt, bh, INTERSIL_IMON);
128 1.1 pk dt.dt_day = bus_space_read_1(bt, bh, INTERSIL_IDAY);
129 1.1 pk year = bus_space_read_1(bt, bh, INTERSIL_IYEAR);
130 1.1 pk dt.dt_wday = bus_space_read_1(bt, bh, INTERSIL_IDOW);
131 1.1 pk
132 1.1 pk /* Done writing (time wears on) */
133 1.1 pk cmd = intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
134 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
135 1.1 pk splx(s);
136 1.1 pk
137 1.1 pk year += sil->sil_year0;
138 1.1 pk if (year < 1970 && intersil7170_auto_century_adjust != 0)
139 1.1 pk year += 100;
140 1.1 pk
141 1.1 pk dt.dt_year = year;
142 1.1 pk
143 1.1 pk tv->tv_sec = clock_ymdhms_to_secs(&dt);
144 1.1 pk tv->tv_usec = 0;
145 1.1 pk return (0);
146 1.1 pk }
147 1.1 pk
148 1.1 pk /*
149 1.1 pk * Reset the clock based on the current time.
150 1.1 pk */
151 1.1 pk int
152 1.1 pk intersil7170_settime(handle, tv)
153 1.1 pk todr_chip_handle_t handle;
154 1.1 pk struct timeval *tv;
155 1.1 pk {
156 1.1 pk struct intersil7170_softc *sil = handle->cookie;
157 1.1 pk bus_space_tag_t bt = sil->sil_bt;
158 1.1 pk bus_space_handle_t bh = sil->sil_bh;
159 1.1 pk struct clock_ymdhms dt;
160 1.1 pk u_int8_t cmd;
161 1.1 pk int year;
162 1.1 pk int s;
163 1.1 pk
164 1.1 pk clock_secs_to_ymdhms(tv->tv_sec, &dt);
165 1.1 pk
166 1.1 pk year = dt.dt_year - sil->sil_year0;
167 1.1 pk if (year > 99 && intersil7170_auto_century_adjust != 0)
168 1.1 pk year -= 100;
169 1.1 pk
170 1.1 pk /* No interrupts while we're fiddling with the chip */
171 1.1 pk s = splhigh();
172 1.1 pk
173 1.1 pk /* Enable write (stop time) */
174 1.1 pk cmd = intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
175 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
176 1.1 pk
177 1.1 pk /* The order of reading writing the clock elements is important */
178 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICSEC, 0);
179 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IHOUR, dt.dt_hour);
180 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IMIN, dt.dt_min);
181 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ISEC, dt.dt_sec);
182 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IMON, dt.dt_mon);
183 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IDAY, dt.dt_day);
184 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IYEAR, year);
185 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IDOW, dt.dt_wday);
186 1.1 pk
187 1.1 pk /* Done writing (time wears on) */
188 1.1 pk cmd = intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
189 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
190 1.1 pk splx(s);
191 1.1 pk
192 1.1 pk return (0);
193 1.1 pk }
194 1.1 pk
195 1.1 pk int
196 1.1 pk intersil7170_getcal(handle, vp)
197 1.1 pk todr_chip_handle_t handle;
198 1.1 pk int *vp;
199 1.1 pk {
200 1.1 pk return (EOPNOTSUPP);
201 1.1 pk }
202 1.1 pk
203 1.1 pk int
204 1.1 pk intersil7170_setcal(handle, v)
205 1.1 pk todr_chip_handle_t handle;
206 1.1 pk int v;
207 1.1 pk {
208 1.1 pk return (EOPNOTSUPP);
209 1.1 pk }
210