The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/goldfish/goldfish_rtc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2020 Jessica Clarke <jrtc27@FreeBSD.org>
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 /*
   29  * RTC for the goldfish virtual hardware platform implemented in QEMU,
   30  * initially for Android but now also used for RISC-V's virt machine.
   31  *
   32  * https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/bus.h>
   41 #include <sys/clock.h>
   42 #include <sys/kernel.h>
   43 #include <sys/lock.h>
   44 #include <sys/module.h>
   45 #include <sys/mutex.h>
   46 #include <sys/types.h>
   47 
   48 #include <dev/ofw/ofw_bus.h>
   49 #include <dev/ofw/ofw_bus_subr.h>
   50 
   51 #include <machine/bus.h>
   52 #include <machine/resource.h>
   53 #include <sys/rman.h>
   54 
   55 #include "clock_if.h"
   56 
   57 #define GOLDFISH_RTC_TIME_LOW   0x00
   58 #define GOLDFISH_RTC_TIME_HIGH  0x04
   59 
   60 struct goldfish_rtc_softc {
   61         struct resource *res;
   62         int             rid;
   63         struct mtx      mtx;
   64 };
   65 
   66 static int
   67 goldfish_rtc_probe(device_t dev)
   68 {
   69 
   70         if (!ofw_bus_status_okay(dev))
   71                 return (ENXIO);
   72 
   73         if (ofw_bus_is_compatible(dev, "google,goldfish-rtc")) {
   74                 device_set_desc(dev, "Goldfish RTC");
   75                 return (BUS_PROBE_DEFAULT);
   76         }
   77 
   78         return (ENXIO);
   79 }
   80 
   81 static int
   82 goldfish_rtc_attach(device_t dev)
   83 {
   84         struct goldfish_rtc_softc *sc;
   85 
   86         sc = device_get_softc(dev);
   87 
   88         sc->rid = 0;
   89         sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
   90             RF_ACTIVE);
   91         if (sc->res == NULL) {
   92                 device_printf(dev, "could not allocate resource\n");
   93                 return (ENXIO);
   94         }
   95 
   96         mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
   97 
   98         /*
   99          * Register as a system realtime clock with 1 second resolution.
  100          */
  101         clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
  102         clock_schedule(dev, 1);
  103 
  104         return (0);
  105 }
  106 
  107 static int
  108 goldfish_rtc_detach(device_t dev)
  109 {
  110         struct goldfish_rtc_softc *sc;
  111 
  112         sc = device_get_softc(dev);
  113 
  114         clock_unregister(dev);
  115         mtx_destroy(&sc->mtx);
  116         bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
  117 
  118         return (0);
  119 }
  120 
  121 static int
  122 goldfish_rtc_gettime(device_t dev, struct timespec *ts)
  123 {
  124         struct goldfish_rtc_softc *sc;
  125         uint64_t low, high, nsec;
  126 
  127         sc = device_get_softc(dev);
  128 
  129         /*
  130          * Reading TIME_HIGH is defined in the documentation to give the high
  131          * 32 bits corresponding to the last TIME_LOW read, so must be done in
  132          * that order, but means we have atomicity guaranteed.
  133          */
  134         mtx_lock(&sc->mtx);
  135         low = bus_read_4(sc->res, GOLDFISH_RTC_TIME_LOW);
  136         high = bus_read_4(sc->res, GOLDFISH_RTC_TIME_HIGH);
  137         mtx_unlock(&sc->mtx);
  138 
  139         nsec = (high << 32) | low;
  140         ts->tv_sec = nsec / 1000000000;
  141         ts->tv_nsec = nsec % 1000000000;
  142 
  143         return (0);
  144 }
  145 
  146 static int
  147 goldfish_rtc_settime(device_t dev, struct timespec *ts)
  148 {
  149         struct goldfish_rtc_softc *sc;
  150         uint64_t nsec;
  151 
  152         sc = device_get_softc(dev);
  153 
  154         /*
  155          * We request a timespec with no resolution-adjustment.  That also
  156          * disables utc adjustment, so apply that ourselves.
  157          */
  158         ts->tv_sec -= utc_offset();
  159         nsec = (uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec;
  160 
  161         mtx_lock(&sc->mtx);
  162         bus_write_4(sc->res, GOLDFISH_RTC_TIME_HIGH, nsec >> 32);
  163         bus_write_4(sc->res, GOLDFISH_RTC_TIME_LOW, nsec);
  164         mtx_unlock(&sc->mtx);
  165 
  166         return (0);
  167 }
  168 
  169 static device_method_t goldfish_rtc_methods[] = {
  170         /* Device interface */
  171         DEVMETHOD(device_probe,         goldfish_rtc_probe),
  172         DEVMETHOD(device_attach,        goldfish_rtc_attach),
  173         DEVMETHOD(device_detach,        goldfish_rtc_detach),
  174 
  175         /* Clock interface */
  176         DEVMETHOD(clock_gettime,        goldfish_rtc_gettime),
  177         DEVMETHOD(clock_settime,        goldfish_rtc_settime),
  178 
  179         DEVMETHOD_END,
  180 };
  181 
  182 DEFINE_CLASS_0(goldfish_rtc, goldfish_rtc_driver, goldfish_rtc_methods,
  183     sizeof(struct goldfish_rtc_softc));
  184 
  185 DRIVER_MODULE(goldfish_rtc, simplebus, goldfish_rtc_driver, NULL, NULL);

Cache object: 2b4b05cbdfda5f719709b34a2c8785c2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.