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