xref: /netbsd-src/sys/arch/dreamcast/dev/g2/g2rtc.c (revision 56e7e9d8c8f6e06c9c91f07506f8a081bbc018a3)
1*56e7e9d8Schristos /* $NetBSD: g2rtc.c,v 1.8 2016/10/09 14:41:47 christos Exp $ */
22a48f4deSuwe 
32a48f4deSuwe /*-
42a48f4deSuwe  * Copyright (c) 2002 The NetBSD Foundation, Inc.
52a48f4deSuwe  * All rights reserved.
62a48f4deSuwe  *
72a48f4deSuwe  * Redistribution and use in source and binary forms, with or without
82a48f4deSuwe  * modification, are permitted provided that the following conditions
92a48f4deSuwe  * are met:
102a48f4deSuwe  * 1. Redistributions of source code must retain the above copyright
112a48f4deSuwe  *    notice, this list of conditions and the following disclaimer.
122a48f4deSuwe  * 2. Redistributions in binary form must reproduce the above copyright
132a48f4deSuwe  *    notice, this list of conditions and the following disclaimer in the
142a48f4deSuwe  *    documentation and/or other materials provided with the distribution.
152a48f4deSuwe  *
162a48f4deSuwe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172a48f4deSuwe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182a48f4deSuwe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192a48f4deSuwe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202a48f4deSuwe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212a48f4deSuwe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222a48f4deSuwe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232a48f4deSuwe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242a48f4deSuwe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252a48f4deSuwe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262a48f4deSuwe  * POSSIBILITY OF SUCH DAMAGE.
272a48f4deSuwe  */
282a48f4deSuwe 
292a48f4deSuwe #include <sys/cdefs.h>
30*56e7e9d8Schristos __KERNEL_RCSID(0, "$NetBSD: g2rtc.c,v 1.8 2016/10/09 14:41:47 christos Exp $");
312a48f4deSuwe 
322a48f4deSuwe #include <sys/param.h>
332a48f4deSuwe #include <sys/systm.h>
34eb14906cShe #include <sys/device.h>
3586b5be6eSdyoung #include <sys/bus.h>
362a48f4deSuwe 
372a48f4deSuwe #include <dev/clock_subr.h>
382a48f4deSuwe 
392a48f4deSuwe #include <dreamcast/dev/g2/g2busvar.h>
402a48f4deSuwe 
412a48f4deSuwe 
422a48f4deSuwe #define G2RTC_REG_BASE	0x00710000
432a48f4deSuwe #define G2RTC_REG_SIZE	12
442a48f4deSuwe 
452a48f4deSuwe /* Offset by 20 years, 5 of them are leap */
46a95736d4Schristos #define G2RTC_OFFSET	(20 * SECS_PER_COMMON_YEAR + 5 * SECS_PER_DAY)
472a48f4deSuwe 
482a48f4deSuwe struct g2rtc_softc {
499553e52cStsutsui 	device_t sc_dev;
502a48f4deSuwe 
512a48f4deSuwe 	bus_space_tag_t sc_bt;
522a48f4deSuwe 	bus_space_handle_t sc_bh;
539553e52cStsutsui 	struct todr_chip_handle sc_tch;
542a48f4deSuwe };
552a48f4deSuwe 
562a48f4deSuwe /* autoconf glue */
579553e52cStsutsui static int g2rtc_match(device_t, cfdata_t, void *);
589553e52cStsutsui static void g2rtc_attach(device_t, device_t, void *);
592a48f4deSuwe 
609553e52cStsutsui CFATTACH_DECL_NEW(g2rtc, sizeof(struct g2rtc_softc),
612a48f4deSuwe     g2rtc_match, g2rtc_attach, NULL, NULL);
622a48f4deSuwe 
632a48f4deSuwe 
642a48f4deSuwe /* todr(9) methods */
65471e528bStsutsui static int g2rtc_todr_gettime(todr_chip_handle_t, struct timeval *);
66471e528bStsutsui static int g2rtc_todr_settime(todr_chip_handle_t, struct timeval *);
672a48f4deSuwe 
682a48f4deSuwe static inline uint32_t g2rtc_read(bus_space_tag_t, bus_space_handle_t);
692a48f4deSuwe 
702a48f4deSuwe 
712a48f4deSuwe static int
g2rtc_match(device_t parent,cfdata_t cf,void * aux)729553e52cStsutsui g2rtc_match(device_t parent, cfdata_t cf, void *aux)
732a48f4deSuwe {
742a48f4deSuwe 	static int g2rtc_matched = 0;
752a48f4deSuwe 
762a48f4deSuwe 	if (g2rtc_matched)
772a48f4deSuwe 		return 0;
782a48f4deSuwe 
792a48f4deSuwe 	g2rtc_matched = 1;
802a48f4deSuwe 	return 1;
812a48f4deSuwe }
822a48f4deSuwe 
832a48f4deSuwe 
842a48f4deSuwe static void
g2rtc_attach(device_t parent,device_t self,void * aux)859553e52cStsutsui g2rtc_attach(device_t parent, device_t self, void *aux)
862a48f4deSuwe {
879553e52cStsutsui 	struct g2rtc_softc *sc = device_private(self);
882a48f4deSuwe 	struct g2bus_attach_args *ga = aux;
899553e52cStsutsui 	todr_chip_handle_t tch;
902a48f4deSuwe 
919553e52cStsutsui 	sc->sc_dev = self;
922a48f4deSuwe 	sc->sc_bt = ga->ga_memt;
932a48f4deSuwe 	if (bus_space_map(sc->sc_bt, G2RTC_REG_BASE, G2RTC_REG_SIZE, 0,
949553e52cStsutsui 	    &sc->sc_bh) != 0) {
952a48f4deSuwe 		printf(": unable to map registers\n");
962a48f4deSuwe 		return;
972a48f4deSuwe 	}
982a48f4deSuwe 	printf(": time-of-day clock\n");
992a48f4deSuwe 
1009553e52cStsutsui 	tch = &sc->sc_tch;
1019553e52cStsutsui 	tch->cookie = sc;
102*56e7e9d8Schristos 	tch->todr_gettime = g2rtc_todr_gettime;
103*56e7e9d8Schristos 	tch->todr_settime = g2rtc_todr_settime;
1049553e52cStsutsui 	todr_attach(tch);
1052a48f4deSuwe }
1062a48f4deSuwe 
1072a48f4deSuwe 
1082a48f4deSuwe static inline uint32_t
g2rtc_read(bus_space_tag_t bt,bus_space_handle_t bh)1092a48f4deSuwe g2rtc_read(bus_space_tag_t bt, bus_space_handle_t bh)
1102a48f4deSuwe {
1112a48f4deSuwe 
1122a48f4deSuwe 	return ((bus_space_read_4(bt, bh, 0) & 0xffff) << 16)
1132a48f4deSuwe 	    | (bus_space_read_4(bt, bh, 4) & 0xffff);
1142a48f4deSuwe }
1152a48f4deSuwe 
1162a48f4deSuwe 
1172a48f4deSuwe /*
1182a48f4deSuwe  * Get time-of-day and convert to `struct timeval'.
1192a48f4deSuwe  * Return 0 on success; an error number otherwise.
1202a48f4deSuwe  */
1212a48f4deSuwe static int
g2rtc_todr_gettime(todr_chip_handle_t handle,struct timeval * tv)122471e528bStsutsui g2rtc_todr_gettime(todr_chip_handle_t handle, struct timeval *tv)
1232a48f4deSuwe {
1242a48f4deSuwe 	struct g2rtc_softc *sc = handle->cookie;
1252a48f4deSuwe 	uint32_t new, old;
1262a48f4deSuwe 	int i;
1272a48f4deSuwe 
1282a48f4deSuwe 	for (old = 0;;) {
1292a48f4deSuwe 		for (i = 0; i < 3; i++) {
1302a48f4deSuwe 			new = g2rtc_read(sc->sc_bt, sc->sc_bh);
1312a48f4deSuwe 			if (new != old)
1322a48f4deSuwe 				break;
1332a48f4deSuwe 		}
1342a48f4deSuwe 		if (i < 3)
1352a48f4deSuwe 			old = new;
1362a48f4deSuwe 		else
1372a48f4deSuwe 			break;
1382a48f4deSuwe 	}
1392a48f4deSuwe 
1402a48f4deSuwe 	tv->tv_sec = new - G2RTC_OFFSET;
1412a48f4deSuwe 	tv->tv_usec = 0;
1422a48f4deSuwe 
1432a48f4deSuwe 	return 0;
1442a48f4deSuwe }
1452a48f4deSuwe 
1462a48f4deSuwe 
1472a48f4deSuwe /*
1482a48f4deSuwe  * Set the time-of-day clock based on the value of the `struct timeval' arg.
1492a48f4deSuwe  * Return 0 on success; an error number otherwise.
1502a48f4deSuwe  */
1512a48f4deSuwe static int
g2rtc_todr_settime(todr_chip_handle_t handle,struct timeval * tv)152471e528bStsutsui g2rtc_todr_settime(todr_chip_handle_t handle, struct timeval *tv)
1532a48f4deSuwe {
1542a48f4deSuwe 	struct g2rtc_softc *sc = handle->cookie;
1552a48f4deSuwe 	uint32_t secs;
1562a48f4deSuwe 	int i, retry;
1572a48f4deSuwe 
1582a48f4deSuwe 	secs = (uint32_t)tv->tv_sec + G2RTC_OFFSET;
1592a48f4deSuwe 
1602a48f4deSuwe 	for (retry = 0; retry < 5; retry++) {
1612a48f4deSuwe 
1622a48f4deSuwe 		/* Don't change the order */
1632a48f4deSuwe 		bus_space_write_4(sc->sc_bt, sc->sc_bh, 8, 1);
1642a48f4deSuwe 		bus_space_write_4(sc->sc_bt, sc->sc_bh, 4, secs & 0xffff);
1652a48f4deSuwe 		bus_space_write_4(sc->sc_bt, sc->sc_bh, 0, secs >> 16);
1662a48f4deSuwe 
1672a48f4deSuwe 		/* verify */
1682a48f4deSuwe 		for (i = 0; i < 3; i++)
1692a48f4deSuwe 			if (g2rtc_read(sc->sc_bt, sc->sc_bh) == secs)
1702a48f4deSuwe 				return 0; /* ok */
1712a48f4deSuwe 	}
1722a48f4deSuwe 
1732a48f4deSuwe 	return EIO;
1742a48f4deSuwe }
175