Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1705867lqs; Sun, 16 Jun 2024 00:33:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU0OI5BhBWgZj1/QRLBmMcYhdW0Fa51w+ky8e5ENt+WBvxTX5rEK1Y7vyrzKa7di+D/6oUMr5n8B/2QsuPYUhkERMP8j3pmvtunSUBSvA== X-Google-Smtp-Source: AGHT+IGDKFh4sS4MLeVf/VV9aGqeVhO3sZ/3xz4E7pnNeqDp7BqLWxBS4TqQEt5QXaCDFAfYPnPU X-Received: by 2002:a05:6214:1249:b0:6b2:cf8b:21db with SMTP id 6a1803df08f44-6b2cf8b2252mr9268516d6.53.1718523226477; Sun, 16 Jun 2024 00:33:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718523226; cv=pass; d=google.com; s=arc-20160816; b=VOlj/o47dJ3wroBVvpM8D+34n7pll+2jowwM894RiFS7MeVLRkTXRiXmY06MD+I7m8 RyBPKRahoF2MVNn23Aa0GGTpOpeypTGODqfNR6ijohETY0DLt2pAJyMV3ElqpbScofZn VyYTe4EoNq7gBS3keGe+Jvte86gnSZ8PAlrX2NBZENkWSasMOh0Omx9cxze3Nc8/ocFx P8mp3MNZ4ym4R34CiDdQNpRBbcXFiPkatgB95iZDDqLp2K7w6LeXxhL7wf2XVT6/EyY6 1G3LVtDlV6OZ98cjG0L8KZ5V65sTchNRyA540XQGt2pJMxjMmCTfXIn2Ahl1DAQvirmi YXfA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=r5D80in0TCKF7CT1Ecrry2kBSeVogFVfKvkdHakj/G8=; fh=3zX84VYHY8mA4nuNJvsp+yMd9hRY5KNIJG7f/Hp16vE=; b=GC54lClLXjKqNkDvSQl3Zk3ij/kjHD63lzYEW669M5hPw2RAzPlTIWTBF5O5MRP3G2 602ozHFasaGWNA0pWwERVUSV5HejKxWCTwkqeV1pwaKKS52hIZ1keHrhTQ5PZeFVjTQO BIfXaT7bSpkXqZu9cKxrA7q6J2+F/LsGjX//BeON3k21uKJMbTjBakFd7DYHLgP+gMLS RT/lwghM5cblqSMTSqLw66G5WcFJbq61aH9YPbK9vVC3lQfA3httbNYm+Jl7RQN6fbKI lXwQaCwte9aLVqFEPh9ZMc+r3jk9NVosdrMAyesCNADvPgvraB9Bsa6ZyUvmTuBFIC5i z0Ww==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tuxon.dev header.s=google header.b=ZS0ss3El; arc=pass (i=1 spf=pass spfdomain=tuxon.dev dkim=pass dkdomain=tuxon.dev); spf=pass (google.com: domain of linux-kernel+bounces-214825-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214825-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6b2a5b2a6b9si78235526d6.365.2024.06.16.00.33.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 00:33:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-214825-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@tuxon.dev header.s=google header.b=ZS0ss3El; arc=pass (i=1 spf=pass spfdomain=tuxon.dev dkim=pass dkdomain=tuxon.dev); spf=pass (google.com: domain of linux-kernel+bounces-214825-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214825-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8B6EB1C22C6D for ; Fri, 14 Jun 2024 11:06:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E5215194C67; Fri, 14 Jun 2024 11:06:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b="ZS0ss3El" Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E19BE1922C6 for ; Fri, 14 Jun 2024 11:06:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718363204; cv=none; b=ld9VrPY80epMHsm3y3BwZG81XQho0ULrgmz4gABlVXY0ncb6lLPkvLGkQbKrj2B4BliDpmK3BEnozjyoQhPKxPcaSTPLeqHJmrTiUzuaUkafjXvtREXVq33aV9CH+rKOh9ftw990lsQ5WHlJabO5xyhH1qRHZpGf0Q4sMh6kmHU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718363204; c=relaxed/simple; bh=ITk4Pmw1wElvoJaLQpDB7x++r9MKwh7KfmJ6kjQS1SQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ClJYw2dJ504OLjqyRh+cXPjuUMzfg4qIN/vvP5I2PrNOPt/Bn0ia4OjYAvl0oD2xmeqYsVeniGNoCcrsF9GQBKPcq0Gr/FOKG1/cG06+chG5qqB2B2VPao3rUigra/zj/XKVliCGZqnV2xiFUolvr87gdExZwod64Dtm042hAT8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev; spf=pass smtp.mailfrom=tuxon.dev; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b=ZS0ss3El; arc=none smtp.client-ip=209.85.167.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tuxon.dev Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-52c819f6146so2870985e87.1 for ; Fri, 14 Jun 2024 04:06:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tuxon.dev; s=google; t=1718363201; x=1718968001; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=r5D80in0TCKF7CT1Ecrry2kBSeVogFVfKvkdHakj/G8=; b=ZS0ss3ElqQML10fu8j8eKaPhMQw1iuTwzguoHoek/Oe8FTuHaFnT4KmHUxH8aAAFUx 3vRTiWK6dgDF+yhhras1S1z+/EitG+4GxE9hKx9vmfG2YF8RCDzYHOYqW3Msj19nkJ2+ bF7XlyQnH0pxNhP0jibLTkt8fAqpzaNBXTGA/e4hOgDd4UrnIavYp0PlLE+R9cFWWQyG 0NRB6aglvZ8hXKBpKxYOn8Uc3tRmHvqpl625gnU3PpaEB60D5ICm65sACbYcRTxKx3Rq 8LHzDO9qPDAZNILgX0d4Wd3TfI9a8zqLwUqs6fD5An30HF55Lr+oxSHol+vhmq94R4HH Q3Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718363201; x=1718968001; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=r5D80in0TCKF7CT1Ecrry2kBSeVogFVfKvkdHakj/G8=; b=soi1uVkGH1K99NI/ksGd2/ku+cW3TdfjEskb5OuO20JvsBbzS3ncKeQyvsoAiLJ2EO dC1y0x8fYIWuPWmMmTvfbMbs9EVFt8PMAQJFtgyjckqUpdg1o2xeCs+h0HV85p6VbQAD Y/ML60GDPp3UEseWRnRTUYksJd3K1fzuO9LRre9z8GDlNbLPPsGlPUXhFq6O9nrILZiz wgbMvhWHstQEsxwsBn9prQXw5A2JctaY15uPYyfuTol+IrWtJv4JA8+RIeSyuVR8W/OS Bd4c9bkaiSbdV+PLilY08LapzVUCjC1tYpurjv0BBf1gcLe4pGl9vhBtitmy/QfW2MOG /iVg== X-Forwarded-Encrypted: i=1; AJvYcCU8vIZClVGquW9FiIBJ0AEzYA/MaDTv3RBgeNe859vKX1UVt3a8XuYELSDTrjznnzjg5SdAzAkjuZw3IUDu/WF68K2Lk/EvsG47i43b X-Gm-Message-State: AOJu0YxtcbTCAjgeybr4EXQeNI5LbW0fVZ5t0aO5EHG2vfPYlvk9AeEP XxTWewsrjQR/BTUTAZ3C4xmKFpxSGL0C6nK1ad7o7VK1U/ZuESKol5d6nmZc7LdxCDlQIYPX8BH I X-Received: by 2002:a05:6512:3ca4:b0:52c:8339:d09b with SMTP id 2adb3069b0e04-52ca6e55096mr1765316e87.1.1718363200927; Fri, 14 Jun 2024 04:06:40 -0700 (PDT) Received: from [192.168.50.4] ([82.78.167.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36075104a1bsm4095242f8f.102.2024.06.14.04.06.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Jun 2024 04:06:40 -0700 (PDT) Message-ID: <4a477079-b4a6-4861-ae24-b3b87adb8ecd@tuxon.dev> Date: Fri, 14 Jun 2024 14:06:38 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 06/12] rtc: renesas-rtca3: Add driver for RTCA-3 available on Renesas RZ/G3S SoC Content-Language: en-US To: Alexandre Belloni Cc: geert+renesas@glider.be, mturquette@baylibre.com, sboyd@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, lee@kernel.org, magnus.damm@gmail.com, linux-renesas-soc@vger.kernel.org, linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rtc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Claudiu Beznea References: <20240614071932.1014067-1-claudiu.beznea.uj@bp.renesas.com> <20240614071932.1014067-7-claudiu.beznea.uj@bp.renesas.com> <2024061409215756e6a10c@mail.local> From: claudiu beznea In-Reply-To: <2024061409215756e6a10c@mail.local> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, Alexandre, On 14.06.2024 12:21, Alexandre Belloni wrote: > Hello Claudiu, > > On 14/06/2024 10:19:26+0300, Claudiu wrote: >> +static int rtca3_initial_setup(struct rtca3_priv *priv) >> +{ >> + unsigned long osc32k_rate; >> + u8 pes, tmp, mask; >> + u32 sleep_us; >> + int ret; >> + >> + osc32k_rate = clk_get_rate(priv->clk); >> + if (!osc32k_rate) >> + return -EINVAL; >> + >> + sleep_us = DIV_ROUND_UP_ULL(1000000ULL, osc32k_rate) * 6; >> + >> + priv->ppb.ten_sec = DIV_ROUND_CLOSEST_ULL(1000000000ULL, (osc32k_rate * 10)); >> + priv->ppb.sixty_sec = DIV_ROUND_CLOSEST_ULL(1000000000ULL, (osc32k_rate * 60)); >> + >> + /* >> + * According to HW manual (section 22.4.2. Clock and count mode setting procedure) >> + * we need to wait at least 6 cycles of the 32KHz clock after clock was enabled. >> + */ >> + usleep_range(sleep_us, sleep_us + 10); >> + >> + /* Disable alarm and carry interrupts. */ >> + mask = RTCA3_RCR1_AIE | RTCA3_RCR1_CIE; >> + rtca3_byte_update_bits(priv, RTCA3_RCR1, mask, 0); >> + ret = readb_poll_timeout(priv->base + RTCA3_RCR1, tmp, !(tmp & mask), >> + 10, RTCA3_DEFAULT_TIMEOUT_US); >> + if (ret) >> + return ret; >> + >> + /* >> + * Stop the RTC and set to 12 hours mode and calendar count mode. >> + * RCR2.START initial value is undefined so we need to stop here >> + * all the time. >> + */ > > Certainly not, if you stop the RTC on probe, you lose the time > information, this must only be done when the RTC has never been > initialised. The whole goal of the RTC is the keep time across reboots, > its lifecycle is longer than the system. This was also my first thought when I read the HW manual. It has been done like this to follow the HW manual. According to HW manual [1], chapter 22.3.19 RTC Control Register 2 (RCR2), initial value of START bit is undefined. If it's 1 while probing but it has never been initialized, we can falsely detect that RTC is started and skip the rest of the initialization steps. W/o initialization configuration, the RTC will not be able to work. Even with this implementation we don't loose the time b/w reboots. Here is the output on my board [2]. The steps I did were the following: 1/ remove the power to the board (I don't have a battery for RTC installed at the moment) 2/ boot the board and issue hwclock -w 3/ reboot 4/ check the systime and rtc time 5/ poweroff 6/ poweron 7/ boot and check systime and RTC time As you can see the time is not lost but continue to increment. I presume the hardware takes into account that time needs to increment when initial configuration is executed. [1] https://www.renesas.com/us/en/products/microcontrollers-microprocessors/rz-mpus/rzg3s-general-purpose-microprocessors-single-core-arm-cortex-a55-11-ghz-cpu-and-dual-core-cortex-m33-250 [2] https://p.fr33tux.org/585cd6 > > Also, why do you insist on 12H-mode? The proper thing to do is to support > 12H-mode on read but always use 24H-mode when setting the time. OK, I wasn't aware of this. I think I followed this approach as it looked to me the number of operation to update the hardware registers was lower for 12h mode. I'll adjust as proposed. > >> + mask = RTCA3_RCR2_START | RTCA3_RCR2_HR24 | RTCA3_RCR2_CNTMD; >> + writeb(0, priv->base + RTCA3_RCR2); >> + ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, !(tmp & mask), >> + 10, RTCA3_DEFAULT_TIMEOUT_US); >> + if (ret) >> + return ret; >> + >> + /* Execute reset and wait for reset and calendar count mode to be applied. */ >> + mask = RTCA3_RCR2_RESET | RTCA3_RCR2_CNTMD; >> + writeb(RTCA3_RCR2_RESET, priv->base + RTCA3_RCR2); >> + ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, !(tmp & mask), >> + 10, RTCA3_RESET_TIMEOUT_US); >> + if (ret) >> + return ret; >> + >> + /* >> + * According to HW manual (section 22.6.3. Notes on writing to and reading >> + * from registers) after reset we need to wait 6 clock cycles before >> + * writing to RTC registers. >> + */ >> + usleep_range(sleep_us, sleep_us + 10); >> + >> + /* Set no adjustment. */ >> + writeb(0, priv->base + RTCA3_RADJ); >> + ret = readb_poll_timeout(priv->base + RTCA3_RADJ, tmp, !tmp, 10, >> + RTCA3_DEFAULT_TIMEOUT_US); >> + >> + /* Start the RTC and enable automatic time error adjustment. */ >> + mask = RTCA3_RCR2_START | RTCA3_RCR2_AADJE; >> + writeb(RTCA3_RCR2_START | RTCA3_RCR2_AADJE, priv->base + RTCA3_RCR2); >> + ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, ((tmp & mask) == mask), >> + 10, RTCA3_START_TIMEOUT_US); >> + if (ret) >> + return ret; >> + >> + /* >> + * According to HW manual (section 22.6.4. Notes on writing to and reading >> + * from registers) we need to wait 1/128 seconds while the clock is operating >> + * (RCR2.START bit = 1) to be able to read the counters after a return from >> + * reset. >> + */ >> + usleep_range(8000, 9000); >> + >> + /* Set period interrupt to 1/64 seconds. It is necessary for alarm setup. */ >> + pes = FIELD_PREP(RTCA3_RCR1_PES, RTCA3_RCR1_PES_1_64_SEC); >> + rtca3_byte_update_bits(priv, RTCA3_RCR1, RTCA3_RCR1_PES, pes); >> + return readb_poll_timeout(priv->base + RTCA3_RCR1, tmp, ((tmp & RTCA3_RCR1_PES) == pes), >> + 10, RTCA3_DEFAULT_TIMEOUT_US); >> +} >> + >> +static int rtca3_request_irqs(struct platform_device *pdev, struct rtca3_priv *priv) >> +{ >> + struct device *dev = &pdev->dev; >> + int ret, irq; >> + >> + irq = platform_get_irq_byname(pdev, "alarm"); >> + if (irq < 0) >> + return dev_err_probe(dev, irq, "Failed to get alarm IRQ!\n"); >> + >> + ret = devm_request_irq(dev, irq, rtca3_alarm_handler, 0, "rtca3-alarm", priv); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to request alarm IRQ!\n"); >> + priv->wakeup_irq = irq; >> + >> + irq = platform_get_irq_byname(pdev, "period"); >> + if (irq < 0) >> + return dev_err_probe(dev, irq, "Failed to get period IRQ!\n"); >> + >> + ret = devm_request_irq(dev, irq, rtca3_periodic_handler, 0, "rtca3-period", priv); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to request period IRQ!\n"); >> + >> + /* >> + * Driver doesn't implement carry handler. Just get the IRQ here >> + * for backward compatibility, in case carry support will be added later. >> + */ >> + irq = platform_get_irq_byname(pdev, "carry"); >> + if (irq < 0) >> + return dev_err_probe(dev, irq, "Failed to get carry IRQ!\n"); >> + >> + return 0; >> +} >> + >> +static int rtca3_probe(struct platform_device *pdev) >> +{ >> + struct device *dev = &pdev->dev; >> + struct rtca3_priv *priv; >> + int ret; >> + >> + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); >> + if (!priv) >> + return -ENOMEM; >> + >> + priv->base = devm_platform_ioremap_resource(pdev, 0); >> + if (IS_ERR(priv->base)) >> + return PTR_ERR(priv->base); >> + >> + priv->clk = devm_clk_get_enabled(dev, "counter"); >> + if (IS_ERR(priv->clk)) >> + return PTR_ERR(priv->clk); >> + >> + platform_set_drvdata(pdev, priv); >> + >> + spin_lock_init(&priv->lock); >> + atomic_set(&priv->alrm_sstep, RTCA3_ALRM_SSTEP_DONE); >> + init_completion(&priv->set_alarm_completion); >> + >> + ret = rtca3_initial_setup(priv); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to setup the RTC!\n"); >> + >> + ret = rtca3_request_irqs(pdev, priv); >> + if (ret) >> + return ret; >> + >> + device_init_wakeup(&pdev->dev, 1); >> + >> + priv->rtc_dev = devm_rtc_allocate_device(&pdev->dev); >> + if (IS_ERR(priv->rtc_dev)) >> + return PTR_ERR(priv->rtc_dev); >> + >> + priv->rtc_dev->ops = &rtca3_ops; >> + priv->rtc_dev->max_user_freq = 256; >> + priv->rtc_dev->range_min = mktime64(1999, 1, 1, 0, 0, 0); >> + priv->rtc_dev->range_max = mktime64(2098, 12, 31, 23, 59, 59); > > This very much looks like the range should be 2000 to 2099, why do you > want to shift it? 2000-2099 was my first option for this but then I saw one of your old commits on this topic and, since I'm not very familiar with RTC, I took it as example. I'll adjust as you proposed. commit beee05dfbead Author: Alexandre Belloni Date: Wed Mar 20 12:30:10 2019 +0100 rtc: sh: set range The SH RTC is a BCD RTC with some version having 4 digits for the year. The range for the RTCs with only 2 digits for the year was unfortunately shifted to handle 1999 to 2098. Reviewed-by: Geert Uytterhoeven Signed-off-by: Alexandre Belloni Thank you for your review, Claudiu Beznea > >