From owner-freebsd-arm@freebsd.org Thu Jul 16 02:32:28 2020 Return-Path: Delivered-To: freebsd-arm@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 32322350633 for ; Thu, 16 Jul 2020 02:32:28 +0000 (UTC) (envelope-from ian@freebsd.org) Received: from outbound5a.ore.mailhop.org (outbound5a.ore.mailhop.org [44.233.67.66]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4B6dZM5NfYz3ZsQ for ; Thu, 16 Jul 2020 02:32:27 +0000 (UTC) (envelope-from ian@freebsd.org) ARC-Seal: i=1; a=rsa-sha256; t=1594866740; cv=none; d=outbound.mailhop.org; s=arc-outbound20181012; b=Qz0cngKP47gKaJR/OIyVox8Vq1vBTfZB35yC9A3E6YLGlWyWez7ToWWAI4UenBHzKutr5ZdXKl2J4 PWTATk5pukuiNpW0OuPxgZ+kk/sDtky0JOzuvjSlf94a/dBW+fpBtOLiRXKcEuHawimF2Dem7A4hcO h/bSX/JMEancaboOFJbtfUDCS8+veJrASIJ1TrKTCqq7zwsIUSCiPLehAG4aaoYE+VLWJ1aOughNeb +wWiG2vHKss6z551M/Sp8tFWnTFiltQKbSXZs9aN8CRJb1vWSBk4BlH3OBq3E7a4OxpIGfUeNipkSG DWBpgWEIPFYKqs9nhX+L/YStyRz96Ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=arc-outbound20181012; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:dkim-signature:from; bh=i/xaeSRIdgGGKMp98pal88IYPLAkqmzFzmSJm1xe5Fk=; b=S07u+t+R0tLzlBCOrZ+y9q/aXEHD0I1GLnDe16t4Lmjnr7OtDxGDrfD/SMJefQZ4ExTfm4+w3W9yA vwmB5vPmBx1yTqD5pEBoGIVi+iOlx2RppiX4YduPallqntlRdRvE/d1XHwjvH/EX42T37I8em1xjhe 5V0jt2xdIScEUBtjvtzm3uP7if6xsjR8oaRhpumnQh9TX9qMHM9REfPUXq4TlUmiwGPuoRM3Lu+m5u WkcGYWWfK+7sB4ZkngyfFIGdJwbnf6+Ak+Om54E/mpA9Pr9+gBuUZ6fMqTySNhjaWOYZLtVmsZk+2z kAzDYSkzlohSLm4dPAzIKSOods6kFaQ== ARC-Authentication-Results: i=1; outbound3.ore.mailhop.org; spf=softfail smtp.mailfrom=freebsd.org smtp.remote-ip=67.177.211.60; dmarc=none header.from=freebsd.org; arc=none header.oldest-pass=0; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=dkim-high; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:from; bh=i/xaeSRIdgGGKMp98pal88IYPLAkqmzFzmSJm1xe5Fk=; b=tXZ0SRaK10dXYmOSo2nqm9sLzi4RX1PafNkOS3j07N93n6sxKDxgEaM5P7LAhJRUBGkLXF4otG9b7 2v6kK5jQzTbz78arhTq6uj8ocz7xWdlRz74+s8vosYUI8oQYKCBbQ7YGzieJMT8hOS9dWF81vnPdQy UVbxE9hRImrBm7i9U0XuQTwFXx/xXN9poiSU2O4Rii9Re4LPrvd5QlelqZ5emFkZVtiskbJv7vcEK9 hgGsAbVNkp4WQw5v+r68aUjN8KRf4QDYUKx90z5b5qAcIY+DHzKytcGihH3efFPaHbf7E509HQA03X +/YjKhxTO/zBq0hxlSipcDB30mrQ9jg== X-MHO-RoutePath: aGlwcGll X-MHO-User: 909ddac2-c70c-11ea-a2ba-9f0c275c2f69 X-Report-Abuse-To: https://support.duocircle.com/support/solutions/articles/5000540958-duocircle-standard-smtp-abuse-information X-Originating-IP: 67.177.211.60 X-Mail-Handler: DuoCircle Outbound SMTP Received: from ilsoft.org (c-67-177-211-60.hsd1.co.comcast.net [67.177.211.60]) by outbound3.ore.mailhop.org (Halon) with ESMTPSA id 909ddac2-c70c-11ea-a2ba-9f0c275c2f69; Thu, 16 Jul 2020 02:32:18 +0000 (UTC) Received: from rev (rev [172.22.42.240]) by ilsoft.org (8.15.2/8.15.2) with ESMTP id 06G2WGxH053596; Wed, 15 Jul 2020 20:32:16 -0600 (MDT) (envelope-from ian@freebsd.org) Message-ID: <99ec5bfbe697e0cbf8b6262783256bffcbf55f55.camel@freebsd.org> Subject: Re: DS3231 on BeagleBone Black with FreeBSD 13-CURRENT exactly 20 h off backwards From: Ian Lepore To: "Dr. Rolf Jansen" , freebsd-arm@freebsd.org Date: Wed, 15 Jul 2020 20:32:16 -0600 In-Reply-To: References: <3BE2A8B4-AD53-4DFE-8C38-D5BB4063CFE9@obsigna.com> Content-Type: text/plain; charset="iso-8859-13" X-Mailer: Evolution 3.28.5 FreeBSD GNOME Team Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4B6dZM5NfYz3ZsQ X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [0.00 / 15.00]; ASN(0.00)[asn:16509, ipnet:44.224.0.0/11, country:US]; local_wl_from(0.00)[freebsd.org] X-BeenThere: freebsd-arm@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "Porting FreeBSD to ARM processors." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Jul 2020 02:32:28 -0000 On Wed, 2020-07-15 at 12:15 -0300, Dr. Rolf Jansen wrote: > > Am 15.07.2020 um 09:52 schrieb Dr. Rolf Jansen < > > freebsd-rj@obsigna.com>: > > > > I added a DS3231 module to the i2c2 bus of the BBB running 13- > > CURRENT. Everything work fine, except that when I set a time in > > the range of 20:00 to 24:00 UTC, then on starting up the RTC > > reports a date/time of exactly 20 hours off backwards. While, when > > I set a time in the range from 0:00 to 19:59 UTC, it would be > > correctly stored by the RTC. > > > > Looking at the Maxim DS3231 datasheet ( > > https://datasheets.maximintegrated.com/en/ds/DS3231.pdf#page=11 < > > https://datasheets.maximintegrated.com/en/ds/DS3231.pdf#page=11>), > > it might be that something gets mixed-up when setting bits 5 and 6 > > of the hours register. In the history of ds3231.c, I saw that 24 > > hour mode is not more forced anymore. Perhaps an unresolved > > ambiguity was introduced by this change. > > > > BTW: the following looks strange: > > > > https://github.com/freebsd/freebsd/blob/b2d136be8c26e5efaf82b7bb25432207a682e250/sys/dev/iicbus/ds3231.c#L526 > > < > > https://github.com/freebsd/freebsd/blob/b2d136be8c26e5efaf82b7bb25432207a682e250/sys/dev/iicbus/ds3231.c#L526 > > > > > > > This would add 256 instead of 100 when rolling over the century, > > really? On real world systems this will let to a Year-2100 problem, > > better we solve this quickly, since the time is running, and 80 > > years compares to nothing on the geologic time scale :-D > > For the time being I resolved the issue for me, by completely > dropping AM/PM support - I don˙t need it, and I anyway always need to > remember that AM means (Am Morgen :-). > > DS3231_HOUR_MASK_24HR is definitely wrong, since this prevents the > setting of times above 20 h. Reading said data sheet, I am almost > sure, that DS3231_HOUR_MASK_24HR must be the same as > DS3231_HOUR_MASK_12HR = 0x3f. > > > # svn diff https://svn.freebsd.org/base/head/sys/dev/iicbus/ds3231.c > ./ds3231.c > Index: ds3231.c > =================================================================== > --- ds3231.c (revision 363224) > +++ ds3231.c (working copy) > @@ -62,7 +62,6 @@ > uint16_t sc_addr; /* DS3231 slave address. */ > uint8_t sc_ctrl; > uint8_t sc_status; > - bool sc_use_ampm; > }; > > static void ds3231_start(void *); > @@ -142,6 +141,25 @@ > } > > static int > +ds3231_set_24hrs_mode(struct ds3231_softc *sc) > +{ > + int error; > + uint8_t hour; > + > + error = ds3231_read1(sc->sc_dev, DS3231_HOUR, &hour); > + if (error) { > + device_printf(sc->sc_dev, "cannot read from RTC.\n"); > + return (error); > + } > + hour &= ~DS3231_C_MASK; > + error = ds3231_write1(sc->sc_dev, DS3231_HOUR, hour); > + if (error != 0) > + device_printf(sc->sc_dev, "cannot write to RTC.\n"); > + > + return (error); > +} > + > +static int > ds3231_temp_read(struct ds3231_softc *sc, int *temp) > { > int error, neg, t; > @@ -440,6 +458,10 @@ > ds3231_status_write(sc, 1, 1); > ds3231_ctrl_write(sc); > > + /* Set the 24 hours mode. */ > + if (ds3231_set_24hrs_mode(sc) != 0) > + return; > + > /* Temperature. */ > SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature", > CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, > @@ -479,7 +501,7 @@ > int c, error; > struct bcd_clocktime bct; > struct ds3231_softc *sc; > - uint8_t data[7], hourmask; > + uint8_t data[7]; > > sc = device_get_softc(dev); > > @@ -498,17 +520,10 @@ > return (error); > } > > - /* If chip is in AM/PM mode remember that. */ > - if (data[DS3231_HOUR] & DS3231_HOUR_USE_AMPM) { > - sc->sc_use_ampm = true; > - hourmask = DS3231_HOUR_MASK_12HR; > - } else > - hourmask = DS3231_HOUR_MASK_24HR; > - > bct.nsec = 0; > bct.sec = data[DS3231_SECS] & DS3231_SECS_MASK; > bct.min = data[DS3231_MINS] & DS3231_MINS_MASK; > - bct.hour = data[DS3231_HOUR] & hourmask; > + bct.hour = data[DS3231_HOUR] & DS3231_HOUR_MASK_12HR; > bct.day = data[DS3231_DATE] & DS3231_DATE_MASK; > bct.mon = data[DS3231_MONTH] & DS3231_MONTH_MASK; > bct.year = data[DS3231_YEAR] & DS3231_YEAR_MASK; > @@ -523,13 +538,13 @@ > if (sc->sc_last_c == -1) > sc->sc_last_c = c; > else if (c != sc->sc_last_c) { > - sc->sc_year0 += 0x100; > + sc->sc_year0 += 100; > sc->sc_last_c = c; > } > bct.year |= sc->sc_year0; > > clock_dbgprint_bcd(sc->sc_dev, CLOCK_DBG_READ, &bct); > - return (clock_bcd_to_ts(&bct, ts, sc->sc_use_ampm)); > + return (clock_bcd_to_ts(&bct, ts, false)); > } > > static int > @@ -539,7 +554,6 @@ > struct bcd_clocktime bct; > struct ds3231_softc *sc; > uint8_t data[7]; > - uint8_t pmflags; > > sc = device_get_softc(dev); > > @@ -548,20 +562,13 @@ > * disables utc adjustment, so apply that ourselves. > */ > ts->tv_sec -= utc_offset(); > - clock_ts_to_bcd(ts, &bct, sc->sc_use_ampm); > + clock_ts_to_bcd(ts, &bct, false); > clock_dbgprint_bcd(sc->sc_dev, CLOCK_DBG_WRITE, &bct); > > - /* If the chip is in AM/PM mode, adjust hour and set flags as > needed. */ > - if (sc->sc_use_ampm) { > - pmflags = DS3231_HOUR_USE_AMPM; > - if (bct.ispm) > - pmflags |= DS3231_HOUR_IS_PM; > - } else > - pmflags = 0; > > data[DS3231_SECS] = bct.sec; > data[DS3231_MINS] = bct.min; > - data[DS3231_HOUR] = bct.hour | pmflags; > + data[DS3231_HOUR] = bct.hour | 0b01000000; > data[DS3231_DATE] = bct.day; > data[DS3231_WEEKDAY] = bct.dow + 1; > data[DS3231_MONTH] = bct.mon; > The driver originally forced the chip into 24-hour mode. I'm the one who added support for 12 or 24 hour mode, so this is probably my fault. It seems nicer to me to try to deal with whatever mode the chip is already in (in case you're dual-booting into some other OS that wants it to be a certain way). I'm pretty sure I've got one of those chips around here somewhere, I'll find some time this weekend to get the driver fixed. -- Ian