Module Name: src Committed By: thorpej Date: Fri Dec 29 23:31:45 UTC 2023
Modified Files: src/sys/conf: files src/sys/dev/fdt: files.fdt gfrtc_fdt.c Added Files: src/sys/dev/goldfish: files.goldfish gfrtc.c gfrtcvar.h Log Message: Re-factor the Goldfish RTC driver into attach-front-end and generic back-end; Goldfish virtual devices can be found on virtual platforms that don't use FDT. To generate a diff of this commit: cvs rdiff -u -r1.1309 -r1.1310 src/sys/conf/files cvs rdiff -u -r1.70 -r1.71 src/sys/dev/fdt/files.fdt cvs rdiff -u -r1.1 -r1.2 src/sys/dev/fdt/gfrtc_fdt.c cvs rdiff -u -r0 -r1.1 src/sys/dev/goldfish/files.goldfish \ src/sys/dev/goldfish/gfrtc.c src/sys/dev/goldfish/gfrtcvar.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/conf/files diff -u src/sys/conf/files:1.1309 src/sys/conf/files:1.1310 --- src/sys/conf/files:1.1309 Sun Sep 10 14:04:28 2023 +++ src/sys/conf/files Fri Dec 29 23:31:45 2023 @@ -1,4 +1,4 @@ -# $NetBSD: files,v 1.1309 2023/09/10 14:04:28 abs Exp $ +# $NetBSD: files,v 1.1310 2023/12/29 23:31:45 thorpej Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 version 20171118 @@ -1660,6 +1660,11 @@ include "dev/ic/files.athn" include "dev/clk/files.clk" # +# Goldfish virtual devices +# +include "dev/goldfish/files.goldfish" + +# # Flattened Device Tree (FDT) support # include "dev/ofw/files.ofw" Index: src/sys/dev/fdt/files.fdt diff -u src/sys/dev/fdt/files.fdt:1.70 src/sys/dev/fdt/files.fdt:1.71 --- src/sys/dev/fdt/files.fdt:1.70 Mon May 8 10:18:03 2023 +++ src/sys/dev/fdt/files.fdt Fri Dec 29 23:31:44 2023 @@ -1,4 +1,4 @@ -# $NetBSD: files.fdt,v 1.70 2023/05/08 10:18:03 skrll Exp $ +# $NetBSD: files.fdt,v 1.71 2023/12/29 23:31:44 thorpej Exp $ include "external/bsd/libfdt/conf/files.libfdt" @@ -217,6 +217,5 @@ attach vmt at fdt with vmt_fdt file dev/fdt/vmt_fdt.c vmt_fdt # Google Goldfish RTC -device gfrtc attach gfrtc at fdt with gfrtc_fdt file dev/fdt/gfrtc_fdt.c gfrtc_fdt Index: src/sys/dev/fdt/gfrtc_fdt.c diff -u src/sys/dev/fdt/gfrtc_fdt.c:1.1 src/sys/dev/fdt/gfrtc_fdt.c:1.2 --- src/sys/dev/fdt/gfrtc_fdt.c:1.1 Mon May 8 07:51:44 2023 +++ src/sys/dev/fdt/gfrtc_fdt.c Fri Dec 29 23:31:44 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: gfrtc_fdt.c,v 1.1 2023/05/08 07:51:44 skrll Exp $ */ +/* $NetBSD: gfrtc_fdt.c,v 1.2 2023/12/29 23:31:44 thorpej Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: gfrtc_fdt.c,v 1.1 2023/05/08 07:51:44 skrll Exp $"); +__KERNEL_RCSID(0, "$NetBSD: gfrtc_fdt.c,v 1.2 2023/12/29 23:31:44 thorpej Exp $"); #include <sys/param.h> @@ -38,6 +38,7 @@ __KERNEL_RCSID(0, "$NetBSD: gfrtc_fdt.c, #include <sys/device.h> #include <dev/fdt/fdtvar.h> +#include <dev/goldfish/gfrtcvar.h> /* * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT @@ -48,54 +49,6 @@ static const struct device_compatible_en DEVICE_COMPAT_EOL }; -struct gfrtc_fdt_softc { - device_t sc_dev; - bus_space_tag_t sc_bst; - bus_space_handle_t sc_bsh; - - struct todr_chip_handle sc_todr; -}; - -#define GOLDFISH_RTC_READ(sc, reg) \ - bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) -#define GOLDFISH_RTC_WRITE(sc, reg, val) \ - bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) - - -#define GOLDFISH_RTC_TIME_LOW 0x00 -#define GOLDFISH_RTC_TIME_HIGH 0x04 - - -static int -gfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) -{ - struct gfrtc_fdt_softc *sc = ch->cookie; - const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW); - const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH); - - uint64_t nsec = (hi << 32) | lo; - - tv->tv_sec = nsec / 1000000000; - tv->tv_usec = (nsec - tv->tv_sec) / 1000; // Always 0? - - return 0; -} - -static int -gfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) -{ - struct gfrtc_fdt_softc *sc = ch->cookie; - - uint64_t nsec = (tv->tv_sec * 1000000 + tv->tv_usec) * 1000; - uint32_t hi = nsec >> 32; - uint32_t lo = nsec; - - GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, hi); - GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, lo); - - return 0; -} - static int gfrtc_fdt_match(device_t parent, cfdata_t cf, void *aux) @@ -108,7 +61,7 @@ gfrtc_fdt_match(device_t parent, cfdata_ static void gfrtc_fdt_attach(device_t parent, device_t self, void *aux) { - struct gfrtc_fdt_softc * const sc = device_private(self); + struct gfrtc_softc * const sc = device_private(self); struct fdt_attach_args * const faa = aux; const int phandle = faa->faa_phandle; bus_addr_t addr; @@ -129,13 +82,8 @@ gfrtc_fdt_attach(device_t parent, device aprint_naive("\n"); aprint_normal(": Google Goldfish RTC\n"); - sc->sc_todr.cookie = sc; - sc->sc_todr.todr_gettime = gfrtc_gettime; - sc->sc_todr.todr_settime = gfrtc_settime; - sc->sc_todr.todr_setwen = NULL; - - todr_attach(&sc->sc_todr); + gfrtc_attach(sc); } -CFATTACH_DECL_NEW(gfrtc_fdt, sizeof(struct gfrtc_fdt_softc), +CFATTACH_DECL_NEW(gfrtc_fdt, sizeof(struct gfrtc_softc), gfrtc_fdt_match, gfrtc_fdt_attach, NULL, NULL); Added files: Index: src/sys/dev/goldfish/files.goldfish diff -u /dev/null src/sys/dev/goldfish/files.goldfish:1.1 --- /dev/null Fri Dec 29 23:31:45 2023 +++ src/sys/dev/goldfish/files.goldfish Fri Dec 29 23:31:44 2023 @@ -0,0 +1,5 @@ +# $NetBSD: files.goldfish,v 1.1 2023/12/29 23:31:44 thorpej Exp $ + +# Goldfish real-time clock / timer +device gfrtc +file dev/goldfish/gfrtc.c gfrtc Index: src/sys/dev/goldfish/gfrtc.c diff -u /dev/null src/sys/dev/goldfish/gfrtc.c:1.1 --- /dev/null Fri Dec 29 23:31:45 2023 +++ src/sys/dev/goldfish/gfrtc.c Fri Dec 29 23:31:44 2023 @@ -0,0 +1,96 @@ +/* $NetBSD: gfrtc.c,v 1.1 2023/12/29 23:31:44 thorpej Exp $ */ + +/*- + * Copyright (c) 2023 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nick Hudson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: gfrtc.c,v 1.1 2023/12/29 23:31:44 thorpej Exp $"); + +#include <sys/param.h> + +#include <sys/bus.h> +#include <sys/device.h> + +#include <dev/goldfish/gfrtcvar.h> + +/* + * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT + */ + +#define GOLDFISH_RTC_READ(sc, reg) \ + bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) +#define GOLDFISH_RTC_WRITE(sc, reg, val) \ + bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) + + +#define GOLDFISH_RTC_TIME_LOW 0x00 +#define GOLDFISH_RTC_TIME_HIGH 0x04 + + +static int +gfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) +{ + struct gfrtc_softc *sc = ch->cookie; + const uint64_t lo = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_LOW); + const uint64_t hi = GOLDFISH_RTC_READ(sc, GOLDFISH_RTC_TIME_HIGH); + + uint64_t nsec = (hi << 32) | lo; + + tv->tv_sec = nsec / 1000000000; + tv->tv_usec = (nsec - tv->tv_sec) / 1000; // Always 0? + + return 0; +} + +static int +gfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) +{ + struct gfrtc_softc *sc = ch->cookie; + + uint64_t nsec = (tv->tv_sec * 1000000 + tv->tv_usec) * 1000; + uint32_t hi = nsec >> 32; + uint32_t lo = nsec; + + GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, hi); + GOLDFISH_RTC_WRITE(sc, GOLDFISH_RTC_TIME_HIGH, lo); + + return 0; +} + + +void +gfrtc_attach(struct gfrtc_softc * const sc) +{ + sc->sc_todr.cookie = sc; + sc->sc_todr.todr_gettime = gfrtc_gettime; + sc->sc_todr.todr_settime = gfrtc_settime; + sc->sc_todr.todr_setwen = NULL; + + todr_attach(&sc->sc_todr); +} Index: src/sys/dev/goldfish/gfrtcvar.h diff -u /dev/null src/sys/dev/goldfish/gfrtcvar.h:1.1 --- /dev/null Fri Dec 29 23:31:45 2023 +++ src/sys/dev/goldfish/gfrtcvar.h Fri Dec 29 23:31:45 2023 @@ -0,0 +1,47 @@ +/* $NetBSD: gfrtcvar.h,v 1.1 2023/12/29 23:31:45 thorpej Exp $ */ + +/*- + * Copyright (c) 2023 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nick Hudson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DEV_GOLDFISH_GFRTCVAR_H_ +#define _DEV_GOLDFISH_GFRTCVAR_H_ + +#include <dev/clock_subr.h> + +struct gfrtc_softc { + device_t sc_dev; + bus_space_tag_t sc_bst; + bus_space_handle_t sc_bsh; + + struct todr_chip_handle sc_todr; +}; + +void gfrtc_attach(struct gfrtc_softc *); + +#endif /* _DEV_GOLDFISH_GFRTCVAR_H_ */