1a3dff126SAndrew Turner /*- 2a3dff126SAndrew Turner * Copyright (c) 2017 Andrew Turner 3a3dff126SAndrew Turner * All rights reserved. 4a3dff126SAndrew Turner * 5a3dff126SAndrew Turner * This software was developed by SRI International and the University of 6a3dff126SAndrew Turner * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 7a3dff126SAndrew Turner * ("CTSRD"), as part of the DARPA CRASH research programme. 8a3dff126SAndrew Turner * 9a3dff126SAndrew Turner * Redistribution and use in source and binary forms, with or without 10a3dff126SAndrew Turner * modification, are permitted provided that the following conditions 11a3dff126SAndrew Turner * are met: 12a3dff126SAndrew Turner * 1. Redistributions of source code must retain the above copyright 13a3dff126SAndrew Turner * notice, this list of conditions and the following disclaimer. 14a3dff126SAndrew Turner * 2. Redistributions in binary form must reproduce the above copyright 15a3dff126SAndrew Turner * notice, this list of conditions and the following disclaimer in the 16a3dff126SAndrew Turner * documentation and/or other materials provided with the distribution. 17a3dff126SAndrew Turner * 18a3dff126SAndrew Turner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19a3dff126SAndrew Turner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20a3dff126SAndrew Turner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21a3dff126SAndrew Turner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22a3dff126SAndrew Turner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23a3dff126SAndrew Turner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24a3dff126SAndrew Turner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25a3dff126SAndrew Turner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26a3dff126SAndrew Turner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27a3dff126SAndrew Turner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28a3dff126SAndrew Turner * SUCH DAMAGE. 29a3dff126SAndrew Turner */ 30a3dff126SAndrew Turner 31a3dff126SAndrew Turner #include <sys/param.h> 32a3dff126SAndrew Turner #include <sys/systm.h> 33a3dff126SAndrew Turner #include <sys/bus.h> 34a3dff126SAndrew Turner #include <sys/clock.h> 35a3dff126SAndrew Turner #include <sys/efi.h> 36a3dff126SAndrew Turner #include <sys/kernel.h> 37a3dff126SAndrew Turner #include <sys/module.h> 38a3dff126SAndrew Turner 39a3dff126SAndrew Turner #include "clock_if.h" 40a3dff126SAndrew Turner 419c45f7b4SIan Lepore static bool efirtc_zeroes_subseconds; 429c45f7b4SIan Lepore static struct timespec efirtc_resadj; 439c45f7b4SIan Lepore 449c45f7b4SIan Lepore static const u_int us_per_s = 1000000; 459c45f7b4SIan Lepore static const u_int ns_per_s = 1000000000; 469c45f7b4SIan Lepore static const u_int ns_per_us = 1000; 479c45f7b4SIan Lepore 48a3dff126SAndrew Turner static void 49a3dff126SAndrew Turner efirtc_identify(driver_t *driver, device_t parent) 50a3dff126SAndrew Turner { 51a3dff126SAndrew Turner 529c45f7b4SIan Lepore /* Don't add the driver unless we have working runtime services. */ 539c45f7b4SIan Lepore if (efi_rt_ok() != 0) 54a3dff126SAndrew Turner return; 55a3dff126SAndrew Turner if (device_find_child(parent, "efirtc", -1) != NULL) 56a3dff126SAndrew Turner return; 57a3dff126SAndrew Turner if (BUS_ADD_CHILD(parent, 0, "efirtc", -1) == NULL) 58a3dff126SAndrew Turner device_printf(parent, "add child failed\n"); 59a3dff126SAndrew Turner } 60a3dff126SAndrew Turner 61a3dff126SAndrew Turner static int 62a3dff126SAndrew Turner efirtc_probe(device_t dev) 63a3dff126SAndrew Turner { 649c45f7b4SIan Lepore struct efi_tm tm; 659c45f7b4SIan Lepore int error; 66a3dff126SAndrew Turner 679c45f7b4SIan Lepore /* 689c45f7b4SIan Lepore * Check whether we can read the time. This will stop us from attaching 699c45f7b4SIan Lepore * when there is EFI Runtime support but the gettime function is 709c45f7b4SIan Lepore * unimplemented, e.g. on some builds of U-Boot. 719c45f7b4SIan Lepore */ 729c45f7b4SIan Lepore if ((error = efi_get_time(&tm)) != 0) { 739c45f7b4SIan Lepore if (bootverbose) 7442038236SKonstantin Belousov device_printf(dev, "cannot read EFI realtime clock, " 7542038236SKonstantin Belousov "error %d\n", error); 769c45f7b4SIan Lepore return (error); 779c45f7b4SIan Lepore } 789c45f7b4SIan Lepore device_set_desc(dev, "EFI Realtime Clock"); 799c45f7b4SIan Lepore return (BUS_PROBE_DEFAULT); 80a3dff126SAndrew Turner } 81a3dff126SAndrew Turner 82a3dff126SAndrew Turner static int 83a3dff126SAndrew Turner efirtc_attach(device_t dev) 84a3dff126SAndrew Turner { 859c45f7b4SIan Lepore struct efi_tmcap tmcap; 869c45f7b4SIan Lepore long res; 879c45f7b4SIan Lepore int error; 88a3dff126SAndrew Turner 899c45f7b4SIan Lepore bzero(&tmcap, sizeof(tmcap)); 909c45f7b4SIan Lepore if ((error = efi_get_time_capabilities(&tmcap)) != 0) { 919c45f7b4SIan Lepore device_printf(dev, "cannot get EFI time capabilities"); 929c45f7b4SIan Lepore return (error); 939c45f7b4SIan Lepore } 949c45f7b4SIan Lepore 959c45f7b4SIan Lepore /* Translate resolution in Hz to tick length in usec. */ 969c45f7b4SIan Lepore if (tmcap.tc_res == 0) 979c45f7b4SIan Lepore res = us_per_s; /* 0 is insane, assume 1 Hz. */ 989c45f7b4SIan Lepore else if (tmcap.tc_res > us_per_s) 999c45f7b4SIan Lepore res = 1; /* 1us is the best we can represent */ 1009c45f7b4SIan Lepore else 1019c45f7b4SIan Lepore res = us_per_s / tmcap.tc_res; 1029c45f7b4SIan Lepore 1039c45f7b4SIan Lepore /* Clock rounding adjustment is 1/2 of resolution, in nsec. */ 1049c45f7b4SIan Lepore efirtc_resadj.tv_nsec = (res * ns_per_us) / 2; 1059c45f7b4SIan Lepore 1069c45f7b4SIan Lepore /* Does the clock zero the subseconds when time is set? */ 1079c45f7b4SIan Lepore efirtc_zeroes_subseconds = tmcap.tc_stz; 1089c45f7b4SIan Lepore 1099c45f7b4SIan Lepore /* 1109c45f7b4SIan Lepore * Register. If the clock zeroes out the subseconds when it's set, 1119c45f7b4SIan Lepore * schedule the SetTime calls to happen just before top-of-second. 1129c45f7b4SIan Lepore */ 1139c45f7b4SIan Lepore clock_register_flags(dev, res, CLOCKF_SETTIME_NO_ADJ); 1149c45f7b4SIan Lepore if (efirtc_zeroes_subseconds) 1159c45f7b4SIan Lepore clock_schedule(dev, ns_per_s - ns_per_us); 1169c45f7b4SIan Lepore 117a3dff126SAndrew Turner return (0); 118a3dff126SAndrew Turner } 119a3dff126SAndrew Turner 120a3dff126SAndrew Turner static int 121a3dff126SAndrew Turner efirtc_detach(device_t dev) 122a3dff126SAndrew Turner { 123a3dff126SAndrew Turner 124a3dff126SAndrew Turner clock_unregister(dev); 125a3dff126SAndrew Turner return (0); 126a3dff126SAndrew Turner } 127a3dff126SAndrew Turner 128a3dff126SAndrew Turner static int 129a3dff126SAndrew Turner efirtc_gettime(device_t dev, struct timespec *ts) 130a3dff126SAndrew Turner { 131a3dff126SAndrew Turner struct clocktime ct; 132a3dff126SAndrew Turner struct efi_tm tm; 133a3dff126SAndrew Turner int error; 134a3dff126SAndrew Turner 135a3dff126SAndrew Turner error = efi_get_time(&tm); 136a3dff126SAndrew Turner if (error != 0) 137a3dff126SAndrew Turner return (error); 138a3dff126SAndrew Turner 139a3dff126SAndrew Turner ct.sec = tm.tm_sec; 140a3dff126SAndrew Turner ct.min = tm.tm_min; 141a3dff126SAndrew Turner ct.hour = tm.tm_hour; 142a3dff126SAndrew Turner ct.day = tm.tm_mday; 143a3dff126SAndrew Turner ct.mon = tm.tm_mon; 144a3dff126SAndrew Turner ct.year = tm.tm_year; 145a3dff126SAndrew Turner ct.nsec = tm.tm_nsec; 146a3dff126SAndrew Turner 1479c45f7b4SIan Lepore clock_dbgprint_ct(dev, CLOCK_DBG_READ, &ct); 148a3dff126SAndrew Turner return (clock_ct_to_ts(&ct, ts)); 149a3dff126SAndrew Turner } 150a3dff126SAndrew Turner 151a3dff126SAndrew Turner static int 152a3dff126SAndrew Turner efirtc_settime(device_t dev, struct timespec *ts) 153a3dff126SAndrew Turner { 154a3dff126SAndrew Turner struct clocktime ct; 155a3dff126SAndrew Turner struct efi_tm tm; 156a3dff126SAndrew Turner 1579c45f7b4SIan Lepore /* 1589c45f7b4SIan Lepore * We request a timespec with no resolution-adjustment so that we can 1599c45f7b4SIan Lepore * apply it ourselves based on whether or not the clock zeroes the 1609c45f7b4SIan Lepore * sub-second part of the time when setting the time. 1619c45f7b4SIan Lepore */ 1629c45f7b4SIan Lepore ts->tv_sec -= utc_offset(); 1639c45f7b4SIan Lepore if (!efirtc_zeroes_subseconds) 1646040822cSAlan Somers timespecadd(ts, &efirtc_resadj, ts); 1659c45f7b4SIan Lepore 166a3dff126SAndrew Turner clock_ts_to_ct(ts, &ct); 1679c45f7b4SIan Lepore clock_dbgprint_ct(dev, CLOCK_DBG_WRITE, &ct); 168a3dff126SAndrew Turner 169a72d6c89SAndrew Turner bzero(&tm, sizeof(tm)); 170a3dff126SAndrew Turner tm.tm_sec = ct.sec; 171a3dff126SAndrew Turner tm.tm_min = ct.min; 172a3dff126SAndrew Turner tm.tm_hour = ct.hour; 173a3dff126SAndrew Turner tm.tm_mday = ct.day; 174a3dff126SAndrew Turner tm.tm_mon = ct.mon; 175a3dff126SAndrew Turner tm.tm_year = ct.year; 176a3dff126SAndrew Turner tm.tm_nsec = ct.nsec; 177a3dff126SAndrew Turner 178a3dff126SAndrew Turner return (efi_set_time(&tm)); 179a3dff126SAndrew Turner } 180a3dff126SAndrew Turner 181a3dff126SAndrew Turner static device_method_t efirtc_methods[] = { 182a3dff126SAndrew Turner /* Device interface */ 183a3dff126SAndrew Turner DEVMETHOD(device_identify, efirtc_identify), 184a3dff126SAndrew Turner DEVMETHOD(device_probe, efirtc_probe), 185a3dff126SAndrew Turner DEVMETHOD(device_attach, efirtc_attach), 186a3dff126SAndrew Turner DEVMETHOD(device_detach, efirtc_detach), 187a3dff126SAndrew Turner 188a3dff126SAndrew Turner /* Clock interface */ 189a3dff126SAndrew Turner DEVMETHOD(clock_gettime, efirtc_gettime), 190a3dff126SAndrew Turner DEVMETHOD(clock_settime, efirtc_settime), 191a3dff126SAndrew Turner 192a3dff126SAndrew Turner DEVMETHOD_END 193a3dff126SAndrew Turner }; 194a3dff126SAndrew Turner 195a3dff126SAndrew Turner static driver_t efirtc_driver = { 196a3dff126SAndrew Turner "efirtc", 197a3dff126SAndrew Turner efirtc_methods, 198a3dff126SAndrew Turner 0 199a3dff126SAndrew Turner }; 200a3dff126SAndrew Turner 201*7bb02a79SJohn Baldwin DRIVER_MODULE(efirtc, nexus, efirtc_driver, 0, 0); 2021bf1d84aSIan Lepore MODULE_VERSION(efirtc, 1); 2031bf1d84aSIan Lepore MODULE_DEPEND(efirtc, efirt, 1, 1, 1); 204