pxa2x0_rtc.c revision 1.2 1 1.2 kiyohara /* $NetBSD: pxa2x0_rtc.c,v 1.2 2009/08/09 06:12:34 kiyohara 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.2 kiyohara __KERNEL_RCSID(0, "$NetBSD: pxa2x0_rtc.c,v 1.2 2009/08/09 06:12:34 kiyohara 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.2 kiyohara if (strcmp(pxa->pxa_name, cf->cf_name) != 0)
76 1.2 kiyohara return 0;
77 1.1 nonaka
78 1.2 kiyohara pxa->pxa_size = CPU_IS_PXA270 ? PXA270_RTC_SIZE : PXA250_RTC_SIZE;
79 1.1 nonaka return 1;
80 1.1 nonaka }
81 1.1 nonaka
82 1.1 nonaka static void
83 1.1 nonaka pxartc_attach(struct device *parent, struct device *self, void *aux)
84 1.1 nonaka {
85 1.1 nonaka struct pxartc_softc *sc = (struct pxartc_softc *)self;
86 1.1 nonaka struct pxaip_attach_args *pxa = aux;
87 1.1 nonaka
88 1.1 nonaka sc->sc_iot = pxa->pxa_iot;
89 1.1 nonaka
90 1.1 nonaka aprint_normal(": PXA2x0 Real-time Clock\n");
91 1.1 nonaka
92 1.2 kiyohara if (bus_space_map(sc->sc_iot, pxa->pxa_addr, pxa->pxa_size, 0,
93 1.1 nonaka &sc->sc_ioh)) {
94 1.1 nonaka aprint_error("%s: couldn't map registers\n",
95 1.1 nonaka sc->sc_dev.dv_xname);
96 1.1 nonaka return;
97 1.1 nonaka }
98 1.1 nonaka
99 1.1 nonaka if (pxa->pxa_size == PXA270_RTC_SIZE) {
100 1.1 nonaka aprint_normal("%s: using wristwatch register\n",
101 1.1 nonaka sc->sc_dev.dv_xname);
102 1.1 nonaka sc->sc_flags |= FLAG_WRISTWATCH;
103 1.1 nonaka }
104 1.1 nonaka
105 1.1 nonaka sc->sc_todr.cookie = sc;
106 1.1 nonaka sc->sc_todr.todr_gettime = pxartc_todr_gettime;
107 1.1 nonaka sc->sc_todr.todr_settime = pxartc_todr_settime;
108 1.1 nonaka sc->sc_todr.todr_setwen = NULL;
109 1.1 nonaka
110 1.1 nonaka todr_attach(&sc->sc_todr);
111 1.1 nonaka }
112 1.1 nonaka
113 1.1 nonaka static int
114 1.1 nonaka pxartc_todr_gettime(todr_chip_handle_t ch, volatile struct timeval *tv)
115 1.1 nonaka {
116 1.1 nonaka struct pxartc_softc *sc = ch->cookie;
117 1.1 nonaka struct clock_ymdhms dt;
118 1.1 nonaka
119 1.1 nonaka if ((sc->sc_flags & FLAG_WRISTWATCH) == 0) {
120 1.1 nonaka tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RCNR);
121 1.1 nonaka tv->tv_usec = 0;
122 1.1 nonaka DPRINTF(("%s: RCNR = %08lx\n", sc->sc_dev.dv_xname,tv->tv_sec));
123 1.1 nonaka #ifdef PXARTC_DEBUG
124 1.1 nonaka clock_secs_to_ymdhms(tv->tv_sec, &dt);
125 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n",
126 1.1 nonaka sc->sc_dev.dv_xname,
127 1.1 nonaka dt.dt_year, dt.dt_mon, dt.dt_day,
128 1.1 nonaka dt.dt_hour, dt.dt_min, dt.dt_sec));
129 1.1 nonaka #endif
130 1.1 nonaka return 0;
131 1.1 nonaka }
132 1.1 nonaka
133 1.1 nonaka memset(&dt, 0, sizeof(dt));
134 1.1 nonaka
135 1.1 nonaka if (pxartc_wristwatch_read(sc, &dt) == 0)
136 1.1 nonaka return -1;
137 1.1 nonaka
138 1.1 nonaka tv->tv_sec = clock_ymdhms_to_secs(&dt);
139 1.1 nonaka tv->tv_usec = 0;
140 1.1 nonaka return 0;
141 1.1 nonaka }
142 1.1 nonaka
143 1.1 nonaka static int
144 1.1 nonaka pxartc_todr_settime(todr_chip_handle_t ch, volatile struct timeval *tv)
145 1.1 nonaka {
146 1.1 nonaka struct pxartc_softc *sc = ch->cookie;
147 1.1 nonaka struct clock_ymdhms dt;
148 1.1 nonaka
149 1.1 nonaka if ((sc->sc_flags & FLAG_WRISTWATCH) == 0) {
150 1.1 nonaka #ifdef PXARTC_DEBUG
151 1.1 nonaka DPRINTF(("%s: RCNR = %08lx\n", sc->sc_dev.dv_xname,tv->tv_sec));
152 1.1 nonaka clock_secs_to_ymdhms(tv->tv_sec, &dt);
153 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n",
154 1.1 nonaka sc->sc_dev.dv_xname,
155 1.1 nonaka dt.dt_year, dt.dt_mon, dt.dt_day,
156 1.1 nonaka dt.dt_hour, dt.dt_min, dt.dt_sec));
157 1.1 nonaka #endif
158 1.1 nonaka bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_RCNR, tv->tv_sec);
159 1.1 nonaka #ifdef PXARTC_DEBUG
160 1.1 nonaka {
161 1.1 nonaka uint32_t cntr;
162 1.1 nonaka delay(1);
163 1.1 nonaka cntr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RCNR);
164 1.1 nonaka DPRINTF(("%s: new RCNR = %08x\n", sc->sc_dev.dv_xname, cntr));
165 1.1 nonaka clock_secs_to_ymdhms(cntr, &dt);
166 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n",
167 1.1 nonaka sc->sc_dev.dv_xname,
168 1.1 nonaka dt.dt_year, dt.dt_mon, dt.dt_day,
169 1.1 nonaka dt.dt_hour, dt.dt_min, dt.dt_sec));
170 1.1 nonaka }
171 1.1 nonaka #endif
172 1.1 nonaka return 0;
173 1.1 nonaka }
174 1.1 nonaka
175 1.1 nonaka clock_secs_to_ymdhms(tv->tv_sec, &dt);
176 1.1 nonaka
177 1.1 nonaka if (pxartc_wristwatch_write(sc, &dt) == 0)
178 1.1 nonaka return -1;
179 1.1 nonaka return 0;
180 1.1 nonaka }
181 1.1 nonaka
182 1.1 nonaka static int
183 1.1 nonaka pxartc_wristwatch_read(struct pxartc_softc *sc, struct clock_ymdhms *dt)
184 1.1 nonaka {
185 1.1 nonaka uint32_t dayr, yearr;
186 1.1 nonaka int s;
187 1.1 nonaka
188 1.1 nonaka DPRINTF(("%s: pxartc_wristwatch_read()\n", sc->sc_dev.dv_xname));
189 1.1 nonaka
190 1.1 nonaka s = splhigh();
191 1.1 nonaka dayr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RDCR);
192 1.1 nonaka yearr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTC_RYCR);
193 1.1 nonaka splx(s);
194 1.1 nonaka
195 1.1 nonaka DPRINTF(("%s: RDCR = %08x, RYCR = %08x\n", sc->sc_dev.dv_xname,
196 1.1 nonaka dayr, yearr));
197 1.1 nonaka
198 1.1 nonaka dt->dt_sec = (dayr >> RDCR_SECOND_SHIFT) & RDCR_SECOND_MASK;
199 1.1 nonaka dt->dt_min = (dayr >> RDCR_MINUTE_SHIFT) & RDCR_MINUTE_MASK;
200 1.1 nonaka dt->dt_hour = (dayr >> RDCR_HOUR_SHIFT) & RDCR_HOUR_MASK;
201 1.1 nonaka dt->dt_day = (yearr >> RYCR_DOM_SHIFT) & RYCR_DOM_MASK;
202 1.1 nonaka dt->dt_mon = (yearr >> RYCR_MONTH_SHIFT) & RYCR_MONTH_MASK;
203 1.1 nonaka dt->dt_year = (yearr >> RYCR_YEAR_SHIFT) & RYCR_YEAR_MASK;
204 1.1 nonaka
205 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n", sc->sc_dev.dv_xname,
206 1.1 nonaka dt->dt_year, dt->dt_mon, dt->dt_day,
207 1.1 nonaka dt->dt_hour, dt->dt_min, dt->dt_sec));
208 1.1 nonaka
209 1.1 nonaka return 1;
210 1.1 nonaka }
211 1.1 nonaka
212 1.1 nonaka static int
213 1.1 nonaka pxartc_wristwatch_write(struct pxartc_softc *sc, struct clock_ymdhms *dt)
214 1.1 nonaka {
215 1.1 nonaka uint32_t dayr, yearr;
216 1.1 nonaka uint32_t wom; /* week of month: 1=first week of month */
217 1.1 nonaka int s;
218 1.1 nonaka
219 1.1 nonaka DPRINTF(("%s: pxartc_wristwatch_write()\n", sc->sc_dev.dv_xname));
220 1.1 nonaka
221 1.1 nonaka DPRINTF(("%s: %02d/%02d/%02d %02d:%02d:%02d\n", sc->sc_dev.dv_xname,
222 1.1 nonaka dt->dt_year, dt->dt_mon, dt->dt_day,
223 1.1 nonaka dt->dt_hour, dt->dt_min, dt->dt_sec));
224 1.1 nonaka
225 1.1 nonaka dayr = (dt->dt_sec & RDCR_SECOND_MASK) << RDCR_SECOND_SHIFT;
226 1.1 nonaka dayr |= (dt->dt_min & RDCR_MINUTE_MASK) << RDCR_MINUTE_SHIFT;
227 1.1 nonaka dayr |= (dt->dt_hour & RDCR_HOUR_MASK) << RDCR_HOUR_SHIFT;
228 1.1 nonaka dayr |= ((dt->dt_wday + 1) & RDCR_DOW_MASK) << RDCR_DOW_SHIFT;
229 1.1 nonaka wom = ((dt->dt_day - 1 + 6 - dt->dt_wday) / 7) + 1;
230 1.1 nonaka dayr |= (wom & RDCR_WOM_MASK) << RDCR_WOM_SHIFT;
231 1.1 nonaka yearr = (dt->dt_day & RYCR_DOM_MASK) << RYCR_DOM_SHIFT;
232 1.1 nonaka yearr |= (dt->dt_mon & RYCR_MONTH_MASK) << RYCR_MONTH_SHIFT;
233 1.1 nonaka yearr |= (dt->dt_year & RYCR_YEAR_MASK) << RYCR_YEAR_SHIFT;
234 1.1 nonaka
235 1.1 nonaka DPRINTF(("%s: RDCR = %08x, RYCR = %08x\n", sc->sc_dev.dv_xname,
236 1.1 nonaka dayr, yearr));
237 1.1 nonaka
238 1.1 nonaka /*
239 1.1 nonaka * We must write RYCR register before write RDCR register.
240 1.1 nonaka *
241 1.1 nonaka * See PXA270 Processor Family Developer's Manual p.946
242 1.1 nonaka * 21.4.2.3.1 Writing RDCR and RYCR Counter Registers with Valid Data.
243 1.1 nonaka */
244 1.1 nonaka s = splhigh();
245 1.1 nonaka bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_RYCR, yearr);
246 1.1 nonaka bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTC_RDCR, dayr);
247 1.1 nonaka splx(s);
248 1.1 nonaka
249 1.1 nonaka #ifdef PXARTC_DEBUG
250 1.1 nonaka {
251 1.1 nonaka struct clock_ymdhms dummy;
252 1.1 nonaka pxartc_wristwatch_read(sc, &dummy);
253 1.1 nonaka }
254 1.1 nonaka #endif
255 1.1 nonaka
256 1.1 nonaka return 1;
257 1.1 nonaka }
258