build/patch/kernel/sunxi-current/general-sunxi-rtc-clocksource.patch
Igor Pečovnik 150ac0c2af
Remove K<4, change branches, new features (#1586)
AR-1 - Adding support category for distributions
AR-4 - Remove Allwinner legacy
AR-5 - Drop Udoo family and move Udoo board into newly created imx6 family
AR-9 - Rename sunxi-next to sunxi-legacy
AR-10 - Rename sunxi-dev to sunxi-current
AR-11 - Adding Radxa Rockpi S support
AR-13 - Rename rockchip64-default to rockchip64-legacy
AR-14 - Add rockchip64-current as mainline source
AR-15 - Drop Rockchip 4.19.y NEXT, current become 5.3.y
AR-16 - Rename RK3399 default to legacy
AR-17 - Rename Odroid XU4 next and default to legacy 4.14.y, add DEV 5.4.y
AR-18 - Add Odroid N2 current mainline
AR-19 - Move Odroid C1 to meson family
AR-20 - Rename mvebu64-default to mvebu64-legacy
AR-21 - Rename mvebu-default to mvebu-legacy
AR-22 - Rename mvebu-next to mvebu-current
AR-23 - Drop meson64 default and next, current becomes former DEV 5.3.y
AR-24 - Drop cubox family and move Cubox/Hummingboard boards under imx6
AR-26 - Adjust motd
AR-27 - Enabling distribution release status
AR-28 - Added new GCC compilers
AR-29 - Implementing Ubuntu Eoan
AR-30 - Add desktop packages per board or family
AR-31 - Remove (Ubuntu/Debian) distribution name from image filename
AR-32 - Move arch configs from configuration.sh to separate arm64 and armhf config files
AR-33 - Revision numbers for beta builds changed to day_in_the_year
AR-34 - Patches support linked patches
AR-35 - Break meson64 family into gxbb and gxl
AR-36 - Add Nanopineo2 Black
AR-38 - Upgrade option from old branches to new one via armbian-config
AR-41 - Show full timezone info
AR-43 - Merge Odroid N2 to meson64
AR-44 - Enable FORCE_BOOTSCRIPT_UPDATE for all builds
2019-11-19 23:25:39 +01:00

113 lines
3.9 KiB
Diff

This adds a sysfs 'clock_source' attribute which can be used to query
and set the clock source of the RTC, either 'internal' or 'external'.
Important note: Probing the pins of the 32kHz crystal with a scope will
not reliably tell you whether the (usually) more accurate external is
selected! On a 'Cubietech Cubietruck' board, the author saw a stable
32768Hz signal on the crystal, even though the internal oscillator was
selected and the clock wildly drifting.
Using adjtimex might help to figure out which oscillator is selected:
~# echo internal > /sys/devices/.../1c20d00.rtc/clock_source
~# adjtimex -n -c=3
--- current --- -- suggested --
cmos time system-cmos error_ppm tick freq tick freq
1469627826 31.224561
1469627835 31.858098 63353.6 10000 0
1469627844 32.490782 63268.4 10000 0 9367 2069425
~# echo external > /sys/devices/.../1c20d00.rtc/clock_source
~# adjtimex -n -c=3
--- current --- -- suggested --
cmos time system-cmos error_ppm tick freq tick freq
1469627851 32.883407
1469627861 32.883380 -2.7 10000 0
1469627871 32.883352 -2.8 10000 0 10000 185937
Signed-off-by: Onno Kortmann <on...@gmx.net>
---
drivers/rtc/rtc-sunxi.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/drivers/rtc/rtc-sunxi.c b/drivers/rtc/rtc-sunxi.c
index abada60..3fcf571 100644
--- a/drivers/rtc/rtc-sunxi.c
+++ b/drivers/rtc/rtc-sunxi.c
@@ -36,6 +36,8 @@
#define SUNXI_LOSC_CTRL 0x0000
#define SUNXI_LOSC_CTRL_RTC_HMS_ACC BIT(8)
#define SUNXI_LOSC_CTRL_RTC_YMD_ACC BIT(7)
+#define SUNXI_LOSC_OSC32K_SRC_SEL BIT(0)
+#define SUNXI_LOSC_KEY_VALUE 0x16aa0000
#define SUNXI_RTC_YMD 0x0004
@@ -432,6 +434,50 @@ static const struct of_device_id sunxi_rtc_dt_ids[] = {
};
MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
+/* As per page 126 of the A20 manual, the lowest bit in LOSC_CTRL_REG controls
+ * the 32.768KHz clock source to use for the RTC. Using the clock_source sysfs
+ * attribute, the clock can be selected between external (accurate 32kHz
+ * crystal) and internal (seems to be an inaccurate RC oscillator) mode. It
+ * appears that this bit is non-volatile and will be kept in the RTC when the
+ * system is powered off.
+ */
+static ssize_t sunxi_rtc_show_clock_source(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
+ u32 val = readl(chip->base + SUNXI_LOSC_CTRL);
+ if (val & SUNXI_LOSC_OSC32K_SRC_SEL)
+ return sprintf(buf, "internal [external]\n");
+ else
+ return sprintf(buf, "[internal] external\n");
+}
+
+static ssize_t sunxi_rtc_store_clock_source(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count) {
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
+ u32 val = readl(chip->base + SUNXI_LOSC_CTRL);
+
+ if (strncmp(buf, "external", 8) == 0)
+ val|=SUNXI_LOSC_OSC32K_SRC_SEL;
+ else if (strncmp(buf, "internal", 8) == 0)
+ val&=~SUNXI_LOSC_OSC32K_SRC_SEL;
+ else
+ return -EINVAL;
+
+ /* Writing this bit requires setting the upper 16 bit to 0x16aa (key
+ * value). */
+ val |= SUNXI_LOSC_KEY_VALUE;
+
+ writel(val, chip->base + SUNXI_LOSC_CTRL);
+ return count;
+}
+
+static DEVICE_ATTR(clock_source, S_IRUGO | S_IWUSR,
+ sunxi_rtc_show_clock_source,
+ sunxi_rtc_store_clock_source);
+
static int sunxi_rtc_probe(struct platform_device *pdev)
{
struct sunxi_rtc_dev *chip;
@@ -490,6 +536,13 @@ static int sunxi_rtc_probe(struct platform_device *pdev)
dev_info(&pdev->dev, "RTC enabled\n");
+ ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
+ if (ret) {
+ dev_err(&pdev->dev, "Unable to create sysfs entry: %s\n",
+ dev_attr_clock_source.attr.name);
+ return ret;
+ }
+
return 0;
}
--
2.2.0.34.gb8f29bf