1 // SPDX-License-Identifier: GPL-2.0+
3 * rtc-dm355evm.c - access battery-backed counter in MSP430 firmware
5 * Copyright (c) 2008 by David Brownell
7 #include <linux/kernel.h>
8 #include <linux/init.h>
10 #include <linux/platform_device.h>
12 #include <linux/mfd/dm355evm_msp.h>
13 #include <linux/module.h>
17 * The MSP430 firmware on the DM355 EVM uses a watch crystal to feed
18 * a 1 Hz counter. When a backup battery is supplied, that makes a
19 * reasonable RTC for applications where alarms and non-NTP drift
20 * compensation aren't important.
22 * The only real glitch is the inability to read or write all four
23 * counter bytes atomically: the count may increment in the middle
24 * of an operation, causing trouble when the LSB rolls over.
26 * This driver was tested with firmware revision A4.
33 static int dm355evm_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
41 * Read LSB(0) to MSB(3) bytes. Defend against the counter
42 * rolling over by re-reading until the value is stable,
43 * and assuming the four reads take at most a few seconds.
45 status
= dm355evm_msp_read(DM355EVM_MSP_RTC_0
);
48 if (tries
&& time
.bytes
[0] == status
)
50 time
.bytes
[0] = status
;
52 status
= dm355evm_msp_read(DM355EVM_MSP_RTC_1
);
55 if (tries
&& time
.bytes
[1] == status
)
57 time
.bytes
[1] = status
;
59 status
= dm355evm_msp_read(DM355EVM_MSP_RTC_2
);
62 if (tries
&& time
.bytes
[2] == status
)
64 time
.bytes
[2] = status
;
66 status
= dm355evm_msp_read(DM355EVM_MSP_RTC_3
);
69 if (tries
&& time
.bytes
[3] == status
)
71 time
.bytes
[3] = status
;
73 } while (++tries
< 5);
75 dev_dbg(dev
, "read timestamp %08x\n", time
.value
);
77 rtc_time64_to_tm(le32_to_cpu(time
.value
), tm
);
81 static int dm355evm_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
87 value
= rtc_tm_to_time64(tm
);
88 time
.value
= cpu_to_le32(value
);
90 dev_dbg(dev
, "write timestamp %08x\n", time
.value
);
93 * REVISIT handle non-atomic writes ... maybe just retry until
94 * byte[1] sticks (no rollover)?
96 status
= dm355evm_msp_write(time
.bytes
[0], DM355EVM_MSP_RTC_0
);
100 status
= dm355evm_msp_write(time
.bytes
[1], DM355EVM_MSP_RTC_1
);
104 status
= dm355evm_msp_write(time
.bytes
[2], DM355EVM_MSP_RTC_2
);
108 status
= dm355evm_msp_write(time
.bytes
[3], DM355EVM_MSP_RTC_3
);
115 static const struct rtc_class_ops dm355evm_rtc_ops
= {
116 .read_time
= dm355evm_rtc_read_time
,
117 .set_time
= dm355evm_rtc_set_time
,
120 /*----------------------------------------------------------------------*/
122 static int dm355evm_rtc_probe(struct platform_device
*pdev
)
124 struct rtc_device
*rtc
;
126 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
130 platform_set_drvdata(pdev
, rtc
);
132 rtc
->ops
= &dm355evm_rtc_ops
;
133 rtc
->range_max
= U32_MAX
;
135 return devm_rtc_register_device(rtc
);
139 * I2C is used to talk to the MSP430, but this platform device is
140 * exposed by an MFD driver that manages I2C communications.
142 static struct platform_driver rtc_dm355evm_driver
= {
143 .probe
= dm355evm_rtc_probe
,
145 .name
= "rtc-dm355evm",
149 module_platform_driver(rtc_dm355evm_driver
);
151 MODULE_LICENSE("GPL");