No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / dreamcast / dev / g2 / g2rtc.c
blob1b8eb96c0d783c1ac7dfa076eb4de51788b90802
1 /* $NetBSD: g2rtc.c,v 1.3 2008/04/28 20:23:16 martin Exp $ */
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: g2rtc.c,v 1.3 2008/04/28 20:23:16 martin Exp $");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
36 #include <dev/clock_subr.h>
38 #include <machine/bus.h>
39 #include <dreamcast/dev/g2/g2busvar.h>
42 #define G2RTC_REG_BASE 0x00710000
43 #define G2RTC_REG_SIZE 12
45 /* Offset by 20 years, 5 of them are leap */
46 #define G2RTC_OFFSET (20 * SECYR + 5 * SECDAY)
48 struct g2rtc_softc {
49 struct device sc_dev;
51 bus_space_tag_t sc_bt;
52 bus_space_handle_t sc_bh;
55 /* autoconf glue */
56 static int g2rtc_match(struct device *, struct cfdata *, void *);
57 static void g2rtc_attach(struct device *, struct device *, void *);
59 CFATTACH_DECL(g2rtc, sizeof(struct g2rtc_softc),
60 g2rtc_match, g2rtc_attach, NULL, NULL);
63 /* todr(9) methods */
64 static int g2rtc_todr_gettime(todr_chip_handle_t, struct timeval *);
65 static int g2rtc_todr_settime(todr_chip_handle_t, struct timeval *);
67 static struct todr_chip_handle g2rtc_todr_handle = {
68 .cookie = NULL, /* set on attach */
69 .todr_gettime = g2rtc_todr_gettime,
70 .todr_settime = g2rtc_todr_settime,
74 static inline uint32_t g2rtc_read(bus_space_tag_t, bus_space_handle_t);
77 static int
78 g2rtc_match(struct device *parent, struct cfdata *cf, void *aux)
80 static int g2rtc_matched = 0;
82 if (g2rtc_matched)
83 return 0;
85 g2rtc_matched = 1;
86 return 1;
90 static void
91 g2rtc_attach(struct device *parent, struct device *self, void *aux)
93 struct g2rtc_softc *sc = (void *)self;
94 struct g2bus_attach_args *ga = aux;
96 sc->sc_bt = ga->ga_memt;
97 if (bus_space_map(sc->sc_bt, G2RTC_REG_BASE, G2RTC_REG_SIZE, 0,
98 &sc->sc_bh) != 0)
100 printf(": unable to map registers\n");
101 return;
103 printf(": time-of-day clock\n");
105 g2rtc_todr_handle.cookie = sc;
106 todr_attach(&g2rtc_todr_handle);
110 static inline uint32_t
111 g2rtc_read(bus_space_tag_t bt, bus_space_handle_t bh)
114 return ((bus_space_read_4(bt, bh, 0) & 0xffff) << 16)
115 | (bus_space_read_4(bt, bh, 4) & 0xffff);
120 * Get time-of-day and convert to `struct timeval'.
121 * Return 0 on success; an error number otherwise.
123 static int
124 g2rtc_todr_gettime(todr_chip_handle_t handle, struct timeval *tv)
126 struct g2rtc_softc *sc = handle->cookie;
127 uint32_t new, old;
128 int i;
130 for (old = 0;;) {
131 for (i = 0; i < 3; i++) {
132 new = g2rtc_read(sc->sc_bt, sc->sc_bh);
133 if (new != old)
134 break;
136 if (i < 3)
137 old = new;
138 else
139 break;
142 tv->tv_sec = new - G2RTC_OFFSET;
143 tv->tv_usec = 0;
145 return 0;
150 * Set the time-of-day clock based on the value of the `struct timeval' arg.
151 * Return 0 on success; an error number otherwise.
153 static int
154 g2rtc_todr_settime(todr_chip_handle_t handle, struct timeval *tv)
156 struct g2rtc_softc *sc = handle->cookie;
157 uint32_t secs;
158 int i, retry;
160 secs = (uint32_t)tv->tv_sec + G2RTC_OFFSET;
162 for (retry = 0; retry < 5; retry++) {
164 /* Don't change the order */
165 bus_space_write_4(sc->sc_bt, sc->sc_bh, 8, 1);
166 bus_space_write_4(sc->sc_bt, sc->sc_bh, 4, secs & 0xffff);
167 bus_space_write_4(sc->sc_bt, sc->sc_bh, 0, secs >> 16);
169 /* verify */
170 for (i = 0; i < 3; i++)
171 if (g2rtc_read(sc->sc_bt, sc->sc_bh) == secs)
172 return 0; /* ok */
175 return EIO;