2 * Copyright (c) 2017 Andrew Turner
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/clock.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
44 static bool efirtc_zeroes_subseconds;
45 static struct timespec efirtc_resadj;
47 static const u_int us_per_s = 1000000;
48 static const u_int ns_per_s = 1000000000;
49 static const u_int ns_per_us = 1000;
52 efirtc_identify(driver_t *driver, device_t parent)
55 /* Don't add the driver unless we have working runtime services. */
58 if (device_find_child(parent, "efirtc", -1) != NULL)
60 if (BUS_ADD_CHILD(parent, 0, "efirtc", -1) == NULL)
61 device_printf(parent, "add child failed\n");
65 efirtc_probe(device_t dev)
71 * Check whether we can read the time. This will stop us from attaching
72 * when there is EFI Runtime support but the gettime function is
73 * unimplemented, e.g. on some builds of U-Boot.
75 if ((error = efi_get_time(&tm)) != 0) {
77 device_printf(dev, "cannot read EFI realtime clock, "
81 device_set_desc(dev, "EFI Realtime Clock");
82 return (BUS_PROBE_DEFAULT);
86 efirtc_attach(device_t dev)
88 struct efi_tmcap tmcap;
92 bzero(&tmcap, sizeof(tmcap));
93 if ((error = efi_get_time_capabilities(&tmcap)) != 0) {
94 device_printf(dev, "cannot get EFI time capabilities");
98 /* Translate resolution in Hz to tick length in usec. */
99 if (tmcap.tc_res == 0)
100 res = us_per_s; /* 0 is insane, assume 1 Hz. */
101 else if (tmcap.tc_res > us_per_s)
102 res = 1; /* 1us is the best we can represent */
104 res = us_per_s / tmcap.tc_res;
106 /* Clock rounding adjustment is 1/2 of resolution, in nsec. */
107 efirtc_resadj.tv_nsec = (res * ns_per_us) / 2;
109 /* Does the clock zero the subseconds when time is set? */
110 efirtc_zeroes_subseconds = tmcap.tc_stz;
113 * Register. If the clock zeroes out the subseconds when it's set,
114 * schedule the SetTime calls to happen just before top-of-second.
116 clock_register_flags(dev, res, CLOCKF_SETTIME_NO_ADJ);
117 if (efirtc_zeroes_subseconds)
118 clock_schedule(dev, ns_per_s - ns_per_us);
124 efirtc_detach(device_t dev)
127 clock_unregister(dev);
132 efirtc_gettime(device_t dev, struct timespec *ts)
138 error = efi_get_time(&tm);
144 ct.hour = tm.tm_hour;
147 ct.year = tm.tm_year;
148 ct.nsec = tm.tm_nsec;
150 clock_dbgprint_ct(dev, CLOCK_DBG_READ, &ct);
151 return (clock_ct_to_ts(&ct, ts));
155 efirtc_settime(device_t dev, struct timespec *ts)
161 * We request a timespec with no resolution-adjustment so that we can
162 * apply it ourselves based on whether or not the clock zeroes the
163 * sub-second part of the time when setting the time.
165 ts->tv_sec -= utc_offset();
166 if (!efirtc_zeroes_subseconds)
167 timespecadd(ts, &efirtc_resadj, ts);
169 clock_ts_to_ct(ts, &ct);
170 clock_dbgprint_ct(dev, CLOCK_DBG_WRITE, &ct);
172 bzero(&tm, sizeof(tm));
175 tm.tm_hour = ct.hour;
178 tm.tm_year = ct.year;
179 tm.tm_nsec = ct.nsec;
181 return (efi_set_time(&tm));
184 static device_method_t efirtc_methods[] = {
185 /* Device interface */
186 DEVMETHOD(device_identify, efirtc_identify),
187 DEVMETHOD(device_probe, efirtc_probe),
188 DEVMETHOD(device_attach, efirtc_attach),
189 DEVMETHOD(device_detach, efirtc_detach),
191 /* Clock interface */
192 DEVMETHOD(clock_gettime, efirtc_gettime),
193 DEVMETHOD(clock_settime, efirtc_settime),
198 static devclass_t efirtc_devclass;
199 static driver_t efirtc_driver = {
205 DRIVER_MODULE(efirtc, nexus, efirtc_driver, efirtc_devclass, 0, 0);
206 MODULE_VERSION(efirtc, 1);
207 MODULE_DEPEND(efirtc, efirt, 1, 1, 1);