From owner-svn-src-head@freebsd.org Mon Jan 15 16:40:45 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 379A4EB934D; Mon, 15 Jan 2018 16:40:45 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 141187FDE2; Mon, 15 Jan 2018 16:40:45 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 424F013A94; Mon, 15 Jan 2018 16:40:44 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w0FGeiHM044712; Mon, 15 Jan 2018 16:40:44 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w0FGein7044711; Mon, 15 Jan 2018 16:40:44 GMT (envelope-from ian@FreeBSD.org) Message-Id: <201801151640.w0FGein7044711@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Mon, 15 Jan 2018 16:40:44 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r328005 - head/sys/x86/isa X-SVN-Group: head X-SVN-Commit-Author: ian X-SVN-Commit-Paths: head/sys/x86/isa X-SVN-Commit-Revision: 328005 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Jan 2018 16:40:45 -0000 Author: ian Date: Mon Jan 15 16:40:43 2018 New Revision: 328005 URL: https://svnweb.freebsd.org/changeset/base/328005 Log: Convert the x86 RTC driver to use new validated BCD<->timespec conversions. New common routines were added to kern/subr_clock.c for converting between calendrical time expressed in BCD and struct timespec. The new functions return EINVAL on error, as expected when the clock hardware does not provide valid time. PR: 224813 Differential Revision: https://reviews.freebsd.org/D13731 (no reviewers) Modified: head/sys/x86/isa/atrtc.c Modified: head/sys/x86/isa/atrtc.c ============================================================================== --- head/sys/x86/isa/atrtc.c Mon Jan 15 16:08:34 2018 (r328004) +++ head/sys/x86/isa/atrtc.c Mon Jan 15 16:40:43 2018 (r328005) @@ -109,17 +109,6 @@ writertc(int reg, u_char val) RTC_UNLOCK; } -static __inline int -readrtc(int port) -{ - int readval; - - readval = rtcin(port); - if (readval >= 0 && (readval & 0xf) < 0xa && (readval & 0xf0) < 0xa0) - return (bcd2bin(readval)); - return (0); -} - static void atrtc_start(void) { @@ -169,25 +158,24 @@ atrtc_restore(void) static void atrtc_set(struct timespec *ts) { - struct clocktime ct; + struct bcd_clocktime ct; - clock_ts_to_ct(ts, &ct); + clock_ts_to_bcd(ts, &ct, false); mtx_lock(&atrtc_time_lock); /* Disable RTC updates and interrupts. */ writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR); - writertc(RTC_SEC, bin2bcd(ct.sec)); /* Write back Seconds */ - writertc(RTC_MIN, bin2bcd(ct.min)); /* Write back Minutes */ - writertc(RTC_HRS, bin2bcd(ct.hour)); /* Write back Hours */ - - writertc(RTC_WDAY, ct.dow + 1); /* Write back Weekday */ - writertc(RTC_DAY, bin2bcd(ct.day)); /* Write back Day */ - writertc(RTC_MONTH, bin2bcd(ct.mon)); /* Write back Month */ - writertc(RTC_YEAR, bin2bcd(ct.year % 100)); /* Write back Year */ + writertc(RTC_SEC, ct.sec); /* Write back Seconds */ + writertc(RTC_MIN, ct.min); /* Write back Minutes */ + writertc(RTC_HRS, ct.hour); /* Write back Hours */ + writertc(RTC_WDAY, ct.dow + 1); /* Write back Weekday */ + writertc(RTC_DAY, ct.day); /* Write back Day */ + writertc(RTC_MONTH, ct.mon); /* Write back Month */ + writertc(RTC_YEAR, ct.year & 0xff); /* Write back Year */ #ifdef USE_RTC_CENTURY - writertc(RTC_CENTURY, bin2bcd(ct.year / 100)); /* ... and Century */ + writertc(RTC_CENTURY, ct.year >> 8); /* ... and Century */ #endif /* Re-enable RTC updates and interrupts. */ @@ -351,7 +339,7 @@ atrtc_settime(device_t dev __unused, struct timespec * static int atrtc_gettime(device_t dev, struct timespec *ts) { - struct clocktime ct; + struct bcd_clocktime ct; /* Look if we have a RTC present and the time is valid */ if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) { @@ -370,24 +358,21 @@ atrtc_gettime(device_t dev, struct timespec *ts) while (rtcin(RTC_STATUSA) & RTCSA_TUP) continue; critical_enter(); - ct.nsec = 0; - ct.sec = readrtc(RTC_SEC); - ct.min = readrtc(RTC_MIN); - ct.hour = readrtc(RTC_HRS); - ct.day = readrtc(RTC_DAY); - ct.dow = readrtc(RTC_WDAY) - 1; - ct.mon = readrtc(RTC_MONTH); - ct.year = readrtc(RTC_YEAR); + ct.sec = rtcin(RTC_SEC); + ct.min = rtcin(RTC_MIN); + ct.hour = rtcin(RTC_HRS); + ct.day = rtcin(RTC_DAY); + ct.mon = rtcin(RTC_MONTH); + ct.year = rtcin(RTC_YEAR); #ifdef USE_RTC_CENTURY - ct.year += readrtc(RTC_CENTURY) * 100; -#else - ct.year += (ct.year < 80 ? 2000 : 1900); + ct.year |= rtcin(RTC_CENTURY) << 8; #endif critical_exit(); mtx_unlock(&atrtc_time_lock); - /* Set dow = -1 because some clocks don't set it correctly. */ - ct.dow = -1; - return (clock_ct_to_ts(&ct, ts)); + /* dow is unused in timespec conversion and we have no nsec info. */ + ct.dow = 0; + ct.nsec = 0; + return (clock_bcd_to_ts(&ct, ts, false)); } static device_method_t atrtc_methods[] = {