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