dsclock.c revision 1.1.4.3 1 1.1.4.3 yamt /* $NetBSD: dsclock.c,v 1.1.4.3 2010/03/11 15:02:54 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*
4 1.1.4.2 yamt * Copyright (c) 2001 Rafal K. Boni
5 1.1.4.2 yamt * Copyright (c) 2001 Christopher Sekiya
6 1.1.4.2 yamt * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
7 1.1.4.2 yamt * All rights reserved.
8 1.1.4.2 yamt *
9 1.1.4.2 yamt * Portions of this code are derived from software contributed to The
10 1.1.4.2 yamt * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
11 1.1.4.2 yamt * Simulation Facility, NASA Ames Research Center.
12 1.1.4.2 yamt *
13 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
14 1.1.4.2 yamt * modification, are permitted provided that the following conditions
15 1.1.4.2 yamt * are met:
16 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
17 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
18 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
19 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
20 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
21 1.1.4.2 yamt * 3. The name of the author may not be used to endorse or promote products
22 1.1.4.2 yamt * derived from this software without specific prior written permission.
23 1.1.4.2 yamt *
24 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.1.4.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1.4.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1.4.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1.4.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1.4.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1.4.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1.4.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1.4.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1.4.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1.4.2 yamt */
35 1.1.4.2 yamt
36 1.1.4.2 yamt #include <sys/cdefs.h>
37 1.1.4.3 yamt __KERNEL_RCSID(0, "$NetBSD: dsclock.c,v 1.1.4.3 2010/03/11 15:02:54 yamt Exp $");
38 1.1.4.2 yamt
39 1.1.4.2 yamt #include <sys/param.h>
40 1.1.4.2 yamt #include <sys/kernel.h>
41 1.1.4.2 yamt #include <sys/systm.h>
42 1.1.4.2 yamt #include <sys/device.h>
43 1.1.4.2 yamt
44 1.1.4.2 yamt #include <machine/bus.h>
45 1.1.4.2 yamt #include <machine/autoconf.h>
46 1.1.4.2 yamt #include <machine/sysconf.h>
47 1.1.4.2 yamt #include <machine/machtype.h>
48 1.1.4.2 yamt
49 1.1.4.2 yamt #include <dev/clock_subr.h>
50 1.1.4.2 yamt #include <dev/ic/ds1286reg.h>
51 1.1.4.2 yamt
52 1.1.4.2 yamt #include <sgimips/sgimips/clockvar.h>
53 1.1.4.2 yamt
54 1.1.4.2 yamt struct dsclock_softc {
55 1.1.4.3 yamt device_t sc_dev;
56 1.1.4.2 yamt
57 1.1.4.2 yamt struct todr_chip_handle sc_todrch;
58 1.1.4.2 yamt
59 1.1.4.2 yamt /* RTC registers */
60 1.1.4.2 yamt bus_space_tag_t sc_rtct;
61 1.1.4.2 yamt bus_space_handle_t sc_rtch;
62 1.1.4.2 yamt };
63 1.1.4.2 yamt
64 1.1.4.3 yamt static int dsclock_match(device_t, cfdata_t, void *);
65 1.1.4.3 yamt static void dsclock_attach(device_t, device_t, void *);
66 1.1.4.3 yamt static int dsclock_gettime_ymdhms(struct todr_chip_handle *,
67 1.1.4.3 yamt struct clock_ymdhms *);
68 1.1.4.3 yamt static int dsclock_settime_ymdhms(struct todr_chip_handle *,
69 1.1.4.3 yamt struct clock_ymdhms *);
70 1.1.4.2 yamt
71 1.1.4.3 yamt CFATTACH_DECL_NEW(dsclock, sizeof(struct dsclock_softc),
72 1.1.4.2 yamt dsclock_match, dsclock_attach, NULL, NULL);
73 1.1.4.2 yamt
74 1.1.4.3 yamt #define ds1286_write(sc, reg, datum) \
75 1.1.4.3 yamt bus_space_write_1((sc)->sc_rtct, (sc)->sc_rtch, (reg), (datum))
76 1.1.4.3 yamt #define ds1286_read(sc, reg) \
77 1.1.4.3 yamt bus_space_read_1((sc)->sc_rtct, (sc)->sc_rtch, (reg))
78 1.1.4.2 yamt
79 1.1.4.2 yamt static int
80 1.1.4.3 yamt dsclock_match(device_t parent, cfdata_t cf, void *aux)
81 1.1.4.2 yamt {
82 1.1.4.2 yamt struct mainbus_attach_args *ma = aux;
83 1.1.4.2 yamt
84 1.1.4.2 yamt if (mach_type == MACH_SGI_IP22 && ma->ma_addr == 0x1fbe0000)
85 1.1.4.3 yamt return 1;
86 1.1.4.2 yamt
87 1.1.4.3 yamt return 0;
88 1.1.4.2 yamt }
89 1.1.4.2 yamt
90 1.1.4.2 yamt static void
91 1.1.4.3 yamt dsclock_attach(device_t parent, device_t self, void *aux)
92 1.1.4.2 yamt {
93 1.1.4.3 yamt struct dsclock_softc *sc = device_private(self);
94 1.1.4.2 yamt struct mainbus_attach_args *ma = aux;
95 1.1.4.2 yamt int err;
96 1.1.4.2 yamt
97 1.1.4.3 yamt aprint_normal("\n");
98 1.1.4.2 yamt
99 1.1.4.2 yamt sc->sc_rtct = SGIMIPS_BUS_SPACE_HPC;
100 1.1.4.2 yamt if ((err = bus_space_map(sc->sc_rtct, ma->ma_addr, 0x1ffff,
101 1.1.4.2 yamt BUS_SPACE_MAP_LINEAR, &sc->sc_rtch)) != 0) {
102 1.1.4.3 yamt aprint_error_dev(self,
103 1.1.4.3 yamt "unable to map RTC registers, error = %d\n", err);
104 1.1.4.2 yamt return;
105 1.1.4.2 yamt }
106 1.1.4.2 yamt
107 1.1.4.2 yamt sc->sc_todrch.cookie = sc;
108 1.1.4.3 yamt sc->sc_todrch.todr_gettime_ymdhms = dsclock_gettime_ymdhms;
109 1.1.4.3 yamt sc->sc_todrch.todr_settime_ymdhms = dsclock_settime_ymdhms;
110 1.1.4.2 yamt sc->sc_todrch.todr_setwen = NULL;
111 1.1.4.2 yamt
112 1.1.4.2 yamt todr_attach(&sc->sc_todrch);
113 1.1.4.2 yamt }
114 1.1.4.2 yamt
115 1.1.4.2 yamt /*
116 1.1.4.2 yamt * Get the time of day, based on the clock's value and/or the base value.
117 1.1.4.2 yamt */
118 1.1.4.2 yamt static int
119 1.1.4.3 yamt dsclock_gettime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
120 1.1.4.2 yamt {
121 1.1.4.3 yamt struct dsclock_softc *sc = todrch->cookie;
122 1.1.4.2 yamt ds1286_todregs regs;
123 1.1.4.2 yamt int s;
124 1.1.4.2 yamt
125 1.1.4.2 yamt s = splhigh();
126 1.1.4.2 yamt DS1286_GETTOD(sc, ®s)
127 1.1.4.2 yamt splx(s);
128 1.1.4.2 yamt
129 1.1.4.3 yamt dt->dt_sec = FROMBCD(regs[DS1286_SEC]);
130 1.1.4.3 yamt dt->dt_min = FROMBCD(regs[DS1286_MIN]);
131 1.1.4.2 yamt
132 1.1.4.2 yamt if (regs[DS1286_HOUR] & DS1286_HOUR_12MODE) {
133 1.1.4.3 yamt dt->dt_hour =
134 1.1.4.3 yamt FROMBCD(regs[DS1286_HOUR] & DS1286_HOUR_12HR_MASK)
135 1.1.4.3 yamt + ((regs[DS1286_HOUR] & DS1286_HOUR_12HR_PM) ? 12 : 0);
136 1.1.4.2 yamt
137 1.1.4.2 yamt /*
138 1.1.4.2 yamt * In AM/PM mode, hour range is 01-12, so adding in 12 hours
139 1.1.4.2 yamt * for PM gives us 01-24, whereas we want 00-23, so map hour
140 1.1.4.2 yamt * 24 to hour 0.
141 1.1.4.2 yamt */
142 1.1.4.3 yamt if (dt->dt_hour == 24)
143 1.1.4.3 yamt dt->dt_hour = 0;
144 1.1.4.2 yamt } else {
145 1.1.4.3 yamt dt->dt_hour =
146 1.1.4.3 yamt FROMBCD(regs[DS1286_HOUR] & DS1286_HOUR_24HR_MASK);
147 1.1.4.2 yamt }
148 1.1.4.2 yamt
149 1.1.4.3 yamt dt->dt_wday = FROMBCD(regs[DS1286_DOW]);
150 1.1.4.3 yamt dt->dt_day = FROMBCD(regs[DS1286_DOM]);
151 1.1.4.3 yamt dt->dt_mon = FROMBCD(regs[DS1286_MONTH] & DS1286_MONTH_MASK);
152 1.1.4.3 yamt dt->dt_year = FROM_IRIX_YEAR(FROMBCD(regs[DS1286_YEAR]));
153 1.1.4.2 yamt
154 1.1.4.3 yamt return 0;
155 1.1.4.2 yamt }
156 1.1.4.2 yamt
157 1.1.4.2 yamt /*
158 1.1.4.2 yamt * Reset the TODR based on the time value.
159 1.1.4.2 yamt */
160 1.1.4.2 yamt static int
161 1.1.4.3 yamt dsclock_settime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
162 1.1.4.2 yamt {
163 1.1.4.3 yamt struct dsclock_softc *sc = todrch->cookie;
164 1.1.4.2 yamt ds1286_todregs regs;
165 1.1.4.2 yamt int s;
166 1.1.4.2 yamt
167 1.1.4.2 yamt s = splhigh();
168 1.1.4.2 yamt DS1286_GETTOD(sc, ®s);
169 1.1.4.2 yamt splx(s);
170 1.1.4.2 yamt
171 1.1.4.2 yamt regs[DS1286_SUBSEC] = 0;
172 1.1.4.3 yamt regs[DS1286_SEC] = TOBCD(dt->dt_sec);
173 1.1.4.3 yamt regs[DS1286_MIN] = TOBCD(dt->dt_min);
174 1.1.4.3 yamt regs[DS1286_HOUR] = TOBCD(dt->dt_hour) & DS1286_HOUR_24HR_MASK;
175 1.1.4.3 yamt regs[DS1286_DOW] = TOBCD(dt->dt_wday);
176 1.1.4.3 yamt regs[DS1286_DOM] = TOBCD(dt->dt_day);
177 1.1.4.2 yamt
178 1.1.4.2 yamt /* Leave wave-generator bits as set originally */
179 1.1.4.2 yamt regs[DS1286_MONTH] &= ~DS1286_MONTH_MASK;
180 1.1.4.3 yamt regs[DS1286_MONTH] |= TOBCD(dt->dt_mon) & DS1286_MONTH_MASK;
181 1.1.4.2 yamt
182 1.1.4.3 yamt regs[DS1286_YEAR] = TOBCD(TO_IRIX_YEAR(dt->dt_year));
183 1.1.4.2 yamt
184 1.1.4.2 yamt s = splhigh();
185 1.1.4.2 yamt DS1286_PUTTOD(sc, ®s);
186 1.1.4.2 yamt splx(s);
187 1.1.4.2 yamt
188 1.1.4.3 yamt return 0;
189 1.1.4.2 yamt }
190