dsrtc.c revision 1.9 1 1.9 gdamore /* $NetBSD: dsrtc.c,v 1.9 2006/09/11 19:14:57 gdamore 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.9 gdamore __KERNEL_RCSID(0, "$NetBSD: dsrtc.c,v 1.9 2006/09/11 19:14:57 gdamore 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.9 gdamore void dsrtcattach(struct device *parent, struct device *self, void *aux);
64 1.9 gdamore int dsrtcmatch(struct device *parent, struct cfdata *cf, void *aux);
65 1.9 gdamore int ds1687_read(struct dsrtc_softc *sc, int addr);
66 1.9 gdamore void ds1687_write(struct dsrtc_softc *sc, int addr, int data);
67 1.9 gdamore #if 0
68 1.9 gdamore int ds1687_ram_read(struct dsrtc_softc *sc, int addr);
69 1.9 gdamore void ds1687_ram_write(struct dsrtc_softc *sc, int addr, int data);
70 1.9 gdamore #endif
71 1.9 gdamore static void ds1687_bank_select(struct dsrtc_softc *, int);
72 1.9 gdamore static int dsrtc_write(void *, rtc_t *);
73 1.9 gdamore static int dsrtc_read(void *, rtc_t *);
74 1.1 chris
75 1.1 chris int
76 1.9 gdamore ds1687_read(struct dsrtc_softc *sc, 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.9 gdamore ds1687_write(struct dsrtc_softc *sc, int addr, 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.9 gdamore ds1687_bank_select(struct dsrtc_softc *sc, int bank)
93 1.1 chris {
94 1.1 chris int data;
95 1.1 chris
96 1.1 chris data = ds1687_read(sc, RTC_REG_A);
97 1.1 chris data &= ~RTC_REG_A_BANK_MASK;
98 1.1 chris if (bank)
99 1.1 chris data |= RTC_REG_A_BANK1;
100 1.1 chris ds1687_write(sc, RTC_REG_A, data);
101 1.1 chris }
102 1.1 chris
103 1.1 chris #if 0
104 1.1 chris /* Nothing uses these yet */
105 1.1 chris int
106 1.9 gdamore ds1687_ram_read(struct dsrtc_softc *sc, int addr)
107 1.1 chris {
108 1.1 chris if (addr < RTC_PC_RAM_SIZE)
109 1.1 chris return(ds1687_read(sc, RTC_PC_RAM_START + addr));
110 1.1 chris
111 1.1 chris addr -= RTC_PC_RAM_SIZE;
112 1.1 chris if (addr < RTC_BANK0_RAM_SIZE)
113 1.1 chris return(ds1687_read(sc, RTC_BANK0_RAM_START + addr));
114 1.1 chris
115 1.1 chris addr -= RTC_BANK0_RAM_SIZE;
116 1.1 chris if (addr < RTC_EXT_RAM_SIZE) {
117 1.1 chris int data;
118 1.1 chris
119 1.1 chris ds1687_bank_select(sc, 1);
120 1.1 chris ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
121 1.1 chris data = ds1687_read(sc, RTC_EXT_RAM_DATA);
122 1.1 chris ds1687_bank_select(sc, 0);
123 1.1 chris return(data);
124 1.1 chris }
125 1.1 chris return(-1);
126 1.1 chris }
127 1.1 chris
128 1.1 chris void
129 1.9 gdamore ds1687_ram_write(struct dsrtc_softc *sc, int addr, int val)
130 1.1 chris {
131 1.1 chris if (addr < RTC_PC_RAM_SIZE)
132 1.1 chris return(ds1687_write(sc, RTC_PC_RAM_START + addr, val));
133 1.1 chris
134 1.1 chris addr -= RTC_PC_RAM_SIZE;
135 1.1 chris if (addr < RTC_BANK0_RAM_SIZE)
136 1.1 chris return(ds1687_write(sc, RTC_BANK0_RAM_START + addr, val));
137 1.1 chris
138 1.1 chris addr -= RTC_BANK0_RAM_SIZE;
139 1.1 chris if (addr < RTC_EXT_RAM_SIZE) {
140 1.1 chris ds1687_bank_select(sc, 1);
141 1.1 chris ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
142 1.1 chris ds1687_write(sc, RTC_EXT_RAM_DATA, val);
143 1.1 chris ds1687_bank_select(sc, 0);
144 1.1 chris }
145 1.1 chris }
146 1.1 chris #endif
147 1.1 chris
148 1.1 chris static int
149 1.9 gdamore dsrtc_write(void *arg, rtc_t *rtc)
150 1.1 chris {
151 1.1 chris struct dsrtc_softc *sc = arg;
152 1.1 chris
153 1.1 chris ds1687_write(sc, RTC_SECONDS, rtc->rtc_sec);
154 1.1 chris ds1687_write(sc, RTC_MINUTES, rtc->rtc_min);
155 1.1 chris ds1687_write(sc, RTC_HOURS, rtc->rtc_hour);
156 1.1 chris ds1687_write(sc, RTC_DAYOFMONTH, rtc->rtc_day);
157 1.1 chris ds1687_write(sc, RTC_MONTH, rtc->rtc_mon);
158 1.1 chris ds1687_write(sc, RTC_YEAR, rtc->rtc_year);
159 1.1 chris ds1687_bank_select(sc, 1);
160 1.1 chris ds1687_write(sc, RTC_CENTURY, rtc->rtc_cen);
161 1.1 chris ds1687_bank_select(sc, 0);
162 1.1 chris return(1);
163 1.1 chris }
164 1.1 chris
165 1.1 chris static int
166 1.9 gdamore dsrtc_read(void *arg, rtc_t *rtc)
167 1.1 chris {
168 1.1 chris struct dsrtc_softc *sc = arg;
169 1.1 chris
170 1.1 chris rtc->rtc_micro = 0;
171 1.1 chris rtc->rtc_centi = 0;
172 1.1 chris rtc->rtc_sec = ds1687_read(sc, RTC_SECONDS);
173 1.1 chris rtc->rtc_min = ds1687_read(sc, RTC_MINUTES);
174 1.1 chris rtc->rtc_hour = ds1687_read(sc, RTC_HOURS);
175 1.1 chris rtc->rtc_day = ds1687_read(sc, RTC_DAYOFMONTH);
176 1.1 chris rtc->rtc_mon = ds1687_read(sc, RTC_MONTH);
177 1.1 chris rtc->rtc_year = ds1687_read(sc, RTC_YEAR);
178 1.1 chris ds1687_bank_select(sc, 1);
179 1.1 chris rtc->rtc_cen = ds1687_read(sc, RTC_CENTURY);
180 1.1 chris ds1687_bank_select(sc, 0);
181 1.1 chris
182 1.1 chris return(1);
183 1.1 chris }
184 1.1 chris
185 1.1 chris /* device and attach structures */
186 1.6 skrll CFATTACH_DECL(ds1687rtc, sizeof(struct dsrtc_softc),
187 1.4 thorpej dsrtcmatch, dsrtcattach, NULL, NULL);
188 1.1 chris
189 1.1 chris /*
190 1.1 chris * dsrtcmatch()
191 1.1 chris *
192 1.1 chris * Validate the IIC address to make sure its an RTC we understand
193 1.1 chris */
194 1.1 chris
195 1.1 chris int
196 1.9 gdamore dsrtcmatch(struct device *parent, struct cfdata *cf, void *aux)
197 1.1 chris {
198 1.1 chris struct isa_attach_args *ia = aux;
199 1.1 chris
200 1.1 chris if (ia->ia_nio < 1 ||
201 1.7 drochner ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
202 1.1 chris return (0);
203 1.1 chris
204 1.1 chris ia->ia_nio = 1;
205 1.1 chris ia->ia_io[0].ir_size = NRTC_PORTS;
206 1.1 chris
207 1.1 chris ia->ia_niomem = 0;
208 1.1 chris ia->ia_nirq = 0;
209 1.1 chris ia->ia_ndrq = 0;
210 1.1 chris
211 1.1 chris return(1);
212 1.1 chris }
213 1.1 chris
214 1.1 chris /*
215 1.1 chris * dsrtcattach()
216 1.1 chris *
217 1.1 chris * Attach the rtc device
218 1.1 chris */
219 1.1 chris
220 1.1 chris void
221 1.9 gdamore dsrtcattach(struct device *parent, struct device *self, void *aux)
222 1.1 chris {
223 1.1 chris struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
224 1.1 chris struct isa_attach_args *ia = aux;
225 1.1 chris struct todclock_attach_args ta;
226 1.1 chris
227 1.1 chris sc->sc_iot = ia->ia_iot;
228 1.1 chris if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
229 1.1 chris ia->ia_io[0].ir_size, 0, &sc->sc_ioh)) {
230 1.1 chris printf(": cannot map I/O space\n");
231 1.1 chris return;
232 1.1 chris }
233 1.1 chris
234 1.1 chris ds1687_write(sc, RTC_REG_A, RTC_REG_A_DV1);
235 1.1 chris ds1687_write(sc, RTC_REG_B, RTC_REG_B_BINARY | RTC_REG_B_24_HOUR);
236 1.1 chris
237 1.1 chris if (!(ds1687_read(sc, RTC_REG_D) & RTC_REG_D_VRT))
238 1.1 chris printf(": lithium cell is dead, RTC unreliable");
239 1.1 chris printf("\n");
240 1.1 chris
241 1.1 chris ta.ta_name = "todclock";
242 1.1 chris ta.ta_rtc_arg = sc;
243 1.1 chris ta.ta_rtc_write = dsrtc_write;
244 1.1 chris ta.ta_rtc_read = dsrtc_read;
245 1.1 chris ta.ta_flags = 0;
246 1.1 chris config_found(self, &ta, NULL);
247 1.1 chris }
248 1.1 chris
249 1.1 chris /* End of dsrtc.c */
250