xref: /netbsd-src/sys/arch/dreamcast/dev/g2/g2rtc.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /* $NetBSD: g2rtc.c,v 1.2 2008/01/06 10:33:24 he Exp $ */
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: g2rtc.c,v 1.2 2008/01/06 10:33:24 he Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 
43 #include <dev/clock_subr.h>
44 
45 #include <machine/bus.h>
46 #include <dreamcast/dev/g2/g2busvar.h>
47 
48 
49 #define G2RTC_REG_BASE	0x00710000
50 #define G2RTC_REG_SIZE	12
51 
52 /* Offset by 20 years, 5 of them are leap */
53 #define G2RTC_OFFSET	(20 * SECYR + 5 * SECDAY)
54 
55 struct g2rtc_softc {
56 	struct device sc_dev;
57 
58 	bus_space_tag_t sc_bt;
59 	bus_space_handle_t sc_bh;
60 };
61 
62 /* autoconf glue */
63 static int g2rtc_match(struct device *, struct cfdata *, void *);
64 static void g2rtc_attach(struct device *, struct device *, void *);
65 
66 CFATTACH_DECL(g2rtc, sizeof(struct g2rtc_softc),
67 	      g2rtc_match, g2rtc_attach, NULL, NULL);
68 
69 
70 /* todr(9) methods */
71 static int g2rtc_todr_gettime(todr_chip_handle_t, volatile struct timeval *);
72 static int g2rtc_todr_settime(todr_chip_handle_t, volatile struct timeval *);
73 
74 static struct todr_chip_handle g2rtc_todr_handle = {
75 	.cookie       = NULL,	/* set on attach */
76 	.todr_gettime = g2rtc_todr_gettime,
77 	.todr_settime = g2rtc_todr_settime,
78 };
79 
80 
81 static inline uint32_t g2rtc_read(bus_space_tag_t, bus_space_handle_t);
82 
83 
84 static int
85 g2rtc_match(struct device *parent, struct cfdata *cf, void *aux)
86 {
87 	static int g2rtc_matched = 0;
88 
89 	if (g2rtc_matched)
90 		return 0;
91 
92 	g2rtc_matched = 1;
93 	return 1;
94 }
95 
96 
97 static void
98 g2rtc_attach(struct device *parent, struct device *self, void *aux)
99 {
100 	struct g2rtc_softc *sc = (void *)self;
101 	struct g2bus_attach_args *ga = aux;
102 
103 	sc->sc_bt = ga->ga_memt;
104 	if (bus_space_map(sc->sc_bt, G2RTC_REG_BASE, G2RTC_REG_SIZE, 0,
105 			  &sc->sc_bh) != 0)
106 	{
107 		printf(": unable to map registers\n");
108 		return;
109 	}
110 	printf(": time-of-day clock\n");
111 
112 	g2rtc_todr_handle.cookie = sc;
113 	todr_attach(&g2rtc_todr_handle);
114 }
115 
116 
117 static inline uint32_t
118 g2rtc_read(bus_space_tag_t bt, bus_space_handle_t bh)
119 {
120 
121 	return ((bus_space_read_4(bt, bh, 0) & 0xffff) << 16)
122 		| (bus_space_read_4(bt, bh, 4) & 0xffff);
123 }
124 
125 
126 /*
127  * Get time-of-day and convert to `struct timeval'.
128  * Return 0 on success; an error number otherwise.
129  */
130 static int
131 g2rtc_todr_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
132 {
133 	struct g2rtc_softc *sc = handle->cookie;
134 	uint32_t new, old;
135 	int i;
136 
137 	for (old = 0;;) {
138 		for (i = 0; i < 3; i++) {
139 			new = g2rtc_read(sc->sc_bt, sc->sc_bh);
140 			if (new != old)
141 				break;
142 		}
143 		if (i < 3)
144 			old = new;
145 		else
146 			break;
147 	}
148 
149 	tv->tv_sec = new - G2RTC_OFFSET;
150 	tv->tv_usec = 0;
151 
152 	return 0;
153 }
154 
155 
156 /*
157  * Set the time-of-day clock based on the value of the `struct timeval' arg.
158  * Return 0 on success; an error number otherwise.
159  */
160 static int
161 g2rtc_todr_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
162 {
163 	struct g2rtc_softc *sc = handle->cookie;
164 	uint32_t secs;
165 	int i, retry;
166 
167 	secs = (uint32_t)tv->tv_sec + G2RTC_OFFSET;
168 
169 	for (retry = 0; retry < 5; retry++) {
170 
171 		/* Don't change the order */
172 		bus_space_write_4(sc->sc_bt, sc->sc_bh, 8, 1);
173 		bus_space_write_4(sc->sc_bt, sc->sc_bh, 4, secs & 0xffff);
174 		bus_space_write_4(sc->sc_bt, sc->sc_bh, 0, secs >> 16);
175 
176 		/* verify */
177 		for (i = 0; i < 3; i++)
178 			if (g2rtc_read(sc->sc_bt, sc->sc_bh) == secs)
179 				return 0; /* ok */
180 	}
181 
182 	return EIO;
183 
184 }
185