intersil7170.c revision 1.11.6.1 1 1.11.6.1 mjf /* $NetBSD: intersil7170.c,v 1.11.6.1 2008/06/02 13:23:23 mjf 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 *
18 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
29 1.1 pk */
30 1.1 pk
31 1.1 pk /*
32 1.1 pk * Intersil 7170 time-of-day chip subroutines.
33 1.1 pk */
34 1.2 lukem
35 1.2 lukem #include <sys/cdefs.h>
36 1.11.6.1 mjf __KERNEL_RCSID(0, "$NetBSD: intersil7170.c,v 1.11.6.1 2008/06/02 13:23:23 mjf Exp $");
37 1.1 pk
38 1.1 pk #include <sys/param.h>
39 1.1 pk #include <sys/malloc.h>
40 1.1 pk #include <sys/systm.h>
41 1.8 tsutsui #include <sys/device.h>
42 1.1 pk #include <sys/errno.h>
43 1.1 pk
44 1.9 ad #include <sys/bus.h>
45 1.1 pk #include <dev/clock_subr.h>
46 1.8 tsutsui #include <dev/ic/intersil7170reg.h>
47 1.8 tsutsui #include <dev/ic/intersil7170var.h>
48 1.1 pk
49 1.7 gdamore int intersil7170_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
50 1.7 gdamore int intersil7170_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
51 1.1 pk
52 1.8 tsutsui void
53 1.8 tsutsui intersil7170_attach(struct intersil7170_softc *sc)
54 1.1 pk {
55 1.1 pk todr_chip_handle_t handle;
56 1.1 pk
57 1.10 tsutsui aprint_normal(": intersil7170");
58 1.1 pk
59 1.8 tsutsui handle = &sc->sc_handle;
60 1.8 tsutsui
61 1.8 tsutsui handle->cookie = sc;
62 1.7 gdamore handle->todr_gettime = NULL;
63 1.7 gdamore handle->todr_settime = NULL;
64 1.7 gdamore handle->todr_gettime_ymdhms = intersil7170_gettime_ymdhms;
65 1.7 gdamore handle->todr_settime_ymdhms = intersil7170_settime_ymdhms;
66 1.8 tsutsui handle->todr_setwen = NULL;
67 1.11 tsutsui
68 1.11 tsutsui todr_attach(handle);
69 1.1 pk }
70 1.1 pk
71 1.1 pk /*
72 1.1 pk * Set up the system's time, given a `reasonable' time value.
73 1.1 pk */
74 1.1 pk int
75 1.7 gdamore intersil7170_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
76 1.1 pk {
77 1.8 tsutsui struct intersil7170_softc *sc = handle->cookie;
78 1.8 tsutsui bus_space_tag_t bt = sc->sc_bst;
79 1.8 tsutsui bus_space_handle_t bh = sc->sc_bsh;
80 1.8 tsutsui uint8_t cmd;
81 1.1 pk int year;
82 1.1 pk int s;
83 1.1 pk
84 1.1 pk /* No interrupts while we're fiddling with the chip */
85 1.1 pk s = splhigh();
86 1.1 pk
87 1.1 pk /* Enable read (stop time) */
88 1.8 tsutsui cmd = INTERSIL_COMMAND(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
89 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
90 1.1 pk
91 1.1 pk /* The order of reading out the clock elements is important */
92 1.8 tsutsui (void)bus_space_read_1(bt, bh, INTERSIL_ICSEC); /* not used */
93 1.7 gdamore dt->dt_hour = bus_space_read_1(bt, bh, INTERSIL_IHOUR);
94 1.7 gdamore dt->dt_min = bus_space_read_1(bt, bh, INTERSIL_IMIN);
95 1.7 gdamore dt->dt_sec = bus_space_read_1(bt, bh, INTERSIL_ISEC);
96 1.7 gdamore dt->dt_mon = bus_space_read_1(bt, bh, INTERSIL_IMON);
97 1.7 gdamore dt->dt_day = bus_space_read_1(bt, bh, INTERSIL_IDAY);
98 1.1 pk year = bus_space_read_1(bt, bh, INTERSIL_IYEAR);
99 1.7 gdamore dt->dt_wday = bus_space_read_1(bt, bh, INTERSIL_IDOW);
100 1.1 pk
101 1.1 pk /* Done writing (time wears on) */
102 1.8 tsutsui cmd = INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
103 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
104 1.1 pk splx(s);
105 1.1 pk
106 1.8 tsutsui year += sc->sc_year0;
107 1.8 tsutsui if (year < POSIX_BASE_YEAR &&
108 1.8 tsutsui (sc->sc_flag & INTERSIL7170_NO_CENT_ADJUST) == 0)
109 1.1 pk year += 100;
110 1.1 pk
111 1.7 gdamore dt->dt_year = year;
112 1.1 pk
113 1.8 tsutsui return 0;
114 1.1 pk }
115 1.1 pk
116 1.1 pk /*
117 1.1 pk * Reset the clock based on the current time.
118 1.1 pk */
119 1.1 pk int
120 1.7 gdamore intersil7170_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
121 1.1 pk {
122 1.8 tsutsui struct intersil7170_softc *sc = handle->cookie;
123 1.8 tsutsui bus_space_tag_t bt = sc->sc_bst;
124 1.8 tsutsui bus_space_handle_t bh = sc->sc_bsh;
125 1.8 tsutsui uint8_t cmd;
126 1.1 pk int year;
127 1.1 pk int s;
128 1.1 pk
129 1.8 tsutsui year = dt->dt_year - sc->sc_year0;
130 1.8 tsutsui if (year > 99 && (sc->sc_flag & INTERSIL7170_NO_CENT_ADJUST) == 0)
131 1.1 pk year -= 100;
132 1.1 pk
133 1.1 pk /* No interrupts while we're fiddling with the chip */
134 1.1 pk s = splhigh();
135 1.1 pk
136 1.1 pk /* Enable write (stop time) */
137 1.8 tsutsui cmd = INTERSIL_COMMAND(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
138 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
139 1.1 pk
140 1.1 pk /* The order of reading writing the clock elements is important */
141 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICSEC, 0);
142 1.7 gdamore bus_space_write_1(bt, bh, INTERSIL_IHOUR, dt->dt_hour);
143 1.7 gdamore bus_space_write_1(bt, bh, INTERSIL_IMIN, dt->dt_min);
144 1.7 gdamore bus_space_write_1(bt, bh, INTERSIL_ISEC, dt->dt_sec);
145 1.7 gdamore bus_space_write_1(bt, bh, INTERSIL_IMON, dt->dt_mon);
146 1.7 gdamore bus_space_write_1(bt, bh, INTERSIL_IDAY, dt->dt_day);
147 1.1 pk bus_space_write_1(bt, bh, INTERSIL_IYEAR, year);
148 1.7 gdamore bus_space_write_1(bt, bh, INTERSIL_IDOW, dt->dt_wday);
149 1.1 pk
150 1.1 pk /* Done writing (time wears on) */
151 1.8 tsutsui cmd = INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
152 1.1 pk bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
153 1.1 pk splx(s);
154 1.1 pk
155 1.8 tsutsui return 0;
156 1.1 pk }
157