mcclock_mace.c revision 1.3.4.3 1 1.3.4.3 skrll /* $NetBSD: mcclock_mace.c,v 1.3.4.3 2004/09/18 14:39:49 skrll Exp $ */
2 1.3.4.2 skrll
3 1.3.4.2 skrll /*
4 1.3.4.2 skrll * Copyright (c) 2001 Antti Kantee. All Rights Reserved.
5 1.3.4.2 skrll *
6 1.3.4.2 skrll * Redistribution and use in source and binary forms, with or without
7 1.3.4.2 skrll * modification, are permitted provided that the following conditions
8 1.3.4.2 skrll * are met:
9 1.3.4.2 skrll * 1. Redistributions of source code must retain the above copyright
10 1.3.4.2 skrll * notice, this list of conditions and the following disclaimer.
11 1.3.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
12 1.3.4.2 skrll * notice, this list of conditions and the following disclaimer in the
13 1.3.4.2 skrll * documentation and/or other materials provided with the distribution.
14 1.3.4.2 skrll * 3. All advertising materials mentioning features or use of this software
15 1.3.4.2 skrll * must display the following acknowledgement:
16 1.3.4.2 skrll * This product includes software developed by the NetBSD
17 1.3.4.2 skrll * Foundation, Inc. and its contributors.
18 1.3.4.2 skrll * 4. The name of the company nor the name of the author may be used to
19 1.3.4.2 skrll * endorse or promote products derived from this software without specific
20 1.3.4.2 skrll * prior written permission.
21 1.3.4.2 skrll *
22 1.3.4.2 skrll * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
23 1.3.4.2 skrll * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.3.4.2 skrll * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 1.3.4.2 skrll * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
26 1.3.4.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.3.4.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 1.3.4.2 skrll * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.3.4.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.3.4.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.3.4.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.3.4.2 skrll * SUCH DAMAGE.
33 1.3.4.2 skrll */
34 1.3.4.2 skrll
35 1.3.4.2 skrll /*
36 1.3.4.2 skrll * Copyright (c) 1998 Mark Brinicombe.
37 1.3.4.2 skrll * Copyright (c) 1998 Causality Limited.
38 1.3.4.2 skrll * All rights reserved.
39 1.3.4.2 skrll *
40 1.3.4.2 skrll * Written by Mark Brinicombe, Causality Limited
41 1.3.4.2 skrll *
42 1.3.4.2 skrll * Redistribution and use in source and binary forms, with or without
43 1.3.4.2 skrll * modification, are permitted provided that the following conditions
44 1.3.4.2 skrll * are met:
45 1.3.4.2 skrll * 1. Redistributions of source code must retain the above copyright
46 1.3.4.2 skrll * notice, this list of conditions and the following disclaimer.
47 1.3.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
48 1.3.4.2 skrll * notice, this list of conditions and the following disclaimer in the
49 1.3.4.2 skrll * documentation and/or other materials provided with the distribution.
50 1.3.4.2 skrll * 3. All advertising materials mentioning features or use of this software
51 1.3.4.2 skrll * must display the following acknowledgement:
52 1.3.4.2 skrll * This product includes software developed by Mark Brinicombe
53 1.3.4.2 skrll * for the NetBSD Project.
54 1.3.4.2 skrll * 4. The name of the company nor the name of the author may be used to
55 1.3.4.2 skrll * endorse or promote products derived from this software without specific
56 1.3.4.2 skrll * prior written permission.
57 1.3.4.2 skrll *
58 1.3.4.2 skrll * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
59 1.3.4.2 skrll * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60 1.3.4.2 skrll * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61 1.3.4.2 skrll * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
62 1.3.4.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.3.4.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
64 1.3.4.2 skrll * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.3.4.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.3.4.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.3.4.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.3.4.2 skrll * SUCH DAMAGE.
69 1.3.4.2 skrll */
70 1.3.4.2 skrll
71 1.3.4.2 skrll #include <sys/cdefs.h>
72 1.3.4.3 skrll __KERNEL_RCSID(0, "$NetBSD: mcclock_mace.c,v 1.3.4.3 2004/09/18 14:39:49 skrll Exp $");
73 1.3.4.2 skrll
74 1.3.4.2 skrll #include <sys/param.h>
75 1.3.4.2 skrll #include <sys/systm.h>
76 1.3.4.2 skrll
77 1.3.4.2 skrll #include <machine/cpu.h>
78 1.3.4.2 skrll #include <machine/autoconf.h>
79 1.3.4.2 skrll #include <machine/bus.h>
80 1.3.4.2 skrll #include <machine/machtype.h>
81 1.3.4.2 skrll
82 1.3.4.2 skrll #include <dev/clock_subr.h>
83 1.3.4.2 skrll #include <dev/ic/ds1687reg.h>
84 1.3.4.2 skrll
85 1.3.4.2 skrll #include <sgimips/mace/macevar.h>
86 1.3.4.2 skrll
87 1.3.4.2 skrll #include <sgimips/sgimips/clockvar.h>
88 1.3.4.2 skrll
89 1.3.4.2 skrll struct mcclock_mace_softc {
90 1.3.4.2 skrll struct device sc_dev;
91 1.3.4.2 skrll
92 1.3.4.2 skrll bus_space_tag_t sc_st;
93 1.3.4.2 skrll bus_space_handle_t sc_sh;
94 1.3.4.2 skrll };
95 1.3.4.2 skrll
96 1.3.4.2 skrll static int mcclock_mace_match(struct device *, struct cfdata *, void *);
97 1.3.4.2 skrll static void mcclock_mace_attach(struct device*, struct device *, void *);
98 1.3.4.2 skrll
99 1.3.4.2 skrll static void mcclock_mace_init(struct device *);
100 1.3.4.2 skrll static void mcclock_mace_get(struct device *, struct clock_ymdhms *);
101 1.3.4.2 skrll static void mcclock_mace_set(struct device *, struct clock_ymdhms *);
102 1.3.4.2 skrll
103 1.3.4.2 skrll unsigned int ds1687_read(void *arg, unsigned int addr);
104 1.3.4.2 skrll void ds1687_write(void *arg, unsigned int addr, unsigned int data);
105 1.3.4.2 skrll
106 1.3.4.2 skrll const struct clockfns mcclock_mace_clockfns = {
107 1.3.4.2 skrll mcclock_mace_init, mcclock_mace_get, mcclock_mace_set
108 1.3.4.2 skrll };
109 1.3.4.2 skrll
110 1.3.4.2 skrll CFATTACH_DECL(mcclock_mace, sizeof(struct mcclock_mace_softc),
111 1.3.4.2 skrll mcclock_mace_match, mcclock_mace_attach, NULL, NULL);
112 1.3.4.2 skrll
113 1.3.4.2 skrll static int
114 1.3.4.2 skrll mcclock_mace_match(struct device *parent, struct cfdata *match, void *aux)
115 1.3.4.2 skrll {
116 1.3.4.2 skrll return 1;
117 1.3.4.2 skrll }
118 1.3.4.2 skrll
119 1.3.4.2 skrll void
120 1.3.4.2 skrll mcclock_mace_attach(struct device *parent, struct device *self, void *aux)
121 1.3.4.2 skrll {
122 1.3.4.2 skrll struct mcclock_mace_softc *sc = (void *)self;
123 1.3.4.2 skrll struct mace_attach_args *maa = aux;
124 1.3.4.2 skrll
125 1.3.4.2 skrll sc->sc_st = maa->maa_st;
126 1.3.4.2 skrll /* XXX should be bus_space_map() */
127 1.3.4.2 skrll if (bus_space_subregion(maa->maa_st, maa->maa_sh,
128 1.3.4.2 skrll maa->maa_offset, 0, &sc->sc_sh))
129 1.3.4.2 skrll panic("mcclock_mace_attach: couldn't map");
130 1.3.4.2 skrll
131 1.3.4.2 skrll /*
132 1.3.4.2 skrll * We want a fixed format: 24-hour, BCD data, so just force the
133 1.3.4.2 skrll * RTC to this mode; if there was junk there we'll be able to
134 1.3.4.2 skrll * fix things up later when we discover the time read back is
135 1.3.4.2 skrll * no good.
136 1.3.4.2 skrll */
137 1.3.4.2 skrll ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1);
138 1.3.4.2 skrll ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS);
139 1.3.4.2 skrll
140 1.3.4.2 skrll /* XXXrkb: init kickstart/wakeup stuff */
141 1.3.4.2 skrll
142 1.3.4.2 skrll if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT))
143 1.3.4.2 skrll printf(": lithium cell is dead, RTC unreliable");
144 1.3.4.2 skrll
145 1.3.4.2 skrll printf("\n");
146 1.3.4.2 skrll clockattach(&sc->sc_dev, &mcclock_mace_clockfns);
147 1.3.4.2 skrll }
148 1.3.4.2 skrll
149 1.3.4.2 skrll /*
150 1.3.4.2 skrll * We need to use the following two functions from
151 1.3.4.2 skrll * DS1687_PUT/GETTOD(). Hence the names.
152 1.3.4.2 skrll */
153 1.3.4.2 skrll unsigned int
154 1.3.4.2 skrll ds1687_read(void *arg, unsigned int addr)
155 1.3.4.2 skrll {
156 1.3.4.2 skrll struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
157 1.3.4.2 skrll
158 1.3.4.2 skrll return (bus_space_read_1(sc->sc_st, sc->sc_sh, addr));
159 1.3.4.2 skrll }
160 1.3.4.2 skrll
161 1.3.4.2 skrll void
162 1.3.4.2 skrll ds1687_write(void *arg, unsigned int addr, unsigned int data)
163 1.3.4.2 skrll {
164 1.3.4.2 skrll struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg;
165 1.3.4.2 skrll
166 1.3.4.2 skrll bus_space_write_1(sc->sc_st, sc->sc_sh, addr, data);
167 1.3.4.2 skrll }
168 1.3.4.2 skrll
169 1.3.4.2 skrll static void
170 1.3.4.2 skrll mcclock_mace_init(struct device *dev)
171 1.3.4.2 skrll {
172 1.3.4.2 skrll
173 1.3.4.2 skrll return;
174 1.3.4.2 skrll }
175 1.3.4.2 skrll
176 1.3.4.2 skrll static void
177 1.3.4.2 skrll mcclock_mace_get(struct device *dev, struct clock_ymdhms *dt)
178 1.3.4.2 skrll {
179 1.3.4.2 skrll struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
180 1.3.4.2 skrll ds1687_todregs regs;
181 1.3.4.2 skrll int s;
182 1.3.4.2 skrll
183 1.3.4.2 skrll s = splhigh();
184 1.3.4.2 skrll DS1687_GETTOD(sc, ®s);
185 1.3.4.2 skrll splx(s);
186 1.3.4.2 skrll
187 1.3.4.2 skrll dt->dt_sec = FROMBCD(regs[DS1687_SOFT_SEC]);
188 1.3.4.2 skrll dt->dt_min = FROMBCD(regs[DS1687_SOFT_MIN]);
189 1.3.4.2 skrll dt->dt_hour = FROMBCD(regs[DS1687_SOFT_HOUR]);
190 1.3.4.2 skrll dt->dt_wday = FROMBCD(regs[DS1687_SOFT_DOW]);
191 1.3.4.2 skrll dt->dt_day = FROMBCD(regs[DS1687_SOFT_DOM]);
192 1.3.4.2 skrll dt->dt_mon = FROMBCD(regs[DS1687_SOFT_MONTH]);
193 1.3.4.2 skrll dt->dt_year = FROMBCD(regs[DS1687_SOFT_YEAR]) +
194 1.3.4.2 skrll (100 * FROMBCD(regs[DS1687_SOFT_CENTURY]));
195 1.3.4.2 skrll }
196 1.3.4.2 skrll
197 1.3.4.2 skrll static void
198 1.3.4.2 skrll mcclock_mace_set(struct device *dev, struct clock_ymdhms *dt)
199 1.3.4.2 skrll {
200 1.3.4.2 skrll struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev;
201 1.3.4.2 skrll ds1687_todregs regs;
202 1.3.4.2 skrll int s;
203 1.3.4.2 skrll
204 1.3.4.2 skrll memset(®s, 0, sizeof(regs));
205 1.3.4.2 skrll
206 1.3.4.2 skrll regs[DS1687_SOFT_SEC] = TOBCD(dt->dt_sec);
207 1.3.4.2 skrll regs[DS1687_SOFT_MIN] = TOBCD(dt->dt_min);
208 1.3.4.2 skrll regs[DS1687_SOFT_HOUR] = TOBCD(dt->dt_hour);
209 1.3.4.2 skrll regs[DS1687_SOFT_DOW] = TOBCD(dt->dt_wday);
210 1.3.4.2 skrll regs[DS1687_SOFT_DOM] = TOBCD(dt->dt_day);
211 1.3.4.2 skrll regs[DS1687_SOFT_MONTH] = TOBCD(dt->dt_mon);
212 1.3.4.2 skrll regs[DS1687_SOFT_YEAR] = TOBCD(dt->dt_year % 100);
213 1.3.4.2 skrll regs[DS1687_SOFT_CENTURY] = TOBCD(dt->dt_year / 100);
214 1.3.4.2 skrll s = splhigh();
215 1.3.4.2 skrll DS1687_PUTTOD(sc, ®s);
216 1.3.4.2 skrll splx(s);
217 1.3.4.2 skrll }
218