2018-05-22 05:45:58 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
//
|
|
|
|
// Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/platform_device.h>
|
2019-03-27 09:18:20 +03:00
|
|
|
#include <linux/pm_wakeirq.h>
|
2012-10-05 04:13:49 +04:00
|
|
|
#include <linux/rtc.h>
|
2014-12-11 02:54:17 +03:00
|
|
|
#include <linux/clk.h>
|
2015-05-26 19:25:57 +03:00
|
|
|
#include <linux/mfd/syscon.h>
|
|
|
|
#include <linux/regmap.h>
|
|
|
|
|
|
|
|
#define SNVS_LPREGISTER_OFFSET 0x34
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* These register offsets are relative to LP (Low Power) range */
|
|
|
|
#define SNVS_LPCR 0x04
|
|
|
|
#define SNVS_LPSR 0x18
|
|
|
|
#define SNVS_LPSRTCMR 0x1c
|
|
|
|
#define SNVS_LPSRTCLR 0x20
|
|
|
|
#define SNVS_LPTAR 0x24
|
|
|
|
#define SNVS_LPPGDR 0x30
|
|
|
|
|
|
|
|
#define SNVS_LPCR_SRTC_ENV (1 << 0)
|
|
|
|
#define SNVS_LPCR_LPTA_EN (1 << 1)
|
|
|
|
#define SNVS_LPCR_LPWUI_EN (1 << 3)
|
|
|
|
#define SNVS_LPSR_LPTA (1 << 0)
|
|
|
|
|
|
|
|
#define SNVS_LPPGDR_INIT 0x41736166
|
|
|
|
#define CNTR_TO_SECS_SH 15
|
|
|
|
|
|
|
|
struct snvs_rtc_data {
|
|
|
|
struct rtc_device *rtc;
|
2015-05-26 19:25:57 +03:00
|
|
|
struct regmap *regmap;
|
|
|
|
int offset;
|
2012-10-05 04:13:49 +04:00
|
|
|
int irq;
|
2014-12-11 02:54:17 +03:00
|
|
|
struct clk *clk;
|
2012-10-05 04:13:49 +04:00
|
|
|
};
|
|
|
|
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
/* Read 64 bit timer register, which could be in inconsistent state */
|
|
|
|
static u64 rtc_read_lpsrt(struct snvs_rtc_data *data)
|
|
|
|
{
|
|
|
|
u32 msb, lsb;
|
|
|
|
|
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPSRTCMR, &msb);
|
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &lsb);
|
|
|
|
return (u64)msb << 32 | lsb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the secure real time counter, taking care to deal with the cases of the
|
|
|
|
* counter updating while being read.
|
|
|
|
*/
|
2015-05-26 19:25:57 +03:00
|
|
|
static u32 rtc_read_lp_counter(struct snvs_rtc_data *data)
|
2012-10-05 04:13:49 +04:00
|
|
|
{
|
|
|
|
u64 read1, read2;
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
unsigned int timeout = 100;
|
2012-10-05 04:13:49 +04:00
|
|
|
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
/* As expected, the registers might update between the read of the LSB
|
|
|
|
* reg and the MSB reg. It's also possible that one register might be
|
|
|
|
* in partially modified state as well.
|
|
|
|
*/
|
|
|
|
read1 = rtc_read_lpsrt(data);
|
2012-10-05 04:13:49 +04:00
|
|
|
do {
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
read2 = read1;
|
|
|
|
read1 = rtc_read_lpsrt(data);
|
|
|
|
} while (read1 != read2 && --timeout);
|
|
|
|
if (!timeout)
|
|
|
|
dev_err(&data->rtc->dev, "Timeout trying to get valid LPSRT Counter read\n");
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* Convert 47-bit counter to 32-bit raw second count */
|
|
|
|
return (u32) (read1 >> CNTR_TO_SECS_SH);
|
|
|
|
}
|
|
|
|
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
/* Just read the lsb from the counter, dealing with inconsistent state */
|
|
|
|
static int rtc_read_lp_counter_lsb(struct snvs_rtc_data *data, u32 *lsb)
|
2012-10-05 04:13:49 +04:00
|
|
|
{
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
u32 count1, count2;
|
|
|
|
unsigned int timeout = 100;
|
|
|
|
|
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count1);
|
|
|
|
do {
|
|
|
|
count2 = count1;
|
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count1);
|
|
|
|
} while (count1 != count2 && --timeout);
|
|
|
|
if (!timeout) {
|
|
|
|
dev_err(&data->rtc->dev, "Timeout trying to get valid LPSRT Counter read\n");
|
|
|
|
return -ETIMEDOUT;
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
|
|
|
|
*lsb = count1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtc_write_sync_lp(struct snvs_rtc_data *data)
|
|
|
|
{
|
|
|
|
u32 count1, count2;
|
|
|
|
u32 elapsed;
|
|
|
|
unsigned int timeout = 1000;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = rtc_read_lp_counter_lsb(data, &count1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Wait for 3 CKIL cycles, about 61.0-91.5 µs */
|
|
|
|
do {
|
|
|
|
ret = rtc_read_lp_counter_lsb(data, &count2);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
elapsed = count2 - count1; /* wrap around _is_ handled! */
|
|
|
|
} while (elapsed < 3 && --timeout);
|
|
|
|
if (!timeout) {
|
|
|
|
dev_err(&data->rtc->dev, "Timeout waiting for LPSRT Counter to change\n");
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int snvs_rtc_enable(struct snvs_rtc_data *data, bool enable)
|
|
|
|
{
|
|
|
|
int timeout = 1000;
|
|
|
|
u32 lpcr;
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_SRTC_ENV,
|
|
|
|
enable ? SNVS_LPCR_SRTC_ENV : 0);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
while (--timeout) {
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPCR, &lpcr);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
if (lpcr & SNVS_LPCR_SRTC_ENV)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (!(lpcr & SNVS_LPCR_SRTC_ENV))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!timeout)
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snvs_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
2015-05-26 19:25:57 +03:00
|
|
|
unsigned long time = rtc_read_lp_counter(data);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
rtc_time_to_tm(time, tm);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snvs_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
unsigned long time;
|
2018-03-28 22:14:05 +03:00
|
|
|
int ret;
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
rtc_tm_to_time(tm, &time);
|
|
|
|
|
|
|
|
/* Disable RTC first */
|
2018-03-28 22:14:05 +03:00
|
|
|
ret = snvs_rtc_enable(data, false);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* Write 32-bit time to 47-bit timer, leaving 15 LSBs blank */
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPSRTCLR, time << CNTR_TO_SECS_SH);
|
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPSRTCMR, time >> (32 - CNTR_TO_SECS_SH));
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* Enable RTC again */
|
2018-03-28 22:14:05 +03:00
|
|
|
ret = snvs_rtc_enable(data, true);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
2018-03-28 22:14:05 +03:00
|
|
|
return ret;
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int snvs_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
|
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
u32 lptar, lpsr;
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPTAR, &lptar);
|
2012-10-05 04:13:49 +04:00
|
|
|
rtc_time_to_tm(lptar, &alrm->time);
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr);
|
2012-10-05 04:13:49 +04:00
|
|
|
alrm->pending = (lpsr & SNVS_LPSR_LPTA) ? 1 : 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snvs_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
|
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_update_bits(data->regmap, data->offset + SNVS_LPCR,
|
|
|
|
(SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN),
|
|
|
|
enable ? (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN) : 0);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
return rtc_write_sync_lp(data);
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int snvs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
|
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
struct rtc_time *alrm_tm = &alrm->time;
|
|
|
|
unsigned long time;
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
int ret;
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
rtc_tm_to_time(alrm_tm, &time);
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_LPTA_EN, 0);
|
rtc: snvs: Add timeouts to avoid kernel lockups
In order to read correctly from asynchronously updated RTC registers,
it's necessary to read repeatedly until their values do not change from
read to read. It's also necessary to wait for three RTC clock ticks for
certain operations. There are no timeouts in this code and these
operations could possibly loop forever.
To avoid kernel hangs, put in timeouts.
The iMX7d can be configured to stop the SRTC on a tamper event, which
will lockup the kernel inside this driver as described above.
These hangs can happen when running under qemu, which doesn't emulate
the SNVS RTC, though currently the driver will refuse to load on qemu
due to a timeout in the driver probe method.
It could also happen if the SRTC block where somehow placed into reset
or the slow speed clock that drives the SRTC counter (but not the CPU)
were to stop.
The symptoms on a two core iMX7d are a work queue hang on
rtc_timer_do_work(), which eventually blocks a systemd fsnotify
operation that triggers a work queue flush, causing systemd to hang and
thus causing all services that should be started by systemd, like a
console getty, to fail to start or stop.
Also optimize the wait code to wait less. It only needs to wait for the
clock to advance three ticks, not to see it change three times.
Cc: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Bryan O'Donoghue <pure.logic@nexus-software.ie>
Signed-off-by: Trent Piepho <tpiepho@impinj.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
2018-05-17 02:45:51 +03:00
|
|
|
ret = rtc_write_sync_lp(data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPTAR, time);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* Clear alarm interrupt status bit */
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPSR, SNVS_LPSR_LPTA);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
return snvs_rtc_alarm_irq_enable(dev, alrm->enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rtc_class_ops snvs_rtc_ops = {
|
|
|
|
.read_time = snvs_rtc_read_time,
|
|
|
|
.set_time = snvs_rtc_set_time,
|
|
|
|
.read_alarm = snvs_rtc_read_alarm,
|
|
|
|
.set_alarm = snvs_rtc_set_alarm,
|
|
|
|
.alarm_irq_enable = snvs_rtc_alarm_irq_enable,
|
|
|
|
};
|
|
|
|
|
|
|
|
static irqreturn_t snvs_rtc_irq_handler(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct device *dev = dev_id;
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
u32 lpsr;
|
|
|
|
u32 events = 0;
|
|
|
|
|
2019-01-11 10:09:02 +03:00
|
|
|
if (data->clk)
|
|
|
|
clk_enable(data->clk);
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
if (lpsr & SNVS_LPSR_LPTA) {
|
|
|
|
events |= (RTC_AF | RTC_IRQF);
|
|
|
|
|
|
|
|
/* RTC alarm should be one-shot */
|
|
|
|
snvs_rtc_alarm_irq_enable(dev, 0);
|
|
|
|
|
|
|
|
rtc_update_irq(data->rtc, 1, events);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear interrupt status */
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPSR, lpsr);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
2019-01-11 10:09:02 +03:00
|
|
|
if (data->clk)
|
|
|
|
clk_disable(data->clk);
|
|
|
|
|
2012-10-05 04:13:49 +04:00
|
|
|
return events ? IRQ_HANDLED : IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
static const struct regmap_config snvs_rtc_config = {
|
|
|
|
.reg_bits = 32,
|
|
|
|
.val_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
};
|
|
|
|
|
2012-12-22 01:09:38 +04:00
|
|
|
static int snvs_rtc_probe(struct platform_device *pdev)
|
2012-10-05 04:13:49 +04:00
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data;
|
|
|
|
int ret;
|
2015-05-26 19:25:57 +03:00
|
|
|
void __iomem *mmio;
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-07-16 10:18:58 +03:00
|
|
|
data->rtc = devm_rtc_allocate_device(&pdev->dev);
|
|
|
|
if (IS_ERR(data->rtc))
|
|
|
|
return PTR_ERR(data->rtc);
|
|
|
|
|
2015-05-26 19:25:57 +03:00
|
|
|
data->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap");
|
|
|
|
|
|
|
|
if (IS_ERR(data->regmap)) {
|
|
|
|
dev_warn(&pdev->dev, "snvs rtc: you use old dts file, please update it\n");
|
|
|
|
|
2019-04-01 08:29:13 +03:00
|
|
|
mmio = devm_platform_ioremap_resource(pdev, 0);
|
2015-05-26 19:25:57 +03:00
|
|
|
if (IS_ERR(mmio))
|
|
|
|
return PTR_ERR(mmio);
|
|
|
|
|
|
|
|
data->regmap = devm_regmap_init_mmio(&pdev->dev, mmio, &snvs_rtc_config);
|
|
|
|
} else {
|
|
|
|
data->offset = SNVS_LPREGISTER_OFFSET;
|
|
|
|
of_property_read_u32(pdev->dev.of_node, "offset", &data->offset);
|
|
|
|
}
|
|
|
|
|
2017-04-23 08:43:24 +03:00
|
|
|
if (IS_ERR(data->regmap)) {
|
2015-05-26 19:25:57 +03:00
|
|
|
dev_err(&pdev->dev, "Can't find snvs syscon\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
data->irq = platform_get_irq(pdev, 0);
|
|
|
|
if (data->irq < 0)
|
|
|
|
return data->irq;
|
|
|
|
|
2014-12-11 02:54:17 +03:00
|
|
|
data->clk = devm_clk_get(&pdev->dev, "snvs-rtc");
|
|
|
|
if (IS_ERR(data->clk)) {
|
|
|
|
data->clk = NULL;
|
|
|
|
} else {
|
|
|
|
ret = clk_prepare_enable(data->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"Could not prepare or enable the snvs clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-05 04:13:49 +04:00
|
|
|
platform_set_drvdata(pdev, data);
|
|
|
|
|
|
|
|
/* Initialize glitch detect */
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPPGDR, SNVS_LPPGDR_INIT);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* Clear interrupt status */
|
2015-05-26 19:25:57 +03:00
|
|
|
regmap_write(data->regmap, data->offset + SNVS_LPSR, 0xffffffff);
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
/* Enable RTC */
|
2018-03-28 22:14:05 +03:00
|
|
|
ret = snvs_rtc_enable(data, true);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "failed to enable rtc %d\n", ret);
|
|
|
|
goto error_rtc_device_register;
|
|
|
|
}
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
device_init_wakeup(&pdev->dev, true);
|
2019-03-27 09:18:20 +03:00
|
|
|
ret = dev_pm_set_wake_irq(&pdev->dev, data->irq);
|
|
|
|
if (ret)
|
|
|
|
dev_err(&pdev->dev, "failed to enable irq wake\n");
|
2012-10-05 04:13:49 +04:00
|
|
|
|
|
|
|
ret = devm_request_irq(&pdev->dev, data->irq, snvs_rtc_irq_handler,
|
|
|
|
IRQF_SHARED, "rtc alarm", &pdev->dev);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "failed to request irq %d: %d\n",
|
|
|
|
data->irq, ret);
|
2014-12-11 02:54:17 +03:00
|
|
|
goto error_rtc_device_register;
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:18:58 +03:00
|
|
|
data->rtc->ops = &snvs_rtc_ops;
|
|
|
|
ret = rtc_register_device(data->rtc);
|
|
|
|
if (ret) {
|
2012-10-05 04:13:49 +04:00
|
|
|
dev_err(&pdev->dev, "failed to register rtc: %d\n", ret);
|
2014-12-11 02:54:17 +03:00
|
|
|
goto error_rtc_device_register;
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-12-11 02:54:17 +03:00
|
|
|
|
|
|
|
error_rtc_device_register:
|
|
|
|
if (data->clk)
|
|
|
|
clk_disable_unprepare(data->clk);
|
|
|
|
|
|
|
|
return ret;
|
2012-10-05 04:13:49 +04:00
|
|
|
}
|
|
|
|
|
2019-04-30 04:07:08 +03:00
|
|
|
static int __maybe_unused snvs_rtc_suspend_noirq(struct device *dev)
|
2015-05-21 18:29:35 +03:00
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
|
2014-12-11 02:54:17 +03:00
|
|
|
if (data->clk)
|
|
|
|
clk_disable_unprepare(data->clk);
|
|
|
|
|
2012-10-05 04:13:49 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-30 04:07:08 +03:00
|
|
|
static int __maybe_unused snvs_rtc_resume_noirq(struct device *dev)
|
2015-05-21 18:29:35 +03:00
|
|
|
{
|
|
|
|
struct snvs_rtc_data *data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (data->clk)
|
|
|
|
return clk_prepare_enable(data->clk);
|
2014-12-11 02:54:17 +03:00
|
|
|
|
2012-10-05 04:13:49 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-11 02:54:20 +03:00
|
|
|
static const struct dev_pm_ops snvs_rtc_pm_ops = {
|
2019-04-30 04:07:08 +03:00
|
|
|
SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(snvs_rtc_suspend_noirq, snvs_rtc_resume_noirq)
|
2014-12-11 02:54:20 +03:00
|
|
|
};
|
2012-10-05 04:13:49 +04:00
|
|
|
|
2012-12-22 01:09:38 +04:00
|
|
|
static const struct of_device_id snvs_dt_ids[] = {
|
2012-10-05 04:13:49 +04:00
|
|
|
{ .compatible = "fsl,sec-v4.0-mon-rtc-lp", },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, snvs_dt_ids);
|
|
|
|
|
|
|
|
static struct platform_driver snvs_rtc_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "snvs_rtc",
|
2019-04-30 04:07:08 +03:00
|
|
|
.pm = &snvs_rtc_pm_ops,
|
2013-11-13 03:10:57 +04:00
|
|
|
.of_match_table = snvs_dt_ids,
|
2012-10-05 04:13:49 +04:00
|
|
|
},
|
|
|
|
.probe = snvs_rtc_probe,
|
|
|
|
};
|
|
|
|
module_platform_driver(snvs_rtc_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
|
|
|
|
MODULE_DESCRIPTION("Freescale SNVS RTC Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|