Linux 4.1.16
[linux/fpc-iii.git] / drivers / rtc / rtc-mv.c
blob423762241042bc40de9e9c9b3db251e46e15fdf1
1 /*
2 * Driver for the RTC in Marvell SoCs.
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 */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
13 #include <linux/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/of.h>
16 #include <linux/delay.h>
17 #include <linux/clk.h>
18 #include <linux/gfp.h>
19 #include <linux/module.h>
22 #define RTC_TIME_REG_OFFS 0
23 #define RTC_SECONDS_OFFS 0
24 #define RTC_MINUTES_OFFS 8
25 #define RTC_HOURS_OFFS 16
26 #define RTC_WDAY_OFFS 24
27 #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
29 #define RTC_DATE_REG_OFFS 4
30 #define RTC_MDAY_OFFS 0
31 #define RTC_MONTH_OFFS 8
32 #define RTC_YEAR_OFFS 16
34 #define RTC_ALARM_TIME_REG_OFFS 8
35 #define RTC_ALARM_DATE_REG_OFFS 0xc
36 #define RTC_ALARM_VALID (1 << 7)
38 #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
39 #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
41 struct rtc_plat_data {
42 struct rtc_device *rtc;
43 void __iomem *ioaddr;
44 int irq;
45 struct clk *clk;
48 static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
50 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
51 void __iomem *ioaddr = pdata->ioaddr;
52 u32 rtc_reg;
54 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
55 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
56 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
57 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
58 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
60 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
61 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
62 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
63 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
65 return 0;
68 static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
70 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
71 void __iomem *ioaddr = pdata->ioaddr;
72 u32 rtc_time, rtc_date;
73 unsigned int year, month, day, hour, minute, second, wday;
75 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
76 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
78 second = rtc_time & 0x7f;
79 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
80 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
81 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
83 day = rtc_date & 0x3f;
84 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
85 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
87 tm->tm_sec = bcd2bin(second);
88 tm->tm_min = bcd2bin(minute);
89 tm->tm_hour = bcd2bin(hour);
90 tm->tm_mday = bcd2bin(day);
91 tm->tm_wday = bcd2bin(wday);
92 tm->tm_mon = bcd2bin(month) - 1;
93 /* hw counts from year 2000, but tm_year is relative to 1900 */
94 tm->tm_year = bcd2bin(year) + 100;
96 return rtc_valid_tm(tm);
99 static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
101 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
102 void __iomem *ioaddr = pdata->ioaddr;
103 u32 rtc_time, rtc_date;
104 unsigned int year, month, day, hour, minute, second, wday;
106 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
107 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
109 second = rtc_time & 0x7f;
110 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
111 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
112 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
114 day = rtc_date & 0x3f;
115 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
116 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
118 alm->time.tm_sec = bcd2bin(second);
119 alm->time.tm_min = bcd2bin(minute);
120 alm->time.tm_hour = bcd2bin(hour);
121 alm->time.tm_mday = bcd2bin(day);
122 alm->time.tm_wday = bcd2bin(wday);
123 alm->time.tm_mon = bcd2bin(month) - 1;
124 /* hw counts from year 2000, but tm_year is relative to 1900 */
125 alm->time.tm_year = bcd2bin(year) + 100;
127 if (rtc_valid_tm(&alm->time) < 0) {
128 dev_err(dev, "retrieved alarm date/time is not valid.\n");
129 rtc_time_to_tm(0, &alm->time);
132 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
133 return 0;
136 static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
138 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
139 void __iomem *ioaddr = pdata->ioaddr;
140 u32 rtc_reg = 0;
142 if (alm->time.tm_sec >= 0)
143 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
144 << RTC_SECONDS_OFFS;
145 if (alm->time.tm_min >= 0)
146 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
147 << RTC_MINUTES_OFFS;
148 if (alm->time.tm_hour >= 0)
149 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
150 << RTC_HOURS_OFFS;
152 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
154 if (alm->time.tm_mday >= 0)
155 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
156 << RTC_MDAY_OFFS;
157 else
158 rtc_reg = 0;
160 if (alm->time.tm_mon >= 0)
161 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
162 << RTC_MONTH_OFFS;
164 if (alm->time.tm_year >= 0)
165 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
166 << RTC_YEAR_OFFS;
168 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
169 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
170 writel(alm->enabled ? 1 : 0,
171 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
173 return 0;
176 static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
178 struct platform_device *pdev = to_platform_device(dev);
179 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
180 void __iomem *ioaddr = pdata->ioaddr;
182 if (pdata->irq < 0)
183 return -EINVAL; /* fall back into rtc-dev's emulation */
185 if (enabled)
186 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
187 else
188 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
189 return 0;
192 static irqreturn_t mv_rtc_interrupt(int irq, void *data)
194 struct rtc_plat_data *pdata = data;
195 void __iomem *ioaddr = pdata->ioaddr;
197 /* alarm irq? */
198 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
199 return IRQ_NONE;
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;
207 static const struct rtc_class_ops mv_rtc_ops = {
208 .read_time = mv_rtc_read_time,
209 .set_time = mv_rtc_set_time,
212 static 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,
217 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
220 static int __init mv_rtc_probe(struct platform_device *pdev)
222 struct resource *res;
223 struct rtc_plat_data *pdata;
224 u32 rtc_time;
225 u32 rtc_date;
226 int ret = 0;
228 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
229 if (!pdata)
230 return -ENOMEM;
232 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
233 pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
234 if (IS_ERR(pdata->ioaddr))
235 return PTR_ERR(pdata->ioaddr);
237 pdata->clk = devm_clk_get(&pdev->dev, NULL);
238 /* Not all SoCs require a clock.*/
239 if (!IS_ERR(pdata->clk))
240 clk_prepare_enable(pdata->clk);
242 /* make sure the 24 hours mode is enabled */
243 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
244 if (rtc_time & RTC_HOURS_12H_MODE) {
245 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
246 ret = -EINVAL;
247 goto out;
250 /* make sure it is actually functional */
251 if (rtc_time == 0x01000000) {
252 ssleep(1);
253 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
254 if (rtc_time == 0x01000000) {
255 dev_err(&pdev->dev, "internal RTC not ticking\n");
256 ret = -ENODEV;
257 goto out;
262 * A date after January 19th, 2038 does not fit on 32 bits and
263 * will confuse the kernel and userspace. Reset to a sane date
264 * (January 1st, 2013) if we're after 2038.
266 rtc_date = readl(pdata->ioaddr + RTC_DATE_REG_OFFS);
267 if (bcd2bin((rtc_date >> RTC_YEAR_OFFS) & 0xff) >= 38) {
268 dev_info(&pdev->dev, "invalid RTC date, resetting to January 1st, 2013\n");
269 writel(0x130101, pdata->ioaddr + RTC_DATE_REG_OFFS);
272 pdata->irq = platform_get_irq(pdev, 0);
274 platform_set_drvdata(pdev, pdata);
276 if (pdata->irq >= 0) {
277 device_init_wakeup(&pdev->dev, 1);
278 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
279 &mv_rtc_alarm_ops,
280 THIS_MODULE);
281 } else {
282 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
283 &mv_rtc_ops, THIS_MODULE);
285 if (IS_ERR(pdata->rtc)) {
286 ret = PTR_ERR(pdata->rtc);
287 goto out;
290 if (pdata->irq >= 0) {
291 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
292 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
293 IRQF_SHARED,
294 pdev->name, pdata) < 0) {
295 dev_warn(&pdev->dev, "interrupt not available.\n");
296 pdata->irq = -1;
300 return 0;
301 out:
302 if (!IS_ERR(pdata->clk))
303 clk_disable_unprepare(pdata->clk);
305 return ret;
308 static int __exit mv_rtc_remove(struct platform_device *pdev)
310 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
312 if (pdata->irq >= 0)
313 device_init_wakeup(&pdev->dev, 0);
315 if (!IS_ERR(pdata->clk))
316 clk_disable_unprepare(pdata->clk);
318 return 0;
321 #ifdef CONFIG_OF
322 static const struct of_device_id rtc_mv_of_match_table[] = {
323 { .compatible = "marvell,orion-rtc", },
326 #endif
328 static struct platform_driver mv_rtc_driver = {
329 .remove = __exit_p(mv_rtc_remove),
330 .driver = {
331 .name = "rtc-mv",
332 .of_match_table = of_match_ptr(rtc_mv_of_match_table),
336 module_platform_driver_probe(mv_rtc_driver, mv_rtc_probe);
338 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
339 MODULE_DESCRIPTION("Marvell RTC driver");
340 MODULE_LICENSE("GPL");
341 MODULE_ALIAS("platform:rtc-mv");