From owner-svn-src-head@FreeBSD.ORG Sun Oct 7 20:36:47 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 642911065708; Sun, 7 Oct 2012 20:36:47 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 4F0468FC23; Sun, 7 Oct 2012 20:36:47 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q97Kal7O066722; Sun, 7 Oct 2012 20:36:47 GMT (envelope-from imp@svn.freebsd.org) Received: (from imp@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q97KalP6066720; Sun, 7 Oct 2012 20:36:47 GMT (envelope-from imp@svn.freebsd.org) Message-Id: <201210072036.q97KalP6066720@svn.freebsd.org> From: Warner Losh Date: Sun, 7 Oct 2012 20:36:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r241333 - head/sys/arm/at91 X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 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: Sun, 07 Oct 2012 20:36:47 -0000 Author: imp Date: Sun Oct 7 20:36:46 2012 New Revision: 241333 URL: http://svn.freebsd.org/changeset/base/241333 Log: Loop reading the RTC registers until the same values are obtained twice, as advised in the atmel docs. Submitted by: Ian Lapore Modified: head/sys/arm/at91/at91_rtc.c Modified: head/sys/arm/at91/at91_rtc.c ============================================================================== --- head/sys/arm/at91/at91_rtc.c Sun Oct 7 20:17:24 2012 (r241332) +++ head/sys/arm/at91/at91_rtc.c Sun Oct 7 20:36:46 2012 (r241333) @@ -256,7 +256,7 @@ static int at91_rtc_gettime(device_t dev, struct timespec *ts) { struct clocktime ct; - uint32_t timr, calr; + uint32_t calr, calr2, timr, timr2; struct at91_rtc_softc *sc; sc = device_get_softc(dev); @@ -266,8 +266,19 @@ at91_rtc_gettime(device_t dev, struct ti if (RD4(sc, RTC_VER) & (RTC_VER_NVTIM | RTC_VER_NVCAL)) return EINVAL; - timr = RD4(sc, RTC_TIMR); - calr = RD4(sc, RTC_CALR); + /* + * The RTC hardware can update registers while the CPU is reading them. + * The manual advises reading until you obtain the same values twice. + * Interleaving the reads (rather than timr, timr2, calr, calr2 order) + * also ensures we don't miss a midnight rollover/carry between reads. + */ + do { + timr = RD4(sc, RTC_TIMR); + calr = RD4(sc, RTC_CALR); + timr2 = RD4(sc, RTC_TIMR); + calr2 = RD4(sc, RTC_CALR); + } while (timr != timr2 || calr != calr2); + ct.nsec = 0; ct.sec = RTC_TIMR_SEC(timr); ct.min = RTC_TIMR_MIN(timr);