dsrtc.c revision 1.5 1 1.5 chris /* $NetBSD: dsrtc.c,v 1.5 2003/03/23 14:12:26 chris Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 1998 Mark Brinicombe.
5 1.1 chris * Copyright (c) 1998 Causality Limited.
6 1.1 chris * All rights reserved.
7 1.1 chris *
8 1.1 chris * Written by Mark Brinicombe, Causality Limited
9 1.1 chris *
10 1.1 chris * Redistribution and use in source and binary forms, with or without
11 1.1 chris * modification, are permitted provided that the following conditions
12 1.1 chris * are met:
13 1.1 chris * 1. Redistributions of source code must retain the above copyright
14 1.1 chris * notice, this list of conditions and the following disclaimer.
15 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 chris * notice, this list of conditions and the following disclaimer in the
17 1.1 chris * documentation and/or other materials provided with the distribution.
18 1.1 chris * 3. All advertising materials mentioning features or use of this software
19 1.1 chris * must display the following acknowledgement:
20 1.1 chris * This product includes software developed by Mark Brinicombe
21 1.1 chris * for the NetBSD Project.
22 1.1 chris * 4. The name of the company nor the name of the author may be used to
23 1.1 chris * endorse or promote products derived from this software without specific
24 1.1 chris * prior written permission.
25 1.1 chris *
26 1.1 chris * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
27 1.1 chris * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 1.1 chris * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 1.1 chris * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
30 1.1 chris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 chris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 chris * SUCH DAMAGE.
37 1.1 chris */
38 1.5 chris
39 1.5 chris #include <sys/cdefs.h>
40 1.5 chris __KERNEL_RCSID(0, "$NetBSD: dsrtc.c,v 1.5 2003/03/23 14:12:26 chris Exp $");
41 1.1 chris
42 1.1 chris #include <sys/param.h>
43 1.1 chris #include <sys/systm.h>
44 1.1 chris #include <sys/kernel.h>
45 1.1 chris #include <sys/conf.h>
46 1.1 chris #include <sys/device.h>
47 1.1 chris
48 1.1 chris #include <machine/rtc.h>
49 1.1 chris
50 1.1 chris #include <arm/footbridge/todclockvar.h>
51 1.1 chris #include <arm/footbridge/isa/ds1687reg.h>
52 1.1 chris
53 1.1 chris #include <dev/isa/isavar.h>
54 1.1 chris
55 1.1 chris #define NRTC_PORTS 2
56 1.1 chris
57 1.1 chris struct dsrtc_softc {
58 1.1 chris struct device sc_dev;
59 1.1 chris bus_space_tag_t sc_iot;
60 1.1 chris bus_space_handle_t sc_ioh;
61 1.1 chris };
62 1.1 chris
63 1.1 chris void dsrtcattach __P((struct device *parent, struct device *self, void *aux));
64 1.1 chris int dsrtcmatch __P((struct device *parent, struct cfdata *cf, void *aux));
65 1.1 chris int ds1687_read __P((struct dsrtc_softc *sc, int addr));
66 1.1 chris void ds1687_write __P((struct dsrtc_softc *sc, int addr, int data));
67 1.1 chris int ds1687_ram_read __P((struct dsrtc_softc *sc, int addr));
68 1.1 chris void ds1687_ram_write __P((struct dsrtc_softc *sc, int addr, int data));
69 1.1 chris static void ds1687_bank_select __P((struct dsrtc_softc *, int));
70 1.1 chris static int dsrtc_write __P((void *, rtc_t *));
71 1.1 chris static int dsrtc_read __P((void *, rtc_t *));
72 1.1 chris
73 1.1 chris int
74 1.1 chris ds1687_read(sc, addr)
75 1.1 chris struct dsrtc_softc *sc;
76 1.1 chris int addr;
77 1.1 chris {
78 1.1 chris
79 1.1 chris bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
80 1.1 chris return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG));
81 1.1 chris }
82 1.1 chris
83 1.1 chris void
84 1.1 chris ds1687_write(sc, addr, data)
85 1.1 chris struct dsrtc_softc *sc;
86 1.1 chris int addr;
87 1.1 chris int data;
88 1.1 chris {
89 1.1 chris
90 1.1 chris bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
91 1.1 chris bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG, data);
92 1.1 chris }
93 1.1 chris
94 1.1 chris static void
95 1.1 chris ds1687_bank_select(sc, bank)
96 1.1 chris struct dsrtc_softc *sc;
97 1.1 chris int bank;
98 1.1 chris {
99 1.1 chris int data;
100 1.1 chris
101 1.1 chris data = ds1687_read(sc, RTC_REG_A);
102 1.1 chris data &= ~RTC_REG_A_BANK_MASK;
103 1.1 chris if (bank)
104 1.1 chris data |= RTC_REG_A_BANK1;
105 1.1 chris ds1687_write(sc, RTC_REG_A, data);
106 1.1 chris }
107 1.1 chris
108 1.1 chris #if 0
109 1.1 chris /* Nothing uses these yet */
110 1.1 chris int
111 1.1 chris ds1687_ram_read(sc, addr)
112 1.1 chris struct dsrtc_softc *sc;
113 1.1 chris int addr;
114 1.1 chris {
115 1.1 chris if (addr < RTC_PC_RAM_SIZE)
116 1.1 chris return(ds1687_read(sc, RTC_PC_RAM_START + addr));
117 1.1 chris
118 1.1 chris addr -= RTC_PC_RAM_SIZE;
119 1.1 chris if (addr < RTC_BANK0_RAM_SIZE)
120 1.1 chris return(ds1687_read(sc, RTC_BANK0_RAM_START + addr));
121 1.1 chris
122 1.1 chris addr -= RTC_BANK0_RAM_SIZE;
123 1.1 chris if (addr < RTC_EXT_RAM_SIZE) {
124 1.1 chris int data;
125 1.1 chris
126 1.1 chris ds1687_bank_select(sc, 1);
127 1.1 chris ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
128 1.1 chris data = ds1687_read(sc, RTC_EXT_RAM_DATA);
129 1.1 chris ds1687_bank_select(sc, 0);
130 1.1 chris return(data);
131 1.1 chris }
132 1.1 chris return(-1);
133 1.1 chris }
134 1.1 chris
135 1.1 chris void
136 1.1 chris ds1687_ram_write(sc, addr, val)
137 1.1 chris struct dsrtc_softc *sc;
138 1.1 chris int addr;
139 1.1 chris int val;
140 1.1 chris {
141 1.1 chris if (addr < RTC_PC_RAM_SIZE)
142 1.1 chris return(ds1687_write(sc, RTC_PC_RAM_START + addr, val));
143 1.1 chris
144 1.1 chris addr -= RTC_PC_RAM_SIZE;
145 1.1 chris if (addr < RTC_BANK0_RAM_SIZE)
146 1.1 chris return(ds1687_write(sc, RTC_BANK0_RAM_START + addr, val));
147 1.1 chris
148 1.1 chris addr -= RTC_BANK0_RAM_SIZE;
149 1.1 chris if (addr < RTC_EXT_RAM_SIZE) {
150 1.1 chris ds1687_bank_select(sc, 1);
151 1.1 chris ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
152 1.1 chris ds1687_write(sc, RTC_EXT_RAM_DATA, val);
153 1.1 chris ds1687_bank_select(sc, 0);
154 1.1 chris }
155 1.1 chris }
156 1.1 chris #endif
157 1.1 chris
158 1.1 chris static int
159 1.1 chris dsrtc_write(arg, rtc)
160 1.1 chris void *arg;
161 1.1 chris rtc_t *rtc;
162 1.1 chris {
163 1.1 chris struct dsrtc_softc *sc = arg;
164 1.1 chris
165 1.1 chris ds1687_write(sc, RTC_SECONDS, rtc->rtc_sec);
166 1.1 chris ds1687_write(sc, RTC_MINUTES, rtc->rtc_min);
167 1.1 chris ds1687_write(sc, RTC_HOURS, rtc->rtc_hour);
168 1.1 chris ds1687_write(sc, RTC_DAYOFMONTH, rtc->rtc_day);
169 1.1 chris ds1687_write(sc, RTC_MONTH, rtc->rtc_mon);
170 1.1 chris ds1687_write(sc, RTC_YEAR, rtc->rtc_year);
171 1.1 chris ds1687_bank_select(sc, 1);
172 1.1 chris ds1687_write(sc, RTC_CENTURY, rtc->rtc_cen);
173 1.1 chris ds1687_bank_select(sc, 0);
174 1.1 chris return(1);
175 1.1 chris }
176 1.1 chris
177 1.1 chris static int
178 1.1 chris dsrtc_read(arg, rtc)
179 1.1 chris void *arg;
180 1.1 chris rtc_t *rtc;
181 1.1 chris {
182 1.1 chris struct dsrtc_softc *sc = arg;
183 1.1 chris
184 1.1 chris rtc->rtc_micro = 0;
185 1.1 chris rtc->rtc_centi = 0;
186 1.1 chris rtc->rtc_sec = ds1687_read(sc, RTC_SECONDS);
187 1.1 chris rtc->rtc_min = ds1687_read(sc, RTC_MINUTES);
188 1.1 chris rtc->rtc_hour = ds1687_read(sc, RTC_HOURS);
189 1.1 chris rtc->rtc_day = ds1687_read(sc, RTC_DAYOFMONTH);
190 1.1 chris rtc->rtc_mon = ds1687_read(sc, RTC_MONTH);
191 1.1 chris rtc->rtc_year = ds1687_read(sc, RTC_YEAR);
192 1.1 chris ds1687_bank_select(sc, 1);
193 1.1 chris rtc->rtc_cen = ds1687_read(sc, RTC_CENTURY);
194 1.1 chris ds1687_bank_select(sc, 0);
195 1.1 chris
196 1.1 chris return(1);
197 1.1 chris }
198 1.1 chris
199 1.1 chris /* device and attach structures */
200 1.3 thorpej CFATTACH_DECL(dsrtc, sizeof(struct dsrtc_softc),
201 1.4 thorpej dsrtcmatch, dsrtcattach, NULL, NULL);
202 1.1 chris
203 1.1 chris /*
204 1.1 chris * dsrtcmatch()
205 1.1 chris *
206 1.1 chris * Validate the IIC address to make sure its an RTC we understand
207 1.1 chris */
208 1.1 chris
209 1.1 chris int
210 1.1 chris dsrtcmatch(parent, cf, aux)
211 1.1 chris struct device *parent;
212 1.1 chris struct cfdata *cf;
213 1.1 chris void *aux;
214 1.1 chris {
215 1.1 chris struct isa_attach_args *ia = aux;
216 1.1 chris
217 1.1 chris if (ia->ia_nio < 1 ||
218 1.1 chris ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
219 1.1 chris return (0);
220 1.1 chris
221 1.1 chris ia->ia_nio = 1;
222 1.1 chris ia->ia_io[0].ir_size = NRTC_PORTS;
223 1.1 chris
224 1.1 chris ia->ia_niomem = 0;
225 1.1 chris ia->ia_nirq = 0;
226 1.1 chris ia->ia_ndrq = 0;
227 1.1 chris
228 1.1 chris return(1);
229 1.1 chris }
230 1.1 chris
231 1.1 chris /*
232 1.1 chris * dsrtcattach()
233 1.1 chris *
234 1.1 chris * Attach the rtc device
235 1.1 chris */
236 1.1 chris
237 1.1 chris void
238 1.1 chris dsrtcattach(parent, self, aux)
239 1.1 chris struct device *parent;
240 1.1 chris struct device *self;
241 1.1 chris void *aux;
242 1.1 chris {
243 1.1 chris struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
244 1.1 chris struct isa_attach_args *ia = aux;
245 1.1 chris struct todclock_attach_args ta;
246 1.1 chris
247 1.1 chris sc->sc_iot = ia->ia_iot;
248 1.1 chris if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
249 1.1 chris ia->ia_io[0].ir_size, 0, &sc->sc_ioh)) {
250 1.1 chris printf(": cannot map I/O space\n");
251 1.1 chris return;
252 1.1 chris }
253 1.1 chris
254 1.1 chris ds1687_write(sc, RTC_REG_A, RTC_REG_A_DV1);
255 1.1 chris ds1687_write(sc, RTC_REG_B, RTC_REG_B_BINARY | RTC_REG_B_24_HOUR);
256 1.1 chris
257 1.1 chris if (!(ds1687_read(sc, RTC_REG_D) & RTC_REG_D_VRT))
258 1.1 chris printf(": lithium cell is dead, RTC unreliable");
259 1.1 chris printf("\n");
260 1.1 chris
261 1.1 chris ta.ta_name = "todclock";
262 1.1 chris ta.ta_rtc_arg = sc;
263 1.1 chris ta.ta_rtc_write = dsrtc_write;
264 1.1 chris ta.ta_rtc_read = dsrtc_read;
265 1.1 chris ta.ta_flags = 0;
266 1.1 chris config_found(self, &ta, NULL);
267 1.1 chris }
268 1.1 chris
269 1.1 chris /* End of dsrtc.c */
270