g2rtc.c revision 1.1
1/* $NetBSD: g2rtc.c,v 1.1 2006/09/05 11:09:36 uwe 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.1 2006/09/05 11:09:36 uwe Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41
42#include <dev/clock_subr.h>
43
44#include <machine/bus.h>
45#include <dreamcast/dev/g2/g2busvar.h>
46
47
48#define G2RTC_REG_BASE	0x00710000
49#define G2RTC_REG_SIZE	12
50
51/* Offset by 20 years, 5 of them are leap */
52#define G2RTC_OFFSET	(20 * SECYR + 5 * SECDAY)
53
54struct g2rtc_softc {
55	struct device sc_dev;
56
57	bus_space_tag_t sc_bt;
58	bus_space_handle_t sc_bh;
59};
60
61/* autoconf glue */
62static int g2rtc_match(struct device *, struct cfdata *, void *);
63static void g2rtc_attach(struct device *, struct device *, void *);
64
65CFATTACH_DECL(g2rtc, sizeof(struct g2rtc_softc),
66	      g2rtc_match, g2rtc_attach, NULL, NULL);
67
68
69/* todr(9) methods */
70static int g2rtc_todr_gettime(todr_chip_handle_t, volatile struct timeval *);
71static int g2rtc_todr_settime(todr_chip_handle_t, volatile struct timeval *);
72
73static struct todr_chip_handle g2rtc_todr_handle = {
74	.cookie       = NULL,	/* set on attach */
75	.todr_gettime = g2rtc_todr_gettime,
76	.todr_settime = g2rtc_todr_settime,
77};
78
79
80static inline uint32_t g2rtc_read(bus_space_tag_t, bus_space_handle_t);
81
82
83static int
84g2rtc_match(struct device *parent, struct cfdata *cf, void *aux)
85{
86	static int g2rtc_matched = 0;
87
88	if (g2rtc_matched)
89		return 0;
90
91	g2rtc_matched = 1;
92	return 1;
93}
94
95
96static void
97g2rtc_attach(struct device *parent, struct device *self, void *aux)
98{
99	struct g2rtc_softc *sc = (void *)self;
100	struct g2bus_attach_args *ga = aux;
101
102	sc->sc_bt = ga->ga_memt;
103	if (bus_space_map(sc->sc_bt, G2RTC_REG_BASE, G2RTC_REG_SIZE, 0,
104			  &sc->sc_bh) != 0)
105	{
106		printf(": unable to map registers\n");
107		return;
108	}
109	printf(": time-of-day clock\n");
110
111	g2rtc_todr_handle.cookie = sc;
112	todr_attach(&g2rtc_todr_handle);
113}
114
115
116static inline uint32_t
117g2rtc_read(bus_space_tag_t bt, bus_space_handle_t bh)
118{
119
120	return ((bus_space_read_4(bt, bh, 0) & 0xffff) << 16)
121		| (bus_space_read_4(bt, bh, 4) & 0xffff);
122}
123
124
125/*
126 * Get time-of-day and convert to `struct timeval'.
127 * Return 0 on success; an error number otherwise.
128 */
129static int
130g2rtc_todr_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
131{
132	struct g2rtc_softc *sc = handle->cookie;
133	uint32_t new, old;
134	int i;
135
136	for (old = 0;;) {
137		for (i = 0; i < 3; i++) {
138			new = g2rtc_read(sc->sc_bt, sc->sc_bh);
139			if (new != old)
140				break;
141		}
142		if (i < 3)
143			old = new;
144		else
145			break;
146	}
147
148	tv->tv_sec = new - G2RTC_OFFSET;
149	tv->tv_usec = 0;
150
151	return 0;
152}
153
154
155/*
156 * Set the time-of-day clock based on the value of the `struct timeval' arg.
157 * Return 0 on success; an error number otherwise.
158 */
159static int
160g2rtc_todr_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
161{
162	struct g2rtc_softc *sc = handle->cookie;
163	uint32_t secs;
164	int i, retry;
165
166	secs = (uint32_t)tv->tv_sec + G2RTC_OFFSET;
167
168	for (retry = 0; retry < 5; retry++) {
169
170		/* Don't change the order */
171		bus_space_write_4(sc->sc_bt, sc->sc_bh, 8, 1);
172		bus_space_write_4(sc->sc_bt, sc->sc_bh, 4, secs & 0xffff);
173		bus_space_write_4(sc->sc_bt, sc->sc_bh, 0, secs >> 16);
174
175		/* verify */
176		for (i = 0; i < 3; i++)
177			if (g2rtc_read(sc->sc_bt, sc->sc_bh) == secs)
178				return 0; /* ok */
179	}
180
181	return EIO;
182
183}
184