blob: 4b198b3778d3aa87ab03bd2abe4e2787b548e55f [file] [log] [blame]
Saeed Bisharadefb4512009-01-06 14:42:24 -08001/*
2 * Driver for the RTC in Marvell SoCs.
3 *
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/rtc.h>
12#include <linux/bcd.h>
Alexandre Bellonife561332015-06-08 16:16:14 +020013#include <linux/bitops.h>
Saeed Bisharadefb4512009-01-06 14:42:24 -080014#include <linux/io.h>
15#include <linux/platform_device.h>
Jason Cooperea983ede2012-03-06 23:53:57 +000016#include <linux/of.h>
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -040017#include <linux/delay.h>
Andrew Lunn89c58c12013-02-03 12:32:06 +010018#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040020#include <linux/module.h>
Saeed Bisharadefb4512009-01-06 14:42:24 -080021
22
23#define RTC_TIME_REG_OFFS 0
24#define RTC_SECONDS_OFFS 0
25#define RTC_MINUTES_OFFS 8
26#define RTC_HOURS_OFFS 16
27#define RTC_WDAY_OFFS 24
Alexandre Bellonife561332015-06-08 16:16:14 +020028#define RTC_HOURS_12H_MODE BIT(22) /* 12 hour mode */
Saeed Bisharadefb4512009-01-06 14:42:24 -080029
30#define RTC_DATE_REG_OFFS 4
31#define RTC_MDAY_OFFS 0
32#define RTC_MONTH_OFFS 8
33#define RTC_YEAR_OFFS 16
34
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080035#define RTC_ALARM_TIME_REG_OFFS 8
36#define RTC_ALARM_DATE_REG_OFFS 0xc
Alexandre Bellonife561332015-06-08 16:16:14 +020037#define RTC_ALARM_VALID BIT(7)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080038
39#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
40#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
Saeed Bisharadefb4512009-01-06 14:42:24 -080041
42struct rtc_plat_data {
43 struct rtc_device *rtc;
44 void __iomem *ioaddr;
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080045 int irq;
Andrew Lunn89c58c12013-02-03 12:32:06 +010046 struct clk *clk;
Saeed Bisharadefb4512009-01-06 14:42:24 -080047};
48
49static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
50{
51 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
52 void __iomem *ioaddr = pdata->ioaddr;
53 u32 rtc_reg;
54
55 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
56 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
57 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
58 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
59 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
60
61 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
62 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
63 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
64 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
65
66 return 0;
67}
68
69static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
70{
71 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
72 void __iomem *ioaddr = pdata->ioaddr;
73 u32 rtc_time, rtc_date;
74 unsigned int year, month, day, hour, minute, second, wday;
75
76 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
77 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
78
79 second = rtc_time & 0x7f;
80 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
Alexandre Belloni4042a142015-06-08 16:16:15 +020081 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
Saeed Bisharadefb4512009-01-06 14:42:24 -080082 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
83
84 day = rtc_date & 0x3f;
85 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
86 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
87
88 tm->tm_sec = bcd2bin(second);
89 tm->tm_min = bcd2bin(minute);
90 tm->tm_hour = bcd2bin(hour);
91 tm->tm_mday = bcd2bin(day);
92 tm->tm_wday = bcd2bin(wday);
93 tm->tm_mon = bcd2bin(month) - 1;
94 /* hw counts from year 2000, but tm_year is relative to 1900 */
95 tm->tm_year = bcd2bin(year) + 100;
96
Alexandre Belloni22652ba2018-02-19 16:23:56 +010097 return 0;
Saeed Bisharadefb4512009-01-06 14:42:24 -080098}
99
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800100static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
101{
102 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
103 void __iomem *ioaddr = pdata->ioaddr;
104 u32 rtc_time, rtc_date;
105 unsigned int year, month, day, hour, minute, second, wday;
106
107 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
108 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
109
110 second = rtc_time & 0x7f;
111 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
Alexandre Belloni4042a142015-06-08 16:16:15 +0200112 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800113 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
114
115 day = rtc_date & 0x3f;
116 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
117 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
118
119 alm->time.tm_sec = bcd2bin(second);
120 alm->time.tm_min = bcd2bin(minute);
121 alm->time.tm_hour = bcd2bin(hour);
122 alm->time.tm_mday = bcd2bin(day);
123 alm->time.tm_wday = bcd2bin(wday);
124 alm->time.tm_mon = bcd2bin(month) - 1;
125 /* hw counts from year 2000, but tm_year is relative to 1900 */
126 alm->time.tm_year = bcd2bin(year) + 100;
127
128 if (rtc_valid_tm(&alm->time) < 0) {
129 dev_err(dev, "retrieved alarm date/time is not valid.\n");
130 rtc_time_to_tm(0, &alm->time);
131 }
132
133 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
134 return 0;
135}
136
137static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
138{
139 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
140 void __iomem *ioaddr = pdata->ioaddr;
141 u32 rtc_reg = 0;
142
143 if (alm->time.tm_sec >= 0)
144 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
145 << RTC_SECONDS_OFFS;
146 if (alm->time.tm_min >= 0)
147 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
148 << RTC_MINUTES_OFFS;
149 if (alm->time.tm_hour >= 0)
150 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
151 << RTC_HOURS_OFFS;
152
153 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
154
155 if (alm->time.tm_mday >= 0)
156 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
157 << RTC_MDAY_OFFS;
158 else
159 rtc_reg = 0;
160
161 if (alm->time.tm_mon >= 0)
162 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
163 << RTC_MONTH_OFFS;
164
165 if (alm->time.tm_year >= 0)
166 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
167 << RTC_YEAR_OFFS;
168
169 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
170 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
171 writel(alm->enabled ? 1 : 0,
172 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
173
174 return 0;
175}
176
John Stultz16380c12011-02-02 17:02:41 -0800177static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800178{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200179 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800180 void __iomem *ioaddr = pdata->ioaddr;
181
182 if (pdata->irq < 0)
John Stultz16380c12011-02-02 17:02:41 -0800183 return -EINVAL; /* fall back into rtc-dev's emulation */
184
185 if (enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800186 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
John Stultz16380c12011-02-02 17:02:41 -0800187 else
188 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800189 return 0;
190}
191
192static irqreturn_t mv_rtc_interrupt(int irq, void *data)
193{
194 struct rtc_plat_data *pdata = data;
195 void __iomem *ioaddr = pdata->ioaddr;
196
197 /* alarm irq? */
198 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
199 return IRQ_NONE;
200
201 /* clear interrupt */
202 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
203 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
204 return IRQ_HANDLED;
205}
206
Saeed Bisharadefb4512009-01-06 14:42:24 -0800207static const struct rtc_class_ops mv_rtc_ops = {
208 .read_time = mv_rtc_read_time,
209 .set_time = mv_rtc_set_time,
210};
211
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800212static const struct rtc_class_ops mv_rtc_alarm_ops = {
213 .read_time = mv_rtc_read_time,
214 .set_time = mv_rtc_set_time,
215 .read_alarm = mv_rtc_read_alarm,
216 .set_alarm = mv_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800217 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800218};
219
Jingoo Hanb69af432013-04-29 16:18:21 -0700220static int __init mv_rtc_probe(struct platform_device *pdev)
Saeed Bisharadefb4512009-01-06 14:42:24 -0800221{
222 struct resource *res;
223 struct rtc_plat_data *pdata;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800224 u32 rtc_time;
Andrew Lunn89c58c12013-02-03 12:32:06 +0100225 int ret = 0;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800226
Saeed Bisharadefb4512009-01-06 14:42:24 -0800227 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
228 if (!pdata)
229 return -ENOMEM;
230
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700231 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
232 pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
233 if (IS_ERR(pdata->ioaddr))
234 return PTR_ERR(pdata->ioaddr);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800235
Andrew Lunn89c58c12013-02-03 12:32:06 +0100236 pdata->clk = devm_clk_get(&pdev->dev, NULL);
237 /* Not all SoCs require a clock.*/
238 if (!IS_ERR(pdata->clk))
239 clk_prepare_enable(pdata->clk);
240
Alexandre Belloni4042a142015-06-08 16:16:15 +0200241 /* make sure the 24 hour mode is enabled */
Saeed Bisharadefb4512009-01-06 14:42:24 -0800242 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
243 if (rtc_time & RTC_HOURS_12H_MODE) {
Alexandre Belloni4042a142015-06-08 16:16:15 +0200244 dev_err(&pdev->dev, "12 Hour mode is enabled but not supported.\n");
Andrew Lunn89c58c12013-02-03 12:32:06 +0100245 ret = -EINVAL;
246 goto out;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800247 }
248
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400249 /* make sure it is actually functional */
250 if (rtc_time == 0x01000000) {
251 ssleep(1);
252 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
253 if (rtc_time == 0x01000000) {
254 dev_err(&pdev->dev, "internal RTC not ticking\n");
Andrew Lunn89c58c12013-02-03 12:32:06 +0100255 ret = -ENODEV;
256 goto out;
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400257 }
258 }
259
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800260 pdata->irq = platform_get_irq(pdev, 0);
261
Saeed Bisharadefb4512009-01-06 14:42:24 -0800262 platform_set_drvdata(pdev, pdata);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800263
264 if (pdata->irq >= 0) {
265 device_init_wakeup(&pdev->dev, 1);
Jingoo Han4241e2b2013-04-29 16:19:08 -0700266 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800267 &mv_rtc_alarm_ops,
268 THIS_MODULE);
Jingoo Han4241e2b2013-04-29 16:19:08 -0700269 } else {
270 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800271 &mv_rtc_ops, THIS_MODULE);
Jingoo Han4241e2b2013-04-29 16:19:08 -0700272 }
Andrew Lunn89c58c12013-02-03 12:32:06 +0100273 if (IS_ERR(pdata->rtc)) {
274 ret = PTR_ERR(pdata->rtc);
275 goto out;
276 }
Saeed Bisharadefb4512009-01-06 14:42:24 -0800277
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800278 if (pdata->irq >= 0) {
279 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
280 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700281 IRQF_SHARED,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800282 pdev->name, pdata) < 0) {
283 dev_warn(&pdev->dev, "interrupt not available.\n");
284 pdata->irq = -1;
285 }
286 }
287
Saeed Bisharadefb4512009-01-06 14:42:24 -0800288 return 0;
Andrew Lunn89c58c12013-02-03 12:32:06 +0100289out:
290 if (!IS_ERR(pdata->clk))
291 clk_disable_unprepare(pdata->clk);
292
293 return ret;
Saeed Bisharadefb4512009-01-06 14:42:24 -0800294}
295
296static int __exit mv_rtc_remove(struct platform_device *pdev)
297{
298 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
299
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800300 if (pdata->irq >= 0)
301 device_init_wakeup(&pdev->dev, 0);
302
Andrew Lunn89c58c12013-02-03 12:32:06 +0100303 if (!IS_ERR(pdata->clk))
304 clk_disable_unprepare(pdata->clk);
305
Saeed Bisharadefb4512009-01-06 14:42:24 -0800306 return 0;
307}
308
Jason Cooperea983ede2012-03-06 23:53:57 +0000309#ifdef CONFIG_OF
Jingoo Han4621bc52014-06-06 14:35:57 -0700310static const struct of_device_id rtc_mv_of_match_table[] = {
Andrew Lunn77843502012-07-18 19:22:54 +0200311 { .compatible = "marvell,orion-rtc", },
Jason Cooperea983ede2012-03-06 23:53:57 +0000312 {}
313};
Javier Martinez Canillas73798d52015-08-27 13:52:02 +0200314MODULE_DEVICE_TABLE(of, rtc_mv_of_match_table);
Jason Cooperea983ede2012-03-06 23:53:57 +0000315#endif
316
Saeed Bisharadefb4512009-01-06 14:42:24 -0800317static struct platform_driver mv_rtc_driver = {
318 .remove = __exit_p(mv_rtc_remove),
319 .driver = {
320 .name = "rtc-mv",
Jason Cooperea983ede2012-03-06 23:53:57 +0000321 .of_match_table = of_match_ptr(rtc_mv_of_match_table),
Saeed Bisharadefb4512009-01-06 14:42:24 -0800322 },
323};
324
Jingoo Han1963f7c2013-04-29 16:18:44 -0700325module_platform_driver_probe(mv_rtc_driver, mv_rtc_probe);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800326
327MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
328MODULE_DESCRIPTION("Marvell RTC driver");
329MODULE_LICENSE("GPL");
330MODULE_ALIAS("platform:rtc-mv");