18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * RTC driver for the Armada 38x Marvell SoCs
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Marvell
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Gregory Clement <gregory.clement@free-electrons.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/of.h>
148c2ecf20Sopenharmony_ci#include <linux/of_device.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/rtc.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define RTC_STATUS	    0x0
198c2ecf20Sopenharmony_ci#define RTC_STATUS_ALARM1	    BIT(0)
208c2ecf20Sopenharmony_ci#define RTC_STATUS_ALARM2	    BIT(1)
218c2ecf20Sopenharmony_ci#define RTC_IRQ1_CONF	    0x4
228c2ecf20Sopenharmony_ci#define RTC_IRQ2_CONF	    0x8
238c2ecf20Sopenharmony_ci#define RTC_IRQ_AL_EN		    BIT(0)
248c2ecf20Sopenharmony_ci#define RTC_IRQ_FREQ_EN		    BIT(1)
258c2ecf20Sopenharmony_ci#define RTC_IRQ_FREQ_1HZ	    BIT(2)
268c2ecf20Sopenharmony_ci#define RTC_CCR		    0x18
278c2ecf20Sopenharmony_ci#define RTC_CCR_MODE		    BIT(15)
288c2ecf20Sopenharmony_ci#define RTC_CONF_TEST	    0x1C
298c2ecf20Sopenharmony_ci#define RTC_NOMINAL_TIMING	    BIT(13)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define RTC_TIME	    0xC
328c2ecf20Sopenharmony_ci#define RTC_ALARM1	    0x10
338c2ecf20Sopenharmony_ci#define RTC_ALARM2	    0x14
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Armada38x SoC registers  */
368c2ecf20Sopenharmony_ci#define RTC_38X_BRIDGE_TIMING_CTL   0x0
378c2ecf20Sopenharmony_ci#define RTC_38X_PERIOD_OFFS		0
388c2ecf20Sopenharmony_ci#define RTC_38X_PERIOD_MASK		(0x3FF << RTC_38X_PERIOD_OFFS)
398c2ecf20Sopenharmony_ci#define RTC_38X_READ_DELAY_OFFS		26
408c2ecf20Sopenharmony_ci#define RTC_38X_READ_DELAY_MASK		(0x1F << RTC_38X_READ_DELAY_OFFS)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* Armada 7K/8K registers  */
438c2ecf20Sopenharmony_ci#define RTC_8K_BRIDGE_TIMING_CTL0    0x0
448c2ecf20Sopenharmony_ci#define RTC_8K_WRCLK_PERIOD_OFFS	0
458c2ecf20Sopenharmony_ci#define RTC_8K_WRCLK_PERIOD_MASK	(0xFFFF << RTC_8K_WRCLK_PERIOD_OFFS)
468c2ecf20Sopenharmony_ci#define RTC_8K_WRCLK_SETUP_OFFS		16
478c2ecf20Sopenharmony_ci#define RTC_8K_WRCLK_SETUP_MASK		(0xFFFF << RTC_8K_WRCLK_SETUP_OFFS)
488c2ecf20Sopenharmony_ci#define RTC_8K_BRIDGE_TIMING_CTL1   0x4
498c2ecf20Sopenharmony_ci#define RTC_8K_READ_DELAY_OFFS		0
508c2ecf20Sopenharmony_ci#define RTC_8K_READ_DELAY_MASK		(0xFFFF << RTC_8K_READ_DELAY_OFFS)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define RTC_8K_ISR		    0x10
538c2ecf20Sopenharmony_ci#define RTC_8K_IMR		    0x14
548c2ecf20Sopenharmony_ci#define RTC_8K_ALARM2			BIT(0)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define SOC_RTC_INTERRUPT	    0x8
578c2ecf20Sopenharmony_ci#define SOC_RTC_ALARM1			BIT(0)
588c2ecf20Sopenharmony_ci#define SOC_RTC_ALARM2			BIT(1)
598c2ecf20Sopenharmony_ci#define SOC_RTC_ALARM1_MASK		BIT(2)
608c2ecf20Sopenharmony_ci#define SOC_RTC_ALARM2_MASK		BIT(3)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define SAMPLE_NR 100
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct value_to_freq {
658c2ecf20Sopenharmony_ci	u32 value;
668c2ecf20Sopenharmony_ci	u8 freq;
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistruct armada38x_rtc {
708c2ecf20Sopenharmony_ci	struct rtc_device   *rtc_dev;
718c2ecf20Sopenharmony_ci	void __iomem	    *regs;
728c2ecf20Sopenharmony_ci	void __iomem	    *regs_soc;
738c2ecf20Sopenharmony_ci	spinlock_t	    lock;
748c2ecf20Sopenharmony_ci	int		    irq;
758c2ecf20Sopenharmony_ci	bool		    initialized;
768c2ecf20Sopenharmony_ci	struct value_to_freq *val_to_freq;
778c2ecf20Sopenharmony_ci	const struct armada38x_rtc_data *data;
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define ALARM1	0
818c2ecf20Sopenharmony_ci#define ALARM2	1
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define ALARM_REG(base, alarm)	 ((base) + (alarm) * sizeof(u32))
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistruct armada38x_rtc_data {
868c2ecf20Sopenharmony_ci	/* Initialize the RTC-MBUS bridge timing */
878c2ecf20Sopenharmony_ci	void (*update_mbus_timing)(struct armada38x_rtc *rtc);
888c2ecf20Sopenharmony_ci	u32 (*read_rtc_reg)(struct armada38x_rtc *rtc, u8 rtc_reg);
898c2ecf20Sopenharmony_ci	void (*clear_isr)(struct armada38x_rtc *rtc);
908c2ecf20Sopenharmony_ci	void (*unmask_interrupt)(struct armada38x_rtc *rtc);
918c2ecf20Sopenharmony_ci	u32 alarm;
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/*
958c2ecf20Sopenharmony_ci * According to the datasheet, the OS should wait 5us after every
968c2ecf20Sopenharmony_ci * register write to the RTC hard macro so that the required update
978c2ecf20Sopenharmony_ci * can occur without holding off the system bus
988c2ecf20Sopenharmony_ci * According to errata RES-3124064, Write to any RTC register
998c2ecf20Sopenharmony_ci * may fail. As a workaround, before writing to RTC
1008c2ecf20Sopenharmony_ci * register, issue a dummy write of 0x0 twice to RTC Status
1018c2ecf20Sopenharmony_ci * register.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	writel(0, rtc->regs + RTC_STATUS);
1078c2ecf20Sopenharmony_ci	writel(0, rtc->regs + RTC_STATUS);
1088c2ecf20Sopenharmony_ci	writel(val, rtc->regs + offset);
1098c2ecf20Sopenharmony_ci	udelay(5);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* Update RTC-MBUS bridge timing parameters */
1138c2ecf20Sopenharmony_cistatic void rtc_update_38x_mbus_timing_params(struct armada38x_rtc *rtc)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	u32 reg;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	reg = readl(rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
1188c2ecf20Sopenharmony_ci	reg &= ~RTC_38X_PERIOD_MASK;
1198c2ecf20Sopenharmony_ci	reg |= 0x3FF << RTC_38X_PERIOD_OFFS; /* Maximum value */
1208c2ecf20Sopenharmony_ci	reg &= ~RTC_38X_READ_DELAY_MASK;
1218c2ecf20Sopenharmony_ci	reg |= 0x1F << RTC_38X_READ_DELAY_OFFS; /* Maximum value */
1228c2ecf20Sopenharmony_ci	writel(reg, rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic void rtc_update_8k_mbus_timing_params(struct armada38x_rtc *rtc)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u32 reg;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
1308c2ecf20Sopenharmony_ci	reg &= ~RTC_8K_WRCLK_PERIOD_MASK;
1318c2ecf20Sopenharmony_ci	reg |= 0x3FF << RTC_8K_WRCLK_PERIOD_OFFS;
1328c2ecf20Sopenharmony_ci	reg &= ~RTC_8K_WRCLK_SETUP_MASK;
1338c2ecf20Sopenharmony_ci	reg |= 0x29 << RTC_8K_WRCLK_SETUP_OFFS;
1348c2ecf20Sopenharmony_ci	writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
1378c2ecf20Sopenharmony_ci	reg &= ~RTC_8K_READ_DELAY_MASK;
1388c2ecf20Sopenharmony_ci	reg |= 0x3F << RTC_8K_READ_DELAY_OFFS;
1398c2ecf20Sopenharmony_ci	writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic u32 read_rtc_register(struct armada38x_rtc *rtc, u8 rtc_reg)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	return readl(rtc->regs + rtc_reg);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic u32 read_rtc_register_38x_wa(struct armada38x_rtc *rtc, u8 rtc_reg)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int i, index_max = 0, max = 0;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	for (i = 0; i < SAMPLE_NR; i++) {
1528c2ecf20Sopenharmony_ci		rtc->val_to_freq[i].value = readl(rtc->regs + rtc_reg);
1538c2ecf20Sopenharmony_ci		rtc->val_to_freq[i].freq = 0;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	for (i = 0; i < SAMPLE_NR; i++) {
1578c2ecf20Sopenharmony_ci		int j = 0;
1588c2ecf20Sopenharmony_ci		u32 value = rtc->val_to_freq[i].value;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		while (rtc->val_to_freq[j].freq) {
1618c2ecf20Sopenharmony_ci			if (rtc->val_to_freq[j].value == value) {
1628c2ecf20Sopenharmony_ci				rtc->val_to_freq[j].freq++;
1638c2ecf20Sopenharmony_ci				break;
1648c2ecf20Sopenharmony_ci			}
1658c2ecf20Sopenharmony_ci			j++;
1668c2ecf20Sopenharmony_ci		}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		if (!rtc->val_to_freq[j].freq) {
1698c2ecf20Sopenharmony_ci			rtc->val_to_freq[j].value = value;
1708c2ecf20Sopenharmony_ci			rtc->val_to_freq[j].freq = 1;
1718c2ecf20Sopenharmony_ci		}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci		if (rtc->val_to_freq[j].freq > max) {
1748c2ecf20Sopenharmony_ci			index_max = j;
1758c2ecf20Sopenharmony_ci			max = rtc->val_to_freq[j].freq;
1768c2ecf20Sopenharmony_ci		}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		/*
1798c2ecf20Sopenharmony_ci		 * If a value already has half of the sample this is the most
1808c2ecf20Sopenharmony_ci		 * frequent one and we can stop the research right now
1818c2ecf20Sopenharmony_ci		 */
1828c2ecf20Sopenharmony_ci		if (max > SAMPLE_NR / 2)
1838c2ecf20Sopenharmony_ci			break;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	return rtc->val_to_freq[index_max].value;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic void armada38x_clear_isr(struct armada38x_rtc *rtc)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void armada38x_unmask_interrupt(struct armada38x_rtc *rtc)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	writel(val | SOC_RTC_ALARM1_MASK, rtc->regs_soc + SOC_RTC_INTERRUPT);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic void armada8k_clear_isr(struct armada38x_rtc *rtc)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_ISR);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic void armada8k_unmask_interrupt(struct armada38x_rtc *rtc)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_IMR);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
2168c2ecf20Sopenharmony_ci	unsigned long time, flags;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc->lock, flags);
2198c2ecf20Sopenharmony_ci	time = rtc->data->read_rtc_reg(rtc, RTC_TIME);
2208c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc->lock, flags);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	rtc_time64_to_tm(time, tm);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return 0;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic void armada38x_rtc_reset(struct armada38x_rtc *rtc)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	u32 reg;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	reg = rtc->data->read_rtc_reg(rtc, RTC_CONF_TEST);
2328c2ecf20Sopenharmony_ci	/* If bits [7:0] are non-zero, assume RTC was uninitialized */
2338c2ecf20Sopenharmony_ci	if (reg & 0xff) {
2348c2ecf20Sopenharmony_ci		rtc_delayed_write(0, rtc, RTC_CONF_TEST);
2358c2ecf20Sopenharmony_ci		msleep(500); /* Oscillator startup time */
2368c2ecf20Sopenharmony_ci		rtc_delayed_write(0, rtc, RTC_TIME);
2378c2ecf20Sopenharmony_ci		rtc_delayed_write(SOC_RTC_ALARM1 | SOC_RTC_ALARM2, rtc,
2388c2ecf20Sopenharmony_ci				  RTC_STATUS);
2398c2ecf20Sopenharmony_ci		rtc_delayed_write(RTC_NOMINAL_TIMING, rtc, RTC_CCR);
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci	rtc->initialized = true;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
2478c2ecf20Sopenharmony_ci	unsigned long time, flags;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	time = rtc_tm_to_time64(tm);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	if (!rtc->initialized)
2528c2ecf20Sopenharmony_ci		armada38x_rtc_reset(rtc);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc->lock, flags);
2558c2ecf20Sopenharmony_ci	rtc_delayed_write(time, rtc, RTC_TIME);
2568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc->lock, flags);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return 0;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
2648c2ecf20Sopenharmony_ci	unsigned long time, flags;
2658c2ecf20Sopenharmony_ci	u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
2668c2ecf20Sopenharmony_ci	u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
2678c2ecf20Sopenharmony_ci	u32 val;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc->lock, flags);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	time = rtc->data->read_rtc_reg(rtc, reg);
2728c2ecf20Sopenharmony_ci	val = rtc->data->read_rtc_reg(rtc, reg_irq) & RTC_IRQ_AL_EN;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc->lock, flags);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	alrm->enabled = val ? 1 : 0;
2778c2ecf20Sopenharmony_ci	rtc_time64_to_tm(time,  &alrm->time);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
2858c2ecf20Sopenharmony_ci	u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
2868c2ecf20Sopenharmony_ci	u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
2878c2ecf20Sopenharmony_ci	unsigned long time, flags;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	time = rtc_tm_to_time64(&alrm->time);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc->lock, flags);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	rtc_delayed_write(time, rtc, reg);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (alrm->enabled) {
2968c2ecf20Sopenharmony_ci		rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
2978c2ecf20Sopenharmony_ci		rtc->data->unmask_interrupt(rtc);
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc->lock, flags);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	return 0;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic int armada38x_rtc_alarm_irq_enable(struct device *dev,
3068c2ecf20Sopenharmony_ci					 unsigned int enabled)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
3098c2ecf20Sopenharmony_ci	u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
3108c2ecf20Sopenharmony_ci	unsigned long flags;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc->lock, flags);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (enabled)
3158c2ecf20Sopenharmony_ci		rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
3168c2ecf20Sopenharmony_ci	else
3178c2ecf20Sopenharmony_ci		rtc_delayed_write(0, rtc, reg_irq);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc->lock, flags);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return 0;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = data;
3278c2ecf20Sopenharmony_ci	u32 val;
3288c2ecf20Sopenharmony_ci	int event = RTC_IRQF | RTC_AF;
3298c2ecf20Sopenharmony_ci	u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	spin_lock(&rtc->lock);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	rtc->data->clear_isr(rtc);
3368c2ecf20Sopenharmony_ci	val = rtc->data->read_rtc_reg(rtc, reg_irq);
3378c2ecf20Sopenharmony_ci	/* disable all the interrupts for alarm*/
3388c2ecf20Sopenharmony_ci	rtc_delayed_write(0, rtc, reg_irq);
3398c2ecf20Sopenharmony_ci	/* Ack the event */
3408c2ecf20Sopenharmony_ci	rtc_delayed_write(1 << rtc->data->alarm, rtc, RTC_STATUS);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	spin_unlock(&rtc->lock);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (val & RTC_IRQ_FREQ_EN) {
3458c2ecf20Sopenharmony_ci		if (val & RTC_IRQ_FREQ_1HZ)
3468c2ecf20Sopenharmony_ci			event |= RTC_UF;
3478c2ecf20Sopenharmony_ci		else
3488c2ecf20Sopenharmony_ci			event |= RTC_PF;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	rtc_update_irq(rtc->rtc_dev, 1, event);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/*
3578c2ecf20Sopenharmony_ci * The information given in the Armada 388 functional spec is complex.
3588c2ecf20Sopenharmony_ci * They give two different formulas for calculating the offset value,
3598c2ecf20Sopenharmony_ci * but when considering "Offset" as an 8-bit signed integer, they both
3608c2ecf20Sopenharmony_ci * reduce down to (we shall rename "Offset" as "val" here):
3618c2ecf20Sopenharmony_ci *
3628c2ecf20Sopenharmony_ci *   val = (f_ideal / f_measured - 1) / resolution   where f_ideal = 32768
3638c2ecf20Sopenharmony_ci *
3648c2ecf20Sopenharmony_ci * Converting to time, f = 1/t:
3658c2ecf20Sopenharmony_ci *   val = (t_measured / t_ideal - 1) / resolution   where t_ideal = 1/32768
3668c2ecf20Sopenharmony_ci *
3678c2ecf20Sopenharmony_ci *   =>  t_measured / t_ideal = val * resolution + 1
3688c2ecf20Sopenharmony_ci *
3698c2ecf20Sopenharmony_ci * "offset" in the RTC interface is defined as:
3708c2ecf20Sopenharmony_ci *   t = t0 * (1 + offset * 1e-9)
3718c2ecf20Sopenharmony_ci * where t is the desired period, t0 is the measured period with a zero
3728c2ecf20Sopenharmony_ci * offset, which is t_measured above. With t0 = t_measured and t = t_ideal,
3738c2ecf20Sopenharmony_ci *   offset = (t_ideal / t_measured - 1) / 1e-9
3748c2ecf20Sopenharmony_ci *
3758c2ecf20Sopenharmony_ci *   => t_ideal / t_measured = offset * 1e-9 + 1
3768c2ecf20Sopenharmony_ci *
3778c2ecf20Sopenharmony_ci * so:
3788c2ecf20Sopenharmony_ci *
3798c2ecf20Sopenharmony_ci *   offset * 1e-9 + 1 = 1 / (val * resolution + 1)
3808c2ecf20Sopenharmony_ci *
3818c2ecf20Sopenharmony_ci * We want "resolution" to be an integer, so resolution = R * 1e-9, giving
3828c2ecf20Sopenharmony_ci *   offset = 1e18 / (val * R + 1e9) - 1e9
3838c2ecf20Sopenharmony_ci *   val = (1e18 / (offset + 1e9) - 1e9) / R
3848c2ecf20Sopenharmony_ci * with a common transformation:
3858c2ecf20Sopenharmony_ci *   f(x) = 1e18 / (x + 1e9) - 1e9
3868c2ecf20Sopenharmony_ci *   offset = f(val * R)
3878c2ecf20Sopenharmony_ci *   val = f(offset) / R
3888c2ecf20Sopenharmony_ci *
3898c2ecf20Sopenharmony_ci * Armada 38x supports two modes, fine mode (954ppb) and coarse mode (3815ppb).
3908c2ecf20Sopenharmony_ci */
3918c2ecf20Sopenharmony_cistatic long armada38x_ppb_convert(long ppb)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	long div = ppb + 1000000000L;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	return div_s64(1000000000000000000LL + div / 2, div) - 1000000000L;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic int armada38x_rtc_read_offset(struct device *dev, long *offset)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
4018c2ecf20Sopenharmony_ci	unsigned long ccr, flags;
4028c2ecf20Sopenharmony_ci	long ppb_cor;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc->lock, flags);
4058c2ecf20Sopenharmony_ci	ccr = rtc->data->read_rtc_reg(rtc, RTC_CCR);
4068c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc->lock, flags);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	ppb_cor = (ccr & RTC_CCR_MODE ? 3815 : 954) * (s8)ccr;
4098c2ecf20Sopenharmony_ci	/* ppb_cor + 1000000000L can never be zero */
4108c2ecf20Sopenharmony_ci	*offset = armada38x_ppb_convert(ppb_cor);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	return 0;
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic int armada38x_rtc_set_offset(struct device *dev, long offset)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc = dev_get_drvdata(dev);
4188c2ecf20Sopenharmony_ci	unsigned long ccr = 0;
4198c2ecf20Sopenharmony_ci	long ppb_cor, off;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/*
4228c2ecf20Sopenharmony_ci	 * The maximum ppb_cor is -128 * 3815 .. 127 * 3815, but we
4238c2ecf20Sopenharmony_ci	 * need to clamp the input.  This equates to -484270 .. 488558.
4248c2ecf20Sopenharmony_ci	 * Not only is this to stop out of range "off" but also to
4258c2ecf20Sopenharmony_ci	 * avoid the division by zero in armada38x_ppb_convert().
4268c2ecf20Sopenharmony_ci	 */
4278c2ecf20Sopenharmony_ci	offset = clamp(offset, -484270L, 488558L);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	ppb_cor = armada38x_ppb_convert(offset);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/*
4328c2ecf20Sopenharmony_ci	 * Use low update mode where possible, which gives a better
4338c2ecf20Sopenharmony_ci	 * resolution of correction.
4348c2ecf20Sopenharmony_ci	 */
4358c2ecf20Sopenharmony_ci	off = DIV_ROUND_CLOSEST(ppb_cor, 954);
4368c2ecf20Sopenharmony_ci	if (off > 127 || off < -128) {
4378c2ecf20Sopenharmony_ci		ccr = RTC_CCR_MODE;
4388c2ecf20Sopenharmony_ci		off = DIV_ROUND_CLOSEST(ppb_cor, 3815);
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	/*
4428c2ecf20Sopenharmony_ci	 * Armada 388 requires a bit pattern in bits 14..8 depending on
4438c2ecf20Sopenharmony_ci	 * the sign bit: { 0, ~S, S, S, S, S, S }
4448c2ecf20Sopenharmony_ci	 */
4458c2ecf20Sopenharmony_ci	ccr |= (off & 0x3fff) ^ 0x2000;
4468c2ecf20Sopenharmony_ci	rtc_delayed_write(ccr, rtc, RTC_CCR);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	return 0;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic const struct rtc_class_ops armada38x_rtc_ops = {
4528c2ecf20Sopenharmony_ci	.read_time = armada38x_rtc_read_time,
4538c2ecf20Sopenharmony_ci	.set_time = armada38x_rtc_set_time,
4548c2ecf20Sopenharmony_ci	.read_alarm = armada38x_rtc_read_alarm,
4558c2ecf20Sopenharmony_ci	.set_alarm = armada38x_rtc_set_alarm,
4568c2ecf20Sopenharmony_ci	.alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
4578c2ecf20Sopenharmony_ci	.read_offset = armada38x_rtc_read_offset,
4588c2ecf20Sopenharmony_ci	.set_offset = armada38x_rtc_set_offset,
4598c2ecf20Sopenharmony_ci};
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic const struct rtc_class_ops armada38x_rtc_ops_noirq = {
4628c2ecf20Sopenharmony_ci	.read_time = armada38x_rtc_read_time,
4638c2ecf20Sopenharmony_ci	.set_time = armada38x_rtc_set_time,
4648c2ecf20Sopenharmony_ci	.read_alarm = armada38x_rtc_read_alarm,
4658c2ecf20Sopenharmony_ci	.read_offset = armada38x_rtc_read_offset,
4668c2ecf20Sopenharmony_ci	.set_offset = armada38x_rtc_set_offset,
4678c2ecf20Sopenharmony_ci};
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic const struct armada38x_rtc_data armada38x_data = {
4708c2ecf20Sopenharmony_ci	.update_mbus_timing = rtc_update_38x_mbus_timing_params,
4718c2ecf20Sopenharmony_ci	.read_rtc_reg = read_rtc_register_38x_wa,
4728c2ecf20Sopenharmony_ci	.clear_isr = armada38x_clear_isr,
4738c2ecf20Sopenharmony_ci	.unmask_interrupt = armada38x_unmask_interrupt,
4748c2ecf20Sopenharmony_ci	.alarm = ALARM1,
4758c2ecf20Sopenharmony_ci};
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic const struct armada38x_rtc_data armada8k_data = {
4788c2ecf20Sopenharmony_ci	.update_mbus_timing = rtc_update_8k_mbus_timing_params,
4798c2ecf20Sopenharmony_ci	.read_rtc_reg = read_rtc_register,
4808c2ecf20Sopenharmony_ci	.clear_isr = armada8k_clear_isr,
4818c2ecf20Sopenharmony_ci	.unmask_interrupt = armada8k_unmask_interrupt,
4828c2ecf20Sopenharmony_ci	.alarm = ALARM2,
4838c2ecf20Sopenharmony_ci};
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
4868c2ecf20Sopenharmony_cistatic const struct of_device_id armada38x_rtc_of_match_table[] = {
4878c2ecf20Sopenharmony_ci	{
4888c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-380-rtc",
4898c2ecf20Sopenharmony_ci		.data = &armada38x_data,
4908c2ecf20Sopenharmony_ci	},
4918c2ecf20Sopenharmony_ci	{
4928c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-8k-rtc",
4938c2ecf20Sopenharmony_ci		.data = &armada8k_data,
4948c2ecf20Sopenharmony_ci	},
4958c2ecf20Sopenharmony_ci	{}
4968c2ecf20Sopenharmony_ci};
4978c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, armada38x_rtc_of_match_table);
4988c2ecf20Sopenharmony_ci#endif
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic __init int armada38x_rtc_probe(struct platform_device *pdev)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	struct resource *res;
5038c2ecf20Sopenharmony_ci	struct armada38x_rtc *rtc;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
5068c2ecf20Sopenharmony_ci			    GFP_KERNEL);
5078c2ecf20Sopenharmony_ci	if (!rtc)
5088c2ecf20Sopenharmony_ci		return -ENOMEM;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	rtc->data = of_device_get_match_data(&pdev->dev);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
5138c2ecf20Sopenharmony_ci				sizeof(struct value_to_freq), GFP_KERNEL);
5148c2ecf20Sopenharmony_ci	if (!rtc->val_to_freq)
5158c2ecf20Sopenharmony_ci		return -ENOMEM;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	spin_lock_init(&rtc->lock);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
5208c2ecf20Sopenharmony_ci	rtc->regs = devm_ioremap_resource(&pdev->dev, res);
5218c2ecf20Sopenharmony_ci	if (IS_ERR(rtc->regs))
5228c2ecf20Sopenharmony_ci		return PTR_ERR(rtc->regs);
5238c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
5248c2ecf20Sopenharmony_ci	rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
5258c2ecf20Sopenharmony_ci	if (IS_ERR(rtc->regs_soc))
5268c2ecf20Sopenharmony_ci		return PTR_ERR(rtc->regs_soc);
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	rtc->irq = platform_get_irq(pdev, 0);
5298c2ecf20Sopenharmony_ci	if (rtc->irq < 0)
5308c2ecf20Sopenharmony_ci		return rtc->irq;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
5338c2ecf20Sopenharmony_ci	if (IS_ERR(rtc->rtc_dev))
5348c2ecf20Sopenharmony_ci		return PTR_ERR(rtc->rtc_dev);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
5378c2ecf20Sopenharmony_ci				0, pdev->name, rtc) < 0) {
5388c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "Interrupt not available.\n");
5398c2ecf20Sopenharmony_ci		rtc->irq = -1;
5408c2ecf20Sopenharmony_ci	}
5418c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rtc);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (rtc->irq != -1) {
5448c2ecf20Sopenharmony_ci		device_init_wakeup(&pdev->dev, 1);
5458c2ecf20Sopenharmony_ci		rtc->rtc_dev->ops = &armada38x_rtc_ops;
5468c2ecf20Sopenharmony_ci	} else {
5478c2ecf20Sopenharmony_ci		/*
5488c2ecf20Sopenharmony_ci		 * If there is no interrupt available then we can't
5498c2ecf20Sopenharmony_ci		 * use the alarm
5508c2ecf20Sopenharmony_ci		 */
5518c2ecf20Sopenharmony_ci		rtc->rtc_dev->ops = &armada38x_rtc_ops_noirq;
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	/* Update RTC-MBUS bridge timing parameters */
5558c2ecf20Sopenharmony_ci	rtc->data->update_mbus_timing(rtc);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	rtc->rtc_dev->range_max = U32_MAX;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	return rtc_register_device(rtc->rtc_dev);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
5638c2ecf20Sopenharmony_cistatic int armada38x_rtc_suspend(struct device *dev)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev)) {
5668c2ecf20Sopenharmony_ci		struct armada38x_rtc *rtc = dev_get_drvdata(dev);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci		return enable_irq_wake(rtc->irq);
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	return 0;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic int armada38x_rtc_resume(struct device *dev)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev)) {
5778c2ecf20Sopenharmony_ci		struct armada38x_rtc *rtc = dev_get_drvdata(dev);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		/* Update RTC-MBUS bridge timing parameters */
5808c2ecf20Sopenharmony_ci		rtc->data->update_mbus_timing(rtc);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci		return disable_irq_wake(rtc->irq);
5838c2ecf20Sopenharmony_ci	}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	return 0;
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci#endif
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
5908c2ecf20Sopenharmony_ci			 armada38x_rtc_suspend, armada38x_rtc_resume);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic struct platform_driver armada38x_rtc_driver = {
5938c2ecf20Sopenharmony_ci	.driver		= {
5948c2ecf20Sopenharmony_ci		.name	= "armada38x-rtc",
5958c2ecf20Sopenharmony_ci		.pm	= &armada38x_rtc_pm_ops,
5968c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
5978c2ecf20Sopenharmony_ci	},
5988c2ecf20Sopenharmony_ci};
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cimodule_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
6038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
6048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
605