1 /* $OpenBSD: gfrtc.c,v 1.1 2021/04/24 05:14:45 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2021 Jonathan Gray <jsg@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Google Goldfish virtual real-time clock described in 21 * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT 22 */ 23 24 #include <sys/param.h> 25 #include <sys/device.h> 26 #include <sys/malloc.h> 27 #include <sys/systm.h> 28 29 #include <machine/bus.h> 30 #include <machine/fdt.h> 31 #include <dev/clock_subr.h> 32 33 #include <dev/ofw/openfirm.h> 34 #include <dev/ofw/fdt.h> 35 36 #define TIME_LOW 0x00 37 #define TIME_HIGH 0x04 38 #define ALARM_LOW 0x0c 39 #define CLEAR_INTERRUPT 0x10 40 41 extern todr_chip_handle_t todr_handle; 42 43 struct gfrtc_softc { 44 struct device sc_dev; 45 bus_space_tag_t sc_iot; 46 bus_space_handle_t sc_ioh; 47 struct todr_chip_handle sc_todr; 48 }; 49 50 int gfrtc_match(struct device *, void *, void *); 51 void gfrtc_attach(struct device *, struct device *, void *); 52 int gfrtc_gettime(struct todr_chip_handle *, struct timeval *); 53 int gfrtc_settime(struct todr_chip_handle *, struct timeval *); 54 55 struct cfattach gfrtc_ca = { 56 sizeof(struct gfrtc_softc), gfrtc_match, gfrtc_attach 57 }; 58 59 struct cfdriver gfrtc_cd = { 60 NULL, "gfrtc", DV_DULL 61 }; 62 63 int 64 gfrtc_gettime(todr_chip_handle_t handle, struct timeval *tv) 65 { 66 struct gfrtc_softc *sc = handle->cookie; 67 uint64_t tl, th; 68 69 tl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, TIME_LOW); 70 th = bus_space_read_4(sc->sc_iot, sc->sc_ioh, TIME_HIGH); 71 72 NSEC_TO_TIMEVAL((th << 32) | tl, tv); 73 74 return 0; 75 } 76 77 int 78 gfrtc_settime(todr_chip_handle_t handle, struct timeval *tv) 79 { 80 struct gfrtc_softc *sc = handle->cookie; 81 uint64_t ns; 82 83 ns = TIMEVAL_TO_NSEC(tv); 84 85 bus_space_write_4(sc->sc_iot, sc->sc_ioh, TIME_HIGH, ns >> 32); 86 bus_space_write_4(sc->sc_iot, sc->sc_ioh, TIME_LOW, ns); 87 88 return 0; 89 } 90 91 int 92 gfrtc_match(struct device *parent, void *match, void *aux) 93 { 94 struct fdt_attach_args *faa = aux; 95 96 return OF_is_compatible(faa->fa_node, "google,goldfish-rtc"); 97 } 98 99 void 100 gfrtc_attach(struct device *parent, struct device *self, void *aux) 101 { 102 struct fdt_attach_args *faa = aux; 103 struct gfrtc_softc *sc = (struct gfrtc_softc *) self; 104 105 sc->sc_iot = faa->fa_iot; 106 107 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 108 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 109 printf(": failed to map mem space\n"); 110 return; 111 } 112 113 sc->sc_todr.cookie = sc; 114 sc->sc_todr.todr_gettime = gfrtc_gettime; 115 sc->sc_todr.todr_settime = gfrtc_settime; 116 todr_handle = &sc->sc_todr; 117 118 printf("\n"); 119 } 120