pxa2x0_rtc.c revision 1.1 1 1.1 nonaka /* $NetBSD: pxa2x0_rtc.c,v 1.1 2007/02/25 13:46:40 nonaka Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*
4 1.1 nonaka * Copyright (c) 2007 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1 nonaka *
6 1.1 nonaka * Redistribution and use in source and binary forms, with or without
7 1.1 nonaka * modification, are permitted provided that the following conditions
8 1.1 nonaka * are met:
9 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
10 1.1 nonaka * notice, this list of conditions and the following disclaimer.
11 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
13 1.1 nonaka * documentation and/or other materials provided with the distribution.
14 1.1 nonaka *
15 1.1 nonaka * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 1.1 nonaka * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 1.1 nonaka * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 1.1 nonaka * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 1.1 nonaka * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 1.1 nonaka * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 1.1 nonaka * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 1.1 nonaka */
23 1.1 nonaka
24 1.1 nonaka #include <sys/cdefs.h>
25 1.1 nonaka __KERNEL_RCSID(0, "$NetBSD: pxa2x0_rtc.c,v 1.1 2007/02/25 13:46:40 nonaka Exp $");
26 1.1 nonaka
27 1.1 nonaka #include <sys/param.h>
28 1.1 nonaka #include <sys/systm.h>
29 1.1 nonaka #include <sys/device.h>
30 1.1 nonaka #include <sys/kernel.h>
31 1.1 nonaka
32 1.1 nonaka #include <dev/clock_subr.h>
33 1.1 nonaka
34 1.1 nonaka #include <machine/bus.h>
35 1.1 nonaka
36 1.1 nonaka #include <arm/xscale/pxa2x0cpu.h>
37 1.1 nonaka #include <arm/xscale/pxa2x0reg.h>
38 1.1 nonaka #include <arm/xscale/pxa2x0var.h>
39 1.1 nonaka
40 1.1 nonaka #ifdef PXARTC_DEBUG
41 1.1 nonaka #define DPRINTF(s) printf s
42 1.1 nonaka #else
43 1.1 nonaka #define DPRINTF(s)
44 1.1 nonaka #endif
45 1.1 nonaka
46 1.1 nonaka struct pxartc_softc {
47 1.1 nonaka struct device sc_dev;
48 1.1 nonaka bus_space_tag_t sc_iot;
49 1.1 nonaka bus_space_handle_t sc_ioh;
50 1.1 nonaka
51 1.1 nonaka struct todr_chip_handle sc_todr;
52 1.1 nonaka
53 1.1 nonaka int sc_flags;
54 1.1 nonaka #define FLAG_WRISTWATCH (1 << 0)
55 1.1 nonaka };
56 1.1 nonaka
57 1.1 nonaka static int pxartc_match(struct device *, struct cfdata *, void *);
58 1.1 nonaka static void pxartc_attach(struct device *, struct device *, void *);
59 1.1 nonaka
60 1.1 nonaka CFATTACH_DECL(pxartc, sizeof(struct pxartc_softc),
61 1.1 nonaka pxartc_match, pxartc_attach, NULL, NULL);
62 1.1 nonaka
63 1.1 nonaka /* todr(9) interface */
64 1.1 nonaka static int pxartc_todr_gettime(todr_chip_handle_t, volatile struct timeval *);
65 1.1 nonaka static int pxartc_todr_settime(todr_chip_handle_t, volatile struct timeval *);
66 1.1 nonaka
67 1.1 nonaka static int pxartc_wristwatch_read(struct pxartc_softc *,struct clock_ymdhms *);
68 1.1 nonaka static int pxartc_wristwatch_write(struct pxartc_softc *,struct clock_ymdhms *);
69 1.1 nonaka
70 1.1 nonaka static int
71 1.1 nonaka pxartc_match(struct device *parent, struct cfdata *cf, void *aux)
72 1.1 nonaka {
73 1.1 nonaka struct pxaip_attach_args *pxa = aux;
74 1.1 nonaka
75 1.1 nonaka if (pxa->pxa_size == PXA270_RTC_SIZE) {
76 1.1 nonaka if (!CPU_IS_PXA270) {
77 1.1 nonaka return 0;
78 1.1 nonaka }
79 1.1 nonaka } else {
80 1.1 nonaka pxa->pxa_size = PXA250_RTC_SIZE;
81 1.1 nonaka }
82 1.1 nonaka
83 1.1 nonaka return 1;
84 1.1 nonaka }
85 1.1 nonaka
86 1.1 nonaka static void
87 1.1 nonaka pxartc_attach(struct device *parent, struct device *self, void *aux)
88 1.1 nonaka {
89 1.1 nonaka struct pxartc_softc *sc = (struct pxartc_softc *)self;
90 1.1 nonaka struct pxaip_attach_args *pxa = aux;
91 1.1 nonaka
92 1.1 nonaka sc->sc_iot = pxa->pxa_iot;
93 1.1 nonaka
94 1.1 nonaka aprint_normal(": PXA2x0 Real-time Clock\n");
95 1.1 nonaka
96 1.1 nonaka if (bus_space_map(sc->sc_iot, PXA2X0_RTC_BASE, pxa->pxa_size, 0,
97 1.1 nonaka &sc->sc_ioh)) {
98 1.1 nonaka aprint_error("%s: couldn't map registers\n",
99 1.1 nonaka sc->sc_dev.dv_xname);
100 1.1 nonaka return;
101 1.1 nonaka }
102 1.1 nonaka
103 1.1 nonaka if (pxa->pxa_size == PXA270_RTC_SIZE) {
104 1.1 nonaka aprint_normal("%s: using wristwatch register\n",
105 1.1 nonaka sc->sc_dev.dv_xname);
106 1.1 nonaka sc->sc_flags |= FLAG_WRISTWATCH;
107 1.1 nonaka }
108 1.1 nonaka
109 1.1 nonaka sc->sc_todr.cookie = sc;
110 1.1 nonaka sc->sc_todr.todr_gettime = pxartc_todr_gettime;
111 1.1 nonaka sc->sc_todr.todr_settime = pxartc_todr_settime;
112 1.1 nonaka sc->sc_todr.todr_setwen = NULL;
113 1.1 nonaka
114 1.1 nonaka todr_attach(&sc->sc_todr);
115 1.1 nonaka }
116 1.1 nonaka
117 1.1 nonaka static int
118 1.1 nonaka pxartc_todr_gettime(todr_chip_handle_t ch, volatile struct timeval *tv)
119 1.1 nonaka {
120 1.1 nonaka struct pxartc_softc *sc = ch->cookie;
121 1.1 nonaka struct clock_ymdhms dt;
122 1.1 nonaka
123 1.1 nonaka if ((sc->sc_flags & FLAG_WRISTWATCH) == 0) {
124 1.1 nonaka tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RCNR);
125 1.1 nonaka tv->tv_usec = 0;
126 1.1 nonaka DPRINTF(("%s: RCNR = %08lx\n", sc->sc_dev.dv_xname,tv->tv_sec));
127 1.1 nonaka #ifdef PXARTC_DEBUG
128 1.1 nonaka clock_secs_to_ymdhms(tv->tv_sec, &dt);
129 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n",
130 1.1 nonaka sc->sc_dev.dv_xname,
131 1.1 nonaka dt.dt_year, dt.dt_mon, dt.dt_day,
132 1.1 nonaka dt.dt_hour, dt.dt_min, dt.dt_sec));
133 1.1 nonaka #endif
134 1.1 nonaka return 0;
135 1.1 nonaka }
136 1.1 nonaka
137 1.1 nonaka memset(&dt, 0, sizeof(dt));
138 1.1 nonaka
139 1.1 nonaka if (pxartc_wristwatch_read(sc, &dt) == 0)
140 1.1 nonaka return -1;
141 1.1 nonaka
142 1.1 nonaka tv->tv_sec = clock_ymdhms_to_secs(&dt);
143 1.1 nonaka tv->tv_usec = 0;
144 1.1 nonaka return 0;
145 1.1 nonaka }
146 1.1 nonaka
147 1.1 nonaka static int
148 1.1 nonaka pxartc_todr_settime(todr_chip_handle_t ch, volatile struct timeval *tv)
149 1.1 nonaka {
150 1.1 nonaka struct pxartc_softc *sc = ch->cookie;
151 1.1 nonaka struct clock_ymdhms dt;
152 1.1 nonaka
153 1.1 nonaka if ((sc->sc_flags & FLAG_WRISTWATCH) == 0) {
154 1.1 nonaka #ifdef PXARTC_DEBUG
155 1.1 nonaka DPRINTF(("%s: RCNR = %08lx\n", sc->sc_dev.dv_xname,tv->tv_sec));
156 1.1 nonaka clock_secs_to_ymdhms(tv->tv_sec, &dt);
157 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n",
158 1.1 nonaka sc->sc_dev.dv_xname,
159 1.1 nonaka dt.dt_year, dt.dt_mon, dt.dt_day,
160 1.1 nonaka dt.dt_hour, dt.dt_min, dt.dt_sec));
161 1.1 nonaka #endif
162 1.1 nonaka bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_RCNR, tv->tv_sec);
163 1.1 nonaka #ifdef PXARTC_DEBUG
164 1.1 nonaka {
165 1.1 nonaka uint32_t cntr;
166 1.1 nonaka delay(1);
167 1.1 nonaka cntr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RCNR);
168 1.1 nonaka DPRINTF(("%s: new RCNR = %08x\n", sc->sc_dev.dv_xname, cntr));
169 1.1 nonaka clock_secs_to_ymdhms(cntr, &dt);
170 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n",
171 1.1 nonaka sc->sc_dev.dv_xname,
172 1.1 nonaka dt.dt_year, dt.dt_mon, dt.dt_day,
173 1.1 nonaka dt.dt_hour, dt.dt_min, dt.dt_sec));
174 1.1 nonaka }
175 1.1 nonaka #endif
176 1.1 nonaka return 0;
177 1.1 nonaka }
178 1.1 nonaka
179 1.1 nonaka clock_secs_to_ymdhms(tv->tv_sec, &dt);
180 1.1 nonaka
181 1.1 nonaka if (pxartc_wristwatch_write(sc, &dt) == 0)
182 1.1 nonaka return -1;
183 1.1 nonaka return 0;
184 1.1 nonaka }
185 1.1 nonaka
186 1.1 nonaka static int
187 1.1 nonaka pxartc_wristwatch_read(struct pxartc_softc *sc, struct clock_ymdhms *dt)
188 1.1 nonaka {
189 1.1 nonaka uint32_t dayr, yearr;
190 1.1 nonaka int s;
191 1.1 nonaka
192 1.1 nonaka DPRINTF(("%s: pxartc_wristwatch_read()\n", sc->sc_dev.dv_xname));
193 1.1 nonaka
194 1.1 nonaka s = splhigh();
195 1.1 nonaka dayr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RDCR);
196 1.1 nonaka yearr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RYCR);
197 1.1 nonaka splx(s);
198 1.1 nonaka
199 1.1 nonaka DPRINTF(("%s: RDCR = %08x, RYCR = %08x\n", sc->sc_dev.dv_xname,
200 1.1 nonaka dayr, yearr));
201 1.1 nonaka
202 1.1 nonaka dt->dt_sec = (dayr >> RDCR_SECOND_SHIFT) & RDCR_SECOND_MASK;
203 1.1 nonaka dt->dt_min = (dayr >> RDCR_MINUTE_SHIFT) & RDCR_MINUTE_MASK;
204 1.1 nonaka dt->dt_hour = (dayr >> RDCR_HOUR_SHIFT) & RDCR_HOUR_MASK;
205 1.1 nonaka dt->dt_day = (yearr >> RYCR_DOM_SHIFT) & RYCR_DOM_MASK;
206 1.1 nonaka dt->dt_mon = (yearr >> RYCR_MONTH_SHIFT) & RYCR_MONTH_MASK;
207 1.1 nonaka dt->dt_year = (yearr >> RYCR_YEAR_SHIFT) & RYCR_YEAR_MASK;
208 1.1 nonaka
209 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n", sc->sc_dev.dv_xname,
210 1.1 nonaka dt->dt_year, dt->dt_mon, dt->dt_day,
211 1.1 nonaka dt->dt_hour, dt->dt_min, dt->dt_sec));
212 1.1 nonaka
213 1.1 nonaka return 1;
214 1.1 nonaka }
215 1.1 nonaka
216 1.1 nonaka static int
217 1.1 nonaka pxartc_wristwatch_write(struct pxartc_softc *sc, struct clock_ymdhms *dt)
218 1.1 nonaka {
219 1.1 nonaka uint32_t dayr, yearr;
220 1.1 nonaka uint32_t wom; /* week of month: 1=first week of month */
221 1.1 nonaka int s;
222 1.1 nonaka
223 1.1 nonaka DPRINTF(("%s: pxartc_wristwatch_write()\n", sc->sc_dev.dv_xname));
224 1.1 nonaka
225 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n", sc->sc_dev.dv_xname,
226 1.1 nonaka dt->dt_year, dt->dt_mon, dt->dt_day,
227 1.1 nonaka dt->dt_hour, dt->dt_min, dt->dt_sec));
228 1.1 nonaka
229 1.1 nonaka dayr = (dt->dt_sec & RDCR_SECOND_MASK) << RDCR_SECOND_SHIFT;
230 1.1 nonaka dayr |= (dt->dt_min & RDCR_MINUTE_MASK) << RDCR_MINUTE_SHIFT;
231 1.1 nonaka dayr |= (dt->dt_hour & RDCR_HOUR_MASK) << RDCR_HOUR_SHIFT;
232 1.1 nonaka dayr |= ((dt->dt_wday + 1) & RDCR_DOW_MASK) << RDCR_DOW_SHIFT;
233 1.1 nonaka wom = ((dt->dt_day - 1 + 6 - dt->dt_wday) / 7) + 1;
234 1.1 nonaka dayr |= (wom & RDCR_WOM_MASK) << RDCR_WOM_SHIFT;
235 1.1 nonaka yearr = (dt->dt_day & RYCR_DOM_MASK) << RYCR_DOM_SHIFT;
236 1.1 nonaka yearr |= (dt->dt_mon & RYCR_MONTH_MASK) << RYCR_MONTH_SHIFT;
237 1.1 nonaka yearr |= (dt->dt_year & RYCR_YEAR_MASK) << RYCR_YEAR_SHIFT;
238 1.1 nonaka
239 1.1 nonaka DPRINTF(("%s: RDCR = %08x, RYCR = %08x\n", sc->sc_dev.dv_xname,
240 1.1 nonaka dayr, yearr));
241 1.1 nonaka
242 1.1 nonaka /*
243 1.1 nonaka * We must write RYCR register before write RDCR register.
244 1.1 nonaka *
245 1.1 nonaka * See PXA270 Processor Family Developer's Manual p.946
246 1.1 nonaka * 21.4.2.3.1 Writing RDCR and RYCR Counter Registers with Valid Data.
247 1.1 nonaka */
248 1.1 nonaka s = splhigh();
249 1.1 nonaka bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_RYCR, yearr);
250 1.1 nonaka bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_RDCR, dayr);
251 1.1 nonaka splx(s);
252 1.1 nonaka
253 1.1 nonaka #ifdef PXARTC_DEBUG
254 1.1 nonaka {
255 1.1 nonaka struct clock_ymdhms dummy;
256 1.1 nonaka pxartc_wristwatch_read(sc, &dummy);
257 1.1 nonaka }
258 1.1 nonaka #endif
259 1.1 nonaka
260 1.1 nonaka return 1;
261 1.1 nonaka }
262