gfrtc_fdt.c revision 1.1
11.1Sskrll/*	$NetBSD: gfrtc_fdt.c,v 1.1 2023/05/08 07:51:44 skrll Exp $	*/
21.1Sskrll
31.1Sskrll/*-
41.1Sskrll * Copyright (c) 2023 The NetBSD Foundation, Inc.
51.1Sskrll * All rights reserved.
61.1Sskrll *
71.1Sskrll * This code is derived from software contributed to The NetBSD Foundation
81.1Sskrll * by Nick Hudson
91.1Sskrll *
101.1Sskrll * Redistribution and use in source and binary forms, with or without
111.1Sskrll * modification, are permitted provided that the following conditions
121.1Sskrll * are met:
131.1Sskrll * 1. Redistributions of source code must retain the above copyright
141.1Sskrll *    notice, this list of conditions and the following disclaimer.
151.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright
161.1Sskrll *    notice, this list of conditions and the following disclaimer in the
171.1Sskrll *    documentation and/or other materials provided with the distribution.
181.1Sskrll *
191.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sskrll * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sskrll * POSSIBILITY OF SUCH DAMAGE.
301.1Sskrll */
311.1Sskrll
321.1Sskrll#include <sys/cdefs.h>
331.1Sskrll__KERNEL_RCSID(0, "$NetBSD: gfrtc_fdt.c,v 1.1 2023/05/08 07:51:44 skrll Exp $");
341.1Sskrll
351.1Sskrll#include <sys/param.h>
361.1Sskrll
371.1Sskrll#include <sys/bus.h>
381.1Sskrll#include <sys/device.h>
391.1Sskrll
401.1Sskrll#include <dev/fdt/fdtvar.h>
411.1Sskrll
421.1Sskrll/*
431.1Sskrll * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
441.1Sskrll */
451.1Sskrll
461.1Sskrllstatic const struct device_compatible_entry compat_data[] = {
471.1Sskrll	{ .compat = "google,goldfish-rtc" },
481.1Sskrll	DEVICE_COMPAT_EOL
491.1Sskrll};
501.1Sskrll
511.1Sskrllstruct gfrtc_fdt_softc {
521.1Sskrll	device_t sc_dev;
531.1Sskrll	bus_space_tag_t sc_bst;
541.1Sskrll	bus_space_handle_t sc_bsh;
551.1Sskrll
561.1Sskrll	struct todr_chip_handle	sc_todr;
571.1Sskrll};
581.1Sskrll
591.1Sskrll#define	GOLDFISH_RTC_READ(sc, reg) \
601.1Sskrll	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
611.1Sskrll#define	GOLDFISH_RTC_WRITE(sc, reg, val) \
621.1Sskrll	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
631.1Sskrll
641.1Sskrll
651.1Sskrll#define	GOLDFISH_RTC_TIME_LOW	0x00
661.1Sskrll#define	GOLDFISH_RTC_TIME_HIGH	0x04
671.1Sskrll
681.1Sskrll
691.1Sskrllstatic int
701.1Sskrllgfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
711.1Sskrll{
721.1Sskrll	struct gfrtc_fdt_softc *sc = ch->cookie;
731.1Sskrll	const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW);
741.1Sskrll	const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH);
751.1Sskrll
761.1Sskrll	uint64_t nsec = (hi << 32) | lo;
771.1Sskrll
781.1Sskrll	tv->tv_sec = nsec / 1000000000;
791.1Sskrll	tv->tv_usec = (nsec - tv->tv_sec) / 1000;	// Always 0?
801.1Sskrll
811.1Sskrll	return 0;
821.1Sskrll}
831.1Sskrll
841.1Sskrllstatic int
851.1Sskrllgfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
861.1Sskrll{
871.1Sskrll	struct gfrtc_fdt_softc *sc = ch->cookie;
881.1Sskrll
891.1Sskrll	uint64_t nsec = (tv->tv_sec * 1000000 + tv->tv_usec) * 1000;
901.1Sskrll	uint32_t hi = nsec >> 32;
911.1Sskrll	uint32_t lo = nsec;
921.1Sskrll
931.1Sskrll	GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, hi);
941.1Sskrll	GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, lo);
951.1Sskrll
961.1Sskrll	return 0;
971.1Sskrll}
981.1Sskrll
991.1Sskrll
1001.1Sskrllstatic int
1011.1Sskrllgfrtc_fdt_match(device_t parent, cfdata_t cf, void *aux)
1021.1Sskrll{
1031.1Sskrll	struct fdt_attach_args * const faa = aux;
1041.1Sskrll
1051.1Sskrll	return of_compatible_match(faa->faa_phandle, compat_data);
1061.1Sskrll}
1071.1Sskrll
1081.1Sskrllstatic void
1091.1Sskrllgfrtc_fdt_attach(device_t parent, device_t self, void *aux)
1101.1Sskrll{
1111.1Sskrll	struct gfrtc_fdt_softc * const sc = device_private(self);
1121.1Sskrll	struct fdt_attach_args * const faa = aux;
1131.1Sskrll	const int phandle = faa->faa_phandle;
1141.1Sskrll	bus_addr_t addr;
1151.1Sskrll	bus_size_t size;
1161.1Sskrll
1171.1Sskrll	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1181.1Sskrll		aprint_error(": couldn't get registers\n");
1191.1Sskrll		return;
1201.1Sskrll	}
1211.1Sskrll
1221.1Sskrll	sc->sc_dev = self;
1231.1Sskrll	sc->sc_bst = faa->faa_bst;
1241.1Sskrll	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1251.1Sskrll		aprint_error(": couldn't map registers\n");
1261.1Sskrll		return;
1271.1Sskrll	}
1281.1Sskrll
1291.1Sskrll	aprint_naive("\n");
1301.1Sskrll	aprint_normal(": Google Goldfish RTC\n");
1311.1Sskrll
1321.1Sskrll	sc->sc_todr.cookie = sc;
1331.1Sskrll	sc->sc_todr.todr_gettime = gfrtc_gettime;
1341.1Sskrll	sc->sc_todr.todr_settime = gfrtc_settime;
1351.1Sskrll	sc->sc_todr.todr_setwen = NULL;
1361.1Sskrll
1371.1Sskrll	todr_attach(&sc->sc_todr);
1381.1Sskrll}
1391.1Sskrll
1401.1SskrllCFATTACH_DECL_NEW(gfrtc_fdt, sizeof(struct gfrtc_fdt_softc),
1411.1Sskrll	gfrtc_fdt_match, gfrtc_fdt_attach, NULL, NULL);
142