Skip to contentSkip to conversation

dt-bindings: rtc: zynqmp: Add clock information

PATCH Superseded Srinivas Neeli 4 participants 8 messages Jun 9, 2022 – Jun 10, 2022

Subsystems (3)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 dt-bindings: rtc: zynqmp: Add clock information

Added clock information and deprecated calibration support.

---
Changes in V6:
-Removed dtc warnings.
Changes in V5:
-Removed quotes and _clk suffix from clocknames.
Changes in V4:
- Deprecated calibrtion support
Changes in V3:
- New patch

---
 .../devicetree/bindings/rtc/xlnx,zynqmp-rtc.yaml     | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/rtc/xlnx,zynqmp-rtc.yaml +11 -1
index bdb72d3ddf2a..638dd1d8bb26 100644
@@ -23,8 +23,15 @@ properties:
   reg:
     maxItems: 1
 
+  clocks:
+    items:
+      - description: rtc_clk is the operating frequency of crystal.
+
+  clock-names:
+    maxItems: 1
+
   interrupts:
-    minItems: 2
+    maxItems: 2
 
   interrupt-names:
     items:
@@ -39,6 +46,7 @@ properties:
     minimum: 0x1
     maximum: 0x1FFFFF
     default: 0x198233
+    deprecated: true
 
 required:
   - compatible
@@ -61,5 +69,7 @@ examples:
         interrupts = <0 26 4>, <0 27 4>;
         interrupt-names = "alarm", "sec";
         calibration = <0x198233>;
+        clock-names = "rtc_clk";
+        clocks = <&rtc_clk>;
       };
     };
Signature · 1 line
2.25.1
#1 lore raw

PATCH v6 2/2 rtc: zynqmp: Add calibration set and get support

Zynqmp RTC controller has a calibration feature to compensate time deviation due to input clock inaccuracy.
Set and get calibration API's are used for setting and getting calibration value from the controller calibration register. As per RTC spec default calibration value is 0x7FFF.

---
Changes in V6:
-None
Changes in V5:
-None
Changes in V4:
-Updated MIN and MAX calibration values.
Changes in V3:
-Calculated tick_mult using crystal frequency.
-Calibration register updating based on crystal frequency in probe. -Supressed MIN an MAX calibration values,Will send separate patch in future. Changes in V2:
-Removed unused macro.
-Updated code with review comments.

---
 drivers/rtc/rtc-zynqmp.c | 115 ++++++++++++++++++++++++++++++++-------
 1 file changed, 95 insertions(+), 20 deletions(-)
drivers/rtc/rtc-zynqmp.c +95 -20
index f440bb52be92..1dd389b891fe 100644
@@ -6,6 +6,7 @@
  *
  */
 
+#include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/init.h>
 #include <linux/io.h>
@@ -36,17 +37,23 @@
 #define RTC_OSC_EN		BIT(24)
 #define RTC_BATT_EN		BIT(31)
 
-#define RTC_CALIB_DEF		0x198233
+#define RTC_CALIB_DEF		0x7FFF
 #define RTC_CALIB_MASK		0x1FFFFF
 #define RTC_ALRM_MASK          BIT(1)
 #define RTC_MSEC               1000
+#define RTC_FR_MASK		0xF0000
+#define RTC_FR_MAX_TICKS	16
+#define RTC_PPB			1000000000LL
+#define RTC_MIN_OFFSET		-32768000
+#define RTC_MAX_OFFSET		32767000
 
 struct xlnx_rtc_dev {
 	struct rtc_device	*rtc;
 	void __iomem		*reg_base;
 	int			alarm_irq;
 	int			sec_irq;
-	unsigned int		calibval;
+	struct clk		*rtc_clk;
+	unsigned int		freq;
 };
 
 static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm)
@@ -61,13 +68,6 @@ static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	 */
 	new_time = rtc_tm_to_time64(tm) + 1;
 
-	/*
-	 * Writing into calibration register will clear the Tick Counter and
-	 * force the next second to be signaled exactly in 1 second period
-	 */
-	xrtcdev->calibval &= RTC_CALIB_MASK;
-	writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
-
 	writel(new_time, xrtcdev->reg_base + RTC_SET_TM_WR);
 
 	/*
@@ -173,15 +173,76 @@ static void xlnx_init_rtc(struct xlnx_rtc_dev *xrtcdev)
 	rtc_ctrl = readl(xrtcdev->reg_base + RTC_CTRL);
 	rtc_ctrl |= RTC_BATT_EN;
 	writel(rtc_ctrl, xrtcdev->reg_base + RTC_CTRL);
+}
 
-	/*
-	 * Based on crystal freq of 33.330 KHz
-	 * set the seconds counter and enable, set fractions counter
-	 * to default value suggested as per design spec
-	 * to correct RTC delay in frequency over period of time.
+static int xlnx_rtc_read_offset(struct device *dev, long *offset)
+{
+	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
+	unsigned long long rtc_ppb = RTC_PPB;
+	unsigned int tick_mult = do_div(rtc_ppb, xrtcdev->freq);
+	unsigned int calibval;
+	long offset_val;
+
+	calibval = readl(xrtcdev->reg_base + RTC_CALIB_RD);
+	/* Offset with seconds ticks */
+	offset_val = calibval & RTC_TICK_MASK;
+	offset_val = offset_val - RTC_CALIB_DEF;
+	offset_val = offset_val * tick_mult;
+
+	/* Offset with fractional ticks */
+	if (calibval & RTC_FR_EN)
+		offset_val += ((calibval & RTC_FR_MASK) >> RTC_FR_DATSHIFT)
+			* (tick_mult / RTC_FR_MAX_TICKS);
+	*offset = offset_val;
+
+	return 0;
+}
+
+static int xlnx_rtc_set_offset(struct device *dev, long offset)
+{
+	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
+	unsigned long long rtc_ppb = RTC_PPB;
+	unsigned int tick_mult = do_div(rtc_ppb, xrtcdev->freq);
+	unsigned char fract_tick;
+	unsigned int calibval;
+	short int  max_tick;
+	int fract_offset;
+
+	if (offset < RTC_MIN_OFFSET || offset > RTC_MAX_OFFSET)
+		return -ERANGE;
+
+	/* Number ticks for given offset */
+	max_tick = div_s64_rem(offset, tick_mult, &fract_offset);
+
+	/* Number fractional ticks for given offset */
+	if (fract_offset) {
+		if (fract_offset < 0) {
+			fract_offset = fract_offset + tick_mult;
+			max_tick--;
+		}
+		if (fract_offset > (tick_mult / RTC_FR_MAX_TICKS)) {
+			for (fract_tick = 1; fract_tick < 16; fract_tick++) {
+				if (fract_offset <=
+				    (fract_tick *
+				     (tick_mult / RTC_FR_MAX_TICKS)))
+					break;
+			}
+		}
+	}
+
+	/* Zynqmp RTC uses second and fractional tick
+	 * counters for compensation
 	 */
-	xrtcdev->calibval &= RTC_CALIB_MASK;
-	writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
+	calibval = max_tick + RTC_CALIB_DEF;
+
+	if (fract_tick)
+		calibval |= RTC_FR_EN;
+
+	calibval |= (fract_tick << RTC_FR_DATSHIFT);
+
+	writel(calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
+
+	return 0;
 }
 
 static const struct rtc_class_ops xlnx_rtc_ops = {
@@ -190,6 +251,8 @@ static const struct rtc_class_ops xlnx_rtc_ops = {
 	.read_alarm	  = xlnx_rtc_read_alarm,
 	.set_alarm	  = xlnx_rtc_set_alarm,
 	.alarm_irq_enable = xlnx_rtc_alarm_irq_enable,
+	.read_offset	  = xlnx_rtc_read_offset,
+	.set_offset	  = xlnx_rtc_set_offset,
 };
 
 static irqreturn_t xlnx_rtc_interrupt(int irq, void *id)
@@ -255,10 +318,22 @@ static int xlnx_rtc_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	ret = of_property_read_u32(pdev->dev.of_node, "calibration",
-				   &xrtcdev->calibval);
-	if (ret)
-		xrtcdev->calibval = RTC_CALIB_DEF;
+	/* Getting the rtc_clk info */
+	xrtcdev->rtc_clk = devm_clk_get_optional(&pdev->dev, "rtc_clk");
+	if (IS_ERR(xrtcdev->rtc_clk)) {
+		if (PTR_ERR(xrtcdev->rtc_clk) != -EPROBE_DEFER)
+			dev_warn(&pdev->dev, "Device clock not found.\n");
+	}
+	xrtcdev->freq = clk_get_rate(xrtcdev->rtc_clk);
+	if (!xrtcdev->freq) {
+		ret = of_property_read_u32(pdev->dev.of_node, "calibration",
+					   &xrtcdev->freq);
+		if (ret)
+			xrtcdev->freq = RTC_CALIB_DEF;
+	}
+	ret = readl(xrtcdev->reg_base + RTC_CALIB_RD);
+	if (!ret)
+		writel(xrtcdev->freq, (xrtcdev->reg_base + RTC_CALIB_WR));
 
 	xlnx_init_rtc(xrtcdev);
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Srinivas" == Srinivas Neeli <[email protected]> writes:

Zynqmp RTC controller has a calibration feature to compensate time deviation due to input clock inaccuracy.
Set and get calibration API's are used for setting and getting calibration value from the controller calibration register. As per RTC spec default calibration value is 0x7FFF.

Where do you see that? ug1085 rev2.2 still lists:

Programming Sequences
init rtc
1. Write the value 0019_8231h into the calibration register, CALIB_WRITE.

Signature · 1 line
Bye, Peter Korsgaard
#3 lore raw

Re: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

Quoted 8 lines · Peter Korsgaard
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Srinivas" == Srinivas Neeli <[email protected]> writes:

Zynqmp RTC controller has a calibration feature to compensate time deviation due to input clock inaccuracy.
Set and get calibration API's are used for setting and getting calibration value from the controller calibration register. As per RTC spec default calibration value is 0x7FFF.

Quoted 6 lines

Where do you see that? ug1085 rev2.2 still lists:

Programming Sequences
init rtc
1. Write the value 0019_8231h into the calibration register, CALIB_WRITE.

This is a very weird value. Last time I had a look, I couldn't find an explanation for it.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Re: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

Quoted 13 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Alexandre" == Alexandre Belloni <[email protected]> writes:

Quoted 10 lines · Peter Korsgaard
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Srinivas" == Srinivas Neeli <[email protected]> writes:

Zynqmp RTC controller has a calibration feature to compensate time deviation due to input clock inaccuracy.
Set and get calibration API's are used for setting and getting calibration value from the controller calibration register. As per RTC spec default calibration value is 0x7FFF.

Where do you see that? ug1085 rev2.2 still lists:

Quoted 7 lines
Quoted 4 lines

Programming Sequences
init rtc
1. Write the value 0019_8231h into the calibration register, CALIB_WRITE.

This is a very weird value. Last time I had a look, I couldn't find an explanation for it.

Agreed. I am not arguing that it is sensible, I just wondered where this new value comes from?

Signature · 1 line
Bye, Peter Korsgaard
#5 lore raw

RE: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

[AMD Official Use Only - General]

Hi,

Quoted 31 lines

-----Original Message-----
From: Peter Korsgaard <[email protected]>
Sent: Thursday, June 9, 2022 8:30 PM
To: Alexandre Belloni <[email protected]>
Cc: Srinivas Neeli <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neeli, Srinivas <[email protected]>; [email protected]; [email protected]; [email protected]; linux-arm- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Alexandre" == Alexandre Belloni <[email protected]>

Quoted 4 lines · writes:
Quoted 3 lines · Peter Korsgaard
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Srinivas" == Srinivas Neeli <[email protected]> writes:

Zynqmp RTC controller has a calibration feature to compensate >> >

time deviation due to input clock inaccuracy.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Set and get calibration API's are used for setting and getting >> >

calibration value from the controller calibration register.

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

As per RTC spec default calibration value is 0x7FFF.

Where do you see that? ug1085 rev2.2 still lists:

Programming Sequences
init rtc
1. Write the value 0019_8231h into the calibration register, CALIB_WRITE.

Quoted 6 lines
Quoted 1 line

This is a very weird value. Last time I had a look, I couldn't find an >

explanation for it.

Agreed. I am not arguing that it is sensible, I just wondered where this new value comes from?

This information is missing in our TRM, as per the internal RTC IP design specification document, the calibration value should be crystal clock frequency minus one. For a crystal oscillator of 32.768KHz(0x8000) frequency, the calibration value will be 0x7FFF.

Will update TRM with the required information.

Quoted 2 lines

--
Bye, Peter Korsgaard

#6 lore raw

Re: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Neeli," == Neeli, Srinivas <[email protected]> writes:

Hi,

Quoted 13 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

Where do you see that? ug1085 rev2.2 still lists:

Programming Sequences
init rtc
1. Write the value 0019_8231h into the calibration register, CALIB_WRITE.

Quoted 6 lines
Quoted 1 line

This is a very weird value. Last time I had a look, I couldn't find an >

explanation for it.

Agreed. I am not arguing that it is sensible, I just wondered where this new value comes from?

Quoted 6 lines

This information is missing in our TRM, as per the internal RTC IP
design specification document, the calibration value should be crystal
clock frequency minus one.
For a crystal oscillator of 32.768KHz(0x8000) frequency, the calibration value will be 0x7FFF.

Will update TRM with the required information.

Ok, great!

Please put this change in a separate commit from the get/set logic.

Signature · 1 line
Bye, Peter Korsgaard
#7 lore raw

Re: [PATCH V6 2/2] rtc: zynqmp: Add calibration set and get support

Quoted 22 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Neeli," == Neeli, Srinivas <[email protected]> writes:

Hi,

Quoted 17 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

Where do you see that? ug1085 rev2.2 still lists:

Programming Sequences
init rtc
1. Write the value 0019_8231h into the calibration register, CALIB_WRITE.

Quoted 6 lines
Quoted 1 line

This is a very weird value. Last time I had a look, I couldn't find an >

explanation for it.

Agreed. I am not arguing that it is sensible, I just wondered where this new value comes from?

This information is missing in our TRM, as per the internal RTC IP
design specification document, the calibration value should be crystal
clock frequency minus one.
For a crystal oscillator of 32.768KHz(0x8000) frequency, the calibration value will be 0x7FFF.

Quoted 5 lines
Quoted 1 line

Will update TRM with the required information.

Ok, great!

Please put this change in a separate commit from the get/set logic.

I'm not sure it is worth it as it is probably being set (to the wrong value) from your device tree anyway and the driver won't forcefully change it in that case. The worse that can happen is that your new boards will have the correct value.

Signature · 8 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel