blob: 6c526e2ec56d8ae5313c03ea88ec4ad2bd0b87bf [file] [log] [blame]
Alexandre Bellonib6743612019-03-15 14:35:08 +01001// SPDX-License-Identifier: GPL-2.0
Saeed Bisharadefb4512009-01-06 14:42:24 -08002/*
3 * Driver for the RTC in Marvell SoCs.
Saeed Bisharadefb4512009-01-06 14:42:24 -08004 */
5
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/rtc.h>
9#include <linux/bcd.h>
Alexandre Bellonife561332015-06-08 16:16:14 +020010#include <linux/bitops.h>
Saeed Bisharadefb4512009-01-06 14:42:24 -080011#include <linux/io.h>
12#include <linux/platform_device.h>
Jason Cooperea983ede2012-03-06 23:53:57 +000013#include <linux/of.h>
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -040014#include <linux/delay.h>
Andrew Lunn89c58c12013-02-03 12:32:06 +010015#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040017#include <linux/module.h>
Saeed Bisharadefb4512009-01-06 14:42:24 -080018
19
20#define RTC_TIME_REG_OFFS 0
21#define RTC_SECONDS_OFFS 0
22#define RTC_MINUTES_OFFS 8
23#define RTC_HOURS_OFFS 16
24#define RTC_WDAY_OFFS 24
Alexandre Bellonife561332015-06-08 16:16:14 +020025#define RTC_HOURS_12H_MODE BIT(22) /* 12 hour mode */
Saeed Bisharadefb4512009-01-06 14:42:24 -080026
27#define RTC_DATE_REG_OFFS 4
28#define RTC_MDAY_OFFS 0
29#define RTC_MONTH_OFFS 8
30#define RTC_YEAR_OFFS 16
31
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080032#define RTC_ALARM_TIME_REG_OFFS 8
33#define RTC_ALARM_DATE_REG_OFFS 0xc
Alexandre Bellonife561332015-06-08 16:16:14 +020034#define RTC_ALARM_VALID BIT(7)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080035
36#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
37#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
Saeed Bisharadefb4512009-01-06 14:42:24 -080038
39struct rtc_plat_data {
40 struct rtc_device *rtc;
41 void __iomem *ioaddr;
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080042 int irq;
Andrew Lunn89c58c12013-02-03 12:32:06 +010043 struct clk *clk;
Saeed Bisharadefb4512009-01-06 14:42:24 -080044};
45
46static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
47{
48 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
49 void __iomem *ioaddr = pdata->ioaddr;
50 u32 rtc_reg;
51
52 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
53 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
54 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
55 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
56 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
57
58 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
59 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
Alexandre Bellonib46c5812019-03-15 14:35:10 +010060 (bin2bcd(tm->tm_year - 100) << RTC_YEAR_OFFS);
Saeed Bisharadefb4512009-01-06 14:42:24 -080061 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
62
63 return 0;
64}
65
66static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
67{
68 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
69 void __iomem *ioaddr = pdata->ioaddr;
70 u32 rtc_time, rtc_date;
71 unsigned int year, month, day, hour, minute, second, wday;
72
73 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
74 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
75
76 second = rtc_time & 0x7f;
77 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
Alexandre Belloni4042a142015-06-08 16:16:15 +020078 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
Saeed Bisharadefb4512009-01-06 14:42:24 -080079 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
80
81 day = rtc_date & 0x3f;
82 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
83 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
84
85 tm->tm_sec = bcd2bin(second);
86 tm->tm_min = bcd2bin(minute);
87 tm->tm_hour = bcd2bin(hour);
88 tm->tm_mday = bcd2bin(day);
89 tm->tm_wday = bcd2bin(wday);
90 tm->tm_mon = bcd2bin(month) - 1;
91 /* hw counts from year 2000, but tm_year is relative to 1900 */
92 tm->tm_year = bcd2bin(year) + 100;
93
Alexandre Belloni22652ba2018-02-19 16:23:56 +010094 return 0;
Saeed Bisharadefb4512009-01-06 14:42:24 -080095}
96
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080097static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
98{
99 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
100 void __iomem *ioaddr = pdata->ioaddr;
101 u32 rtc_time, rtc_date;
102 unsigned int year, month, day, hour, minute, second, wday;
103
104 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
105 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
106
107 second = rtc_time & 0x7f;
108 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
Alexandre Belloni4042a142015-06-08 16:16:15 +0200109 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800110 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
111
112 day = rtc_date & 0x3f;
113 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
114 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
115
116 alm->time.tm_sec = bcd2bin(second);
117 alm->time.tm_min = bcd2bin(minute);
118 alm->time.tm_hour = bcd2bin(hour);
119 alm->time.tm_mday = bcd2bin(day);
120 alm->time.tm_wday = bcd2bin(wday);
121 alm->time.tm_mon = bcd2bin(month) - 1;
122 /* hw counts from year 2000, but tm_year is relative to 1900 */
123 alm->time.tm_year = bcd2bin(year) + 100;
124
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800125 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
Alexandre Belloni959e8b72018-10-02 00:05:25 +0200126
127 return rtc_valid_tm(&alm->time);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800128}
129
130static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
131{
132 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
133 void __iomem *ioaddr = pdata->ioaddr;
134 u32 rtc_reg = 0;
135
136 if (alm->time.tm_sec >= 0)
137 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
138 << RTC_SECONDS_OFFS;
139 if (alm->time.tm_min >= 0)
140 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
141 << RTC_MINUTES_OFFS;
142 if (alm->time.tm_hour >= 0)
143 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
144 << RTC_HOURS_OFFS;
145
146 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
147
148 if (alm->time.tm_mday >= 0)
149 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
150 << RTC_MDAY_OFFS;
151 else
152 rtc_reg = 0;
153
154 if (alm->time.tm_mon >= 0)
155 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
156 << RTC_MONTH_OFFS;
157
158 if (alm->time.tm_year >= 0)
Alexandre Bellonib46c5812019-03-15 14:35:10 +0100159 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year - 100))
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800160 << RTC_YEAR_OFFS;
161
162 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
163 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
164 writel(alm->enabled ? 1 : 0,
165 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
166
167 return 0;
168}
169
John Stultz16380c12011-02-02 17:02:41 -0800170static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800171{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200172 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800173 void __iomem *ioaddr = pdata->ioaddr;
174
175 if (pdata->irq < 0)
John Stultz16380c12011-02-02 17:02:41 -0800176 return -EINVAL; /* fall back into rtc-dev's emulation */
177
178 if (enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800179 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
John Stultz16380c12011-02-02 17:02:41 -0800180 else
181 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800182 return 0;
183}
184
185static irqreturn_t mv_rtc_interrupt(int irq, void *data)
186{
187 struct rtc_plat_data *pdata = data;
188 void __iomem *ioaddr = pdata->ioaddr;
189
190 /* alarm irq? */
191 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
192 return IRQ_NONE;
193
194 /* clear interrupt */
195 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
196 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
197 return IRQ_HANDLED;
198}
199
Saeed Bisharadefb4512009-01-06 14:42:24 -0800200static const struct rtc_class_ops mv_rtc_ops = {
201 .read_time = mv_rtc_read_time,
202 .set_time = mv_rtc_set_time,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800203 .read_alarm = mv_rtc_read_alarm,
204 .set_alarm = mv_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800205 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800206};
207
Jingoo Hanb69af432013-04-29 16:18:21 -0700208static int __init mv_rtc_probe(struct platform_device *pdev)
Saeed Bisharadefb4512009-01-06 14:42:24 -0800209{
Saeed Bisharadefb4512009-01-06 14:42:24 -0800210 struct rtc_plat_data *pdata;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800211 u32 rtc_time;
Andrew Lunn89c58c12013-02-03 12:32:06 +0100212 int ret = 0;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800213
Saeed Bisharadefb4512009-01-06 14:42:24 -0800214 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
215 if (!pdata)
216 return -ENOMEM;
217
YueHaibing09ef18b2019-10-06 18:29:20 +0800218 pdata->ioaddr = devm_platform_ioremap_resource(pdev, 0);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700219 if (IS_ERR(pdata->ioaddr))
220 return PTR_ERR(pdata->ioaddr);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800221
Andrew Lunn89c58c12013-02-03 12:32:06 +0100222 pdata->clk = devm_clk_get(&pdev->dev, NULL);
223 /* Not all SoCs require a clock.*/
224 if (!IS_ERR(pdata->clk))
225 clk_prepare_enable(pdata->clk);
226
Alexandre Belloni4042a142015-06-08 16:16:15 +0200227 /* make sure the 24 hour mode is enabled */
Saeed Bisharadefb4512009-01-06 14:42:24 -0800228 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
229 if (rtc_time & RTC_HOURS_12H_MODE) {
Alexandre Belloni4042a142015-06-08 16:16:15 +0200230 dev_err(&pdev->dev, "12 Hour mode is enabled but not supported.\n");
Andrew Lunn89c58c12013-02-03 12:32:06 +0100231 ret = -EINVAL;
232 goto out;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800233 }
234
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400235 /* make sure it is actually functional */
236 if (rtc_time == 0x01000000) {
237 ssleep(1);
238 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
239 if (rtc_time == 0x01000000) {
240 dev_err(&pdev->dev, "internal RTC not ticking\n");
Andrew Lunn89c58c12013-02-03 12:32:06 +0100241 ret = -ENODEV;
242 goto out;
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400243 }
244 }
245
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800246 pdata->irq = platform_get_irq(pdev, 0);
247
Saeed Bisharadefb4512009-01-06 14:42:24 -0800248 platform_set_drvdata(pdev, pdata);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800249
Alexandre Belloni5b25a712019-03-15 14:35:09 +0100250 pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
Andrew Lunn89c58c12013-02-03 12:32:06 +0100251 if (IS_ERR(pdata->rtc)) {
252 ret = PTR_ERR(pdata->rtc);
253 goto out;
254 }
Saeed Bisharadefb4512009-01-06 14:42:24 -0800255
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800256 if (pdata->irq >= 0) {
257 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
258 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700259 IRQF_SHARED,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800260 pdev->name, pdata) < 0) {
261 dev_warn(&pdev->dev, "interrupt not available.\n");
262 pdata->irq = -1;
263 }
264 }
265
Alexandre Bellonib0d42de2021-01-11 00:17:40 +0100266 if (pdata->irq >= 0)
Alexandre Belloni5b25a712019-03-15 14:35:09 +0100267 device_init_wakeup(&pdev->dev, 1);
Alexandre Bellonib0d42de2021-01-11 00:17:40 +0100268 else
269 clear_bit(RTC_FEATURE_ALARM, pdata->rtc->features);
Alexandre Belloni5b25a712019-03-15 14:35:09 +0100270
Alexandre Bellonib0d42de2021-01-11 00:17:40 +0100271 pdata->rtc->ops = &mv_rtc_ops;
Alexandre Bellonib46c5812019-03-15 14:35:10 +0100272 pdata->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
273 pdata->rtc->range_max = RTC_TIMESTAMP_END_2099;
274
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100275 ret = devm_rtc_register_device(pdata->rtc);
Alexandre Belloni5b25a712019-03-15 14:35:09 +0100276 if (!ret)
277 return 0;
Andrew Lunn89c58c12013-02-03 12:32:06 +0100278out:
279 if (!IS_ERR(pdata->clk))
280 clk_disable_unprepare(pdata->clk);
281
282 return ret;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800283}
284
285static int __exit mv_rtc_remove(struct platform_device *pdev)
286{
287 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
288
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800289 if (pdata->irq >= 0)
290 device_init_wakeup(&pdev->dev, 0);
291
Andrew Lunn89c58c12013-02-03 12:32:06 +0100292 if (!IS_ERR(pdata->clk))
293 clk_disable_unprepare(pdata->clk);
294
Saeed Bisharadefb4512009-01-06 14:42:24 -0800295 return 0;
296}
297
Jason Cooperea983ede2012-03-06 23:53:57 +0000298#ifdef CONFIG_OF
Jingoo Han4621bc52014-06-06 14:35:57 -0700299static const struct of_device_id rtc_mv_of_match_table[] = {
Andrew Lunn77843502012-07-18 19:22:54 +0200300 { .compatible = "marvell,orion-rtc", },
Jason Cooperea983ede2012-03-06 23:53:57 +0000301 {}
302};
Javier Martinez Canillas73798d52015-08-27 13:52:02 +0200303MODULE_DEVICE_TABLE(of, rtc_mv_of_match_table);
Jason Cooperea983ede2012-03-06 23:53:57 +0000304#endif
305
Saeed Bisharadefb4512009-01-06 14:42:24 -0800306static struct platform_driver mv_rtc_driver = {
307 .remove = __exit_p(mv_rtc_remove),
308 .driver = {
309 .name = "rtc-mv",
Jason Cooperea983ede2012-03-06 23:53:57 +0000310 .of_match_table = of_match_ptr(rtc_mv_of_match_table),
Saeed Bisharadefb4512009-01-06 14:42:24 -0800311 },
312};
313
Jingoo Han1963f7c2013-04-29 16:18:44 -0700314module_platform_driver_probe(mv_rtc_driver, mv_rtc_probe);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800315
316MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
317MODULE_DESCRIPTION("Marvell RTC driver");
318MODULE_LICENSE("GPL");
319MODULE_ALIAS("platform:rtc-mv");