dsrtc.c revision 1.1 1 1.1 chris /* $NetBSD: dsrtc.c,v 1.1 2002/02/10 12:26:01 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.1 chris
39 1.1 chris #include <sys/param.h>
40 1.1 chris #include <sys/systm.h>
41 1.1 chris #include <sys/kernel.h>
42 1.1 chris #include <sys/conf.h>
43 1.1 chris #include <sys/device.h>
44 1.1 chris
45 1.1 chris #include <machine/rtc.h>
46 1.1 chris
47 1.1 chris #include <arm/footbridge/todclockvar.h>
48 1.1 chris #include <arm/footbridge/isa/ds1687reg.h>
49 1.1 chris
50 1.1 chris #include <dev/isa/isavar.h>
51 1.1 chris
52 1.1 chris #define NRTC_PORTS 2
53 1.1 chris
54 1.1 chris struct dsrtc_softc {
55 1.1 chris struct device sc_dev;
56 1.1 chris bus_space_tag_t sc_iot;
57 1.1 chris bus_space_handle_t sc_ioh;
58 1.1 chris };
59 1.1 chris
60 1.1 chris void dsrtcattach __P((struct device *parent, struct device *self, void *aux));
61 1.1 chris int dsrtcmatch __P((struct device *parent, struct cfdata *cf, void *aux));
62 1.1 chris int ds1687_read __P((struct dsrtc_softc *sc, int addr));
63 1.1 chris void ds1687_write __P((struct dsrtc_softc *sc, int addr, int data));
64 1.1 chris int ds1687_ram_read __P((struct dsrtc_softc *sc, int addr));
65 1.1 chris void ds1687_ram_write __P((struct dsrtc_softc *sc, int addr, int data));
66 1.1 chris static void ds1687_bank_select __P((struct dsrtc_softc *, int));
67 1.1 chris static int dsrtc_write __P((void *, rtc_t *));
68 1.1 chris static int dsrtc_read __P((void *, rtc_t *));
69 1.1 chris
70 1.1 chris int
71 1.1 chris ds1687_read(sc, addr)
72 1.1 chris struct dsrtc_softc *sc;
73 1.1 chris int addr;
74 1.1 chris {
75 1.1 chris
76 1.1 chris bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
77 1.1 chris return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG));
78 1.1 chris }
79 1.1 chris
80 1.1 chris void
81 1.1 chris ds1687_write(sc, addr, data)
82 1.1 chris struct dsrtc_softc *sc;
83 1.1 chris int addr;
84 1.1 chris int data;
85 1.1 chris {
86 1.1 chris
87 1.1 chris bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
88 1.1 chris bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG, data);
89 1.1 chris }
90 1.1 chris
91 1.1 chris static void
92 1.1 chris ds1687_bank_select(sc, bank)
93 1.1 chris struct dsrtc_softc *sc;
94 1.1 chris int bank;
95 1.1 chris {
96 1.1 chris int data;
97 1.1 chris
98 1.1 chris data = ds1687_read(sc, RTC_REG_A);
99 1.1 chris data &= ~RTC_REG_A_BANK_MASK;
100 1.1 chris if (bank)
101 1.1 chris data |= RTC_REG_A_BANK1;
102 1.1 chris ds1687_write(sc, RTC_REG_A, data);
103 1.1 chris }
104 1.1 chris
105 1.1 chris #if 0
106 1.1 chris /* Nothing uses these yet */
107 1.1 chris int
108 1.1 chris ds1687_ram_read(sc, addr)
109 1.1 chris struct dsrtc_softc *sc;
110 1.1 chris int addr;
111 1.1 chris {
112 1.1 chris if (addr < RTC_PC_RAM_SIZE)
113 1.1 chris return(ds1687_read(sc, RTC_PC_RAM_START + addr));
114 1.1 chris
115 1.1 chris addr -= RTC_PC_RAM_SIZE;
116 1.1 chris if (addr < RTC_BANK0_RAM_SIZE)
117 1.1 chris return(ds1687_read(sc, RTC_BANK0_RAM_START + addr));
118 1.1 chris
119 1.1 chris addr -= RTC_BANK0_RAM_SIZE;
120 1.1 chris if (addr < RTC_EXT_RAM_SIZE) {
121 1.1 chris int data;
122 1.1 chris
123 1.1 chris ds1687_bank_select(sc, 1);
124 1.1 chris ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
125 1.1 chris data = ds1687_read(sc, RTC_EXT_RAM_DATA);
126 1.1 chris ds1687_bank_select(sc, 0);
127 1.1 chris return(data);
128 1.1 chris }
129 1.1 chris return(-1);
130 1.1 chris }
131 1.1 chris
132 1.1 chris void
133 1.1 chris ds1687_ram_write(sc, addr, val)
134 1.1 chris struct dsrtc_softc *sc;
135 1.1 chris int addr;
136 1.1 chris int val;
137 1.1 chris {
138 1.1 chris if (addr < RTC_PC_RAM_SIZE)
139 1.1 chris return(ds1687_write(sc, RTC_PC_RAM_START + addr, val));
140 1.1 chris
141 1.1 chris addr -= RTC_PC_RAM_SIZE;
142 1.1 chris if (addr < RTC_BANK0_RAM_SIZE)
143 1.1 chris return(ds1687_write(sc, RTC_BANK0_RAM_START + addr, val));
144 1.1 chris
145 1.1 chris addr -= RTC_BANK0_RAM_SIZE;
146 1.1 chris if (addr < RTC_EXT_RAM_SIZE) {
147 1.1 chris ds1687_bank_select(sc, 1);
148 1.1 chris ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
149 1.1 chris ds1687_write(sc, RTC_EXT_RAM_DATA, val);
150 1.1 chris ds1687_bank_select(sc, 0);
151 1.1 chris }
152 1.1 chris }
153 1.1 chris #endif
154 1.1 chris
155 1.1 chris static int
156 1.1 chris dsrtc_write(arg, rtc)
157 1.1 chris void *arg;
158 1.1 chris rtc_t *rtc;
159 1.1 chris {
160 1.1 chris struct dsrtc_softc *sc = arg;
161 1.1 chris
162 1.1 chris ds1687_write(sc, RTC_SECONDS, rtc->rtc_sec);
163 1.1 chris ds1687_write(sc, RTC_MINUTES, rtc->rtc_min);
164 1.1 chris ds1687_write(sc, RTC_HOURS, rtc->rtc_hour);
165 1.1 chris ds1687_write(sc, RTC_DAYOFMONTH, rtc->rtc_day);
166 1.1 chris ds1687_write(sc, RTC_MONTH, rtc->rtc_mon);
167 1.1 chris ds1687_write(sc, RTC_YEAR, rtc->rtc_year);
168 1.1 chris ds1687_bank_select(sc, 1);
169 1.1 chris ds1687_write(sc, RTC_CENTURY, rtc->rtc_cen);
170 1.1 chris ds1687_bank_select(sc, 0);
171 1.1 chris return(1);
172 1.1 chris }
173 1.1 chris
174 1.1 chris static int
175 1.1 chris dsrtc_read(arg, rtc)
176 1.1 chris void *arg;
177 1.1 chris rtc_t *rtc;
178 1.1 chris {
179 1.1 chris struct dsrtc_softc *sc = arg;
180 1.1 chris
181 1.1 chris rtc->rtc_micro = 0;
182 1.1 chris rtc->rtc_centi = 0;
183 1.1 chris rtc->rtc_sec = ds1687_read(sc, RTC_SECONDS);
184 1.1 chris rtc->rtc_min = ds1687_read(sc, RTC_MINUTES);
185 1.1 chris rtc->rtc_hour = ds1687_read(sc, RTC_HOURS);
186 1.1 chris rtc->rtc_day = ds1687_read(sc, RTC_DAYOFMONTH);
187 1.1 chris rtc->rtc_mon = ds1687_read(sc, RTC_MONTH);
188 1.1 chris rtc->rtc_year = ds1687_read(sc, RTC_YEAR);
189 1.1 chris ds1687_bank_select(sc, 1);
190 1.1 chris rtc->rtc_cen = ds1687_read(sc, RTC_CENTURY);
191 1.1 chris ds1687_bank_select(sc, 0);
192 1.1 chris
193 1.1 chris return(1);
194 1.1 chris }
195 1.1 chris
196 1.1 chris /* device and attach structures */
197 1.1 chris struct cfattach dsrtc_ca = {
198 1.1 chris sizeof(struct dsrtc_softc), dsrtcmatch, dsrtcattach
199 1.1 chris };
200 1.1 chris
201 1.1 chris /*
202 1.1 chris * dsrtcmatch()
203 1.1 chris *
204 1.1 chris * Validate the IIC address to make sure its an RTC we understand
205 1.1 chris */
206 1.1 chris
207 1.1 chris int
208 1.1 chris dsrtcmatch(parent, cf, aux)
209 1.1 chris struct device *parent;
210 1.1 chris struct cfdata *cf;
211 1.1 chris void *aux;
212 1.1 chris {
213 1.1 chris struct isa_attach_args *ia = aux;
214 1.1 chris
215 1.1 chris if (ia->ia_nio < 1 ||
216 1.1 chris ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
217 1.1 chris return (0);
218 1.1 chris
219 1.1 chris ia->ia_nio = 1;
220 1.1 chris ia->ia_io[0].ir_size = NRTC_PORTS;
221 1.1 chris
222 1.1 chris ia->ia_niomem = 0;
223 1.1 chris ia->ia_nirq = 0;
224 1.1 chris ia->ia_ndrq = 0;
225 1.1 chris
226 1.1 chris return(1);
227 1.1 chris }
228 1.1 chris
229 1.1 chris /*
230 1.1 chris * dsrtcattach()
231 1.1 chris *
232 1.1 chris * Attach the rtc device
233 1.1 chris */
234 1.1 chris
235 1.1 chris void
236 1.1 chris dsrtcattach(parent, self, aux)
237 1.1 chris struct device *parent;
238 1.1 chris struct device *self;
239 1.1 chris void *aux;
240 1.1 chris {
241 1.1 chris struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
242 1.1 chris struct isa_attach_args *ia = aux;
243 1.1 chris struct todclock_attach_args ta;
244 1.1 chris
245 1.1 chris sc->sc_iot = ia->ia_iot;
246 1.1 chris if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
247 1.1 chris ia->ia_io[0].ir_size, 0, &sc->sc_ioh)) {
248 1.1 chris printf(": cannot map I/O space\n");
249 1.1 chris return;
250 1.1 chris }
251 1.1 chris
252 1.1 chris ds1687_write(sc, RTC_REG_A, RTC_REG_A_DV1);
253 1.1 chris ds1687_write(sc, RTC_REG_B, RTC_REG_B_BINARY | RTC_REG_B_24_HOUR);
254 1.1 chris
255 1.1 chris if (!(ds1687_read(sc, RTC_REG_D) & RTC_REG_D_VRT))
256 1.1 chris printf(": lithium cell is dead, RTC unreliable");
257 1.1 chris printf("\n");
258 1.1 chris
259 1.1 chris ta.ta_name = "todclock";
260 1.1 chris ta.ta_rtc_arg = sc;
261 1.1 chris ta.ta_rtc_write = dsrtc_write;
262 1.1 chris ta.ta_rtc_read = dsrtc_read;
263 1.1 chris ta.ta_flags = 0;
264 1.1 chris config_found(self, &ta, NULL);
265 1.1 chris }
266 1.1 chris
267 1.1 chris /* End of dsrtc.c */
268