ds1743.c revision 1.5 1 1.5 scw /* $NetBSD: ds1743.c,v 1.5 2003/10/06 16:04:15 scw Exp $ */
2 1.1 scw
3 1.1 scw /*
4 1.1 scw * Copyright (c) 2001-2002 Wasabi Sysetms, Inc.
5 1.1 scw * Copyright (c) 1998 Mark Brinicombe.
6 1.1 scw * Copyright (c) 1998 Causality Limited.
7 1.1 scw * All rights reserved.
8 1.1 scw *
9 1.1 scw * Written by Mark Brinicombe, Causality Limited
10 1.1 scw *
11 1.1 scw * Redistribution and use in source and binary forms, with or without
12 1.1 scw * modification, are permitted provided that the following conditions
13 1.1 scw * are met:
14 1.1 scw * 1. Redistributions of source code must retain the above copyright
15 1.1 scw * notice, this list of conditions and the following disclaimer.
16 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 scw * notice, this list of conditions and the following disclaimer in the
18 1.1 scw * documentation and/or other materials provided with the distribution.
19 1.1 scw * 3. All advertising materials mentioning features or use of this software
20 1.1 scw * must display the following acknowledgement:
21 1.1 scw * This product includes software developed by Mark Brinicombe
22 1.1 scw * for the NetBSD Project.
23 1.1 scw * 4. The name of the company nor the name of the author may be used to
24 1.1 scw * endorse or promote products derived from this software without specific
25 1.1 scw * prior written permission.
26 1.1 scw *
27 1.1 scw * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
28 1.1 scw * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 1.1 scw * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 1.1 scw * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
31 1.1 scw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 scw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 1.1 scw * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 scw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 scw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 scw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 scw * SUCH DAMAGE.
38 1.1 scw */
39 1.2 lukem
40 1.2 lukem #include <sys/cdefs.h>
41 1.5 scw __KERNEL_RCSID(0, "$NetBSD: ds1743.c,v 1.5 2003/10/06 16:04:15 scw Exp $");
42 1.1 scw
43 1.1 scw #include <sys/param.h>
44 1.1 scw #include <sys/systm.h>
45 1.1 scw #include <sys/device.h>
46 1.1 scw
47 1.1 scw #include <machine/rtc.h>
48 1.1 scw #include <machine/bus.h>
49 1.1 scw
50 1.1 scw #include <evbppc/walnut/dev/ds1743reg.h>
51 1.1 scw #include <evbppc/walnut/dev/todclockvar.h>
52 1.1 scw #include <evbppc/walnut/dev/pbusvar.h>
53 1.1 scw
54 1.1 scw struct dsrtc_softc {
55 1.1 scw struct device sc_dev;
56 1.1 scw bus_space_tag_t sc_iot;
57 1.1 scw bus_space_handle_t sc_ioh;
58 1.1 scw };
59 1.1 scw
60 1.1 scw static void dsrtcattach(struct device *, struct device *, void *);
61 1.1 scw static int dsrtcmatch(struct device *, struct cfdata *, void *);
62 1.1 scw #if 0 /* Nothing uses these yet */
63 1.1 scw static int ds1743_ram_read(struct dsrtc_softc *, int);
64 1.1 scw static void ds1743_ram_write(struct dsrtc_softc *, int, int);
65 1.1 scw #endif
66 1.1 scw
67 1.1 scw static int dsrtc_read(void *, rtc_t *);
68 1.1 scw static int dsrtc_write(void *, rtc_t *);
69 1.1 scw static inline u_char ds1743_read(struct dsrtc_softc *, int);
70 1.1 scw static inline void ds1743_write(struct dsrtc_softc *, int, u_char);
71 1.1 scw static u_char ds1743_lock(struct dsrtc_softc *, u_char);
72 1.1 scw static void ds1743_unlock(struct dsrtc_softc *, u_char);
73 1.1 scw
74 1.1 scw /* device and attach structures */
75 1.5 scw CFATTACH_DECL(ds1743rtc, sizeof(struct dsrtc_softc),
76 1.1 scw dsrtcmatch, dsrtcattach, NULL, NULL);
77 1.1 scw
78 1.1 scw /*
79 1.1 scw * dsrtcmatch()
80 1.1 scw *
81 1.1 scw * Validate the IIC address to make sure its an RTC we understand
82 1.1 scw */
83 1.1 scw int ds1743found = 0;
84 1.1 scw
85 1.1 scw #define DS_SCRATCH_ADDR 0x1FF7
86 1.1 scw
87 1.1 scw static int
88 1.1 scw dsrtcmatch(struct device *parent, struct cfdata *cf, void *aux)
89 1.1 scw {
90 1.1 scw struct pbus_attach_args *paa = aux;
91 1.1 scw int retval = !ds1743found;
92 1.1 scw bus_space_handle_t h;
93 1.1 scw u_int8_t x;
94 1.1 scw
95 1.1 scw /* match only RTC devices */
96 1.1 scw if (strcmp(paa->pb_name, cf->cf_name) != 0)
97 1.1 scw return 0;
98 1.1 scw
99 1.3 scw if (bus_space_map(paa->pb_bt, paa->pb_addr, DS_SIZE, 0, &h)) {
100 1.1 scw printf("%s: can't map i/o space\n", paa->pb_name);
101 1.1 scw return 0;
102 1.1 scw }
103 1.1 scw
104 1.1 scw /* Read one byte of what's supposed to be NVRAM */
105 1.3 scw x = bus_space_read_1(paa->pb_bt, h, DS_SCRATCH_ADDR);
106 1.3 scw bus_space_write_1(paa->pb_bt, h, DS_SCRATCH_ADDR, 0xAA);
107 1.3 scw if (bus_space_read_1(paa->pb_bt, h, DS_SCRATCH_ADDR) != 0xAA) {
108 1.1 scw retval = 0;
109 1.1 scw goto done;
110 1.1 scw }
111 1.1 scw
112 1.3 scw bus_space_write_1(paa->pb_bt, h, DS_SCRATCH_ADDR, 0x55);
113 1.3 scw if (bus_space_read_1(paa->pb_bt, h, DS_SCRATCH_ADDR) != 0x55) {
114 1.1 scw retval = 0;
115 1.1 scw goto done;
116 1.1 scw }
117 1.1 scw
118 1.1 scw /* Restore scratch byte value */
119 1.3 scw bus_space_write_1(paa->pb_bt, h, DS_SCRATCH_ADDR, x);
120 1.1 scw done:
121 1.3 scw bus_space_unmap(paa->pb_bt, h, DS_SIZE);
122 1.1 scw
123 1.1 scw return retval;
124 1.1 scw }
125 1.1 scw
126 1.1 scw /*
127 1.1 scw * dsrtcattach()
128 1.1 scw *
129 1.1 scw * Attach the rtc device
130 1.1 scw */
131 1.1 scw
132 1.1 scw static void
133 1.1 scw dsrtcattach(struct device *parent, struct device *self, void *aux)
134 1.1 scw {
135 1.1 scw struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
136 1.1 scw struct pbus_attach_args *paa = aux;
137 1.1 scw struct todclock_attach_args ta;
138 1.1 scw
139 1.1 scw ds1743found = 1;
140 1.1 scw
141 1.3 scw sc->sc_iot = paa->pb_bt;
142 1.1 scw if (bus_space_map(sc->sc_iot, paa->pb_addr, DS_SIZE, 0, &sc->sc_ioh)) {
143 1.1 scw printf(": can't map i/o space\n");
144 1.1 scw return;
145 1.1 scw }
146 1.1 scw
147 1.4 simonb ds1743_unlock(sc, 0); /* Make sure the clock is running */
148 1.1 scw if ((ds1743_read(sc, DS_DAY) & DS_CTL_BF) == 0)
149 1.1 scw printf(": lithium cell is dead, RTC unreliable");
150 1.1 scw printf("\n");
151 1.1 scw
152 1.1 scw #ifdef DEBUG
153 1.1 scw {
154 1.1 scw rtc_t rtc;
155 1.4 simonb dsrtc_read(sc, &rtc);
156 1.1 scw printf("RTC: %d/%d/%02d%02d %d:%02d:%02d\n",
157 1.1 scw rtc.rtc_mon, rtc.rtc_day, rtc.rtc_cen, rtc.rtc_year,
158 1.1 scw rtc.rtc_hour, rtc.rtc_min, rtc.rtc_sec);
159 1.1 scw }
160 1.1 scw #endif
161 1.1 scw
162 1.1 scw
163 1.1 scw ta.ta_name = "todclock";
164 1.1 scw ta.ta_rtc_arg = sc;
165 1.1 scw ta.ta_rtc_write = dsrtc_write;
166 1.1 scw ta.ta_rtc_read = dsrtc_read;
167 1.1 scw ta.ta_flags = 0;
168 1.1 scw config_found(self, &ta, NULL);
169 1.1 scw }
170 1.1 scw
171 1.1 scw static inline u_char
172 1.1 scw ds1743_read(struct dsrtc_softc *sc, int addr)
173 1.1 scw {
174 1.1 scw
175 1.1 scw return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, addr));
176 1.1 scw }
177 1.1 scw
178 1.1 scw static inline void
179 1.1 scw ds1743_write(struct dsrtc_softc *sc, int addr, u_char data)
180 1.1 scw {
181 1.1 scw
182 1.1 scw bus_space_write_1(sc->sc_iot, sc->sc_ioh, addr, data);
183 1.1 scw }
184 1.1 scw
185 1.1 scw
186 1.1 scw #if 0 /* Nothing uses these yet */
187 1.1 scw static u_char
188 1.1 scw ds1743_ram_read(struct dsrtc_softc *sc, int addr)
189 1.1 scw {
190 1.1 scw
191 1.1 scw if (addr >= DS_RAM_SIZE)
192 1.1 scw return(-1);
193 1.1 scw return(ds1743_read(sc, addr));
194 1.1 scw }
195 1.1 scw
196 1.1 scw static void
197 1.1 scw ds1743_ram_write(struct dsrtc_softc *sc, int addr, u_char val)
198 1.1 scw {
199 1.1 scw
200 1.1 scw if (addr >= DS_RAM_SIZE)
201 1.1 scw return (-1);
202 1.4 simonb ds1743_write(sc, addr, val);
203 1.1 scw }
204 1.1 scw #endif
205 1.1 scw
206 1.4 simonb #define BCD(x) ((((x) / 10) << 4) | (x % 10))
207 1.4 simonb #define unBCD(v, x) v = x; v = ((v >> 4) & 0xf) * 10 + (v & 0xf)
208 1.1 scw
209 1.1 scw static u_char
210 1.1 scw ds1743_lock(struct dsrtc_softc *sc, u_char mode)
211 1.1 scw {
212 1.4 simonb u_char octl, ctl;
213 1.1 scw
214 1.4 simonb octl = ds1743_read(sc, DS_CENTURY);
215 1.1 scw ctl = octl | (mode & DS_CTL_RW);
216 1.4 simonb ds1743_write(sc, DS_CENTURY, ctl); /* Lock RTC for both reading and writing */
217 1.1 scw return octl;
218 1.1 scw }
219 1.1 scw
220 1.1 scw static void
221 1.1 scw ds1743_unlock(struct dsrtc_softc *sc, u_char key)
222 1.1 scw {
223 1.1 scw int ctl;
224 1.1 scw
225 1.4 simonb ctl = ds1743_read(sc, DS_CENTURY);
226 1.1 scw ctl = (ctl & 0x3f) | (key & DS_CTL_RW);
227 1.4 simonb ds1743_write(sc, DS_CENTURY, ctl); /* Enable updates */
228 1.1 scw }
229 1.1 scw
230 1.1 scw static int
231 1.1 scw dsrtc_write(void * arg, rtc_t * rtc)
232 1.1 scw {
233 1.1 scw struct dsrtc_softc *sc = arg;
234 1.1 scw u_char key;
235 1.1 scw
236 1.4 simonb key = ds1743_lock(sc, DS_CTL_W);
237 1.1 scw
238 1.1 scw ds1743_write(sc, DS_SECONDS, BCD(rtc->rtc_sec) & 0x7f);
239 1.1 scw ds1743_write(sc, DS_MINUTES, BCD(rtc->rtc_min) & 0x7f);
240 1.1 scw ds1743_write(sc, DS_HOURS, BCD(rtc->rtc_hour) & 0x3f);
241 1.1 scw ds1743_write(sc, DS_DATE, BCD(rtc->rtc_day) & 0x3f);
242 1.1 scw ds1743_write(sc, DS_MONTH, BCD(rtc->rtc_mon) & 0x1f);
243 1.1 scw ds1743_write(sc, DS_YEAR, BCD(rtc->rtc_year));
244 1.4 simonb ds1743_write(sc, DS_CENTURY, ((ds1743_read(sc, DS_CENTURY) & DS_CTL_RW)
245 1.1 scw | BCD(rtc->rtc_cen)));
246 1.1 scw
247 1.4 simonb ds1743_unlock(sc, key);
248 1.4 simonb dsrtc_read(arg, rtc);
249 1.1 scw return(1);
250 1.1 scw }
251 1.1 scw
252 1.1 scw static int
253 1.1 scw dsrtc_read(void *arg, rtc_t *rtc)
254 1.1 scw {
255 1.1 scw struct dsrtc_softc *sc = arg;
256 1.1 scw u_char key;
257 1.1 scw
258 1.4 simonb key = ds1743_lock(sc, DS_CTL_R);
259 1.1 scw rtc->rtc_micro = 0;
260 1.1 scw rtc->rtc_centi = 0;
261 1.4 simonb unBCD(rtc->rtc_sec, ds1743_read(sc, DS_SECONDS) & 0x7f);
262 1.4 simonb unBCD(rtc->rtc_min, ds1743_read(sc, DS_MINUTES) & 0x7f);
263 1.1 scw unBCD(rtc->rtc_hour, ds1743_read(sc, DS_HOURS) & 0x3f);
264 1.1 scw unBCD(rtc->rtc_day, ds1743_read(sc, DS_DATE) & 0x3f);
265 1.4 simonb unBCD(rtc->rtc_mon, ds1743_read(sc, DS_MONTH) & 0x1f);
266 1.1 scw unBCD(rtc->rtc_year, ds1743_read(sc, DS_YEAR));
267 1.1 scw unBCD(rtc->rtc_cen, ds1743_read(sc, DS_CENTURY) & ~DS_CTL_RW);
268 1.1 scw
269 1.4 simonb ds1743_unlock(sc, key);
270 1.1 scw return(1);
271 1.1 scw }
272