Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2364132pxk; Sat, 3 Oct 2020 18:45:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy62Zr8PuoKIEauLYlsg53uLh+Lcc/AFoiIwRhQb1x6oPbHX/WdLsLqUFG+cqOwXmjB+LoQ X-Received: by 2002:a17:906:7113:: with SMTP id x19mr8669031ejj.59.1601775941377; Sat, 03 Oct 2020 18:45:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601775941; cv=none; d=google.com; s=arc-20160816; b=p7Vh/oVxyFcSycrvF2y7P9wm1PhfTKswaR9S9XAgBzHlZbcqb8g2YWgQ8N0VCuTIsp B9WM0vLyUpB1jLfYNjGjKNJCVgTiHFOvzquqva5qeSxeS7npexhn8h9eI9AP7K2Z62+1 Nd4cgS166dlpVq3yFvjm7eX2LV83Co6UkbNL2oGSt07CKMjVqDFScBr4I5VxkzCjoI06 QmQAgP8rjWWOd0bjTj9mGjepUNN2EJXezqq75i/x/Hy6bDBWxfVC6D7bp2jDKbgI++Dd 3bhgVCwmeEi79s04cksZloJ0JbDa2Nghau7MTOKHrk9dEL2JstnDXDjLXMl+ZR6VaAXq OFGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ugdh1gptypTtvfFihSWynQ9ocpWl4t8EpGkozjWIQQ0=; b=eI3jNz7NLIGtqEgg1ivn9/ycc4zdbUgPmVg3pONfLeDpOVrp9GmthgRNeHKlbi78Pz dwHPE1uCmDGfqyz7eSxfd0n5QNDqB+2pRGPC4FyDok+0OIRqutlsHjDXJw+LXSGK6Z67 wYDHf9WQ9jmQG6cj3YNORbbAe3wVntJZqWoPs0TAmwOBrtYJJ8AfmJqd+TMmKkqNrnlI O1y71mAlhZVt8HIPK8qgDFhK9VWR+K+pPct2u1y1XYsjIslz8BkVOOvaLGa+UhNKxZBI GAis+UxV0GvFC0fBDD8rF6lz9E5PTjsSKnU9nceQXEljMWUjkA4pVk2gKPNz2Shh6awX JTNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=lkVvKEah; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l14si4233330edv.409.2020.10.03.18.45.19; Sat, 03 Oct 2020 18:45:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=lkVvKEah; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726251AbgJDBoa (ORCPT + 99 others); Sat, 3 Oct 2020 21:44:30 -0400 Received: from mout.gmx.net ([212.227.15.15]:57833 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726114AbgJDBoa (ORCPT ); Sat, 3 Oct 2020 21:44:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1601775810; bh=uk/zgEMcmh53hyYJsD9rVSTVCXFi5qhVJPo/Njr0ULU=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:References:In-Reply-To; b=lkVvKEahe14N+6mVJ1eaYHHPhjRQiG9auUhIv7H8IJiWALY3R/rkdNiUDO8OJ6LBr 7BOkd2Kd5l4W12pGgsTC+Swuh6QxWpeli8OrqS2r/9xA44LRuFXI/9eDm9sUs52+sm bfezvwaCEoVlEyf6S5y3lpMUC0323GBJmP1YKWAE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from longitude ([5.146.195.151]) by mail.gmx.com (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MIdiZ-1kCV8e3OGD-00Efw6; Sun, 04 Oct 2020 03:43:29 +0200 Date: Sun, 4 Oct 2020 03:43:23 +0200 From: Jonathan =?utf-8?Q?Neusch=C3=A4fer?= To: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= Cc: Jonathan =?utf-8?Q?Neusch=C3=A4fer?= , linux-kernel@vger.kernel.org, Alexandre Belloni , Heiko Stuebner , linux-pwm@vger.kernel.org, Linus Walleij , Thierry Reding , Fabio Estevam , linux-rtc@vger.kernel.org, Arnd Bergmann , Mauro Carvalho Chehab , Sam Ravnborg , Daniel Palmer , Andy Shevchenko , Andreas Kemnade , NXP Linux Team , devicetree@vger.kernel.org, Stephan Gerhold , allen , Sascha Hauer , Lubomir Rintel , Rob Herring , Lee Jones , linux-arm-kernel@lists.infradead.org, Alessandro Zummo , Mark Brown , Pengutronix Kernel Team , Heiko Stuebner , Josua Mayer , Shawn Guo , "David S. Miller" Subject: Re: [PATCH v3 5/7] rtc: New driver for RTC in Netronix embedded controller Message-ID: <20201004014323.GD500800@latitude> References: <20200924192455.2484005-1-j.neuschaefer@gmx.net> <20200924192455.2484005-6-j.neuschaefer@gmx.net> <20200925054424.snlr3lggnsv575wu@pengutronix.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="n2Pv11Ogg/Ox8ay5" Content-Disposition: inline In-Reply-To: <20200925054424.snlr3lggnsv575wu@pengutronix.de> X-Provags-ID: V03:K1:3QmB/hlTSK7COVsSvQUbFXyPQLlzF/h6TMiid01Z4Clm5Arc98v uler7ylH8j9QBxD0tykisMycm+wD9UERrrd+ewA2/OjwuUspDeo2xtT/2Qnkl8U5/H4az8y 4tGKFKCvQ7SDiOJTQPqyHUPXztu5tbDBN2q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4j7JlGQHhvyRnlpD 9icHQe1LmAph3YuJ4mvHQ8Z9I0IDvErkMU6TfPdryqTTnjovTGjdYrQqM2V7ziDy9W3zpP2C5 19GCHCZVjPvA00/++eoR2fg7OGGCzyHTnv72g9mNyhAB9/kFrZZ03piPpQz1TB8B7s9/8Y1D6 xR7iVeJXa6RSupP2lJ5hpMgHuPJ3Swm1j25cP0jS0gSY82mhy06uRwgz3QNwRrhp7J6oVzJSv 3KHnV5sze8908B9gVk3QW0975cwmRxmWr7DW9vrAlxWuP+uNAqR0BCOXDOX+ovsQZrGLXwkY4 6MjryYJHzKT9VnFRpG2yIaRbLukcSBHkXKyvGaA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --n2Pv11Ogg/Ox8ay5 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Sep 25, 2020 at 07:44:24AM +0200, Uwe Kleine-K=C3=B6nig wrote: > Hello Jonathan, >=20 > On Thu, Sep 24, 2020 at 09:24:53PM +0200, Jonathan Neusch=C3=A4fer wrote: > > +#define NTXEC_REG_WRITE_YEAR 0x10 > > +#define NTXEC_REG_WRITE_MONTH 0x11 > > +#define NTXEC_REG_WRITE_DAY 0x12 > > +#define NTXEC_REG_WRITE_HOUR 0x13 > > +#define NTXEC_REG_WRITE_MINUTE 0x14 > > +#define NTXEC_REG_WRITE_SECOND 0x15 > > + > > +#define NTXEC_REG_READ_YM 0x20 > > +#define NTXEC_REG_READ_DH 0x21 > > +#define NTXEC_REG_READ_MS 0x23 >=20 > Is this an official naming? I think at least ..._MS is a poor name. > Maybe consider ..._MINSEC instead and make the other two names a bit long= er > for consistency? It's inofficial (the vendor kernel uses 0x10 etc. directly). I'll pick longer names. > > +static int ntxec_read_time(struct device *dev, struct rtc_time *tm) > > +{ > > + struct ntxec_rtc *rtc =3D dev_get_drvdata(dev); > > + unsigned int value; > > + int res; > > + > > + res =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_YM, &value); > > + if (res < 0) > > + return res; > > + > > + tm->tm_year =3D (value >> 8) + 100; > > + tm->tm_mon =3D (value & 0xff) - 1; > > + > > + res =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_DH, &value); > > + if (res < 0) > > + return res; > > + > > + tm->tm_mday =3D value >> 8; > > + tm->tm_hour =3D value & 0xff; > > + > > + res =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MS, &value); > > + if (res < 0) > > + return res; > > + > > + tm->tm_min =3D value >> 8; > > + tm->tm_sec =3D value & 0xff; > > + > > + return 0; > > +} > > + > > +static int ntxec_set_time(struct device *dev, struct rtc_time *tm) > > +{ > > + struct ntxec_rtc *rtc =3D dev_get_drvdata(dev); > > + int res =3D 0; > > + > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_YEAR, ntxec_reg= 8(tm->tm_year - 100)); > > + if (res) > > + return res; > > + > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MONTH, ntxec_re= g8(tm->tm_mon + 1)); > > + if (res) > > + return res; > > + > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_DAY, ntxec_reg8= (tm->tm_mday)); > > + if (res) > > + return res; > > + > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_HOUR, ntxec_reg= 8(tm->tm_hour)); > > + if (res) > > + return res; > > + > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MINUTE, ntxec_r= eg8(tm->tm_min)); > > + if (res) > > + return res; > > + > > + return regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, ntxec_re= g8(tm->tm_sec)); >=20 > I wonder: Is this racy? If you write minute, does the seconds reset to > zero or something like that? Or can it happen, that after writing the > minute register and before writing the second register the seconds > overflow and you end up with the time set to a minute later than > intended? If so it might be worth to set the seconds to 0 at the start > of the function (with an explaining comment). The setting the minutes does not reset the seconds, so I think this race condition is possible. I'll add the workaround. > .read_time has a similar race. What happens if minutes overflow between > reading NTXEC_REG_READ_DH and NTXEC_REG_READ_MS? Yes, we get read tearing in that case. It could even propagate all the way to the year/month field, for example when the following time rolls over: A | B | C 2020-10-31 23:59:59 2020-11-01 00:00:00 - If the increment happens after reading C, we get 2020-10-31 23:59= :59 - If the increment happens between reading B and C, we get 2020-10-31 23:00= :00 - If the increment happens between reading A and B, we get 2020-10-01 00:00= :00 - If the increment happens before reading A, we get 2020-11-01 00:00= :00 =2E.. both of which are far from correct. To mitigate this issue, I think something like the following is needed: - Read year/month - Read day/hour - Read minute/second - Read day/hour, compare with previously read value, restart on mismatch - Read year/month, compare with previously read value, restart on mismatch The order of the last two steps doesn't matter, as far as I can see, but if I remove one of them, I can't catch all cases of read tearing. > > +static struct platform_driver ntxec_rtc_driver =3D { > > + .driver =3D { > > + .name =3D "ntxec-rtc", > > + }, > > + .probe =3D ntxec_rtc_probe, >=20 > No .remove function? I don't think it would serve a purpose in this driver. There are no device-specific resources to release (no clocks to unprepare, for example). Thanks, Jonathan Neusch=C3=A4fer --n2Pv11Ogg/Ox8ay5 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEvHAHGBBjQPVy+qvDCDBEmo7zX9sFAl95KK0ACgkQCDBEmo7z X9uoww/+PcGp5yYzOom8eUs+afA4KaQWFu+aRNt6DhICWM5RH1t+rEHK5NAqQyFA LsJK0aMFJ3SMrE2IWNiOSH65ihoD9ZO0+npMwue9kiDrNAAk64Brk/jm0mAtYsOE LbtFOeDTtGd1UcUV8/5TFUzfo3BlkaScGAYfG/RBoJ6Celw3cjwo/OT8klmh0Aaj Q0kPMwQhgfdLMpd+NfB249ONuPTp0jWXevpGNJv+oAjwM12RxMJSJw9K+Yeld32h Zx2ZeI8wbsR3KfxRlATXuK/CZZPvLJmKUJZO3/dWdV1QwX3bMbCWo62xaiyaDft5 wBSbSDgKwnvjXp4FT8aGzvVExhNfuhfHdGKYGWB5Lx+PQNGk7rvnAPrWIVikq9Sj IqPUFmHUN/LGyPr8OtKNjOUubjkiuv0GvN4kFHTBrZAV6J7uJEiqHWL6OEBg3Wbo rDlmKBZEqFMNPV78vWQFquN7r+LB8z2ZMsJpJnpaHhOCCgwH2iFKKVHZACU5VMAE g3oJUUFSgddcsKn6I41Om9BsKlVuzkb8tEhGk8/WTaYNPKfiRNLu91/PXVE4g5dK 8arbUOsKDE29RTJz8XjCUhoKUY4W93+IJ14x+Qh1K2FvHBW94ZyCJm2UlRyJmZgF pIjduVQCndusSLmJ7YMxhtFWXpi5DpX6s9FE8kQillC1U2kg/Rg= =bYs8 -----END PGP SIGNATURE----- --n2Pv11Ogg/Ox8ay5--